MappedCollection.st
author Claus Gittinger <cg@exept.de>
Sun, 05 Jun 2016 09:57:02 +0200
changeset 3881 16890fe7ef2e
parent 3862 d8bdc6819c50
child 4270 3c9fa01002e2
permissions -rw-r--r--
#DOCUMENTATION by cg class: BitArray comment/format in: #documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
claus
parents:
diff changeset
     1
"
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
48
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
     3
	      All Rights Reserved
1
claus
parents:
diff changeset
     4
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
claus
parents:
diff changeset
    10
 hereby transferred.
claus
parents:
diff changeset
    11
"
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    13
3862
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
    14
"{ NameSpace: Smalltalk }"
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
    15
19
c25a529288cf *** empty log message ***
claus
parents: 2
diff changeset
    16
Collection subclass:#MappedCollection
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    17
	instanceVariableNames:'domain map'
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    18
	classVariableNames:''
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    19
	poolDictionaries:''
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    20
	category:'Collections-Sequenceable'
1
claus
parents:
diff changeset
    21
!
claus
parents:
diff changeset
    22
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    23
!MappedCollection class methodsFor:'documentation'!
1
claus
parents:
diff changeset
    24
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    25
copyright
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    26
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    27
 COPYRIGHT (c) 1993 by Claus Gittinger
48
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
    28
	      All Rights Reserved
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    29
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    30
 This software is furnished under a license and may be used
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    31
 only in accordance with the terms of that license and with the
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    33
 be provided or otherwise made available to, or used by, any
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    34
 other person.  No title to or ownership of the software is
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    35
 hereby transferred.
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    36
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    37
!
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
    38
28
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    39
documentation
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    40
"
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    41
    MappedCollections represent collections of objects that are indirectly indexed by names.
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    42
    There are really two collections involved: domain and a map.  The map maps
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    43
    between external names and indices into domain, which contains the
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    44
    real association.  In order to work properly, the domain and map objects must
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    45
    be instances of a subclass of SequenceableCollection or Dictionary. 
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    46
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    47
    [author:]
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    48
        Claus Gittinger
28
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    49
"
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    50
!
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    51
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    52
examples
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    53
"
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    54
                                            [exBegin]
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    55
    |mapped dict|
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    56
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    57
    dict := Dictionary new
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    58
                at:'uno' put:1;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    59
                at:'due' put:2;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    60
                at:'tre' put:3;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    61
                at:'quattro' put:4;
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    62
                yourself.
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    63
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    64
    mapped := MappedCollection
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    65
                    collection:#(one two three four)
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    66
                    map:dict.
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    67
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    68
    mapped at:'tre'.
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    69
    mapped select:[:each| each ~= 'two']
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
    70
"
28
350f8e9493a4 *** empty log message ***
claus
parents: 19
diff changeset
    71
! !
1
claus
parents:
diff changeset
    72
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
    73
!MappedCollection class methodsFor:'instance creation'!
1
claus
parents:
diff changeset
    74
claus
parents:
diff changeset
    75
collection:domainCollection map:mapCollection
claus
parents:
diff changeset
    76
    "return a new MappedCollection"
claus
parents:
diff changeset
    77
claus
parents:
diff changeset
    78
    ^ super new setCollection:domainCollection map:mapCollection
claus
parents:
diff changeset
    79
!
claus
parents:
diff changeset
    80
claus
parents:
diff changeset
    81
new
claus
parents:
diff changeset
    82
    "report an error; mappedCollections may not be created using new"
claus
parents:
diff changeset
    83
claus
parents:
diff changeset
    84
    self error:'use collection:map: to create a MappedCollection'
claus
parents:
diff changeset
    85
! !
claus
parents:
diff changeset
    86
claus
parents:
diff changeset
    87
!MappedCollection methodsFor:'accessing'!
claus
parents:
diff changeset
    88
claus
parents:
diff changeset
    89
at:key
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    90
    "retrieve an element"
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    91
1
claus
parents:
diff changeset
    92
    ^ domain at:(map at:key)
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    93
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    94
    "Modified: 2.8.1996 / 21:04:01 / cg"
1
claus
parents:
diff changeset
    95
!
claus
parents:
diff changeset
    96
claus
parents:
diff changeset
    97
at:key put:anObject
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    98
    "store an element"
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
    99
1
claus
parents:
diff changeset
   100
    ^ domain at:(map at:key) put:anObject
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   101
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   102
    "Modified: 2.8.1996 / 21:04:06 / cg"
1
claus
parents:
diff changeset
   103
!
claus
parents:
diff changeset
   104
claus
parents:
diff changeset
   105
contents
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   106
    "return the contents as a bag"
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   107
1
claus
parents:
diff changeset
   108
    |contents|
2990
cf16ba07221c class: MappedCollection
Claus Gittinger <cg@exept.de>
parents: 2967
diff changeset
   109
1
claus
parents:
diff changeset
   110
    contents := Bag new.
49
ec5828a97593 *** empty log message ***
claus
parents: 48
diff changeset
   111
    map do:[:key | contents add:(domain at:key)].
1
claus
parents:
diff changeset
   112
    ^ contents
426
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   113
768770c2ff33 comments
Claus Gittinger <cg@exept.de>
parents: 254
diff changeset
   114
    "Modified: 2.8.1996 / 21:04:10 / cg"
1
claus
parents:
diff changeset
   115
! !
claus
parents:
diff changeset
   116
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   117
!MappedCollection methodsFor:'adding & removing'!
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   118
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   119
add:anObject
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   120
    "report an error; mappedCollections cannot add elements (without a key)"
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   121
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   122
    self shouldNotImplement
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   123
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   124
    "Modified: 2.8.1996 / 21:03:57 / cg"
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   125
! !
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   126
42
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   127
!MappedCollection methodsFor:'copying'!
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   128
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   129
postCopy
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   130
    domain := domain copy.
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   131
    map := map copy
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   132
! !
506596f9a1a8 *** empty log message ***
claus
parents: 36
diff changeset
   133
1
claus
parents:
diff changeset
   134
!MappedCollection methodsFor:'enumerating'!
claus
parents:
diff changeset
   135
claus
parents:
diff changeset
   136
collect:aBlock
claus
parents:
diff changeset
   137
    "for each element in the receiver, evaluate the argument, aBlock
claus
parents:
diff changeset
   138
     and return a new collection with the results"
claus
parents:
diff changeset
   139
claus
parents:
diff changeset
   140
    |newCollection|
claus
parents:
diff changeset
   141
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   142
    newCollection := self speciesForAdding new:self size.
1
claus
parents:
diff changeset
   143
    self do:[:each | newCollection add:(aBlock value:each)].
claus
parents:
diff changeset
   144
    ^ newCollection
claus
parents:
diff changeset
   145
!
claus
parents:
diff changeset
   146
123
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   147
do:aBlock
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   148
    "evaluate the argument, aBlock for each element"
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   149
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   150
    map do:[:mapValue | aBlock value:(domain at:mapValue)].
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   151
!
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   152
1
claus
parents:
diff changeset
   153
select:aBlock
claus
parents:
diff changeset
   154
    "return a new collection with all elements from the receiver, for which
claus
parents:
diff changeset
   155
     the argument aBlock evaluates to true"
claus
parents:
diff changeset
   156
claus
parents:
diff changeset
   157
    |newCollection|
claus
parents:
diff changeset
   158
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   159
    newCollection := self speciesForAdding new:self size.
1
claus
parents:
diff changeset
   160
    self do:[:each |
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   161
        (aBlock value:each) ifTrue:[newCollection add:each].
1
claus
parents:
diff changeset
   162
    ].
claus
parents:
diff changeset
   163
    ^ newCollection
claus
parents:
diff changeset
   164
! !
claus
parents:
diff changeset
   165
claus
parents:
diff changeset
   166
!MappedCollection methodsFor:'private'!
claus
parents:
diff changeset
   167
claus
parents:
diff changeset
   168
setCollection:domainCollection map:mapCollection
claus
parents:
diff changeset
   169
    domain := domainCollection.
claus
parents:
diff changeset
   170
    map := mapCollection
48
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
   171
! !
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
   172
18b9353c9d07 *** empty log message ***
claus
parents: 42
diff changeset
   173
!MappedCollection methodsFor:'queries'!
1
claus
parents:
diff changeset
   174
3018
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   175
isFixedSize
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   176
    "return true if the receiver cannot grow"
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   177
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   178
    ^ false
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   179
!
8abd4a4fdf06 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2990
diff changeset
   180
123
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   181
size
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   182
    "return the number of elements in the receiver"
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   183
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   184
    ^ map size
906
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   185
!
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   186
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   187
species
3862
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
   188
    "return the type of collection to be returned by collect, select etc."
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
   189
906
30c1ab2aee8d category change
Claus Gittinger <cg@exept.de>
parents: 904
diff changeset
   190
    ^ domain species
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   191
!
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   192
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   193
speciesForAdding
3862
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
   194
    "like species, but redefined for collections which cannot grow easily.
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
   195
     Used by functions which create a growing collection 
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
   196
     (see collect:with:, for example)"
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
   197
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   198
    ^ domain speciesForAdding
123
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   199
! !
4a1b75bf25a8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   200
904
f4791de2295b category change
Claus Gittinger <cg@exept.de>
parents: 426
diff changeset
   201
!MappedCollection class methodsFor:'documentation'!
131
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 123
diff changeset
   202
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 123
diff changeset
   203
version
3862
d8bdc6819c50 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3018
diff changeset
   204
    ^ '$Header$'
131
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 123
diff changeset
   205
! !
2967
9ebb5e9a705a class: MappedCollection
Stefan Vogel <sv@exept.de>
parents: 906
diff changeset
   206