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