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