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