author | Claus Gittinger <cg@exept.de> |
Thu, 17 Aug 2006 17:24:39 +0200 | |
changeset 9569 | b0bf179c9bf5 |
parent 9413 | 195f7caf6610 |
child 9570 | 008049e411b5 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1991 by Claus Gittinger |
155 | 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 |
" |
|
12 |
||
5362 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
12 | 15 |
Set subclass:#Dictionary |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
16 |
instanceVariableNames:'valueArray' |
5336 | 17 |
classVariableNames:'NilKey' |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
18 |
poolDictionaries:'' |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
19 |
category:'Collections-Unordered' |
1 | 20 |
! |
21 |
||
5336 | 22 |
Object subclass:#NilKey |
23 |
instanceVariableNames:'' |
|
24 |
classVariableNames:'TheOneAndOnlyInstance' |
|
25 |
poolDictionaries:'' |
|
26 |
privateIn:Dictionary |
|
27 |
! |
|
28 |
||
2094
42cd02703bf4
handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
29 |
!Dictionary class methodsFor:'documentation'! |
68 | 30 |
|
88 | 31 |
copyright |
32 |
" |
|
33 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
155 | 34 |
All Rights Reserved |
88 | 35 |
|
36 |
This software is furnished under a license and may be used |
|
37 |
only in accordance with the terms of that license and with the |
|
38 |
inclusion of the above copyright notice. This software may not |
|
39 |
be provided or otherwise made available to, or used by, any |
|
40 |
other person. No title to or ownership of the software is |
|
41 |
hereby transferred. |
|
42 |
" |
|
43 |
! |
|
44 |
||
68 | 45 |
documentation |
46 |
" |
|
47 |
a Dictionary is (conceptionally) a set of Associations storing key-value pairs. |
|
48 |
(The implementation uses two arrays to store the keys and values separately.) |
|
49 |
Searching for an element is done using a hash into the key array. |
|
362 | 50 |
Another way of looking at a dictionary is as a array which uses |
345 | 51 |
arbitrary access keys (i.e. not just integers as arrays do). |
52 |
||
53 |
Since the keys are unordered, no internal element order is defined |
|
362 | 54 |
(i.e. enumerating them may return elements in any order - even changing |
345 | 55 |
over time). |
56 |
||
68 | 57 |
Many methods for searching and hashing are inherited from Set. |
92 | 58 |
|
1279 | 59 |
[Instance variables:] |
345 | 60 |
|
1279 | 61 |
keyArray <Array> (from Set) the keys |
62 |
||
63 |
valueArray <Array> the values ('valueArray at:index' corresponds |
|
64 |
to the value stored under 'keyArray at:index') |
|
92 | 65 |
|
345 | 66 |
Performance hints: |
362 | 67 |
since the dictionary does not really store associations internally, |
68 |
it is less efficient, to store/retrieve associations. The reason is |
|
399 | 69 |
that these assocs are created temporarily in some extract methods. |
345 | 70 |
I.e. 'at:key put:value' is faster than 'add:anAssoc' |
71 |
and 'keysAndValuesDo:' is faster than 'associationsDo:' etc. |
|
92 | 72 |
|
362 | 73 |
If only symbols or smallIntegers are used as keys, use IdentityDictionaries |
74 |
for slightly better performance, since both hashing and comparison is faster. |
|
345 | 75 |
|
76 |
If you have a rough idea how big the dictionary is going to grow, |
|
77 |
create it using #new: instead of #new. Even if the size given is a |
|
78 |
poor guess (say half of the real size), there is some 20-30% performance |
|
79 |
win to expect, since many resizing operations are avoided when associations |
|
80 |
are added. |
|
362 | 81 |
|
5336 | 82 |
Special note: |
83 |
in previous versions, nil was not allowed as valid key |
|
84 |
(due to the inheritance from Set, which still does not allow for |
|
85 |
nil elements). |
|
86 |
This has been changed; internally, a special nil-key is used, |
|
87 |
which is converted back to nil whenever keys are accessed. |
|
88 |
||
1290 | 89 |
[See also:] |
90 |
Set, IdentityDictionary, IdentitySet, WeakIdentitySet and |
|
91 |
WeakIdentityDictionary |
|
92 |
||
93 |
[author:] |
|
94 |
Claus Gittinger |
|
68 | 95 |
" |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
96 |
! |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
97 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
98 |
examples |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
99 |
" |
5577 | 100 |
[exBegin] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
101 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
102 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
103 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
104 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
105 |
d at:2 put:'two'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
106 |
d at:2 |
5577 | 107 |
[exEnd] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
108 |
|
5577 | 109 |
[exBegin] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
110 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
111 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
112 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
113 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
114 |
d at:2 put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
115 |
d. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
116 |
d at:2 |
5577 | 117 |
[exEnd] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
118 |
|
5577 | 119 |
[exBegin] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
120 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
121 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
122 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
123 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
124 |
d at:2 put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
125 |
d includes:nil. |
5577 | 126 |
[exEnd] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
127 |
|
5577 | 128 |
[exBegin] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
129 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
130 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
131 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
132 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
133 |
d includes:nil. |
5577 | 134 |
[exEnd] |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
135 |
" |
68 | 136 |
! ! |
137 |
||
5336 | 138 |
!Dictionary class methodsFor:'initialization'! |
139 |
||
140 |
initialize |
|
141 |
"initialize the NilKey singleton" |
|
142 |
||
143 |
NilKey isNil ifTrue:[ |
|
144 |
NilKey := Dictionary::NilKey new |
|
145 |
]. |
|
146 |
||
147 |
" |
|
148 |
Dictionary initialize |
|
149 |
" |
|
150 |
||
151 |
||
152 |
||
153 |
||
154 |
! ! |
|
155 |
||
2094
42cd02703bf4
handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
156 |
!Dictionary class methodsFor:'instance creation'! |
345 | 157 |
|
6100 | 158 |
decodeFromLiteralArray:anArray |
159 |
"create & return a new instance from information encoded in anArray." |
|
160 |
||
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8434
diff
changeset
|
161 |
|dictionary |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8434
diff
changeset
|
162 |
sz "{ Class: SmallInteger }"| |
6100 | 163 |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8434
diff
changeset
|
164 |
sz := anArray size. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8434
diff
changeset
|
165 |
dictionary := self new:sz//2. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8434
diff
changeset
|
166 |
2 to:sz by:2 do:[:idx | |key val| |
6100 | 167 |
key := (anArray at:idx) decodeAsLiteralArray. |
168 |
val := (anArray at:idx+1) decodeAsLiteralArray. |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8434
diff
changeset
|
169 |
dictionary at:key put:val |
6100 | 170 |
]. |
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8434
diff
changeset
|
171 |
^ dictionary |
6100 | 172 |
|
173 |
" |
|
174 |
(Dictionary new |
|
175 |
at:1 put:'one'; |
|
176 |
at:2 put:'two'; |
|
177 |
yourself |
|
178 |
) literalArrayEncoding decodeAsLiteralArray |
|
179 |
" |
|
180 |
! |
|
181 |
||
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
182 |
withAssociations:aCollectionOfAsociations |
5255 | 183 |
"return a new instance where associations are taken from the argument" |
184 |
||
185 |
|newDict sz "{ Class: SmallInteger }"| |
|
186 |
||
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
187 |
sz := aCollectionOfAsociations size. |
5255 | 188 |
newDict := self new:sz. |
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
189 |
aCollectionOfAsociations do:[:assoc | |
5255 | 190 |
newDict at:assoc key put:assoc value |
191 |
]. |
|
192 |
^ newDict |
|
193 |
||
194 |
" |
|
195 |
Dictionary withAssociations:(Array |
|
196 |
with:#'one'->1 |
|
197 |
with:#'two'->2 |
|
198 |
with:#'three'->3 |
|
199 |
with:#'four'->4) |
|
200 |
" |
|
201 |
||
202 |
"Created: / 11.2.2000 / 10:05:54 / cg" |
|
203 |
! |
|
204 |
||
357 | 205 |
withKeys:keyArray andValues:valueArray |
206 |
"return a new instance where keys and values are taken from |
|
207 |
the argumentArrays." |
|
208 |
||
209 |
|newDict sz "{ Class: SmallInteger }"| |
|
210 |
||
211 |
sz := keyArray size. |
|
212 |
newDict := self new:sz. |
|
213 |
keyArray with:valueArray do:[:key :value | |
|
214 |
newDict at:key put:value |
|
215 |
]. |
|
216 |
^ newDict |
|
217 |
||
218 |
" |
|
219 |
Dictionary withKeys:#('one' 'two' 'three' 'four') |
|
220 |
andValues:#(1 2 3 4) |
|
221 |
" |
|
54 | 222 |
! |
223 |
||
609 | 224 |
withKeysAndValues:anArray |
225 |
"return a new instance where keys and values are taken from alternating |
|
226 |
elements of anArray" |
|
54 | 227 |
|
609 | 228 |
|newDict sz "{ Class: SmallInteger }"| |
345 | 229 |
|
609 | 230 |
sz := anArray size. |
231 |
newDict := self new:(sz // 2). |
|
232 |
1 to:sz by:2 do:[:i | |
|
233 |
newDict at:(anArray at:i) put:(anArray at:i+1) |
|
234 |
]. |
|
235 |
^ newDict |
|
345 | 236 |
|
609 | 237 |
" |
238 |
Dictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4) |
|
239 |
" |
|
1 | 240 |
! ! |
241 |
||
7306 | 242 |
!Dictionary class methodsFor:'Compatibility-Squeak'! |
5362 | 243 |
|
244 |
newFrom:aCollectionOfAssociations |
|
245 |
"return a new instance where associations are taken from the argument" |
|
246 |
||
247 |
^ self withAssociations:aCollectionOfAssociations |
|
248 |
||
249 |
" |
|
250 |
Dictionary newFrom:(Array with:#foo->#Foo |
|
251 |
with:#bar->#Bar) |
|
252 |
" |
|
253 |
! ! |
|
254 |
||
9413 | 255 |
|
1 | 256 |
!Dictionary methodsFor:'accessing'! |
257 |
||
609 | 258 |
associationAt:aKey |
259 |
"return an association consisting of aKey and the element indexed |
|
260 |
by aKey - |
|
261 |
report an error, if no element is stored under aKey" |
|
262 |
||
2897 | 263 |
^ Association key:aKey value:(self at:aKey) |
609 | 264 |
! |
265 |
||
266 |
associationAt:aKey ifAbsent:exceptionBlock |
|
267 |
"return an association consisting of aKey and the element indexed by aKey - |
|
268 |
return result of exceptionBlock if no element is stored under aKey" |
|
269 |
||
2897 | 270 |
^ Association key:aKey value:(self at:aKey ifAbsent:[^ exceptionBlock value]) |
609 | 271 |
! |
272 |
||
273 |
associations |
|
274 |
"return an ordered collection containing the receivers associations." |
|
275 |
||
276 |
|coll| |
|
277 |
||
278 |
coll := OrderedCollection new:(keyArray size). |
|
279 |
self associationsDo:[:assoc | coll add:assoc]. |
|
280 |
^ coll |
|
281 |
! |
|
282 |
||
1 | 283 |
at:aKey |
284 |
"return the element indexed by aKey - report an error if none found" |
|
285 |
||
6233 | 286 |
^ self at:aKey ifAbsent:[self errorKeyNotFound:aKey] |
1 | 287 |
! |
288 |
||
289 |
at:aKey ifAbsent:exceptionBlock |
|
2 | 290 |
"return the element indexed by aKey - |
68 | 291 |
return result of exceptionBlock if no element is stored under aKey" |
2 | 292 |
|
5336 | 293 |
|index k| |
1 | 294 |
|
5336 | 295 |
(k := aKey) isNil ifTrue:[ |
296 |
"/ nil is not allowed as key |
|
297 |
"/ |
|
298 |
"/ previous versions of ST/X raised an error |
|
299 |
"/ here. However, there seem to exist applications |
|
300 |
"/ which depend on getting the exceptionBlocks value |
|
301 |
"/ in this case ... well ... |
|
302 |
"/ ^ self errorInvalidKey:aKey |
|
303 |
"/ no longer invalid. |
|
304 |
"/ ^ exceptionBlock value |
|
305 |
k := NilKey |
|
155 | 306 |
]. |
362 | 307 |
|
308 |
"/ I could have written: |
|
309 |
"/ index := self find:aKey ifAbsent:[^ exceptionBlock value] |
|
310 |
"/ but the code below is slighlty more efficient, since it avoids |
|
1146 | 311 |
"/ a block creation - thus speeding up the good case. |
362 | 312 |
|
5336 | 313 |
index := self find:k ifAbsent:0. |
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
1056
diff
changeset
|
314 |
index ~~ 0 ifTrue:[ |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
1056
diff
changeset
|
315 |
^ valueArray basicAt:index |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
1056
diff
changeset
|
316 |
]. |
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
1056
diff
changeset
|
317 |
^ exceptionBlock value. |
1 | 318 |
! |
319 |
||
3304
19ee5d1685d8
Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents:
3252
diff
changeset
|
320 |
at:aKey ifAbsentPut:valueBlock |
3215 | 321 |
"return the element indexed by aKey if present, |
3304
19ee5d1685d8
Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents:
3252
diff
changeset
|
322 |
if not present, store the result of evaluating valueBlock |
19ee5d1685d8
Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents:
3252
diff
changeset
|
323 |
under aKey and return it. |
3215 | 324 |
WARNING: do not add elements while iterating over the receiver. |
325 |
Iterate over a copy to do this." |
|
326 |
||
327 |
|index "{ Class: SmallInteger }" |
|
5336 | 328 |
k newValue| |
3215 | 329 |
|
5336 | 330 |
(k := aKey) isNil ifTrue:[ |
3215 | 331 |
"nil is not allowed as key" |
5336 | 332 |
"/ no longer invalid. |
333 |
"/ ^ self errorInvalidKey:aKey |
|
334 |
k := NilKey |
|
3215 | 335 |
]. |
5336 | 336 |
|
337 |
index := self findKeyOrNil:k. |
|
338 |
(keyArray basicAt:index) notNil ifTrue:[ |
|
339 |
"/ already present |
|
340 |
^ valueArray at:index. |
|
341 |
]. |
|
342 |
"/ a new one |
|
343 |
newValue := valueBlock value. |
|
344 |
keyArray basicAt:index put:k. |
|
345 |
valueArray basicAt:index put:newValue. |
|
346 |
tally := tally + 1. |
|
347 |
self fullCheck. |
|
3215 | 348 |
^ newValue |
349 |
||
350 |
" |
|
351 |
|d| |
|
352 |
||
353 |
d := Dictionary new. |
|
354 |
Transcript showCR:(d at:'foo' ifAbsentPut:'bar'). |
|
355 |
Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2'). |
|
356 |
Transcript showCR:(d at:'foo' ifAbsentPut:'barX'). |
|
357 |
Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2X'). |
|
358 |
" |
|
359 |
||
360 |
"Created: / 23.1.1998 / 18:28:26 / cg" |
|
3304
19ee5d1685d8
Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents:
3252
diff
changeset
|
361 |
"Modified: / 26.2.1998 / 19:10:09 / stefan" |
3215 | 362 |
! |
363 |
||
4871 | 364 |
at:aKey ifPresent:aBlock |
365 |
"if the receiver contains an element stored under aKey, |
|
366 |
retrieve it and evaluate aBlock passing the element as argument, |
|
367 |
return the blocks value. |
|
368 |
If not, do nothing and return nil." |
|
369 |
||
370 |
|v| |
|
371 |
||
372 |
v := self at:aKey ifAbsent:[^ nil]. |
|
373 |
^ aBlock value:v. |
|
374 |
! |
|
375 |
||
1 | 376 |
at:aKey put:anObject |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
377 |
"add the argument anObject under key, aKey to the receiver. |
1221 | 378 |
Return anObject (sigh). |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
379 |
WARNING: do not add elements while iterating over the receiver. |
1221 | 380 |
Iterate over a copy to do this." |
1 | 381 |
|
5336 | 382 |
|k index "{ Class: SmallInteger }"| |
1 | 383 |
|
5336 | 384 |
(k := aKey) isNil ifTrue:[ |
1221 | 385 |
"nil is not allowed as key" |
5336 | 386 |
"/ no longer invalid |
387 |
"/ ^ self errorInvalidKey:aKey |
|
388 |
k := NilKey |
|
389 |
]. |
|
390 |
||
391 |
index := self findKeyOrNil:k. |
|
392 |
(keyArray basicAt:index) notNil ifTrue:[ |
|
393 |
"/ key already present |
|
1221 | 394 |
valueArray basicAt:index put:anObject. |
5336 | 395 |
^ anObject |
396 |
]. |
|
397 |
"/ a new key |
|
398 |
keyArray basicAt:index put:k. |
|
399 |
valueArray basicAt:index put:anObject. |
|
400 |
tally := tally + 1. |
|
1 | 401 |
|
5336 | 402 |
self fullCheck. |
362 | 403 |
^ anObject |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
404 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2324
diff
changeset
|
405 |
"Modified: 30.1.1997 / 14:59:10 / cg" |
1 | 406 |
! |
407 |
||
609 | 408 |
keyAtEqualValue:aValue |
409 |
"return the key whose value is equal (i.e. using #= for compare) |
|
410 |
to the argument, nil if none found. |
|
411 |
This is a slow access, since there is no fast reverse mapping. |
|
412 |
NOTICE: |
|
413 |
The value is searched using equality compare; |
|
414 |
use #keyAtValue: to compare for identity." |
|
77 | 415 |
|
609 | 416 |
^ self keyAtEqualValue:aValue ifAbsent:[nil] |
10 | 417 |
! |
418 |
||
609 | 419 |
keyAtEqualValue:aValue ifAbsent:exceptionBlock |
420 |
"return the key whose value is equal (i.e. using #= for compare) |
|
421 |
to the argument, if not found, return the value of exceptionBlock. |
|
422 |
This is a slow access, since there is no fast reverse mapping. |
|
423 |
NOTICE: |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
424 |
The value is searched using equality compare; |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
425 |
use #keyAtValue:ifAbsent: to compare for identity." |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
426 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
427 |
|idx k| |
54 | 428 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
429 |
idx := 0. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
430 |
[true] whileTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
431 |
idx := valueArray indexOf:aValue startingAt:idx+1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
432 |
idx == 0 ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
433 |
^ exceptionBlock value |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
434 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
435 |
(k := keyArray at:idx) notNil ifTrue:[ |
5701
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
436 |
k ~~ DeletedEntry ifTrue:[ |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
437 |
k == NilKey ifTrue:[ |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
438 |
^ nil |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
439 |
]. |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
440 |
^ k |
5336 | 441 |
]. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
442 |
]. |
609 | 443 |
]. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
444 |
"/ NOT REACHED |
54 | 445 |
! |
446 |
||
5336 | 447 |
keyAtIdentityValue:aValue |
345 | 448 |
"return the key whose value is identical (i.e. using #== for compare) |
449 |
to the argument, nil if none found. |
|
77 | 450 |
This is a slow access, since there is no fast reverse mapping. |
357 | 451 |
NOTICE: |
5336 | 452 |
The value is searched using identity compare; |
453 |
use #keyAtEqualValue: to compare for equality." |
|
10 | 454 |
|
5336 | 455 |
^ self keyAtIdentityValue:aValue ifAbsent:[nil] |
10 | 456 |
! |
457 |
||
5336 | 458 |
keyAtIdentityValue:aValue ifAbsent:exceptionBlock |
345 | 459 |
"return the key whose value is identical (i.e. using #== for compare) |
460 |
to the argument, if not found, return the value of exceptionBlock. |
|
357 | 461 |
This is a slow access, since there is no fast reverse mapping. |
462 |
NOTICE: |
|
2466 | 463 |
The value is searched using identity compare; |
464 |
use #keyAtEqualValue:ifAbsent: to compare for equality." |
|
10 | 465 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
466 |
|idx k| |
359 | 467 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
468 |
idx := 0. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
469 |
[true] whileTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
470 |
idx := valueArray identityIndexOf:aValue startingAt:idx+1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
471 |
idx == 0 ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
472 |
^ exceptionBlock value |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
473 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
474 |
(k := keyArray at:idx) notNil ifTrue:[ |
5701
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
475 |
k ~~ DeletedEntry ifTrue:[ |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
476 |
k == NilKey ifTrue:[ |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
477 |
^ nil |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
478 |
]. |
d4afb8a63e67
oops - keyAtValue did not care for deleted entries.
Claus Gittinger <cg@exept.de>
parents:
5577
diff
changeset
|
479 |
^ k |
5336 | 480 |
]. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
481 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
482 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
483 |
"/ NOT REACHED |
357 | 484 |
! |
485 |
||
5336 | 486 |
keyAtValue:aValue |
487 |
"return the key whose value is identical (i.e. using #== for compare) |
|
488 |
to the argument, nil if none found. |
|
489 |
This is a slow access, since there is no fast reverse mapping. |
|
490 |
NOTICE: |
|
491 |
The value is searched using identity compare; |
|
492 |
use #keyAtEqualValue: to compare for equality." |
|
493 |
||
494 |
^ self keyAtIdentityValue:aValue ifAbsent:[nil] |
|
495 |
! |
|
496 |
||
497 |
keyAtValue:aValue ifAbsent:exceptionBlock |
|
498 |
"return the key whose value is identical (i.e. using #== for compare) |
|
499 |
to the argument, if not found, return the value of exceptionBlock. |
|
500 |
This is a slow access, since there is no fast reverse mapping. |
|
501 |
NOTICE: |
|
502 |
The value is searched using identity compare; |
|
503 |
use #keyAtEqualValue:ifAbsent: to compare for equality." |
|
504 |
||
505 |
^ self keyAtIdentityValue:aValue ifAbsent:exceptionBlock |
|
506 |
! |
|
507 |
||
609 | 508 |
keys |
509 |
"return a collection containing all keys of the receiver" |
|
510 |
||
511 |
|keySet| |
|
357 | 512 |
|
609 | 513 |
keySet := self emptyCollectionForKeys. |
514 |
keyArray do:[:key | |
|
5336 | 515 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
516 |
key == NilKey ifTrue:[ |
|
517 |
keySet add:nil |
|
518 |
] ifFalse:[ |
|
519 |
keySet add:key |
|
520 |
] |
|
521 |
] |
|
609 | 522 |
]. |
523 |
^ keySet |
|
357 | 524 |
! |
525 |
||
609 | 526 |
values |
527 |
"return a collection containing all values of the receiver" |
|
359 | 528 |
|
609 | 529 |
|aCollection| |
359 | 530 |
|
609 | 531 |
aCollection := OrderedCollection new:valueArray size. |
532 |
self do:[:value| aCollection add:value]. |
|
533 |
^ aCollection |
|
1 | 534 |
! ! |
535 |
||
536 |
!Dictionary methodsFor:'adding & removing'! |
|
537 |
||
538 |
add:anAssociation |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
539 |
"add the argument, anAssociation to the receiver. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
540 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
541 |
WARNING: do not add elements while iterating over the receiver. |
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
1056
diff
changeset
|
542 |
Iterate over a copy to do this." |
1 | 543 |
|
544 |
self at:(anAssociation key) put:(anAssociation value). |
|
545 |
^ anAssociation |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
546 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
547 |
"Modified: 1.3.1996 / 21:23:53 / cg" |
1 | 548 |
! |
549 |
||
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
550 |
addAll:aCollection |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
551 |
"ANSI 5.7.2.1: |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
552 |
Message: addAll: dictionary |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
553 |
Synopsis |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
554 |
Store the elements of dictionary in the receiver at the |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
555 |
corresponding keys from dictionary. |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
556 |
Definition: <abstractDictionary> |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
557 |
This message is equivalent to repeatedly sending the #at:put: |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
558 |
message to the receiver with each of the keys and elements in |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
559 |
dictionary in turn. If a key in dictionary is key equivalent |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
560 |
to a key in the receiver, the associated element in dictionary |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
561 |
replaces the element in the receiver. |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
562 |
|
7750 | 563 |
Returns the argument, aCollection (sigh). |
564 |
||
565 |
WARNING: do not add elements while iterating over the receiver. |
|
566 |
Iterate over a copy to do this." |
|
567 |
||
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
568 |
|
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
569 |
self ~~ aCollection ifTrue:[ |
9183
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
570 |
aCollection isSequenceable ifTrue:[ |
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
571 |
aCollection do:[:eachPair | |
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
572 |
self at:eachPair key put:eachPair value. |
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
573 |
]. |
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
574 |
] ifFalse:[ |
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
575 |
aCollection keysAndValuesDo:[:eachKey :eachValue | |
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
576 |
self at:eachKey put:eachValue. |
cc54dc84de9e
addAll: fixed (care for collection of pairs)
Claus Gittinger <cg@exept.de>
parents:
9066
diff
changeset
|
577 |
] |
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
578 |
]. |
7750 | 579 |
]. |
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
580 |
^ aCollection |
7750 | 581 |
|
582 |
" |
|
583 |
|d1 d2| |
|
584 |
||
585 |
d1 := Dictionary new. |
|
586 |
d1 at:1 put:'one'. |
|
587 |
d1 at:2 put:'two'. |
|
588 |
d2 := Dictionary new. |
|
589 |
d2 at:3 put:'three'. |
|
590 |
d2 at:4 put:'four'. |
|
591 |
d1 addAll:d2. |
|
592 |
d1. |
|
593 |
" |
|
594 |
! |
|
595 |
||
2711 | 596 |
addPairsFrom:aSequenceableCollection |
597 |
"merge all key-value pairs from aDictionary into the receiver." |
|
598 |
||
599 |
aSequenceableCollection pairWiseDo:[:key :value | |
|
600 |
self at:key put:value. |
|
601 |
] |
|
602 |
||
7751 | 603 |
" |
604 |
|d1 arr| |
|
605 |
||
606 |
d1 := Dictionary new. |
|
607 |
d1 at:1 put:'one'. |
|
608 |
d1 at:2 put:'two'. |
|
609 |
arr := #(3 'three' 4 'four'). |
|
610 |
d1 addPairsFrom:arr. |
|
611 |
d1. |
|
612 |
" |
|
613 |
||
2711 | 614 |
"Modified: 1.3.1996 / 21:24:03 / cg" |
615 |
! |
|
616 |
||
345 | 617 |
declare:key from:aDictionary |
618 |
"if the receiver does not include an association for key, |
|
619 |
take the association from aDictionary and add it to the receiver. |
|
620 |
If aDictionary does not contain such an association, use nil |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
621 |
as the value of the new dictionary. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
622 |
|
9066 | 623 |
Stubidity Notice: |
624 |
Incompatibility with #declareAllFrom:, where the other values are |
|
625 |
defined unconditionally. |
|
626 |
||
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
627 |
WARNING: do not add elements while iterating over the receiver. |
9066 | 628 |
Iterate over a copy to do this." |
345 | 629 |
|
630 |
|value| |
|
631 |
||
632 |
(self includesKey:key) ifFalse:[ |
|
9066 | 633 |
value := aDictionary at:key ifAbsent:nil. |
634 |
self at:key put:value. |
|
345 | 635 |
] |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
636 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
637 |
"Modified: 1.3.1996 / 21:24:03 / cg" |
345 | 638 |
! |
639 |
||
2711 | 640 |
declareAllFrom:aDictionary |
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
641 |
"merge all key-value pairs from aDictionary into the receiver. |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
642 |
|
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
643 |
For compatibility with #declare:from: the behavior should be changes as following: |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
644 |
If the receiver already contains a key, the existing value is retained." |
2711 | 645 |
|
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
646 |
self ~~ aDictionary ifTrue:[ |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
647 |
aDictionary keysAndValuesDo:[:key :value | |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
648 |
"/ (self includesKey:key) ifFalse:[ |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
649 |
self at:key put:value. |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
650 |
"/ ]. |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
651 |
] |
2711 | 652 |
] |
653 |
! |
|
654 |
||
1 | 655 |
remove:oldObject ifAbsent:aBlock |
656 |
"remove oldObject from the collection and return it. |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
657 |
If it was not in the collection return the value of aBlock. |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
658 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
659 |
This is blocked here; you have to use one of |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
660 |
#removeKey:, #saveRemoveKey:, #removeAssociation:, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
661 |
#removeValue: or #saveRemoveValue:" |
1 | 662 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
663 |
^ self shouldNotImplement |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
664 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
665 |
"Modified: 1.3.1996 / 21:21:38 / cg" |
1 | 666 |
! |
667 |
||
6053
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
668 |
removeAllKeys:aKeyCollection |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
669 |
"remove all associations under each key in aKeyCollection from the collection. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
670 |
If it was not in the collection report an error. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
671 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
672 |
WARNING: do not remove elements while iterating over the receiver. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
673 |
See #saveRemoveKey: to do this." |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
674 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
675 |
aKeyCollection do:[:eachKey | |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
676 |
self removeKey:eachKey ifAbsent:[self errorKeyNotFound:eachKey] |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
677 |
]. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
678 |
! |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
679 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
680 |
removeAllKeys:aKeyCollection ifAbsent:aBlock |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
681 |
"remove all associations under each key in aKeyCollection from the collection. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
682 |
If it was not in the collection return the result from evaluating aBlock. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
683 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
684 |
WARNING: do not remove elements while iterating over the receiver. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
685 |
See #saveRemoveKey: to do this." |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
686 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
687 |
aKeyCollection do:[:eachKey | |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
688 |
self removeKey:eachKey ifAbsent:aBlock |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
689 |
]. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
690 |
! |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
691 |
|
1 | 692 |
removeAssociation:assoc |
693 |
"remove the association from the collection. |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
694 |
If it was not in the collection report an error. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
695 |
Only the key is used in the passed argument, and a new |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
696 |
association, for the key and the previously stored value is returned. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
697 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
698 |
WARNING: do not remove elements while iterating over the receiver. |
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
1056
diff
changeset
|
699 |
See #saveRemoveKey: to do this." |
1 | 700 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
701 |
|key| |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
702 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
703 |
key := assoc key. |
2897 | 704 |
^ Association key:key value:(self removeKey:key) |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
705 |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
706 |
"Modified: 1.3.1996 / 21:21:11 / cg" |
1 | 707 |
! |
708 |
||
709 |
removeKey:aKey |
|
710 |
"remove the association under aKey from the collection. |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
711 |
If it was not in the collection report an error. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
712 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
713 |
WARNING: do not remove elements while iterating over the receiver. |
1126
497de696dff0
OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents:
1056
diff
changeset
|
714 |
See #saveRemoveKey: to do this." |
1 | 715 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
716 |
^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey] |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
717 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
718 |
"Modified: 1.3.1996 / 21:21:52 / cg" |
1 | 719 |
! |
720 |
||
721 |
removeKey:aKey ifAbsent:aBlock |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
722 |
"remove the association under aKey from the collection, |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
723 |
return the value previously stored there.. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
724 |
If it was not in the collection return the result |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
725 |
from evaluating aBlock. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
726 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
727 |
WARNING: do not remove elements while iterating over the receiver. |
5336 | 728 |
See #saveRemoveKey: to do this." |
1 | 729 |
|
3 | 730 |
|index "{ Class:SmallInteger }" |
5816 | 731 |
"/ next "{ Class:SmallInteger }" |
5336 | 732 |
oldValue k| |
1 | 733 |
|
5336 | 734 |
(k := aKey) isNil ifTrue:[ |
735 |
"/ no longer invalid |
|
736 |
"/ ^ self errorInvalidKey:aKey |
|
737 |
k := NilKey |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
738 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
739 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
740 |
"/ below, I could have written: |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
741 |
"/ index := self find:aKey ifAbsent:[^ aBlock value] |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
742 |
"/ but the code below is slighlty more efficient, since it avoids |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
743 |
"/ a garbage block creation - thus speeding up the good case. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
744 |
|
5336 | 745 |
index := self find:k ifAbsent:0. |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
746 |
index == 0 ifTrue:[^ aBlock value]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
747 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
748 |
oldValue := valueArray basicAt:index. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
749 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
750 |
valueArray basicAt:index put:nil. |
5336 | 751 |
keyArray basicAt:index put:DeletedEntry. |
77 | 752 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
753 |
tally := tally - 1. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
754 |
tally == 0 ifTrue:[ |
5336 | 755 |
self setTally:0 |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
756 |
] ifFalse:[ |
5336 | 757 |
"/ index == keyArray basicSize ifTrue:[ |
758 |
"/ next := 1 |
|
759 |
"/ ] ifFalse:[ |
|
760 |
"/ next := index + 1. |
|
761 |
"/ ]. |
|
762 |
"/ (keyArray basicAt:next) notNil ifTrue:[ |
|
763 |
"/ keyArray basicAt:index put:DeletedEntry |
|
764 |
"/ ]. |
|
765 |
self emptyCheck |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
766 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
767 |
^ oldValue |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
768 |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
769 |
"Modified: 1.3.1996 / 21:21:01 / cg" |
77 | 770 |
! |
771 |
||
5184
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
772 |
removeValue:aValue |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
773 |
"remove (first) the association to aValue from the collection, |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
774 |
return the key under which it was stored previously. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
775 |
If it was not in the collection, report an error. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
776 |
The value is searched using equality compare here, |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
777 |
but identity compare in the IdentityDictionary subclass. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
778 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
779 |
Notice, this does a linear search through the values and may |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
780 |
therefore be slow for big dictionaries. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
781 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
782 |
WARNING: do not remove elements while iterating over the receiver. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
783 |
See #saveRemoveValue: to do this." |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
784 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
785 |
^ self removeValue:aValue ifAbsent:[self errorValueNotFound:aValue] |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
786 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
787 |
" |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
788 |
|d| |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
789 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
790 |
d := Dictionary new. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
791 |
d at:1 put:'one'. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
792 |
d at:2 put:'two'. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
793 |
d at:3 put:'three'. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
794 |
d removeValue:'two'. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
795 |
d |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
796 |
" |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
797 |
! |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
798 |
|
77 | 799 |
removeValue:aValue ifAbsent:aBlock |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
800 |
"remove (first) the association to aValue from the collection, |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
801 |
return the key under which it was stored previously. |
77 | 802 |
If it was not in the collection return result from evaluating aBlock. |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
803 |
The value is searched using equality compare here, |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
804 |
but identity compare in the IdentityDictionary subclass. |
77 | 805 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
806 |
Notice, this does a linear search through the values and may |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
807 |
therefore be slow for big dictionaries. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
808 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
809 |
WARNING: do not remove elements while iterating over the receiver. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
810 |
See #saveRemoveValue: to do this." |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
811 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
812 |
|next "{ Class:SmallInteger }" |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
813 |
oldKey| |
77 | 814 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
815 |
keyArray keysAndValuesDo:[:index :aKey | |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
816 |
|idx "{Class:SmallInteger}"| |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
817 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
818 |
(aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
819 |
idx := index. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
820 |
(self compareSame:(valueArray at:idx) with:aValue) ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
821 |
"found it" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
822 |
valueArray basicAt:idx put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
823 |
oldKey := keyArray basicAt:idx. |
5336 | 824 |
oldKey == NilKey ifTrue:[ |
825 |
oldKey := nil |
|
826 |
]. |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
827 |
keyArray basicAt:idx put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
828 |
tally := tally - 1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
829 |
tally == 0 ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
830 |
self setTally:0. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
831 |
^ oldKey |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
832 |
]. |
5336 | 833 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
834 |
idx == keyArray basicSize ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
835 |
next := 1 |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
836 |
] ifFalse:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
837 |
next := index + 1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
838 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
839 |
(keyArray basicAt:next) notNil ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
840 |
keyArray basicAt:idx put:DeletedEntry |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
841 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
842 |
self emptyCheck. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
843 |
^ oldKey |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
844 |
] |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
845 |
] |
77 | 846 |
]. |
847 |
^ aBlock value |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
848 |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
849 |
"Modified: 1.3.1996 / 21:22:11 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
850 |
! |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
851 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
852 |
saveRemoveKey:aKey |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
853 |
"remove the association under aKey from the collection. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
854 |
Return the value previously stored there. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
855 |
If it was not in the collection return nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
856 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
857 |
In contrast to #removeKey:, this does not resize the underlying collection |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
858 |
and therefore does NOT rehash & change the elements order. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
859 |
Therefor this can be used while enumerating the receiver, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
860 |
which is not possible if #removeKey: is used. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
861 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
862 |
WARNING: since no resizing is done, the physical amount of memory used |
5336 | 863 |
by the container remains the same, although the logical size shrinks. |
864 |
You may want to manually resize the receiver using #emptyCheck." |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
865 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
866 |
|index "{ Class:SmallInteger }" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
867 |
next "{ Class:SmallInteger }" |
5336 | 868 |
oldValue k| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
869 |
|
5336 | 870 |
(k := aKey) isNil ifTrue:[ |
871 |
k := NilKey |
|
872 |
]. |
|
873 |
"/ aKey isNil ifTrue:[^ nil]. |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
874 |
|
5336 | 875 |
index := self find:k ifAbsent:0. |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
876 |
index == 0 ifTrue:[^ nil]. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
877 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
878 |
oldValue := valueArray basicAt:index. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
879 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
880 |
valueArray basicAt:index put:nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
881 |
keyArray basicAt:index put:nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
882 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
883 |
tally := tally - 1. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
884 |
tally ~~ 0 ifTrue:[ |
5336 | 885 |
index == keyArray basicSize ifTrue:[ |
886 |
next := 1 |
|
887 |
] ifFalse:[ |
|
888 |
next := index + 1. |
|
889 |
]. |
|
890 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
891 |
keyArray basicAt:index put:DeletedEntry |
|
892 |
]. |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
893 |
]. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
894 |
^ oldValue |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
895 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
896 |
"does NOT work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
897 |
|
5336 | 898 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
899 |
|
5336 | 900 |
d := Dictionary new. |
901 |
d at:'one' put:1. |
|
902 |
d at:'two' put:2. |
|
903 |
d at:'three' put:3. |
|
904 |
d at:'four' put:4. |
|
905 |
d at:'five' put:5. |
|
906 |
d at:'six' put:6. |
|
907 |
d at:'seven' put:7. |
|
908 |
d at:'eight' put:8. |
|
909 |
d at:'nine' put:9. |
|
910 |
d keysAndValuesDo:[:k :v | |
|
911 |
v odd ifTrue:[ |
|
912 |
d removeKey:k |
|
913 |
] |
|
914 |
]. |
|
915 |
d inspect |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
916 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
917 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
918 |
"DOES work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
919 |
|
5336 | 920 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
921 |
|
5336 | 922 |
d := Dictionary new. |
923 |
d at:'one' put:1. |
|
924 |
d at:'two' put:2. |
|
925 |
d at:'three' put:3. |
|
926 |
d at:'four' put:4. |
|
927 |
d at:'five' put:5. |
|
928 |
d at:'six' put:6. |
|
929 |
d at:'seven' put:7. |
|
930 |
d at:'eight' put:8. |
|
931 |
d at:'nine' put:9. |
|
932 |
d keysAndValuesDo:[:k :v | |
|
933 |
v odd ifTrue:[ |
|
934 |
d saveRemoveKey:k |
|
935 |
] |
|
936 |
]. |
|
937 |
d inspect |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
938 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
939 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
940 |
"Created: 1.3.1996 / 21:14:42 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
941 |
"Modified: 1.3.1996 / 21:14:53 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
942 |
! |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
943 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
944 |
saveRemoveValue:aValue |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
945 |
"remove the (first) association to aValue from the collection, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
946 |
return the key under which it was stored previously. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
947 |
If it was not in the collection return nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
948 |
The value is searched using equality compare here, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
949 |
but identity compare in the IdentityDictionary subclass. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
950 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
951 |
In contrast to #removeValue:, this does not resize the underlying collection |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
952 |
and therefore does NOT rehash & change the elements order. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
953 |
Therefore, this can be used while enumerating the receiver, |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
954 |
which is not possible if #removeValue: is used. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
955 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
956 |
WARNING: since no resizing is done, the physical amount of memory used |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
957 |
by the container remains the same, although the logical size shrinks. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
958 |
You may want to manually resize the receiver using #emptyCheck." |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
959 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
960 |
|next "{ Class:SmallInteger }" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
961 |
oldKey| |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
962 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
963 |
keyArray keysAndValuesDo:[:index :aKey | |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
964 |
|idx "{Class:SmallInteger}"| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
965 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
966 |
(aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
967 |
idx := index. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
968 |
(self compareSame:(valueArray at:idx) with:aValue) ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
969 |
"found it" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
970 |
valueArray basicAt:idx put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
971 |
oldKey := keyArray basicAt:idx. |
5336 | 972 |
oldKey == NilKey ifTrue:[ |
973 |
oldKey := nil |
|
974 |
]. |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
975 |
keyArray basicAt:idx put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
976 |
tally := tally - 1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
977 |
tally ~~ 0 ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
978 |
idx == keyArray basicSize ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
979 |
next := 1 |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
980 |
] ifFalse:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
981 |
next := index + 1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
982 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
983 |
(keyArray basicAt:next) notNil ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
984 |
keyArray basicAt:idx put:DeletedEntry |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
985 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
986 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
987 |
^ oldKey |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
988 |
] |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
989 |
] |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
990 |
]. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
991 |
^ aValue |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
992 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
993 |
"does NOT work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
994 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
995 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
996 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
997 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
998 |
d at:'one' put:1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
999 |
d at:'two' put:2. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1000 |
d at:'three' put:3. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1001 |
d at:'four' put:4. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1002 |
d at:'five' put:5. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1003 |
d at:'six' put:6. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1004 |
d at:'seven' put:7. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1005 |
d at:'eight' put:8. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1006 |
d at:'nine' put:9. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1007 |
d keysAndValuesDo:[:k :v | |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1008 |
v odd ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1009 |
d removeValue:v ifAbsent:nil |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1010 |
] |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1011 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1012 |
d inspect |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1013 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1014 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1015 |
"DOES work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1016 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1017 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1018 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1019 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1020 |
d at:'one' put:1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1021 |
d at:'two' put:2. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1022 |
d at:'three' put:3. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1023 |
d at:'four' put:4. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1024 |
d at:'five' put:5. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1025 |
d at:'six' put:6. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1026 |
d at:'seven' put:7. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1027 |
d at:'eight' put:8. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1028 |
d at:'nine' put:9. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1029 |
d keysAndValuesDo:[:k :v | |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1030 |
v odd ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1031 |
d saveRemoveValue:v |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1032 |
] |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1033 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1034 |
d inspect |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1035 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1036 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1037 |
"Created: 1.3.1996 / 21:17:10 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1038 |
"Modified: 1.3.1996 / 21:23:04 / cg" |
1 | 1039 |
! ! |
1040 |
||
5279 | 1041 |
!Dictionary methodsFor:'comparing'! |
1042 |
||
1043 |
= aCollection |
|
1044 |
"return true, if the argument is a Dictionary containing the same |
|
1045 |
key-value pairs as I do" |
|
1046 |
||
1047 |
aCollection species == self species ifFalse:[^ false]. |
|
1048 |
aCollection size == self size ifFalse:[^ false]. |
|
1049 |
"/ all of of my key-value associations must be in the other collection ... |
|
1050 |
self keysAndValuesDo:[:key :value | |
|
1051 |
((aCollection at:key ifAbsent:[^ false]) = value) ifFalse:[^ false] |
|
1052 |
]. |
|
1053 |
^ true |
|
1054 |
||
1055 |
" |
|
1056 |
|d1 d2| |
|
1057 |
||
1058 |
d1 := Dictionary new. |
|
1059 |
d2 := Dictionary new. |
|
1060 |
d1 at:1 put:'one'. |
|
1061 |
d1 at:'one' put:1. |
|
1062 |
d1 at:2 put:#two. |
|
1063 |
d1 at:'two' put:2. |
|
1064 |
||
1065 |
d2 at:1 put:'one'. |
|
1066 |
d2 at:'one' put:1. |
|
1067 |
d2 at:2 put:#two. |
|
1068 |
d2 at:'two' put:2. |
|
1069 |
d1 = d2 |
|
1070 |
" |
|
1071 |
||
1072 |
" |
|
1073 |
|d1 d2| |
|
1074 |
||
1075 |
d1 := Dictionary new. |
|
1076 |
d2 := Dictionary new. |
|
1077 |
d1 at:1 put:'uno'. |
|
1078 |
d1 at:'one' put:1. |
|
1079 |
d1 at:2 put:#two. |
|
1080 |
d1 at:'two' put:2. |
|
1081 |
||
1082 |
d2 at:1 put:'one'. |
|
1083 |
d2 at:'one' put:1. |
|
1084 |
d2 at:2 put:#two. |
|
1085 |
d2 at:'two' put:2. |
|
1086 |
d1 = d2 |
|
1087 |
" |
|
1088 |
||
1089 |
" |
|
1090 |
|d1 d2| |
|
1091 |
||
1092 |
d1 := Dictionary new. |
|
1093 |
d2 := Dictionary new. |
|
1094 |
d1 at:10 put:'one'. |
|
1095 |
d1 at:'one' put:1. |
|
1096 |
d1 at:2 put:#two. |
|
1097 |
d1 at:'two' put:2. |
|
1098 |
||
1099 |
d2 at:1 put:'one'. |
|
1100 |
d2 at:'one' put:1. |
|
1101 |
d2 at:2 put:#two. |
|
1102 |
d2 at:'two' put:2. |
|
1103 |
d1 = d2 |
|
1104 |
" |
|
1105 |
||
1106 |
||
1107 |
||
1108 |
! ! |
|
1109 |
||
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1110 |
!Dictionary methodsFor:'converting'! |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1111 |
|
7906 | 1112 |
asDictionary |
1113 |
^ self |
|
1114 |
! |
|
1115 |
||
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1116 |
fromLiteralArrayEncoding:encoding |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1117 |
"read my values from an encoding. |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1118 |
The encoding is supposed to be of the form: |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1119 |
(Dictionary key1 val1 ... keyN valN)" |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1120 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1121 |
2 to:encoding size by:2 do:[:i | |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1122 |
|key val| |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1123 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1124 |
key := encoding at:i. |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1125 |
val := encoding at:i+1. |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1126 |
self at:key put:val |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1127 |
]. |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1128 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1129 |
" |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1130 |
Dictionary new fromLiteralArrayEncoding:#(Dictionary 'hello' 'world' 1 'foo') |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1131 |
" |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1132 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1133 |
"Created: / 30.1.1998 / 04:30:47 / cg" |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1134 |
! |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1135 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1136 |
literalArrayEncoding |
9413 | 1137 |
|literalArrray idx| |
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1138 |
|
9413 | 1139 |
literalArrray := Array new:(self size * 2)+1. |
1140 |
literalArrray at:1 put:self class name. |
|
1141 |
idx := 2. |
|
1142 |
self keysAndValuesDo:[:eachKey :eachValue | |
|
1143 |
literalArrray |
|
1144 |
at:idx put:eachKey literalArrayEncoding; |
|
1145 |
at:idx+1 put:eachValue literalArrayEncoding. |
|
1146 |
idx := idx + 2. |
|
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1147 |
]. |
9413 | 1148 |
^ literalArrray |
1149 |
||
1150 |
" |
|
1151 |
|dict| |
|
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1152 |
|
9413 | 1153 |
dict := Dictionary new. |
1154 |
dict at:1 put:'bla'. |
|
1155 |
dict at:'fasel' put:#[1 2 3 4]. |
|
1156 |
dict literalArrayEncoding |
|
1157 |
" |
|
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1158 |
! ! |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1159 |
|
155 | 1160 |
!Dictionary methodsFor:'copying'! |
1161 |
||
9066 | 1162 |
, anotherDictionary |
1163 |
"return a new dictionary containing a merged set of associations" |
|
1164 |
||
1165 |
|newDictionary| |
|
1166 |
||
1167 |
newDictionary := self species new. |
|
1168 |
self keysAndValuesDo:[:key :value | newDictionary at:key put:value ]. |
|
1169 |
anotherDictionary keysAndValuesDo:[:key :value | newDictionary at:key put:value ]. |
|
1170 |
^ newDictionary |
|
1171 |
! |
|
1172 |
||
155 | 1173 |
postCopy |
1174 |
"have to copy the valueArray too" |
|
1175 |
||
1176 |
super postCopy. |
|
1177 |
valueArray := valueArray shallowCopy |
|
1178 |
! ! |
|
1179 |
||
217 | 1180 |
!Dictionary methodsFor:'enumerating'! |
1 | 1181 |
|
1182 |
allKeysDo:aBlock |
|
302 | 1183 |
"perform the block for all keys in the collection. |
1184 |
Obsolete: use keysDo: for ST-80 compatibility." |
|
1 | 1185 |
|
5865 | 1186 |
<resource:#obsolete> |
1187 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1188 |
self obsoleteMethodWarning:'please use #keysDo:'. |
155 | 1189 |
^ super do:aBlock |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1190 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1191 |
"Modified: 20.4.1996 / 11:22:01 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1192 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1193 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1194 |
associationsCollect:aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1195 |
"for each key-value pair in the receiver, evaluate the argument, aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1196 |
and return a collection with the results. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1197 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1198 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1199 |
#keysAndValuesCollect: (which passes separate keys & values) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1200 |
#collect: (which only passes values) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1201 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1202 |
This is much like #keysAndValuesCollect:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1203 |
key and value as a single association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1204 |
#keysAndValuesCollect: and is a bit faster therefore (no intermediate objects). |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1205 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1206 |
WARNING: do not add/remove elements while iterating over the receiver. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1207 |
Iterate over a copy to do this." |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1208 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1209 |
|newCollection| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1210 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1211 |
newCollection := OrderedCollection new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1212 |
self keysAndValuesDo:[:key :value | |
2897 | 1213 |
newCollection add:(aBlock value:(Association key:key value:value)) |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1214 |
]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1215 |
^ newCollection |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1216 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1217 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1218 |
|ages| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1219 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1220 |
ages := Dictionary new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1221 |
ages at:'cg' put:37. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1222 |
ages at:'ca' put:33. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1223 |
ages at:'sv' put:36. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1224 |
ages at:'tk' put:28. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1225 |
ages associationsCollect:[:assoc | |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1226 |
assoc key , '''s age is ' , assoc value printString] |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1227 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1228 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1229 |
"Modified: 20.4.1996 / 11:31:27 / cg" |
1 | 1230 |
! |
1231 |
||
1232 |
associationsDo:aBlock |
|
1056 | 1233 |
"perform the block for all associations in the collection. |
1234 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1235 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1236 |
#do: (which passes values to its block) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1237 |
#keysDo: (which passes only keys to its block) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1238 |
#keysAndValuesDo: (which passes keys&values) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1239 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1240 |
This is much like #keysAndValuesDo:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1241 |
key and value as a single association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1242 |
#keysAndValuesDo: and is a bit faster therefore (no intermediate objects). |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1243 |
|
1056 | 1244 |
WARNING: do not add/remove elements while iterating over the receiver. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1245 |
Iterate over a copy to do this." |
1 | 1246 |
|
38 | 1247 |
|key n "{ Class: SmallInteger }"| |
1 | 1248 |
|
1249 |
tally == 0 ifTrue:[^ self]. |
|
38 | 1250 |
n := keyArray basicSize. |
1251 |
1 to:n do:[:index | |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1252 |
key := keyArray basicAt:index. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1253 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
5336 | 1254 |
key == NilKey ifTrue:[ |
1255 |
key := nil |
|
1256 |
]. |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1257 |
aBlock value:(Association key:key value:(valueArray basicAt:index)) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1258 |
] |
1 | 1259 |
] |
1056 | 1260 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1261 |
"Modified: 20.4.1996 / 11:31:39 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1262 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1263 |
|
2425
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1264 |
associationsReverseDo:aBlock |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1265 |
"perform the block for all associations in the collection. |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1266 |
Since dictionary does not define any order of its elements, |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1267 |
this is the same as #associationsDo: here. |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1268 |
Provided for protocol compatibility with OrderedDictionary" |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1269 |
|
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1270 |
^ self associationsDo:aBlock |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1271 |
|
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1272 |
"Created: 28.2.1997 / 16:08:52 / cg" |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1273 |
! |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1274 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1275 |
associationsSelect:aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1276 |
"return a new collection with all elements from the receiver, for which |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1277 |
the argument aBlock evaluates to true. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1278 |
The block gets keys and values as an association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1279 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1280 |
See also: #keysAndValuesSelect: (which is slightly faster), |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1281 |
#select: (which only passes the value) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1282 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1283 |
This is much like #keysAndValuesSelect:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1284 |
key and value as a single association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1285 |
#keysAndValuesSelect: and is a bit faster therefore (no intermediate objects). |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1286 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1287 |
WARNING: do not add/remove elements while iterating over the receiver. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1288 |
Iterate over a copy to do this." |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1289 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1290 |
|newCollection| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1291 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1292 |
newCollection := self species new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1293 |
self keysAndValuesDo:[:key :value | |
2897 | 1294 |
(aBlock value:(Association key:key value:value)) ifTrue:[ |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1295 |
newCollection at:key put:value |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1296 |
] |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1297 |
]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1298 |
^ newCollection |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1299 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1300 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1301 |
|ages| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1302 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1303 |
ages := Dictionary new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1304 |
ages at:'cg' put:37. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1305 |
ages at:'ca' put:33. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1306 |
ages at:'sv' put:36. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1307 |
ages at:'tk' put:28. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1308 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1309 |
ages associationsSelect:[:assoc | |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1310 |
(assoc key startsWith:'c') or:[assoc value < 30]]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1311 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1312 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1313 |
"Modified: 20.4.1996 / 11:31:15 / cg" |
1 | 1314 |
! |
1315 |
||
609 | 1316 |
collect:aBlock |
1317 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1056 | 1318 |
and return a Bag with the results. |
1319 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1320 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1321 |
#associationsCollect: (which passes key-value associations) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1322 |
#keysAndValuesCollect: (which passes keys & values separately) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1323 |
|
1056 | 1324 |
WARNING: do not add/remove elements while iterating over the receiver. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1325 |
Iterate over a copy to do this." |
609 | 1326 |
|
1327 |
|newCollection| |
|
1328 |
||
1329 |
newCollection := Bag new. |
|
1330 |
self do:[:each | |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1331 |
newCollection add:(aBlock value:each) |
609 | 1332 |
]. |
1333 |
^ newCollection |
|
1056 | 1334 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1335 |
"Modified: 20.4.1996 / 11:29:58 / cg" |
609 | 1336 |
! |
1337 |
||
1 | 1338 |
do:aBlock |
1056 | 1339 |
"perform the block for all values in the collection. |
1340 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1341 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1342 |
#associationsDo: (which passes key-value associations) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1343 |
#keysAndValuesDo: (which passes keys & values separately) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1344 |
#keysDo: (which passes keys only) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1345 |
|
1056 | 1346 |
WARNING: do not add/remove elements while iterating over the receiver. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1347 |
Iterate over a copy to do this." |
1 | 1348 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1349 |
|key n "{ Class: SmallInteger }" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1350 |
deletedEntry| |
1 | 1351 |
|
1352 |
tally == 0 ifTrue:[^ self]. |
|
38 | 1353 |
n := keyArray basicSize. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1354 |
deletedEntry := DeletedEntry. |
38 | 1355 |
1 to:n do:[:index | |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1356 |
key := keyArray basicAt:index. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1357 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1358 |
aBlock value:(valueArray basicAt:index) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1359 |
]. |
1 | 1360 |
] |
1056 | 1361 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1362 |
"Modified: 20.4.1996 / 11:32:11 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1363 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1364 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1365 |
keysAndValuesCollect:aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1366 |
"for each key-value pair in the receiver, evaluate the argument, aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1367 |
and return a collection with the results. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1368 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1369 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1370 |
#associationsCollect: (which passes keys->value pairs) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1371 |
#collect: (which only passes values) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1372 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1373 |
This is much like #associationsCollect:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1374 |
key and value as two separate arguments. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1375 |
#associationsCollect: is a bit slower. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1376 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1377 |
WARNING: do not add/remove elements while iterating over the receiver. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1378 |
Iterate over a copy to do this." |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1379 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1380 |
|newCollection| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1381 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1382 |
newCollection := OrderedCollection new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1383 |
self keysAndValuesDo:[:key :value | |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1384 |
newCollection add:(aBlock value:key value:value) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1385 |
]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1386 |
^ newCollection |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1387 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1388 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1389 |
|ages| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1390 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1391 |
ages := Dictionary new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1392 |
ages at:'cg' put:37. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1393 |
ages at:'ca' put:33. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1394 |
ages at:'sv' put:36. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1395 |
ages at:'tk' put:28. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1396 |
ages keysAndValuesCollect:[:name :age | |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1397 |
name , '''s age is ' , age printString] |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1398 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1399 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1400 |
"Modified: 20.4.1996 / 11:33:50 / cg" |
1 | 1401 |
! |
1402 |
||
3 | 1403 |
keysAndValuesDo:aTwoArgBlock |
1404 |
"evaluate the argument, aBlock for every element in the collection, |
|
1056 | 1405 |
passing both key and element as arguments. |
1406 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1407 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1408 |
#associationsDo: (which passes keys->value pairs) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1409 |
#do: (which only passes values) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1410 |
#keysDo: (which only passes keys) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1411 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1412 |
This is much like #associationsDo:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1413 |
key and value as two separate arguments. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1414 |
#associationsDo: is a bit slower. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1415 |
|
1056 | 1416 |
WARNING: do not add/remove elements while iterating over the receiver. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1417 |
Iterate over a copy to do this." |
3 | 1418 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1419 |
|key n "{ Class: SmallInteger }" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1420 |
deletedEntry| |
3 | 1421 |
|
1422 |
tally == 0 ifTrue:[^ self]. |
|
38 | 1423 |
n := keyArray basicSize. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1424 |
deletedEntry := DeletedEntry. |
38 | 1425 |
1 to:n do:[:index | |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1426 |
key := keyArray basicAt:index. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1427 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
5336 | 1428 |
key == NilKey ifTrue:[ |
1429 |
key := nil |
|
1430 |
]. |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1431 |
aTwoArgBlock value:key value:(valueArray basicAt:index) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1432 |
]. |
3 | 1433 |
] |
1056 | 1434 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1435 |
"Modified: 20.4.1996 / 11:33:42 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1436 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1437 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1438 |
keysAndValuesSelect:aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1439 |
"return a new collection with all elements from the receiver, for which |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1440 |
the argument aBlock evaluates to true. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1441 |
The block gets keys and values as separate arguments. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1442 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1443 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1444 |
#associationsSelect: (which passes key-value pairs), |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1445 |
#select: (which only passes the value) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1446 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1447 |
This is much like #associationsSelect:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1448 |
key and value as two separate arguments. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1449 |
#associationsSelect: is a bit slower. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1450 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1451 |
WARNING: do not add/remove elements while iterating over the receiver. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1452 |
Iterate over a copy to do this." |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1453 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1454 |
|newCollection| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1455 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1456 |
newCollection := self species new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1457 |
self keysAndValuesDo:[:key :value | |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1458 |
(aBlock value:key value:value) ifTrue:[ |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1459 |
newCollection at:key put:value |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1460 |
] |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1461 |
]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1462 |
^ newCollection |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1463 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1464 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1465 |
|ages| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1466 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1467 |
ages := Dictionary new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1468 |
ages at:'cg' put:37. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1469 |
ages at:'ca' put:33. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1470 |
ages at:'sv' put:36. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1471 |
ages at:'tk' put:28. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1472 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1473 |
ages keysAndValuesSelect:[:name :age | |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1474 |
(name startsWith:'c') or:[age < 30]]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1475 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1476 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1477 |
"Modified: 20.4.1996 / 11:34:29 / cg" |
3 | 1478 |
! |
1479 |
||
609 | 1480 |
keysDo:aBlock |
1056 | 1481 |
"perform the block for all keys in the collection. |
1482 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1483 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1484 |
#associationsDo: (which passes key-value associations) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1485 |
#keysAndValuesDo: (which passes keys & values separately) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1486 |
#do: (which passes values only) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1487 |
|
1056 | 1488 |
WARNING: do not add/remove elements while iterating over the receiver. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1489 |
Iterate over a copy to do this." |
1 | 1490 |
|
5336 | 1491 |
|sz "{ Class: SmallInteger }" |
1492 |
key| |
|
1056 | 1493 |
|
5336 | 1494 |
sz := keyArray size. |
1495 |
1 to:sz do:[:index | |
|
1496 |
key := keyArray at:index. |
|
1497 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
1498 |
key == NilKey ifTrue:[ |
|
1499 |
key := nil |
|
1500 |
]. |
|
1501 |
aBlock value:key |
|
1502 |
] |
|
1503 |
] |
|
1 | 1504 |
! |
1505 |
||
1506 |
select:aBlock |
|
1507 |
"return a new collection with all elements from the receiver, for which |
|
77 | 1508 |
the argument aBlock evaluates to true. The block gets the individual values |
1056 | 1509 |
as its single argument. |
1510 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1511 |
See also: |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1512 |
#associationsSelect: (which passes key->value associations), |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1513 |
#keysAndValuesSelect: (which passes key & value args) |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1514 |
|
1056 | 1515 |
WARNING: do not add/remove elements while iterating over the receiver. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1516 |
Iterate over a copy to do this." |
1 | 1517 |
|
1518 |
|newCollection| |
|
1519 |
||
1520 |
newCollection := self species new. |
|
77 | 1521 |
self keysAndValuesDo:[:key :value | |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1522 |
(aBlock value:value) ifTrue:[ |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1523 |
newCollection at:key put:value |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1524 |
] |
1 | 1525 |
]. |
1526 |
^ newCollection |
|
77 | 1527 |
|
1528 |
" |
|
1529 |
|d| |
|
1530 |
||
1531 |
d := Dictionary new. |
|
1532 |
d at:#foo put:#bar. |
|
1533 |
d at:#bar put:#baz. |
|
1534 |
d at:#baz put:#foo. |
|
1535 |
||
1536 |
d select:[:el | el startsWith:'b']. |
|
1537 |
" |
|
1056 | 1538 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1539 |
"Modified: 20.4.1996 / 11:35:07 / cg" |
7653
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1540 |
! |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1541 |
|
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1542 |
valuesDo:aBlock |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1543 |
"perform the block for all values in the collection. |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1544 |
Same as #do: - for VisualWorks compatibility" |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1545 |
|
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1546 |
^ self do:aBlock |
1 | 1547 |
! ! |
1548 |
||
609 | 1549 |
!Dictionary methodsFor:'inspecting'! |
1550 |
||
1551 |
inspectorClass |
|
1552 |
"redefined to use DictionaryInspector |
|
1553 |
(instead of the default Inspector)." |
|
1554 |
||
1555 |
^ DictionaryInspectorView |
|
1556 |
! ! |
|
1557 |
||
1558 |
!Dictionary methodsFor:'printing & storing'! |
|
1559 |
||
3192 | 1560 |
printElementsDo:aBlock |
1561 |
"redefined, so #printOn: prints associations" |
|
609 | 1562 |
|
3192 | 1563 |
^ self associationsDo:aBlock |
609 | 1564 |
|
3192 | 1565 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
609 | 1566 |
! |
1567 |
||
1568 |
storeOn:aStream |
|
1569 |
"output a printed representation (which can be re-read) |
|
1570 |
onto the argument aStream" |
|
1571 |
||
1572 |
|isEmpty| |
|
1573 |
||
1574 |
thisContext isRecursive ifTrue:[ |
|
2288 | 1575 |
('Dictionary [error]: storeOn: of self referencing collection.') errorPrintCR. |
1417 | 1576 |
aStream nextPutAll:'#recursive'. |
1577 |
^ self |
|
609 | 1578 |
]. |
1579 |
||
1580 |
aStream nextPutAll:'('. |
|
1581 |
aStream nextPutAll:(self class name). |
|
1582 |
aStream nextPutAll:' new'. |
|
1583 |
isEmpty := true. |
|
1584 |
self keysAndValuesDo:[:key :value | |
|
1417 | 1585 |
aStream nextPutAll:' at:'. |
1586 |
key storeOn:aStream. |
|
1587 |
aStream nextPutAll:' put:'. |
|
1588 |
value storeOn:aStream. |
|
1589 |
aStream nextPutAll:'; '. |
|
1590 |
isEmpty := false |
|
609 | 1591 |
]. |
1592 |
isEmpty ifFalse:[aStream nextPutAll:' yourself']. |
|
1593 |
aStream nextPut:$) |
|
1594 |
||
1595 |
" |
|
1596 |
Dictionary new storeOn:Transcript |
|
1597 |
||
1598 |
(Dictionary new at:1 put:'hello'; yourself) storeOn:Transcript |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1599 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1600 |
(Dictionary new at:1 put:'hello'; at:2 put:nil; yourself) storeOn:Transcript |
609 | 1601 |
" |
1602 |
||
1603 |
" |
|
1604 |
|d| |
|
1605 |
d := Dictionary new. |
|
1606 |
d at:1 put:'hello'. |
|
1607 |
d at:'hello' put:#world. |
|
1608 |
d storeOn:Transcript |
|
1609 |
" |
|
1610 |
||
1611 |
" |
|
1612 |
|d| |
|
1613 |
d := Dictionary new. |
|
1614 |
d at:1 put:'hello'. |
|
1615 |
d at:'hello' put:#world. |
|
1616 |
d at:2 put:d. |
|
1617 |
d storeOn:Transcript |
|
1618 |
" |
|
1417 | 1619 |
|
2288 | 1620 |
"Modified: 28.1.1997 / 00:37:46 / cg" |
609 | 1621 |
! ! |
1622 |
||
1 | 1623 |
!Dictionary methodsFor:'private'! |
1624 |
||
345 | 1625 |
compareSame:element1 with:element2 |
1256 | 1626 |
"compare two elements for being the same. Here, return true if the |
1627 |
elements are equal (i.e. using #=). |
|
1628 |
Redefinable in subclasses." |
|
1629 |
||
345 | 1630 |
^ element1 = element2 |
1256 | 1631 |
|
1632 |
"Modified: 22.4.1996 / 17:34:27 / cg" |
|
345 | 1633 |
! |
1634 |
||
609 | 1635 |
emptyCollectionForKeys |
1256 | 1636 |
"return an empty collection to hold keys. Here, a Set is returned. |
1637 |
Redefinable in subclasses." |
|
1638 |
||
609 | 1639 |
^ Set new:(self size) |
1256 | 1640 |
|
1641 |
"Modified: 22.4.1996 / 17:35:17 / cg" |
|
1 | 1642 |
! |
1643 |
||
1644 |
grow:newSize |
|
1645 |
"grow the receiver to make space for at least newSize elements. |
|
1646 |
To do this, we have to rehash into the new arrays. |
|
1647 |
(which is done by re-adding all elements to a new, empty key/value array pair)." |
|
1648 |
||
92 | 1649 |
|key deletedEntry oldKeyArray oldValueArray n |
1650 |
oldSize "{ Class:SmallInteger }" |
|
10 | 1651 |
newIndex "{ Class:SmallInteger }" | |
1 | 1652 |
|
10 | 1653 |
oldKeyArray := keyArray. |
1654 |
oldValueArray := valueArray. |
|
1 | 1655 |
|
249 | 1656 |
n := self class goodSizeFrom:newSize. |
1657 |
oldSize := oldKeyArray size. |
|
1658 |
n == oldSize ifTrue:[^ self]. |
|
1659 |
||
10 | 1660 |
keyArray := self keyContainerOfSize:n. |
92 | 1661 |
valueArray := self valueContainerOfSize:n. |
1 | 1662 |
|
249 | 1663 |
|
92 | 1664 |
deletedEntry := DeletedEntry. |
10 | 1665 |
1 to:oldSize do:[:index | |
155 | 1666 |
key := oldKeyArray basicAt:index. |
1667 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
|
1668 |
newIndex := self findNil:key. |
|
1669 |
keyArray basicAt:newIndex put:key. |
|
1670 |
valueArray basicAt:newIndex put:(oldValueArray basicAt:index). |
|
1671 |
] |
|
1 | 1672 |
] |
1673 |
! |
|
1674 |
||
1675 |
rehash |
|
1676 |
"rehash contents - is done by re-adding all elements to a new, empty key/value array pair)." |
|
1677 |
||
2 | 1678 |
| oldKeyArray oldValueArray key |
1679 |
n "{ Class:SmallInteger }" |
|
1680 |
newIndex "{ Class:SmallInteger }" | |
|
1 | 1681 |
|
1682 |
oldKeyArray := keyArray. |
|
1683 |
oldValueArray := valueArray. |
|
1684 |
||
1685 |
n := keyArray size. |
|
10 | 1686 |
keyArray := self keyContainerOfSize:n. |
92 | 1687 |
valueArray := self valueContainerOfSize:n. |
1 | 1688 |
|
2 | 1689 |
1 to:n do:[:index | |
155 | 1690 |
key := oldKeyArray basicAt:index. |
1691 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
1692 |
newIndex := self findNil:key. |
|
1693 |
keyArray basicAt:newIndex put:key. |
|
1694 |
valueArray basicAt:newIndex put:(oldValueArray basicAt:index). |
|
1695 |
] |
|
1 | 1696 |
] |
1697 |
! |
|
1698 |
||
1699 |
rehashFrom:startIndex |
|
77 | 1700 |
"rehash elements starting at index - after a remove. |
1701 |
NOTE: this method is no longer needed; |
|
155 | 1702 |
the trick using DeletedEntry avoids the need to do this time |
1703 |
consuming operation, making remove pretty fast :-) |
|
77 | 1704 |
" |
1 | 1705 |
|
1706 |
|key i length |
|
1707 |
index "{ Class:SmallInteger }" | |
|
1708 |
||
1709 |
length := keyArray basicSize. |
|
1710 |
index := startIndex. |
|
1711 |
key := keyArray basicAt:index. |
|
1712 |
[key notNil] whileTrue:[ |
|
155 | 1713 |
key ~~ DeletedEntry ifTrue:[ |
1714 |
i := self findNil:key. |
|
1715 |
i == index ifTrue:[ |
|
1716 |
^ self |
|
1717 |
]. |
|
1718 |
keyArray basicAt:i put:key. |
|
1719 |
valueArray basicAt:i put:(valueArray basicAt:index). |
|
1720 |
keyArray basicAt:index put:nil. |
|
1721 |
valueArray basicAt:index put:nil. |
|
1722 |
]. |
|
1723 |
index == length ifTrue:[ |
|
1724 |
index := 1 |
|
1725 |
] ifFalse:[ |
|
1726 |
index := index + 1. |
|
1727 |
]. |
|
1728 |
key := keyArray basicAt:index. |
|
1 | 1729 |
] |
77 | 1730 |
! |
1731 |
||
609 | 1732 |
setTally:count |
1733 |
"initialize the contents array (for at least count slots) |
|
1734 |
and set tally to zero. |
|
1735 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 1736 |
|
609 | 1737 |
|n| |
1 | 1738 |
|
609 | 1739 |
n := self class goodSizeFrom:count. |
3727
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1740 |
n == keyArray size ifTrue:[ |
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1741 |
keyArray atAllPut:nil. |
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1742 |
valueArray atAllPut:nil. |
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1743 |
] ifFalse:[ |
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1744 |
keyArray := self keyContainerOfSize:n. |
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1745 |
valueArray := self valueContainerOfSize:n. |
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1746 |
]. |
609 | 1747 |
tally := 0 |
3727
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1748 |
|
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1749 |
"Modified: / 5.8.1998 / 10:48:51 / cg" |
44 | 1750 |
! |
1751 |
||
609 | 1752 |
valueContainerOfSize:n |
1753 |
"return a container for values of size n. |
|
1754 |
Extracted to make life of weak subclasses easier ..." |
|
44 | 1755 |
|
609 | 1756 |
^ Array basicNew:n |
1757 |
! ! |
|
1758 |
||
1460 | 1759 |
!Dictionary methodsFor:'searching'! |
1760 |
||
1761 |
findFirstKey:aBlock |
|
1762 |
"find and return the first key, for which evaluation of the argument, aBlock |
|
1703 | 1763 |
returns true; return nil if none is detected." |
1460 | 1764 |
|
1765 |
self keysDo:[:key | |
|
1766 |
(aBlock value:key) ifTrue:[^ key]. |
|
1767 |
]. |
|
1768 |
^ nil |
|
1769 |
||
1770 |
"Created: 5.6.1996 / 11:55:50 / stefan" |
|
1703 | 1771 |
"Modified: 8.10.1996 / 22:01:59 / cg" |
1460 | 1772 |
! ! |
1773 |
||
609 | 1774 |
!Dictionary methodsFor:'testing'! |
44 | 1775 |
|
609 | 1776 |
includes:anObject |
1777 |
"return true, if the argument, aValue is stored in the dictionary, |
|
1778 |
i.e. if there is an associaten, with aValue as value. |
|
1779 |
This is a slow search, since there is no fast reverse mapping; |
|
1780 |
the values have to be all scanned without any hashing. |
|
1781 |
You need a special collection (or two Dictionaries) to get this |
|
1782 |
reverse mapping fast." |
|
1783 |
||
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1784 |
^ self includesEqualValue:anObject |
1256 | 1785 |
|
1786 |
"Modified: 22.4.1996 / 17:20:11 / cg" |
|
609 | 1787 |
! |
68 | 1788 |
|
609 | 1789 |
includesAssociation:anAssociation |
1790 |
"return true, if there is an association in the receiver with the |
|
1791 |
same key and value as the argument, anAssociation. |
|
1792 |
NOTICE: in contrast to #includes:, this compares both key and value." |
|
1793 |
||
1794 |
|val| |
|
44 | 1795 |
|
609 | 1796 |
val := self at:(anAssociation key) ifAbsent:[^ false]. |
3318
d8173a888c93
oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents:
3304
diff
changeset
|
1797 |
^ self compareSame:val with:anAssociation value |
d8173a888c93
oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents:
3304
diff
changeset
|
1798 |
|
d8173a888c93
oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents:
3304
diff
changeset
|
1799 |
"Modified: / 5.3.1998 / 20:35:00 / cg" |
609 | 1800 |
! |
1801 |
||
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1802 |
includesEqualValue:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1803 |
"return true, if the argument, aValue is stored in the dictionary, |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1804 |
i.e. if there is an associaten, with aValue as value. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1805 |
This is a slow search, since there is no fast reverse mapping; |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1806 |
the values have to be all scanned without any hashing. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1807 |
You need a special collection (or two Dictionaries) to get this |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1808 |
reverse mapping fast." |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1809 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1810 |
aValue isNil ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1811 |
"/ need a special case for that ... |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1812 |
^ self includesIdenticalValue:aValue. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1813 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1814 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1815 |
^ valueArray includes:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1816 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1817 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1818 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1819 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1820 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1821 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1822 |
d includes:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1823 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1824 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1825 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1826 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1827 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1828 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1829 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1830 |
d at:2 put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1831 |
d includes:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1832 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1833 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1834 |
! |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1835 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1836 |
includesIdenticalValue:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1837 |
"return true, if the argument, aValue is stored in the dictionary, |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1838 |
i.e. if there is an associaten, with aValue as value. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1839 |
This is a slow search, since there is no fast reverse mapping; |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1840 |
the values have to be all scanned without any hashing. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1841 |
You need a special collection (or two Dictionaries) to get this |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1842 |
reverse mapping fast." |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1843 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1844 |
|idx| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1845 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1846 |
aValue isNil ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1847 |
"/ need a special case for that ... |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1848 |
idx := 0. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1849 |
[true] whileTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1850 |
idx := valueArray identityIndexOf:nil startingAt:idx+1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1851 |
idx == 0 ifTrue:[^ false]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1852 |
(keyArray at:idx) notNil ifTrue:[^ true]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1853 |
] |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1854 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1855 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1856 |
^ valueArray includesIdentical:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1857 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1858 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1859 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1860 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1861 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1862 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1863 |
d includes:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1864 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1865 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1866 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1867 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1868 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1869 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1870 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1871 |
d at:2 put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1872 |
d includes:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1873 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1874 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1875 |
! |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1876 |
|
609 | 1877 |
includesKey:aKey |
1878 |
"return true, if the argument, aKey is a key in the receiver" |
|
1879 |
||
1880 |
^ (self find:aKey ifAbsent:0) ~~ 0 |
|
1881 |
! |
|
293 | 1882 |
|
609 | 1883 |
includesValue:aValue |
1884 |
"return true, if the argument, aValue is stored in the dictionary, |
|
1885 |
i.e. if there is an associaten, with aValue as value. |
|
1886 |
This is a slow search, since there is no fast reverse mapping; |
|
1887 |
the values have to be all scanned without any hashing. |
|
1888 |
You need a special collection (or two Dictionaries) to get this |
|
1889 |
reverse mapping fast." |
|
293 | 1890 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1891 |
^ self includesEqualValue:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1892 |
|
609 | 1893 |
! |
293 | 1894 |
|
609 | 1895 |
occurrencesOf:anObject |
1896 |
"count & return how often anObject is stored in the dictionary. |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1897 |
This counts values - not keys. Uses #= (i.e. equality) compare." |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1898 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1899 |
|idx count| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1900 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1901 |
anObject isNil ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1902 |
"/ need a special case for that ... |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1903 |
idx := 0. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1904 |
count := 0. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1905 |
[true] whileTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1906 |
idx := valueArray identityIndexOf:nil startingAt:idx+1. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1907 |
idx == 0 ifTrue:[^ count]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1908 |
(keyArray at:idx) notNil ifTrue:[ |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1909 |
count := count + 1 |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1910 |
] |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1911 |
] |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1912 |
]. |
609 | 1913 |
|
1914 |
^ valueArray occurrencesOf:anObject |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1915 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1916 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1917 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1918 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1919 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1920 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1921 |
d occurrencesOf:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1922 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1923 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1924 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1925 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1926 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1927 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1928 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1929 |
d at:2 put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1930 |
d at:5 put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1931 |
d occurrencesOf:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1932 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1933 |
|
1 | 1934 |
! ! |
609 | 1935 |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
1936 |
!Dictionary methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
1937 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
1938 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
1939 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
1940 |
^ aVisitor visitDictionary:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
1941 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
1942 |
|
5336 | 1943 |
!Dictionary::NilKey class methodsFor:'instance creation'! |
1944 |
||
1945 |
basicNew |
|
1946 |
TheOneAndOnlyInstance isNil ifTrue:[ |
|
1947 |
TheOneAndOnlyInstance := super basicNew |
|
1948 |
]. |
|
1949 |
^ TheOneAndOnlyInstance |
|
1950 |
||
1951 |
||
1952 |
! ! |
|
1953 |
||
2094
42cd02703bf4
handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
1954 |
!Dictionary class methodsFor:'documentation'! |
633 | 1955 |
|
1956 |
version |
|
9413 | 1957 |
^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.87 2006-07-03 16:11:03 stefan Exp $' |
633 | 1958 |
! ! |
7306 | 1959 |
|
5336 | 1960 |
Dictionary initialize! |