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