author | Stefan Vogel <sv@exept.de> |
Wed, 29 Jan 2014 17:08:17 +0100 | |
changeset 15923 | 6bc82606b914 |
parent 15857 | bf865a059191 |
child 16451 | 59f63673573d |
child 18113 | 92b4242b2b0b |
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 |
||
15527 | 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 |
||
9938 | 643 |
declareAllFrom:aDictionaryOrNil |
8434
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
644 |
"merge all key-value pairs from aDictionary into the receiver. |
98ae6daa9aac
Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
645 |
|
9938 | 646 |
sigh: |
14318 | 647 |
For compatibility with #declare:from: the behavior should be changed as following: |
648 |
If the receiver already contains a key, the existing value is retained. |
|
649 |
To keep the compatibility with other smalltalks, the semantics of this remains |
|
650 |
as is, and #declareAllNewFrom: was added for convenience. |
|
651 |
See #declareAllNewFrom: which does exactly what this name implies." |
|
2711 | 652 |
|
9938 | 653 |
self ~~ aDictionaryOrNil ifTrue:[ |
14318 | 654 |
aDictionaryOrNil notNil ifTrue:[ |
655 |
aDictionaryOrNil keysAndValuesDo:[:key :value | |
|
656 |
self at:key put:value. |
|
657 |
]. |
|
658 |
] |
|
2711 | 659 |
] |
9938 | 660 |
|
9939 | 661 |
"Modified: / 18-09-2006 / 22:01:12 / cg" |
9938 | 662 |
! |
663 |
||
664 |
declareAllNewFrom:aDictionaryOrNil |
|
665 |
"merge all new key-value pairs from aDictionary into the receiver |
|
666 |
i.e. If the receiver already contains a key, the existing value is retained. |
|
667 |
See also #declareAllFrom:" |
|
668 |
||
669 |
self ~~ aDictionaryOrNil ifTrue:[ |
|
14318 | 670 |
aDictionaryOrNil notNil ifTrue:[ |
671 |
aDictionaryOrNil keysAndValuesDo:[:key :value | |
|
672 |
(self includesKey:key) ifFalse:[ |
|
673 |
self at:key put:value. |
|
674 |
]. |
|
675 |
] |
|
676 |
] |
|
9938 | 677 |
] |
678 |
||
679 |
"Created: / 18-09-2006 / 21:58:54 / cg" |
|
2711 | 680 |
! |
681 |
||
1 | 682 |
remove:oldObject ifAbsent:aBlock |
683 |
"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
|
684 |
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
|
685 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
686 |
This is blocked here; you have to use one of |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
687 |
#removeKey:, #saveRemoveKey:, #removeAssociation:, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
688 |
#removeValue: or #saveRemoveValue:" |
1 | 689 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
690 |
^ self shouldNotImplement |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
691 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
692 |
"Modified: 1.3.1996 / 21:21:38 / cg" |
1 | 693 |
! |
694 |
||
6053
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
695 |
removeAllKeys:aKeyCollection |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
696 |
"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
|
697 |
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
|
698 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
699 |
WARNING: do not remove elements while iterating over the receiver. |
14318 | 700 |
See #saveRemoveKey: to do this." |
6053
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
701 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
702 |
aKeyCollection do:[:eachKey | |
14318 | 703 |
self removeKey:eachKey ifAbsent:[self errorKeyNotFound:eachKey] |
6053
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
704 |
]. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
705 |
! |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
706 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
707 |
removeAllKeys:aKeyCollection ifAbsent:aBlock |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
708 |
"remove all associations under each key in aKeyCollection from the collection. |
15527 | 709 |
If it was not in the collection return the result from evaluating aBlock |
710 |
(invoked for each missing element). |
|
6053
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
711 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
712 |
WARNING: do not remove elements while iterating over the receiver. |
15527 | 713 |
See #saveRemoveKey: to do this." |
6053
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
714 |
|
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
715 |
aKeyCollection do:[:eachKey | |
15527 | 716 |
self removeKey:eachKey ifAbsent:aBlock |
6053
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
717 |
]. |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
718 |
! |
a97f27828c08
added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5865
diff
changeset
|
719 |
|
1 | 720 |
removeAssociation:assoc |
721 |
"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
|
722 |
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
|
723 |
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
|
724 |
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
|
725 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
726 |
WARNING: do not remove elements while iterating over the receiver. |
12669
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
727 |
See #saveRemoveKey: to do this." |
1 | 728 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
729 |
|key| |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
730 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
731 |
key := assoc key. |
2897 | 732 |
^ 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
|
733 |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
734 |
"Modified: 1.3.1996 / 21:21:11 / cg" |
1 | 735 |
! |
736 |
||
737 |
removeKey:aKey |
|
738 |
"remove the association under aKey from the collection. |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
739 |
If it was not in the collection report an error. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
740 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
741 |
WARNING: do not remove elements while iterating over the receiver. |
12669
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
742 |
See #saveRemoveKey: to do this." |
1 | 743 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
744 |
^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey] |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
745 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
746 |
"Modified: 1.3.1996 / 21:21:52 / cg" |
1 | 747 |
! |
748 |
||
749 |
removeKey:aKey ifAbsent:aBlock |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
750 |
"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
|
751 |
return the value previously stored there.. |
14318 | 752 |
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
|
753 |
from evaluating aBlock. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
754 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
755 |
WARNING: do not remove elements while iterating over the receiver. |
14318 | 756 |
See #saveRemoveKey: to do this." |
1 | 757 |
|
3 | 758 |
|index "{ Class:SmallInteger }" |
14318 | 759 |
"/ next "{ Class:SmallInteger }" |
5336 | 760 |
oldValue k| |
1 | 761 |
|
5336 | 762 |
(k := aKey) isNil ifTrue:[ |
763 |
"/ no longer invalid |
|
764 |
"/ ^ self errorInvalidKey:aKey |
|
14318 | 765 |
k := NilEntry |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
766 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
767 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
768 |
"/ below, I could have written: |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
769 |
"/ 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
|
770 |
"/ 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
|
771 |
"/ 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
|
772 |
|
5336 | 773 |
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
|
774 |
index == 0 ifTrue:[^ aBlock value]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
775 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
776 |
oldValue := valueArray basicAt:index. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
777 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
778 |
valueArray basicAt:index put:nil. |
5336 | 779 |
keyArray basicAt:index put:DeletedEntry. |
77 | 780 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
781 |
tally := tally - 1. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
782 |
tally == 0 ifTrue:[ |
14318 | 783 |
self setTally:0 |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
784 |
] ifFalse:[ |
5336 | 785 |
"/ index == keyArray basicSize ifTrue:[ |
786 |
"/ next := 1 |
|
787 |
"/ ] ifFalse:[ |
|
788 |
"/ next := index + 1. |
|
789 |
"/ ]. |
|
790 |
"/ (keyArray basicAt:next) notNil ifTrue:[ |
|
791 |
"/ keyArray basicAt:index put:DeletedEntry |
|
792 |
"/ ]. |
|
14318 | 793 |
self emptyCheck |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
794 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
795 |
^ oldValue |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
796 |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
797 |
"Modified: 1.3.1996 / 21:21:01 / cg" |
77 | 798 |
! |
799 |
||
5184
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
800 |
removeValue:aValue |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
801 |
"remove (first) the association to aValue from the collection, |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
802 |
return the key under which it was stored previously. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
803 |
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
|
804 |
The value is searched using equality compare here, |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
805 |
but identity compare in the IdentityDictionary subclass. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
806 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
807 |
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
|
808 |
therefore be slow for big dictionaries. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
809 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
810 |
WARNING: do not remove elements while iterating over the receiver. |
14318 | 811 |
See #saveRemoveValue: to do this." |
5184
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
812 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
813 |
^ self removeValue:aValue ifAbsent:[self errorValueNotFound:aValue] |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
814 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
815 |
" |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
816 |
|d| |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
817 |
|
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
818 |
d := Dictionary new. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
819 |
d at:1 put:'one'. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
820 |
d at:2 put:'two'. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
821 |
d at:3 put:'three'. |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
822 |
d removeValue:'two'. |
14318 | 823 |
d |
5184
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
824 |
" |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
825 |
! |
cbf20f052178
added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
5050
diff
changeset
|
826 |
|
77 | 827 |
removeValue:aValue ifAbsent:aBlock |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
828 |
"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
|
829 |
return the key under which it was stored previously. |
77 | 830 |
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
|
831 |
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
|
832 |
but identity compare in the IdentityDictionary subclass. |
77 | 833 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
834 |
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
|
835 |
therefore be slow for big dictionaries. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
836 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
837 |
WARNING: do not remove elements while iterating over the receiver. |
14318 | 838 |
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
|
839 |
|
14318 | 840 |
|next "{ Class:SmallInteger }" |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
841 |
oldKey| |
77 | 842 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
843 |
keyArray keysAndValuesDo:[:index :aKey | |
14318 | 844 |
|idx "{Class:SmallInteger}"| |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
845 |
|
14318 | 846 |
(aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[ |
847 |
idx := index. |
|
848 |
(self compareSame:(valueArray at:idx) with:aValue) ifTrue:[ |
|
849 |
"found it" |
|
850 |
valueArray basicAt:idx put:nil. |
|
851 |
oldKey := keyArray basicAt:idx. |
|
852 |
oldKey == NilEntry ifTrue:[ |
|
853 |
oldKey := nil |
|
854 |
]. |
|
855 |
keyArray basicAt:idx put:nil. |
|
856 |
tally := tally - 1. |
|
857 |
tally == 0 ifTrue:[ |
|
858 |
self setTally:0. |
|
859 |
^ oldKey |
|
860 |
]. |
|
5336 | 861 |
|
14318 | 862 |
idx == keyArray basicSize ifTrue:[ |
863 |
next := 1 |
|
864 |
] ifFalse:[ |
|
865 |
next := index + 1. |
|
866 |
]. |
|
867 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
868 |
keyArray basicAt:idx put:DeletedEntry |
|
869 |
]. |
|
870 |
self emptyCheck. |
|
871 |
^ oldKey |
|
872 |
] |
|
873 |
] |
|
77 | 874 |
]. |
875 |
^ aBlock value |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
876 |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
877 |
"Modified: 1.3.1996 / 21:22:11 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
878 |
! |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
879 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
880 |
saveRemoveKey:aKey |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
881 |
"remove the association under aKey from the collection. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
882 |
Return the value previously stored there. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
883 |
If it was not in the collection return nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
884 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
885 |
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
|
886 |
and therefore does NOT rehash & change the elements order. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
887 |
Therefor this can be used while enumerating the receiver, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
888 |
which is not possible if #removeKey: is used. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
889 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
890 |
WARNING: since no resizing is done, the physical amount of memory used |
14318 | 891 |
by the container remains the same, although the logical size shrinks. |
892 |
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
|
893 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
894 |
|index "{ Class:SmallInteger }" |
14318 | 895 |
next "{ Class:SmallInteger }" |
5336 | 896 |
oldValue k| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
897 |
|
5336 | 898 |
(k := aKey) isNil ifTrue:[ |
14318 | 899 |
k := NilEntry |
5336 | 900 |
]. |
901 |
"/ aKey isNil ifTrue:[^ nil]. |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
902 |
|
5336 | 903 |
index := self find:k ifAbsent:0. |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
904 |
index == 0 ifTrue:[^ nil]. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
905 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
906 |
oldValue := valueArray basicAt:index. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
907 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
908 |
valueArray basicAt:index put:nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
909 |
keyArray basicAt:index put:nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
910 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
911 |
tally := tally - 1. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
912 |
tally ~~ 0 ifTrue:[ |
14318 | 913 |
index == keyArray basicSize ifTrue:[ |
914 |
next := 1 |
|
915 |
] ifFalse:[ |
|
916 |
next := index + 1. |
|
917 |
]. |
|
918 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
919 |
keyArray basicAt:index put:DeletedEntry |
|
920 |
]. |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
921 |
]. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
922 |
^ oldValue |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
923 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
924 |
"does NOT work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
925 |
|
14318 | 926 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
927 |
|
14318 | 928 |
d := Dictionary new. |
929 |
d at:'one' put:1. |
|
930 |
d at:'two' put:2. |
|
931 |
d at:'three' put:3. |
|
932 |
d at:'four' put:4. |
|
933 |
d at:'five' put:5. |
|
934 |
d at:'six' put:6. |
|
935 |
d at:'seven' put:7. |
|
936 |
d at:'eight' put:8. |
|
937 |
d at:'nine' put:9. |
|
938 |
d keysAndValuesDo:[:k :v | |
|
939 |
v odd ifTrue:[ |
|
940 |
d removeKey:k |
|
941 |
] |
|
942 |
]. |
|
943 |
d inspect |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
944 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
945 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
946 |
"DOES work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
947 |
|
14318 | 948 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
949 |
|
14318 | 950 |
d := Dictionary new. |
951 |
d at:'one' put:1. |
|
952 |
d at:'two' put:2. |
|
953 |
d at:'three' put:3. |
|
954 |
d at:'four' put:4. |
|
955 |
d at:'five' put:5. |
|
956 |
d at:'six' put:6. |
|
957 |
d at:'seven' put:7. |
|
958 |
d at:'eight' put:8. |
|
959 |
d at:'nine' put:9. |
|
960 |
d keysAndValuesDo:[:k :v | |
|
961 |
v odd ifTrue:[ |
|
962 |
d saveRemoveKey:k |
|
963 |
] |
|
964 |
]. |
|
965 |
d inspect |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
966 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
967 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
968 |
"Created: 1.3.1996 / 21:14:42 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
969 |
"Modified: 1.3.1996 / 21:14:53 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
970 |
! |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
971 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
972 |
saveRemoveValue:aValue |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
973 |
"remove the (first) association to aValue from the collection, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
974 |
return the key under which it was stored previously. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
975 |
If it was not in the collection return nil. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
976 |
The value is searched using equality compare here, |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
977 |
but identity compare in the IdentityDictionary subclass. |
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 |
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
|
980 |
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
|
981 |
Therefore, this can be used while enumerating the receiver, |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
982 |
which is not possible if #removeValue: is used. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
983 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
984 |
WARNING: since no resizing is done, the physical amount of memory used |
14318 | 985 |
by the container remains the same, although the logical size shrinks. |
986 |
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
|
987 |
|
14318 | 988 |
|next "{ Class:SmallInteger }" |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
989 |
oldKey| |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
990 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
991 |
keyArray keysAndValuesDo:[:index :aKey | |
14318 | 992 |
|idx "{Class:SmallInteger}"| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
993 |
|
14318 | 994 |
(aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[ |
995 |
idx := index. |
|
996 |
(self compareSame:(valueArray at:idx) with:aValue) ifTrue:[ |
|
997 |
"found it" |
|
998 |
valueArray basicAt:idx put:nil. |
|
999 |
oldKey := keyArray basicAt:idx. |
|
1000 |
oldKey == NilEntry ifTrue:[ |
|
1001 |
oldKey := nil |
|
1002 |
]. |
|
1003 |
keyArray basicAt:idx put:nil. |
|
1004 |
tally := tally - 1. |
|
1005 |
tally ~~ 0 ifTrue:[ |
|
1006 |
idx == keyArray basicSize ifTrue:[ |
|
1007 |
next := 1 |
|
1008 |
] ifFalse:[ |
|
1009 |
next := index + 1. |
|
1010 |
]. |
|
1011 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
1012 |
keyArray basicAt:idx put:DeletedEntry |
|
1013 |
]. |
|
1014 |
]. |
|
1015 |
^ oldKey |
|
1016 |
] |
|
1017 |
] |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1018 |
]. |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1019 |
^ aValue |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1020 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1021 |
"does NOT work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1022 |
|
14318 | 1023 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1024 |
|
14318 | 1025 |
d := Dictionary new. |
1026 |
d at:'one' put:1. |
|
1027 |
d at:'two' put:2. |
|
1028 |
d at:'three' put:3. |
|
1029 |
d at:'four' put:4. |
|
1030 |
d at:'five' put:5. |
|
1031 |
d at:'six' put:6. |
|
1032 |
d at:'seven' put:7. |
|
1033 |
d at:'eight' put:8. |
|
1034 |
d at:'nine' put:9. |
|
1035 |
d keysAndValuesDo:[:k :v | |
|
1036 |
v odd ifTrue:[ |
|
1037 |
d removeValue:v ifAbsent:nil |
|
1038 |
] |
|
1039 |
]. |
|
1040 |
d inspect |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1041 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1042 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1043 |
"DOES work: |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1044 |
|
14318 | 1045 |
|d| |
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1046 |
|
14318 | 1047 |
d := Dictionary new. |
1048 |
d at:'one' put:1. |
|
1049 |
d at:'two' put:2. |
|
1050 |
d at:'three' put:3. |
|
1051 |
d at:'four' put:4. |
|
1052 |
d at:'five' put:5. |
|
1053 |
d at:'six' put:6. |
|
1054 |
d at:'seven' put:7. |
|
1055 |
d at:'eight' put:8. |
|
1056 |
d at:'nine' put:9. |
|
1057 |
d keysAndValuesDo:[:k :v | |
|
1058 |
v odd ifTrue:[ |
|
1059 |
d saveRemoveValue:v |
|
1060 |
] |
|
1061 |
]. |
|
1062 |
d inspect |
|
1053
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1063 |
" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1064 |
|
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1065 |
"Created: 1.3.1996 / 21:17:10 / cg" |
ac1ea83233e6
added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents:
641
diff
changeset
|
1066 |
"Modified: 1.3.1996 / 21:23:04 / cg" |
1 | 1067 |
! ! |
1068 |
||
5279 | 1069 |
!Dictionary methodsFor:'comparing'! |
1070 |
||
1071 |
= aCollection |
|
14318 | 1072 |
"return true, if the argument is a Dictionary containing the same |
5279 | 1073 |
key-value pairs as I do" |
1074 |
||
1075 |
aCollection species == self species ifFalse:[^ false]. |
|
1076 |
aCollection size == self size ifFalse:[^ false]. |
|
1077 |
"/ all of of my key-value associations must be in the other collection ... |
|
1078 |
self keysAndValuesDo:[:key :value | |
|
14318 | 1079 |
((aCollection at:key ifAbsent:[^ false]) = value) ifFalse:[^ false] |
5279 | 1080 |
]. |
1081 |
^ true |
|
1082 |
||
1083 |
" |
|
1084 |
|d1 d2| |
|
1085 |
||
1086 |
d1 := Dictionary new. |
|
1087 |
d2 := Dictionary new. |
|
1088 |
d1 at:1 put:'one'. |
|
1089 |
d1 at:'one' put:1. |
|
1090 |
d1 at:2 put:#two. |
|
1091 |
d1 at:'two' put:2. |
|
1092 |
||
1093 |
d2 at:1 put:'one'. |
|
1094 |
d2 at:'one' put:1. |
|
1095 |
d2 at:2 put:#two. |
|
1096 |
d2 at:'two' put:2. |
|
14318 | 1097 |
d1 = d2 |
5279 | 1098 |
" |
1099 |
||
1100 |
" |
|
1101 |
|d1 d2| |
|
1102 |
||
1103 |
d1 := Dictionary new. |
|
1104 |
d2 := Dictionary new. |
|
1105 |
d1 at:1 put:'uno'. |
|
1106 |
d1 at:'one' put:1. |
|
1107 |
d1 at:2 put:#two. |
|
1108 |
d1 at:'two' put:2. |
|
1109 |
||
1110 |
d2 at:1 put:'one'. |
|
1111 |
d2 at:'one' put:1. |
|
1112 |
d2 at:2 put:#two. |
|
1113 |
d2 at:'two' put:2. |
|
14318 | 1114 |
d1 = d2 |
5279 | 1115 |
" |
1116 |
||
1117 |
" |
|
1118 |
|d1 d2| |
|
1119 |
||
1120 |
d1 := Dictionary new. |
|
1121 |
d2 := Dictionary new. |
|
1122 |
d1 at:10 put:'one'. |
|
1123 |
d1 at:'one' put:1. |
|
1124 |
d1 at:2 put:#two. |
|
1125 |
d1 at:'two' put:2. |
|
1126 |
||
1127 |
d2 at:1 put:'one'. |
|
1128 |
d2 at:'one' put:1. |
|
1129 |
d2 at:2 put:#two. |
|
1130 |
d2 at:'two' put:2. |
|
14318 | 1131 |
d1 = d2 |
5279 | 1132 |
" |
1133 |
||
1134 |
||
1135 |
||
1136 |
! ! |
|
1137 |
||
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1138 |
!Dictionary methodsFor:'converting'! |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1139 |
|
7906 | 1140 |
asDictionary |
1141 |
^ self |
|
1142 |
! |
|
1143 |
||
14997 | 1144 |
asNewDictionary |
1145 |
"return myself as an unique new dictionary" |
|
1146 |
||
1147 |
^ self copy |
|
1148 |
! |
|
1149 |
||
14271 | 1150 |
associationsOrderedBy:aCollectionOfKeys |
1151 |
"return an OrderedCollection of my key-value pairs, ordered by the given key list" |
|
1152 |
||
1153 |
self assert:(aCollectionOfKeys size == self size). |
|
1154 |
^ aCollectionOfKeys collect:[:eachKey | eachKey -> (self at:eachKey) ] as: OrderedCollection |
|
1155 |
||
1156 |
" |
|
1157 |
|d| |
|
1158 |
||
1159 |
d := Dictionary new. |
|
1160 |
d at:'zzz' put:3. |
|
1161 |
d at:'aaa' put:1. |
|
1162 |
d at:'eee' put:2. |
|
1163 |
d. |
|
14318 | 1164 |
d valuesOrderedBy:#('aaa' 'eee' 'zzz'). |
1165 |
d associationsOrderedBy:#('aaa' 'eee' 'zzz'). |
|
14271 | 1166 |
" |
1167 |
||
1168 |
"Created: / 31-07-2012 / 17:32:34 / cg" |
|
1169 |
! |
|
1170 |
||
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1171 |
fromLiteralArrayEncoding:encoding |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1172 |
"read my values from an encoding. |
14318 | 1173 |
The encoding is supposed to be of the form: |
1174 |
(Dictionary key1 val1 ... keyN valN)" |
|
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1175 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1176 |
2 to:encoding size by:2 do:[:i | |
14318 | 1177 |
|key val| |
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1178 |
|
14318 | 1179 |
key := encoding at:i. |
1180 |
val := encoding at:i+1. |
|
1181 |
self at:key put:val |
|
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1182 |
]. |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1183 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1184 |
" |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1185 |
Dictionary new fromLiteralArrayEncoding:#(Dictionary 'hello' 'world' 1 'foo') |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1186 |
" |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1187 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1188 |
"Created: / 30.1.1998 / 04:30:47 / cg" |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1189 |
! |
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1190 |
|
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1191 |
literalArrayEncoding |
9413 | 1192 |
|literalArrray idx| |
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1193 |
|
9413 | 1194 |
literalArrray := Array new:(self size * 2)+1. |
1195 |
literalArrray at:1 put:self class name. |
|
1196 |
idx := 2. |
|
1197 |
self keysAndValuesDo:[:eachKey :eachValue | |
|
14318 | 1198 |
literalArrray |
1199 |
at:idx put:eachKey literalArrayEncoding; |
|
1200 |
at:idx+1 put:eachValue literalArrayEncoding. |
|
1201 |
idx := idx + 2. |
|
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1202 |
]. |
9413 | 1203 |
^ literalArrray |
1204 |
||
1205 |
" |
|
1206 |
|dict| |
|
3252
da066eafc2c5
added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents:
3215
diff
changeset
|
1207 |
|
9413 | 1208 |
dict := Dictionary new. |
1209 |
dict at:1 put:'bla'. |
|
1210 |
dict at:'fasel' put:#[1 2 3 4]. |
|
1211 |
dict literalArrayEncoding |
|
1212 |
" |
|
14271 | 1213 |
! |
1214 |
||
1215 |
valuesOrderedBy:aCollectionOfKeys |
|
1216 |
"return an OrderedCollection of my values, ordered by the given key list" |
|
1217 |
||
1218 |
self assert:(aCollectionOfKeys size == self size). |
|
1219 |
^ aCollectionOfKeys collect:[:eachKey | self at:eachKey ] as: OrderedCollection |
|
1220 |
||
1221 |
"Created: / 31-07-2012 / 17:33:12 / cg" |
|
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 |
|
155 | 1224 |
!Dictionary methodsFor:'copying'! |
1225 |
||
11909 | 1226 |
, anotherDictionaryOrAssociation |
1227 |
"return a new dictionary containing a merged set of associations. |
|
1228 |
If anotherDictionaryOrAssociation includes any of the receiver's keys, |
|
14318 | 1229 |
the value from anotherDictionaryOrAssociation will be placed into the |
11909 | 1230 |
returned result." |
9066 | 1231 |
|
1232 |
|newDictionary| |
|
1233 |
||
11909 | 1234 |
newDictionary := self copy. |
1235 |
newDictionary declareAllFrom:anotherDictionaryOrAssociation. |
|
9066 | 1236 |
^ newDictionary |
11909 | 1237 |
|
1238 |
" |
|
1239 |
|d1 d2| |
|
1240 |
||
1241 |
d1 := Dictionary new. |
|
1242 |
d1 at:#a put:'aaa'. |
|
1243 |
d1 at:#b put:'bbb'. |
|
1244 |
||
1245 |
d2 := Dictionary new. |
|
1246 |
d2 at:#b put:'bbbb'. |
|
1247 |
d2 at:#c put:'ccc'. |
|
1248 |
||
1249 |
d1 , d2 |
|
1250 |
" |
|
1251 |
" |
|
1252 |
|d1 d2| |
|
1253 |
||
1254 |
d1 := Dictionary new. |
|
1255 |
d1 at:#a put:'aaa'. |
|
1256 |
d1 at:#b put:'bbb'. |
|
1257 |
d2 := d1 , (#c -> 'ccc'). |
|
1258 |
d2 |
|
1259 |
" |
|
9066 | 1260 |
! |
1261 |
||
155 | 1262 |
postCopy |
1263 |
"have to copy the valueArray too" |
|
1264 |
||
1265 |
super postCopy. |
|
1266 |
valueArray := valueArray shallowCopy |
|
1267 |
! ! |
|
1268 |
||
217 | 1269 |
!Dictionary methodsFor:'enumerating'! |
1 | 1270 |
|
1271 |
allKeysDo:aBlock |
|
302 | 1272 |
"perform the block for all keys in the collection. |
1273 |
Obsolete: use keysDo: for ST-80 compatibility." |
|
1 | 1274 |
|
5865 | 1275 |
<resource:#obsolete> |
1276 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1277 |
self obsoleteMethodWarning:'please use #keysDo:'. |
155 | 1278 |
^ super do:aBlock |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1279 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1280 |
"Modified: 20.4.1996 / 11:22:01 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1281 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1282 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1283 |
associationsCollect:aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1284 |
"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
|
1285 |
and return a collection with the results. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1286 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1287 |
See also: |
14318 | 1288 |
#keysAndValuesCollect: (which passes separate keys & values) |
1289 |
#collect: (which only passes values) |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1290 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1291 |
This is much like #keysAndValuesCollect:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1292 |
key and value as a single association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1293 |
#keysAndValuesCollect: and is a bit faster therefore (no intermediate objects). |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1294 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1295 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1296 |
Iterate over a copy to do this." |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1297 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1298 |
|newCollection| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1299 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1300 |
newCollection := OrderedCollection new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1301 |
self keysAndValuesDo:[:key :value | |
14318 | 1302 |
newCollection add:(aBlock value:(Association key:key value:value)) |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1303 |
]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1304 |
^ newCollection |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1305 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1306 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1307 |
|ages| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1308 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1309 |
ages := Dictionary new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1310 |
ages at:'cg' put:37. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1311 |
ages at:'ca' put:33. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1312 |
ages at:'sv' put:36. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1313 |
ages at:'tk' put:28. |
14318 | 1314 |
ages associationsCollect:[:assoc | |
1315 |
assoc key , '''s age is ' , assoc value printString] |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1316 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1317 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1318 |
"Modified: 20.4.1996 / 11:31:27 / cg" |
1 | 1319 |
! |
1320 |
||
1321 |
associationsDo:aBlock |
|
1056 | 1322 |
"perform the block for all associations in the collection. |
1323 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1324 |
See also: |
14318 | 1325 |
#do: (which passes values to its block) |
1326 |
#keysDo: (which passes only keys to its block) |
|
1327 |
#keysAndValuesDo: (which passes keys&values) |
|
1233
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 |
This is much like #keysAndValuesDo:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1330 |
key and value as a single association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1331 |
#keysAndValuesDo: and is a bit faster therefore (no intermediate objects). |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1332 |
|
1056 | 1333 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1334 |
Iterate over a copy to do this." |
1 | 1335 |
|
38 | 1336 |
|key n "{ Class: SmallInteger }"| |
1 | 1337 |
|
1338 |
tally == 0 ifTrue:[^ self]. |
|
38 | 1339 |
n := keyArray basicSize. |
1340 |
1 to:n do:[:index | |
|
14318 | 1341 |
key := keyArray basicAt:index. |
1342 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
1343 |
key == NilEntry ifTrue:[ |
|
1344 |
key := nil |
|
1345 |
]. |
|
1346 |
aBlock value:(Association key:key value:(valueArray basicAt:index)) |
|
1347 |
] |
|
1 | 1348 |
] |
1056 | 1349 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1350 |
"Modified: 20.4.1996 / 11:31:39 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1351 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1352 |
|
13721
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1353 |
associationsDo:aBlock separatedBy:sepBlock |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1354 |
"perform the block for all associations in the collection. |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1355 |
|
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1356 |
See also: |
14318 | 1357 |
#do: (which passes values to its block) |
1358 |
#keysDo: (which passes only keys to its block) |
|
1359 |
#keysAndValuesDo: (which passes keys&values) |
|
13721
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1360 |
|
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1361 |
This is much like #keysAndValuesDo:, but aBlock gets the |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1362 |
key and value as a single association argument. |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1363 |
#keysAndValuesDo: and is a bit faster therefore (no intermediate objects). |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1364 |
|
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1365 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1366 |
Iterate over a copy to do this." |
13721
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1367 |
|
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1368 |
|key n "{ Class: SmallInteger }" |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1369 |
first| |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1370 |
|
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1371 |
tally == 0 ifTrue:[^ self]. |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1372 |
|
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1373 |
first := true. |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1374 |
n := keyArray basicSize. |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1375 |
1 to:n do:[:index | |
14318 | 1376 |
key := keyArray basicAt:index. |
1377 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
1378 |
key == NilEntry ifTrue:[ |
|
1379 |
key := nil |
|
1380 |
]. |
|
1381 |
first ifTrue:[ |
|
1382 |
first := false. |
|
1383 |
] ifFalse:[ |
|
1384 |
sepBlock value |
|
1385 |
]. |
|
1386 |
aBlock value:(Association key:key value:(valueArray basicAt:index)) |
|
1387 |
] |
|
13721
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1388 |
] |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1389 |
|
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1390 |
"Modified: / 20-04-1996 / 11:31:39 / cg" |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1391 |
"Created: / 23-09-2011 / 14:07:43 / cg" |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1392 |
! |
733cce16b968
added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents:
13328
diff
changeset
|
1393 |
|
2425
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1394 |
associationsReverseDo:aBlock |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1395 |
"perform the block for all associations in the collection. |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1396 |
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
|
1397 |
this is the same as #associationsDo: here. |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1398 |
Provided for protocol compatibility with OrderedDictionary" |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1399 |
|
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1400 |
^ self associationsDo:aBlock |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1401 |
|
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1402 |
"Created: 28.2.1997 / 16:08:52 / cg" |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1403 |
! |
3c23ae3de6d6
added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents:
2328
diff
changeset
|
1404 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1405 |
associationsSelect:aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1406 |
"return a new collection with all elements from the receiver, for which |
14318 | 1407 |
the argument aBlock evaluates to true. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1408 |
The block gets keys and values as an association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1409 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1410 |
See also: #keysAndValuesSelect: (which is slightly faster), |
14318 | 1411 |
#select: (which only passes the value) |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1412 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1413 |
This is much like #keysAndValuesSelect:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1414 |
key and value as a single association argument. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1415 |
#keysAndValuesSelect: and is a bit faster therefore (no intermediate objects). |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1416 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1417 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1418 |
Iterate over a copy to do this." |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1419 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1420 |
|newCollection| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1421 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1422 |
newCollection := self species new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1423 |
self keysAndValuesDo:[:key :value | |
14318 | 1424 |
(aBlock value:(Association key:key value:value)) ifTrue:[ |
1425 |
newCollection at:key put:value |
|
1426 |
] |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1427 |
]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1428 |
^ newCollection |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1429 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1430 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1431 |
|ages| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1432 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1433 |
ages := Dictionary new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1434 |
ages at:'cg' put:37. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1435 |
ages at:'ca' put:33. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1436 |
ages at:'sv' put:36. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1437 |
ages at:'tk' put:28. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1438 |
|
14318 | 1439 |
ages associationsSelect:[:assoc | |
1440 |
(assoc key startsWith:'c') or:[assoc value < 30]]. |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1441 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1442 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1443 |
"Modified: 20.4.1996 / 11:31:15 / cg" |
1 | 1444 |
! |
1445 |
||
609 | 1446 |
collect:aBlock |
1447 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1056 | 1448 |
and return a Bag with the results. |
1449 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1450 |
See also: |
14318 | 1451 |
#associationsCollect: (which passes key-value associations) |
1452 |
#keysAndValuesCollect: (which passes keys & values separately) |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1453 |
|
1056 | 1454 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1455 |
Iterate over a copy to do this." |
609 | 1456 |
|
1457 |
|newCollection| |
|
1458 |
||
1459 |
newCollection := Bag new. |
|
1460 |
self do:[:each | |
|
14318 | 1461 |
newCollection add:(aBlock value:each) |
609 | 1462 |
]. |
1463 |
^ newCollection |
|
1056 | 1464 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1465 |
"Modified: 20.4.1996 / 11:29:58 / cg" |
609 | 1466 |
! |
1467 |
||
1 | 1468 |
do:aBlock |
1056 | 1469 |
"perform the block for all values in the collection. |
1470 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1471 |
See also: |
14318 | 1472 |
#associationsDo: (which passes key-value associations) |
1473 |
#keysAndValuesDo: (which passes keys & values separately) |
|
1474 |
#keysDo: (which passes keys only) |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1475 |
|
1056 | 1476 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1477 |
Iterate over a copy to do this." |
1 | 1478 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1479 |
|key n "{ Class: SmallInteger }" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1480 |
deletedEntry| |
1 | 1481 |
|
1482 |
tally == 0 ifTrue:[^ self]. |
|
38 | 1483 |
n := keyArray basicSize. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1484 |
deletedEntry := DeletedEntry. |
38 | 1485 |
1 to:n do:[:index | |
14318 | 1486 |
key := keyArray basicAt:index. |
1487 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
|
1488 |
aBlock value:(valueArray basicAt:index) |
|
1489 |
]. |
|
1 | 1490 |
] |
1056 | 1491 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1492 |
"Modified: 20.4.1996 / 11:32:11 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1493 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1494 |
|
15786 | 1495 |
keysAndValuesDetect: aBlock ifNone:exceptionBlock |
1496 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1497 |
the block returns true; in this case return the element which caused |
|
1498 |
the true evaluation. |
|
1499 |
If none of the evaluations returns true, return the result of the |
|
1500 |
evaluation of the exceptionBlock" |
|
1501 |
||
1502 |
self keysAndValuesDo:[:eachKey :eachValue| |
|
1503 |
(aBlock value:eachKey value:eachValue) ifTrue:[^ eachValue] |
|
1504 |
]. |
|
1505 |
^ exceptionBlock value |
|
1506 |
! |
|
1507 |
||
3 | 1508 |
keysAndValuesDo:aTwoArgBlock |
1509 |
"evaluate the argument, aBlock for every element in the collection, |
|
1056 | 1510 |
passing both key and element as arguments. |
1511 |
||
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1512 |
See also: |
14318 | 1513 |
#associationsDo: (which passes keys->value pairs) |
1514 |
#do: (which only passes values) |
|
1515 |
#keysDo: (which only passes keys) |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1516 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1517 |
This is much like #associationsDo:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1518 |
key and value as two separate arguments. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1519 |
#associationsDo: is a bit slower. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1520 |
|
1056 | 1521 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1522 |
Iterate over a copy to do this." |
3 | 1523 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1524 |
|key n "{ Class: SmallInteger }" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1525 |
deletedEntry| |
3 | 1526 |
|
1527 |
tally == 0 ifTrue:[^ self]. |
|
38 | 1528 |
n := keyArray basicSize. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1529 |
deletedEntry := DeletedEntry. |
38 | 1530 |
1 to:n do:[:index | |
14318 | 1531 |
key := keyArray basicAt:index. |
1532 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
|
1533 |
key == NilEntry ifTrue:[ |
|
1534 |
key := nil |
|
1535 |
]. |
|
1536 |
aTwoArgBlock value:key value:(valueArray basicAt:index) |
|
1537 |
]. |
|
3 | 1538 |
] |
1056 | 1539 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1540 |
"Modified: 20.4.1996 / 11:33:42 / cg" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1541 |
! |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1542 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1543 |
keysAndValuesSelect:aBlock |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1544 |
"return a new collection with all elements from the receiver, for which |
14318 | 1545 |
the argument aBlock evaluates to true. |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1546 |
The block gets keys and values as separate arguments. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1547 |
|
14318 | 1548 |
See also: |
1549 |
#associationsSelect: (which passes key-value pairs), |
|
1550 |
#keysSelect: (which passes key values), |
|
1551 |
#select: (which only passes the value) |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1552 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1553 |
This is much like #associationsSelect:, but aBlock gets the |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1554 |
key and value as two separate arguments. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1555 |
#associationsSelect: is a bit slower. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1556 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1557 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1558 |
Iterate over a copy to do this." |
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1559 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1560 |
|newCollection| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1561 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1562 |
newCollection := self species new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1563 |
self keysAndValuesDo:[:key :value | |
14318 | 1564 |
(aBlock value:key value:value) ifTrue:[ |
1565 |
newCollection at:key put:value |
|
1566 |
] |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1567 |
]. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1568 |
^ newCollection |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1569 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1570 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1571 |
|ages| |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1572 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1573 |
ages := Dictionary new. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1574 |
ages at:'cg' put:37. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1575 |
ages at:'ca' put:33. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1576 |
ages at:'sv' put:36. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1577 |
ages at:'tk' put:28. |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1578 |
|
14318 | 1579 |
ages keysAndValuesSelect:[:name :age | |
1580 |
(name startsWith:'c') or:[age < 30]]. |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1581 |
" |
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1582 |
|
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1583 |
"Modified: 20.4.1996 / 11:34:29 / cg" |
3 | 1584 |
! |
1585 |
||
609 | 1586 |
keysDo:aBlock |
13044 | 1587 |
"evaluate the argument, aBlock for every key in the collection. |
1056 | 1588 |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1589 |
See also: |
14318 | 1590 |
#associationsDo: (which passes key-value associations) |
1591 |
#keysAndValuesDo: (which passes keys & values separately) |
|
1592 |
#do: (which passes values only) |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1593 |
|
1056 | 1594 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1595 |
Iterate over a copy to do this." |
1 | 1596 |
|
5336 | 1597 |
|sz "{ Class: SmallInteger }" |
1598 |
key| |
|
1056 | 1599 |
|
5336 | 1600 |
sz := keyArray size. |
1601 |
1 to:sz do:[:index | |
|
14318 | 1602 |
key := keyArray at:index. |
1603 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
1604 |
key == NilEntry ifTrue:[ |
|
1605 |
key := nil |
|
1606 |
]. |
|
1607 |
aBlock value:key |
|
1608 |
] |
|
5336 | 1609 |
] |
13044 | 1610 |
|
1611 |
"Modified: / 24-08-2010 / 10:13:58 / cg" |
|
1 | 1612 |
! |
1613 |
||
11430 | 1614 |
keysSelect:aBlock |
1615 |
"return a new collection with all elements from the receiver, for which |
|
14318 | 1616 |
the argument aBlock evaluates to true. |
11430 | 1617 |
The block gets the individual keys as its single argument. |
1618 |
||
14318 | 1619 |
See also: |
1620 |
#associationsSelect: (which passes key->value associations), |
|
1621 |
#keysAndValuesSelect: (which passes key & value args) |
|
1622 |
#select: (which passes values as arg), |
|
11430 | 1623 |
|
1624 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
14318 | 1625 |
Iterate over a copy to do this." |
11430 | 1626 |
|
1627 |
|newCollection| |
|
1628 |
||
1629 |
newCollection := self species new. |
|
1630 |
self keysAndValuesDo:[:key :value | |
|
14318 | 1631 |
(aBlock value:key) ifTrue:[ |
1632 |
newCollection at:key put:value |
|
1633 |
] |
|
11430 | 1634 |
]. |
1635 |
^ newCollection |
|
1636 |
||
1637 |
" |
|
1638 |
|d| |
|
1639 |
||
1640 |
d := Dictionary new. |
|
1641 |
d at:#foo put:#bar. |
|
1642 |
d at:#bar put:#baz. |
|
1643 |
d at:#baz put:#foo. |
|
1644 |
||
1645 |
d select:[:el | el startsWith:'b']. |
|
1646 |
" |
|
1647 |
||
1648 |
"Modified: / 12-10-2006 / 11:24:06 / cg" |
|
1649 |
! |
|
1650 |
||
1 | 1651 |
select:aBlock |
1652 |
"return a new collection with all elements from the receiver, for which |
|
14318 | 1653 |
the argument aBlock evaluates to true. |
10077 | 1654 |
The block gets the individual values as its single argument. |
1056 | 1655 |
|
14318 | 1656 |
See also: |
1657 |
#associationsSelect: (which passes key->value associations), |
|
1658 |
#keysAndValuesSelect: (which passes key & value args) |
|
1659 |
#keysSelect: (which passes key values), |
|
1233
21452b1c61eb
more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
1660 |
|
1056 | 1661 |
WARNING: do not add/remove elements while iterating over the receiver. |
14318 | 1662 |
Iterate over a copy to do this." |
1 | 1663 |
|
1664 |
|newCollection| |
|
1665 |
||
1666 |
newCollection := self species new. |
|
77 | 1667 |
self keysAndValuesDo:[:key :value | |
14318 | 1668 |
(aBlock value:value) ifTrue:[ |
1669 |
newCollection at:key put:value |
|
1670 |
] |
|
1 | 1671 |
]. |
1672 |
^ newCollection |
|
77 | 1673 |
|
1674 |
" |
|
1675 |
|d| |
|
1676 |
||
1677 |
d := Dictionary new. |
|
1678 |
d at:#foo put:#bar. |
|
1679 |
d at:#bar put:#baz. |
|
1680 |
d at:#baz put:#foo. |
|
1681 |
||
1682 |
d select:[:el | el startsWith:'b']. |
|
1683 |
" |
|
1056 | 1684 |
|
10077 | 1685 |
"Modified: / 12-10-2006 / 11:24:06 / cg" |
7653
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1686 |
! |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1687 |
|
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1688 |
valuesDo:aBlock |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1689 |
"perform the block for all values in the collection. |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1690 |
Same as #do: - for VisualWorks compatibility" |
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1691 |
|
0a81451a7a59
added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents:
7306
diff
changeset
|
1692 |
^ self do:aBlock |
1 | 1693 |
! ! |
1694 |
||
609 | 1695 |
|
1696 |
!Dictionary methodsFor:'printing & storing'! |
|
1697 |
||
3192 | 1698 |
printElementsDo:aBlock |
1699 |
"redefined, so #printOn: prints associations" |
|
609 | 1700 |
|
3192 | 1701 |
^ self associationsDo:aBlock |
609 | 1702 |
|
3192 | 1703 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
609 | 1704 |
! |
1705 |
||
1706 |
storeOn:aStream |
|
1707 |
"output a printed representation (which can be re-read) |
|
1708 |
onto the argument aStream" |
|
1709 |
||
1710 |
|isEmpty| |
|
1711 |
||
1712 |
thisContext isRecursive ifTrue:[ |
|
15857 | 1713 |
RecursiveStoreError raiseRequestWith:self. |
1714 |
('Dictionary [error]: storeOn: of self referencing collection.') errorPrintCR. |
|
1715 |
aStream nextPutAll:'#recursive'. |
|
1716 |
^ self |
|
609 | 1717 |
]. |
1718 |
||
1719 |
aStream nextPutAll:'('. |
|
1720 |
aStream nextPutAll:(self class name). |
|
1721 |
aStream nextPutAll:' new'. |
|
1722 |
isEmpty := true. |
|
1723 |
self keysAndValuesDo:[:key :value | |
|
15857 | 1724 |
aStream nextPutAll:' at:'. |
1725 |
key storeOn:aStream. |
|
1726 |
aStream nextPutAll:' put:'. |
|
1727 |
value storeOn:aStream. |
|
1728 |
aStream nextPutAll:'; '. |
|
1729 |
isEmpty := false |
|
609 | 1730 |
]. |
1731 |
isEmpty ifFalse:[aStream nextPutAll:' yourself']. |
|
1732 |
aStream nextPut:$) |
|
1733 |
||
1734 |
" |
|
1735 |
Dictionary new storeOn:Transcript |
|
1736 |
||
1737 |
(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
|
1738 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1739 |
(Dictionary new at:1 put:'hello'; at:2 put:nil; yourself) storeOn:Transcript |
609 | 1740 |
" |
1741 |
||
1742 |
" |
|
1743 |
|d| |
|
1744 |
d := Dictionary new. |
|
1745 |
d at:1 put:'hello'. |
|
1746 |
d at:'hello' put:#world. |
|
1747 |
d storeOn:Transcript |
|
1748 |
" |
|
1749 |
||
1750 |
" |
|
1751 |
|d| |
|
1752 |
d := Dictionary new. |
|
1753 |
d at:1 put:'hello'. |
|
1754 |
d at:'hello' put:#world. |
|
1755 |
d at:2 put:d. |
|
1756 |
d storeOn:Transcript |
|
1757 |
" |
|
1417 | 1758 |
|
2288 | 1759 |
"Modified: 28.1.1997 / 00:37:46 / cg" |
609 | 1760 |
! ! |
1761 |
||
1 | 1762 |
!Dictionary methodsFor:'private'! |
1763 |
||
345 | 1764 |
compareSame:element1 with:element2 |
1256 | 1765 |
"compare two elements for being the same. Here, return true if the |
1766 |
elements are equal (i.e. using #=). |
|
1767 |
Redefinable in subclasses." |
|
1768 |
||
345 | 1769 |
^ element1 = element2 |
1256 | 1770 |
|
1771 |
"Modified: 22.4.1996 / 17:34:27 / cg" |
|
345 | 1772 |
! |
1773 |
||
609 | 1774 |
emptyCollectionForKeys |
1256 | 1775 |
"return an empty collection to hold keys. Here, a Set is returned. |
1776 |
Redefinable in subclasses." |
|
1777 |
||
609 | 1778 |
^ Set new:(self size) |
1256 | 1779 |
|
1780 |
"Modified: 22.4.1996 / 17:35:17 / cg" |
|
1 | 1781 |
! |
1782 |
||
1783 |
grow:newSize |
|
1784 |
"grow the receiver to make space for at least newSize elements. |
|
1785 |
To do this, we have to rehash into the new arrays. |
|
1786 |
(which is done by re-adding all elements to a new, empty key/value array pair)." |
|
1787 |
||
92 | 1788 |
|key deletedEntry oldKeyArray oldValueArray n |
14318 | 1789 |
oldSize "{ Class:SmallInteger }" |
10 | 1790 |
newIndex "{ Class:SmallInteger }" | |
1 | 1791 |
|
10 | 1792 |
oldKeyArray := keyArray. |
1793 |
oldValueArray := valueArray. |
|
1 | 1794 |
|
249 | 1795 |
n := self class goodSizeFrom:newSize. |
1796 |
oldSize := oldKeyArray size. |
|
1797 |
n == oldSize ifTrue:[^ self]. |
|
1798 |
||
10 | 1799 |
keyArray := self keyContainerOfSize:n. |
92 | 1800 |
valueArray := self valueContainerOfSize:n. |
1 | 1801 |
|
249 | 1802 |
|
92 | 1803 |
deletedEntry := DeletedEntry. |
10 | 1804 |
1 to:oldSize do:[:index | |
12669
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1805 |
key := oldKeyArray basicAt:index. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1806 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1807 |
newIndex := self findNil:key. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1808 |
keyArray basicAt:newIndex put:key. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1809 |
valueArray basicAt:newIndex put:(oldValueArray basicAt:index). |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1810 |
] |
1 | 1811 |
] |
1812 |
! |
|
1813 |
||
1814 |
rehash |
|
1815 |
"rehash contents - is done by re-adding all elements to a new, empty key/value array pair)." |
|
1816 |
||
14318 | 1817 |
| oldKeyArray oldValueArray key |
2 | 1818 |
n "{ Class:SmallInteger }" |
1819 |
newIndex "{ Class:SmallInteger }" | |
|
1 | 1820 |
|
1821 |
oldKeyArray := keyArray. |
|
1822 |
oldValueArray := valueArray. |
|
1823 |
||
1824 |
n := keyArray size. |
|
10 | 1825 |
keyArray := self keyContainerOfSize:n. |
92 | 1826 |
valueArray := self valueContainerOfSize:n. |
1 | 1827 |
|
2 | 1828 |
1 to:n do:[:index | |
12669
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1829 |
key := oldKeyArray basicAt:index. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1830 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1831 |
newIndex := self findNil:key. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1832 |
keyArray basicAt:newIndex put:key. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1833 |
valueArray basicAt:newIndex put:(oldValueArray basicAt:index). |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1834 |
] |
1 | 1835 |
] |
1836 |
! |
|
1837 |
||
1838 |
rehashFrom:startIndex |
|
77 | 1839 |
"rehash elements starting at index - after a remove. |
14318 | 1840 |
NOTE: this method is no longer needed; |
1841 |
the trick using DeletedEntry avoids the need to do this time |
|
12669
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1842 |
consuming operation, making remove pretty fast :-) |
77 | 1843 |
" |
1 | 1844 |
|
1845 |
|key i length |
|
1846 |
index "{ Class:SmallInteger }" | |
|
1847 |
||
1848 |
length := keyArray basicSize. |
|
1849 |
index := startIndex. |
|
1850 |
key := keyArray basicAt:index. |
|
1851 |
[key notNil] whileTrue:[ |
|
12669
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1852 |
key ~~ DeletedEntry ifTrue:[ |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1853 |
i := self findNil:key. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1854 |
i == index ifTrue:[ |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1855 |
^ self |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1856 |
]. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1857 |
keyArray basicAt:i put:key. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1858 |
valueArray basicAt:i put:(valueArray basicAt:index). |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1859 |
keyArray basicAt:index put:nil. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1860 |
valueArray basicAt:index put:nil. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1861 |
]. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1862 |
index == length ifTrue:[ |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1863 |
index := 1 |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1864 |
] ifFalse:[ |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1865 |
index := index + 1. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1866 |
]. |
2acc2409c3f4
changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents:
12668
diff
changeset
|
1867 |
key := keyArray basicAt:index. |
1 | 1868 |
] |
77 | 1869 |
! |
1870 |
||
609 | 1871 |
setTally:count |
1872 |
"initialize the contents array (for at least count slots) |
|
1873 |
and set tally to zero. |
|
1874 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 1875 |
|
609 | 1876 |
|n| |
1 | 1877 |
|
609 | 1878 |
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
|
1879 |
n == keyArray size ifTrue:[ |
14318 | 1880 |
keyArray atAllPut:nil. |
1881 |
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
|
1882 |
] ifFalse:[ |
14318 | 1883 |
keyArray := self keyContainerOfSize:n. |
1884 |
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
|
1885 |
]. |
609 | 1886 |
tally := 0 |
3727
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1887 |
|
2b05022de748
try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3319
diff
changeset
|
1888 |
"Modified: / 5.8.1998 / 10:48:51 / cg" |
44 | 1889 |
! |
1890 |
||
609 | 1891 |
valueContainerOfSize:n |
1892 |
"return a container for values of size n. |
|
1893 |
Extracted to make life of weak subclasses easier ..." |
|
44 | 1894 |
|
609 | 1895 |
^ Array basicNew:n |
1896 |
! ! |
|
1897 |
||
1460 | 1898 |
!Dictionary methodsFor:'searching'! |
1899 |
||
1900 |
findFirstKey:aBlock |
|
1901 |
"find and return the first key, for which evaluation of the argument, aBlock |
|
1703 | 1902 |
returns true; return nil if none is detected." |
1460 | 1903 |
|
1904 |
self keysDo:[:key | |
|
14318 | 1905 |
(aBlock value:key) ifTrue:[^ key]. |
1460 | 1906 |
]. |
1907 |
^ nil |
|
1908 |
||
1909 |
"Created: 5.6.1996 / 11:55:50 / stefan" |
|
1703 | 1910 |
"Modified: 8.10.1996 / 22:01:59 / cg" |
1460 | 1911 |
! ! |
1912 |
||
609 | 1913 |
!Dictionary methodsFor:'testing'! |
44 | 1914 |
|
609 | 1915 |
includes:anObject |
1916 |
"return true, if the argument, aValue is stored in the dictionary, |
|
1917 |
i.e. if there is an associaten, with aValue as value. |
|
1918 |
This is a slow search, since there is no fast reverse mapping; |
|
1919 |
the values have to be all scanned without any hashing. |
|
1920 |
You need a special collection (or two Dictionaries) to get this |
|
1921 |
reverse mapping fast." |
|
1922 |
||
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1923 |
^ self includesEqualValue:anObject |
1256 | 1924 |
|
1925 |
"Modified: 22.4.1996 / 17:20:11 / cg" |
|
609 | 1926 |
! |
68 | 1927 |
|
609 | 1928 |
includesAssociation:anAssociation |
1929 |
"return true, if there is an association in the receiver with the |
|
1930 |
same key and value as the argument, anAssociation. |
|
1931 |
NOTICE: in contrast to #includes:, this compares both key and value." |
|
1932 |
||
1933 |
|val| |
|
44 | 1934 |
|
609 | 1935 |
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
|
1936 |
^ self compareSame:val with:anAssociation value |
d8173a888c93
oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents:
3304
diff
changeset
|
1937 |
|
d8173a888c93
oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents:
3304
diff
changeset
|
1938 |
"Modified: / 5.3.1998 / 20:35:00 / cg" |
609 | 1939 |
! |
1940 |
||
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1941 |
includesEqualValue:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1942 |
"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
|
1943 |
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
|
1944 |
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
|
1945 |
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
|
1946 |
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
|
1947 |
reverse mapping fast." |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1948 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1949 |
aValue isNil ifTrue:[ |
14318 | 1950 |
"/ need a special case for that ... |
1951 |
^ self includesIdenticalValue:aValue. |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1952 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1953 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1954 |
^ valueArray includes:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1955 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1956 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1957 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1958 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1959 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1960 |
d at:'1' put:'one'. |
14318 | 1961 |
d includes:nil. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1962 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1963 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1964 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1965 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1966 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1967 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1968 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1969 |
d at:2 put:nil. |
14318 | 1970 |
d includes:nil. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1971 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1972 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1973 |
! |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1974 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1975 |
includesIdenticalValue:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1976 |
"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
|
1977 |
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
|
1978 |
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
|
1979 |
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
|
1980 |
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
|
1981 |
reverse mapping fast." |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1982 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1983 |
|idx| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1984 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1985 |
aValue isNil ifTrue:[ |
14318 | 1986 |
"/ need a special case for that ... |
1987 |
idx := 0. |
|
1988 |
[true] whileTrue:[ |
|
1989 |
idx := valueArray identityIndexOf:nil startingAt:idx+1. |
|
1990 |
idx == 0 ifTrue:[^ false]. |
|
1991 |
(keyArray at:idx) notNil ifTrue:[^ true]. |
|
1992 |
] |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1993 |
]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1994 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1995 |
^ valueArray includesIdentical:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1996 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1997 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1998 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
1999 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2000 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2001 |
d at:'1' put:'one'. |
14318 | 2002 |
d includes:nil. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2003 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2004 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2005 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2006 |
|d| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2007 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2008 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2009 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2010 |
d at:2 put:nil. |
14318 | 2011 |
d includes:nil. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2012 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2013 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2014 |
! |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2015 |
|
609 | 2016 |
includesKey:aKey |
2017 |
"return true, if the argument, aKey is a key in the receiver" |
|
2018 |
||
14318 | 2019 |
^ (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
|
2020 |
|
008049e411b5
includesKey: returned a wrong answer for a nil key.
Claus Gittinger <cg@exept.de>
parents:
9413
diff
changeset
|
2021 |
"Modified: / 17-08-2006 / 21:06:41 / cg" |
609 | 2022 |
! |
293 | 2023 |
|
609 | 2024 |
includesValue:aValue |
2025 |
"return true, if the argument, aValue is stored in the dictionary, |
|
2026 |
i.e. if there is an associaten, with aValue as value. |
|
2027 |
This is a slow search, since there is no fast reverse mapping; |
|
2028 |
the values have to be all scanned without any hashing. |
|
2029 |
You need a special collection (or two Dictionaries) to get this |
|
2030 |
reverse mapping fast." |
|
293 | 2031 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2032 |
^ self includesEqualValue:aValue |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2033 |
|
609 | 2034 |
! |
293 | 2035 |
|
15363 | 2036 |
isDictionary |
2037 |
"return true, if the receiver is some kind of dictionary; |
|
2038 |
true returned here - the method is redefined from Object." |
|
2039 |
||
2040 |
^ true |
|
2041 |
! |
|
2042 |
||
609 | 2043 |
occurrencesOf:anObject |
2044 |
"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
|
2045 |
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
|
2046 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2047 |
|idx count| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2048 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2049 |
anObject isNil ifTrue:[ |
14318 | 2050 |
"/ need a special case for that ... |
2051 |
idx := 0. |
|
2052 |
count := 0. |
|
2053 |
[true] whileTrue:[ |
|
2054 |
idx := valueArray identityIndexOf:nil startingAt:idx+1. |
|
2055 |
idx == 0 ifTrue:[^ count]. |
|
2056 |
(keyArray at:idx) notNil ifTrue:[ |
|
2057 |
count := count + 1 |
|
2058 |
] |
|
2059 |
] |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2060 |
]. |
609 | 2061 |
|
2062 |
^ valueArray occurrencesOf:anObject |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2063 |
|
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| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2066 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2067 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2068 |
d at:'1' put:'one'. |
14318 | 2069 |
d occurrencesOf:nil. |
5050
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 |
|
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| |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2074 |
|
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2075 |
d := Dictionary new. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2076 |
d at:'1' put:'one'. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2077 |
d at:2 put:nil. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2078 |
d at:5 put:nil. |
14318 | 2079 |
d occurrencesOf:nil. |
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2080 |
" |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
4871
diff
changeset
|
2081 |
|
1 | 2082 |
! ! |
609 | 2083 |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
2084 |
!Dictionary methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
2085 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
2086 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
2087 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
2088 |
^ aVisitor visitDictionary:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
2089 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7906
diff
changeset
|
2090 |
|
2094
42cd02703bf4
handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents:
1703
diff
changeset
|
2091 |
!Dictionary class methodsFor:'documentation'! |
633 | 2092 |
|
2093 |
version |
|
15857 | 2094 |
^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.117 2013-12-06 14:41:16 stefan Exp $' |
12659 | 2095 |
! |
2096 |
||
2097 |
version_CVS |
|
15857 | 2098 |
^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.117 2013-12-06 14:41:16 stefan Exp $' |
633 | 2099 |
! ! |
14897 | 2100 |