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