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