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