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