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