5078
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
1
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
48
|
5 |
All Rights Reserved
|
1
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
904
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
3862
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
19
|
18 |
Collection subclass:#MappedCollection
|
254
|
19 |
instanceVariableNames:'domain map'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Sequenceable'
|
1
|
23 |
!
|
|
24 |
|
904
|
25 |
!MappedCollection class methodsFor:'documentation'!
|
1
|
26 |
|
31
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
48
|
30 |
All Rights Reserved
|
31
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
28
|
41 |
documentation
|
|
42 |
"
|
|
43 |
MappedCollections represent collections of objects that are indirectly indexed by names.
|
5078
|
44 |
There are really two collections involved: domain and a map.
|
|
45 |
The map maps between external names and indices into domain,
|
|
46 |
which contains the real association.
|
|
47 |
In order to work properly, the domain and map objects must
|
|
48 |
be instances of a subclass of SequenceableCollection or Dictionary.
|
|
49 |
The valueDomain of the map must be the keyDomain of the domain collection.
|
|
50 |
i.e.
|
|
51 |
mappedCollection at:k ==> domain at:(map at:k)
|
254
|
52 |
|
|
53 |
[author:]
|
|
54 |
Claus Gittinger
|
28
|
55 |
"
|
2967
|
56 |
!
|
|
57 |
|
|
58 |
examples
|
|
59 |
"
|
|
60 |
[exBegin]
|
5078
|
61 |
|mapped keyMapping|
|
2967
|
62 |
|
5078
|
63 |
keyMapping := Dictionary new
|
2967
|
64 |
at:'uno' put:1;
|
|
65 |
at:'due' put:2;
|
|
66 |
at:'tre' put:3;
|
|
67 |
at:'quattro' put:4;
|
|
68 |
yourself.
|
|
69 |
|
|
70 |
mapped := MappedCollection
|
|
71 |
collection:#(one two three four)
|
5078
|
72 |
map:keyMapping.
|
2967
|
73 |
|
|
74 |
mapped at:'tre'.
|
|
75 |
mapped select:[:each| each ~= 'two']
|
|
76 |
"
|
28
|
77 |
! !
|
1
|
78 |
|
904
|
79 |
!MappedCollection class methodsFor:'instance creation'!
|
1
|
80 |
|
|
81 |
collection:domainCollection map:mapCollection
|
|
82 |
"return a new MappedCollection"
|
|
83 |
|
|
84 |
^ super new setCollection:domainCollection map:mapCollection
|
|
85 |
!
|
|
86 |
|
|
87 |
new
|
|
88 |
"report an error; mappedCollections may not be created using new"
|
|
89 |
|
|
90 |
self error:'use collection:map: to create a MappedCollection'
|
|
91 |
! !
|
|
92 |
|
|
93 |
!MappedCollection methodsFor:'accessing'!
|
|
94 |
|
|
95 |
at:key
|
426
|
96 |
"retrieve an element"
|
|
97 |
|
1
|
98 |
^ domain at:(map at:key)
|
426
|
99 |
|
|
100 |
"Modified: 2.8.1996 / 21:04:01 / cg"
|
1
|
101 |
!
|
|
102 |
|
|
103 |
at:key put:anObject
|
426
|
104 |
"store an element"
|
|
105 |
|
1
|
106 |
^ domain at:(map at:key) put:anObject
|
426
|
107 |
|
|
108 |
"Modified: 2.8.1996 / 21:04:06 / cg"
|
1
|
109 |
!
|
|
110 |
|
|
111 |
contents
|
426
|
112 |
"return the contents as a bag"
|
|
113 |
|
1
|
114 |
|contents|
|
2990
|
115 |
|
1
|
116 |
contents := Bag new.
|
49
|
117 |
map do:[:key | contents add:(domain at:key)].
|
1
|
118 |
^ contents
|
426
|
119 |
|
|
120 |
"Modified: 2.8.1996 / 21:04:10 / cg"
|
1
|
121 |
! !
|
|
122 |
|
904
|
123 |
!MappedCollection methodsFor:'adding & removing'!
|
|
124 |
|
|
125 |
add:anObject
|
|
126 |
"report an error; mappedCollections cannot add elements (without a key)"
|
|
127 |
|
|
128 |
self shouldNotImplement
|
|
129 |
|
|
130 |
"Modified: 2.8.1996 / 21:03:57 / cg"
|
|
131 |
! !
|
|
132 |
|
4959
|
133 |
!MappedCollection methodsFor:'copying-private'!
|
42
|
134 |
|
|
135 |
postCopy
|
|
136 |
domain := domain copy.
|
|
137 |
map := map copy
|
|
138 |
! !
|
|
139 |
|
1
|
140 |
!MappedCollection methodsFor:'enumerating'!
|
|
141 |
|
123
|
142 |
do:aBlock
|
|
143 |
"evaluate the argument, aBlock for each element"
|
|
144 |
|
|
145 |
map do:[:mapValue | aBlock value:(domain at:mapValue)].
|
1
|
146 |
! !
|
|
147 |
|
|
148 |
!MappedCollection methodsFor:'private'!
|
|
149 |
|
|
150 |
setCollection:domainCollection map:mapCollection
|
|
151 |
domain := domainCollection.
|
|
152 |
map := mapCollection
|
48
|
153 |
! !
|
|
154 |
|
|
155 |
!MappedCollection methodsFor:'queries'!
|
1
|
156 |
|
3018
|
157 |
isFixedSize
|
|
158 |
"return true if the receiver cannot grow"
|
|
159 |
|
|
160 |
^ false
|
|
161 |
!
|
|
162 |
|
123
|
163 |
size
|
|
164 |
"return the number of elements in the receiver"
|
|
165 |
|
|
166 |
^ map size
|
906
|
167 |
!
|
|
168 |
|
|
169 |
species
|
3862
|
170 |
"return the type of collection to be returned by collect, select etc."
|
|
171 |
|
906
|
172 |
^ domain species
|
2967
|
173 |
!
|
|
174 |
|
|
175 |
speciesForAdding
|
3862
|
176 |
"like species, but redefined for collections which cannot grow easily.
|
|
177 |
Used by functions which create a growing collection
|
|
178 |
(see collect:with:, for example)"
|
|
179 |
|
2967
|
180 |
^ domain speciesForAdding
|
123
|
181 |
! !
|
|
182 |
|
904
|
183 |
!MappedCollection class methodsFor:'documentation'!
|
131
|
184 |
|
|
185 |
version
|
3862
|
186 |
^ '$Header$'
|
131
|
187 |
! !
|
2967
|
188 |
|