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