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