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