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