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