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