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