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