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