author | Claus Gittinger <cg@exept.de> |
Sat, 16 Dec 1995 14:04:33 +0100 | |
changeset 778 | afbb3ebde874 |
parent 641 | ef3bcf9de54b |
child 1053 | ac1ea83233e6 |
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 |
||
12 | 13 |
Set subclass:#Dictionary |
155 | 14 |
instanceVariableNames:'valueArray' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Unordered' |
|
1 | 18 |
! |
19 |
||
68 | 20 |
!Dictionary class methodsFor:'documentation'! |
21 |
||
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
155 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
68 | 36 |
documentation |
37 |
" |
|
38 |
a Dictionary is (conceptionally) a set of Associations storing key-value pairs. |
|
39 |
(The implementation uses two arrays to store the keys and values separately.) |
|
40 |
Searching for an element is done using a hash into the key array. |
|
362 | 41 |
Another way of looking at a dictionary is as a array which uses |
345 | 42 |
arbitrary access keys (i.e. not just integers as arrays do). |
43 |
||
44 |
Since the keys are unordered, no internal element order is defined |
|
362 | 45 |
(i.e. enumerating them may return elements in any order - even changing |
345 | 46 |
over time). |
47 |
||
68 | 48 |
Many methods for searching and hashing are inherited from Set. |
92 | 49 |
|
50 |
Instance variables: |
|
345 | 51 |
|
155 | 52 |
keyArray <Array> (from Set) the keys |
53 |
valueArray <Array> the values ('valueArray at:index' corresponds |
|
54 |
to the value stored under 'keyArray at:index') |
|
92 | 55 |
|
345 | 56 |
Performance hints: |
362 | 57 |
since the dictionary does not really store associations internally, |
58 |
it is less efficient, to store/retrieve associations. The reason is |
|
399 | 59 |
that these assocs are created temporarily in some extract methods. |
345 | 60 |
I.e. 'at:key put:value' is faster than 'add:anAssoc' |
61 |
and 'keysAndValuesDo:' is faster than 'associationsDo:' etc. |
|
92 | 62 |
|
362 | 63 |
If only symbols or smallIntegers are used as keys, use IdentityDictionaries |
64 |
for slightly better performance, since both hashing and comparison is faster. |
|
345 | 65 |
|
66 |
If you have a rough idea how big the dictionary is going to grow, |
|
67 |
create it using #new: instead of #new. Even if the size given is a |
|
68 |
poor guess (say half of the real size), there is some 20-30% performance |
|
69 |
win to expect, since many resizing operations are avoided when associations |
|
70 |
are added. |
|
362 | 71 |
|
72 |
See also: Set, IdentityDictionary, IdentitySet, WeakIdentitySet and |
|
73 |
WeakIdentityDictionary |
|
68 | 74 |
" |
75 |
! ! |
|
76 |
||
345 | 77 |
!Dictionary class methodsFor:'instance creation'! |
78 |
||
357 | 79 |
withKeys:keyArray andValues:valueArray |
80 |
"return a new instance where keys and values are taken from |
|
81 |
the argumentArrays." |
|
82 |
||
83 |
|newDict sz "{ Class: SmallInteger }"| |
|
84 |
||
85 |
sz := keyArray size. |
|
86 |
newDict := self new:sz. |
|
87 |
keyArray with:valueArray do:[:key :value | |
|
88 |
newDict at:key put:value |
|
89 |
]. |
|
90 |
^ newDict |
|
91 |
||
92 |
" |
|
93 |
Dictionary withKeys:#('one' 'two' 'three' 'four') |
|
94 |
andValues:#(1 2 3 4) |
|
95 |
" |
|
54 | 96 |
! |
97 |
||
609 | 98 |
withKeysAndValues:anArray |
99 |
"return a new instance where keys and values are taken from alternating |
|
100 |
elements of anArray" |
|
54 | 101 |
|
609 | 102 |
|newDict sz "{ Class: SmallInteger }"| |
345 | 103 |
|
609 | 104 |
sz := anArray size. |
105 |
newDict := self new:(sz // 2). |
|
106 |
1 to:sz by:2 do:[:i | |
|
107 |
newDict at:(anArray at:i) put:(anArray at:i+1) |
|
108 |
]. |
|
109 |
^ newDict |
|
345 | 110 |
|
609 | 111 |
" |
112 |
Dictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4) |
|
113 |
" |
|
1 | 114 |
! ! |
115 |
||
116 |
!Dictionary methodsFor:'accessing'! |
|
117 |
||
609 | 118 |
associationAt:aKey |
119 |
"return an association consisting of aKey and the element indexed |
|
120 |
by aKey - |
|
121 |
report an error, if no element is stored under aKey" |
|
122 |
||
123 |
^ aKey -> (self at:aKey) |
|
124 |
! |
|
125 |
||
126 |
associationAt:aKey ifAbsent:exceptionBlock |
|
127 |
"return an association consisting of aKey and the element indexed by aKey - |
|
128 |
return result of exceptionBlock if no element is stored under aKey" |
|
129 |
||
130 |
^ aKey -> (self at:aKey ifAbsent:[^ exceptionBlock value]) |
|
131 |
! |
|
132 |
||
133 |
associations |
|
134 |
"return an ordered collection containing the receivers associations." |
|
135 |
||
136 |
|coll| |
|
137 |
||
138 |
coll := OrderedCollection new:(keyArray size). |
|
139 |
self associationsDo:[:assoc | coll add:assoc]. |
|
140 |
^ coll |
|
141 |
! |
|
142 |
||
1 | 143 |
at:aKey |
144 |
"return the element indexed by aKey - report an error if none found" |
|
145 |
||
146 |
|index| |
|
147 |
||
148 |
aKey isNil ifTrue:[ |
|
362 | 149 |
"/ nil is not allowed as key |
345 | 150 |
^ self errorInvalidKey:aKey |
155 | 151 |
]. |
362 | 152 |
|
153 |
"/ |
|
154 |
"/ I could have written: |
|
155 |
"/ index := self find:aKey ifAbsent:[^ self errorKeyNotFound:aKey] |
|
156 |
"/ but the code below is slighlty more efficient, since it avoids |
|
157 |
"/ a block creation ([0] is very cheap) - thus speeding up the good case. |
|
158 |
||
159 |
index := self find:aKey ifAbsent:0. |
|
155 | 160 |
index == 0 ifTrue:[ |
161 |
"no such key" |
|
345 | 162 |
^ self errorKeyNotFound:aKey |
155 | 163 |
]. |
164 |
^ valueArray basicAt:index |
|
1 | 165 |
! |
166 |
||
167 |
at:aKey ifAbsent:exceptionBlock |
|
2 | 168 |
"return the element indexed by aKey - |
68 | 169 |
return result of exceptionBlock if no element is stored under aKey" |
2 | 170 |
|
1 | 171 |
|index| |
172 |
||
173 |
aKey isNil ifTrue:[ |
|
362 | 174 |
"/ nil is not allowed as key |
175 |
"/ |
|
176 |
"/ previous versions of ST/X raised an error |
|
177 |
"/ here. However, there seem to exist applications |
|
178 |
"/ which depend on getting the exceptionBlocks value |
|
179 |
"/ in this case ... well ... |
|
180 |
"/ ^ self errorInvalidKey:aKey |
|
181 |
^ exceptionBlock value |
|
155 | 182 |
]. |
362 | 183 |
|
184 |
"/ I could have written: |
|
185 |
"/ index := self find:aKey ifAbsent:[^ exceptionBlock value] |
|
186 |
"/ but the code below is slighlty more efficient, since it avoids |
|
187 |
"/ a block creation ([0] is very cheap) - thus speeding up the good case. |
|
188 |
||
189 |
index := self find:aKey ifAbsent:0. |
|
155 | 190 |
index == 0 ifTrue:[^ exceptionBlock value]. |
191 |
^ valueArray basicAt:index |
|
1 | 192 |
! |
193 |
||
194 |
at:aKey put:anObject |
|
195 |
"add the argument anObject under key, aKey to the receiver" |
|
196 |
||
77 | 197 |
|index| |
1 | 198 |
|
199 |
aKey isNil ifTrue:[ |
|
155 | 200 |
"nil is not allowed as key" |
360 | 201 |
self errorInvalidKey:aKey |
1 | 202 |
] ifFalse:[ |
155 | 203 |
index := self findKeyOrNil:aKey. |
204 |
(valueArray basicAt:index) notNil ifTrue:[ |
|
205 |
valueArray basicAt:index put:anObject. |
|
206 |
^ anObject |
|
207 |
]. |
|
208 |
keyArray basicAt:index put:aKey. |
|
209 |
valueArray basicAt:index put:anObject. |
|
210 |
tally := tally + 1. |
|
1 | 211 |
|
155 | 212 |
self fullCheck. |
362 | 213 |
]. |
214 |
^ anObject |
|
1 | 215 |
! |
216 |
||
609 | 217 |
keyAtEqualValue:aValue |
218 |
"return the key whose value is equal (i.e. using #= for compare) |
|
219 |
to the argument, nil if none found. |
|
220 |
This is a slow access, since there is no fast reverse mapping. |
|
221 |
NOTICE: |
|
222 |
The value is searched using equality compare; |
|
223 |
use #keyAtValue: to compare for identity." |
|
77 | 224 |
|
609 | 225 |
^ self keyAtEqualValue:aValue ifAbsent:[nil] |
10 | 226 |
! |
227 |
||
609 | 228 |
keyAtEqualValue:aValue ifAbsent:exceptionBlock |
229 |
"return the key whose value is equal (i.e. using #= for compare) |
|
230 |
to the argument, if not found, return the value of exceptionBlock. |
|
231 |
This is a slow access, since there is no fast reverse mapping. |
|
232 |
NOTICE: |
|
233 |
The value is searched using equality compare; |
|
234 |
use #keyAtValue:ifAbsent: to compare for identity." |
|
54 | 235 |
|
609 | 236 |
|idx| |
360 | 237 |
|
609 | 238 |
idx := valueArray indexOf:aValue. |
239 |
idx ~~ 0 ifTrue:[ |
|
240 |
^ keyArray at:idx |
|
241 |
]. |
|
54 | 242 |
|
609 | 243 |
"/ keyArray keysAndValuesDo:[:index :aKey | |
244 |
"/ (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[ |
|
245 |
"/ (valueArray at:index) = aValue ifTrue:[^ aKey]. |
|
246 |
"/ ]. |
|
247 |
"/ ]. |
|
54 | 248 |
|
609 | 249 |
^ exceptionBlock value |
54 | 250 |
! |
251 |
||
10 | 252 |
keyAtValue:aValue |
345 | 253 |
"return the key whose value is identical (i.e. using #== for compare) |
254 |
to the argument, nil if none found. |
|
77 | 255 |
This is a slow access, since there is no fast reverse mapping. |
357 | 256 |
NOTICE: |
362 | 257 |
The value is searched using identity compare; |
357 | 258 |
use #keyAtEqualValue: to compare for equality." |
10 | 259 |
|
260 |
^ self keyAtValue:aValue ifAbsent:[nil] |
|
261 |
! |
|
262 |
||
263 |
keyAtValue:aValue ifAbsent:exceptionBlock |
|
345 | 264 |
"return the key whose value is identical (i.e. using #== for compare) |
265 |
to the argument, if not found, return the value of exceptionBlock. |
|
357 | 266 |
This is a slow access, since there is no fast reverse mapping. |
267 |
NOTICE: |
|
362 | 268 |
The value is searched using identity compare; |
357 | 269 |
use #keyAtEqualValue:ifAbsent: to compare for equality." |
10 | 270 |
|
359 | 271 |
|idx| |
272 |
||
273 |
idx := valueArray identityIndexOf:aValue. |
|
274 |
idx ~~ 0 ifTrue:[ |
|
275 |
^ keyArray at:idx |
|
10 | 276 |
]. |
359 | 277 |
|
278 |
"/ keyArray keysAndValuesDo:[:index :aKey | |
|
360 | 279 |
"/ (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[ |
280 |
"/ (valueArray at:index) == aValue ifTrue:[^ aKey]. |
|
281 |
"/ ]. |
|
359 | 282 |
"/ ]. |
283 |
||
10 | 284 |
^ exceptionBlock value |
357 | 285 |
! |
286 |
||
609 | 287 |
keys |
288 |
"return a collection containing all keys of the receiver" |
|
289 |
||
290 |
|keySet| |
|
357 | 291 |
|
609 | 292 |
keySet := self emptyCollectionForKeys. |
293 |
keyArray do:[:key | |
|
294 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
295 |
keySet add:key |
|
296 |
] |
|
297 |
]. |
|
298 |
^ keySet |
|
357 | 299 |
! |
300 |
||
609 | 301 |
values |
302 |
"return a collection containing all values of the receiver" |
|
359 | 303 |
|
609 | 304 |
"old: |
305 |
kk: this fails if the receiver contains nils |
|
306 |
^ valueArray asBag |
|
307 |
new: |
|
308 |
" |
|
309 |
|aCollection| |
|
359 | 310 |
|
609 | 311 |
aCollection := OrderedCollection new:valueArray size. |
312 |
self do:[:value| aCollection add:value]. |
|
313 |
^ aCollection |
|
1 | 314 |
! ! |
315 |
||
316 |
!Dictionary methodsFor:'adding & removing'! |
|
317 |
||
318 |
add:anAssociation |
|
319 |
"add the argument, anAssociation to the receiver" |
|
320 |
||
321 |
self at:(anAssociation key) put:(anAssociation value). |
|
322 |
^ anAssociation |
|
323 |
! |
|
324 |
||
345 | 325 |
declare:key from:aDictionary |
326 |
"if the receiver does not include an association for key, |
|
327 |
take the association from aDictionary and add it to the receiver. |
|
328 |
If aDictionary does not contain such an association, use nil |
|
329 |
as the value of the new dictionary." |
|
330 |
||
331 |
|value| |
|
332 |
||
333 |
(self includesKey:key) ifFalse:[ |
|
334 |
value := aDictionary at:key ifAbsent:nil. |
|
335 |
self at:key put:value. |
|
336 |
] |
|
337 |
! |
|
338 |
||
1 | 339 |
remove:oldObject ifAbsent:aBlock |
340 |
"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
|
341 |
If it was not in the collection return the value of aBlock. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
342 |
This is blocked here; you have to use #removeKey:, |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
343 |
#removeAssociation: or #removeValue:" |
1 | 344 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
345 |
^ self shouldNotImplement |
1 | 346 |
! |
347 |
||
348 |
removeAssociation:assoc |
|
349 |
"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
|
350 |
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
|
351 |
Only the key is used in the passed argument, and a new |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
352 |
association, for the key and the previously stored value is returned." |
1 | 353 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
354 |
|key| |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
355 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
356 |
key := assoc key. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
357 |
^ key -> (self removeKey:key) |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
358 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
359 |
"Modified: 24.11.1995 / 13:04:09 / cg" |
1 | 360 |
! |
361 |
||
362 |
removeKey:aKey |
|
363 |
"remove the association under aKey from the collection. |
|
364 |
If it was not in the collection report an error" |
|
365 |
||
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
366 |
^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey] |
1 | 367 |
! |
368 |
||
369 |
removeKey:aKey ifAbsent:aBlock |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
370 |
"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
|
371 |
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
|
372 |
If it was not in the collection return the result |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
373 |
from evaluating aBlock" |
1 | 374 |
|
3 | 375 |
|index "{ Class:SmallInteger }" |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
376 |
next "{ Class:SmallInteger }" |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
377 |
oldValue| |
1 | 378 |
|
379 |
aKey isNil ifTrue:[ |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
380 |
^ self errorInvalidKey:aKey |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
381 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
382 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
383 |
"/ below, I could have written: |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
384 |
"/ 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
|
385 |
"/ 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
|
386 |
"/ 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
|
387 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
388 |
index := self find:aKey ifAbsent:0. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
389 |
index == 0 ifTrue:[^ aBlock value]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
390 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
391 |
oldValue := valueArray basicAt:index. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
392 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
393 |
valueArray basicAt:index put:nil. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
394 |
keyArray basicAt:index put:nil. |
77 | 395 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
396 |
tally := tally - 1. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
397 |
tally == 0 ifTrue:[ |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
398 |
self setTally:0 |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
399 |
] ifFalse:[ |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
400 |
index == keyArray basicSize ifTrue:[ |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
401 |
next := 1 |
155 | 402 |
] ifFalse:[ |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
403 |
next := index + 1. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
404 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
405 |
(keyArray basicAt:next) notNil ifTrue:[ |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
406 |
keyArray basicAt:index put:DeletedEntry |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
407 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
408 |
self emptyCheck |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
409 |
]. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
410 |
^ oldValue |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
411 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
412 |
"Modified: 24.11.1995 / 13:00:51 / cg" |
77 | 413 |
! |
414 |
||
415 |
removeValue:aValue ifAbsent:aBlock |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
416 |
"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
|
417 |
return the key under which it was stored previously. |
77 | 418 |
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
|
419 |
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
|
420 |
but identity compare in the IdentityDictionary subclass. |
77 | 421 |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
422 |
Notice, this does a linear search through the values and may |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
423 |
therefore be slow for big dictionaries." |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
424 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
425 |
|next "{ Class:SmallInteger }" |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
426 |
oldKey| |
77 | 427 |
|
428 |
aValue notNil ifTrue:[ |
|
155 | 429 |
keyArray keysAndValuesDo:[:index :aKey | |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
430 |
|idx "{Class:SmallInteger}"| |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
431 |
|
155 | 432 |
(aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[ |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
433 |
idx := index. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
434 |
(self compareSame:(valueArray at:idx) with:aValue) ifTrue:[ |
155 | 435 |
"found it" |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
436 |
valueArray basicAt:idx put:nil. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
437 |
oldKey := keyArray basicAt:idx. |
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
438 |
keyArray basicAt:idx put:nil. |
155 | 439 |
tally := tally - 1. |
440 |
tally == 0 ifTrue:[ |
|
441 |
self setTally:0. |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
442 |
^ oldKey |
155 | 443 |
]. |
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
444 |
idx == keyArray basicSize ifTrue:[ |
155 | 445 |
next := 1 |
446 |
] ifFalse:[ |
|
447 |
next := index + 1. |
|
448 |
]. |
|
449 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
450 |
keyArray basicAt:idx put:DeletedEntry |
155 | 451 |
]. |
452 |
self emptyCheck. |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
453 |
^ oldKey |
155 | 454 |
] |
455 |
] |
|
456 |
] |
|
77 | 457 |
]. |
458 |
^ aBlock value |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
459 |
|
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
460 |
"Modified: 24.11.1995 / 13:11:25 / cg" |
1 | 461 |
! ! |
462 |
||
155 | 463 |
!Dictionary methodsFor:'copying'! |
464 |
||
465 |
postCopy |
|
466 |
"have to copy the valueArray too" |
|
467 |
||
468 |
super postCopy. |
|
469 |
valueArray := valueArray shallowCopy |
|
470 |
! ! |
|
471 |
||
217 | 472 |
!Dictionary methodsFor:'enumerating'! |
1 | 473 |
|
474 |
allKeysDo:aBlock |
|
302 | 475 |
"perform the block for all keys in the collection. |
476 |
Obsolete: use keysDo: for ST-80 compatibility." |
|
1 | 477 |
|
155 | 478 |
^ super do:aBlock |
1 | 479 |
! |
480 |
||
481 |
associationsDo:aBlock |
|
482 |
"perform the block for all associations in the collection." |
|
483 |
||
38 | 484 |
|key n "{ Class: SmallInteger }"| |
1 | 485 |
|
486 |
tally == 0 ifTrue:[^ self]. |
|
38 | 487 |
n := keyArray basicSize. |
488 |
1 to:n do:[:index | |
|
155 | 489 |
key := keyArray basicAt:index. |
490 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
491 |
aBlock value:(Association key:key value:(valueArray basicAt:index)) |
|
492 |
] |
|
1 | 493 |
] |
494 |
! |
|
495 |
||
609 | 496 |
collect:aBlock |
497 |
"for each element in the receiver, evaluate the argument, aBlock |
|
498 |
and return a Bag with the results" |
|
499 |
||
500 |
|newCollection| |
|
501 |
||
502 |
newCollection := Bag new. |
|
503 |
self do:[:each | |
|
504 |
newCollection add:(aBlock value:each) |
|
505 |
]. |
|
506 |
^ newCollection |
|
507 |
! |
|
508 |
||
1 | 509 |
do:aBlock |
510 |
"perform the block for all values in the collection." |
|
511 |
||
38 | 512 |
|key n "{ Class: SmallInteger }"| |
1 | 513 |
|
514 |
tally == 0 ifTrue:[^ self]. |
|
38 | 515 |
n := keyArray basicSize. |
516 |
1 to:n do:[:index | |
|
155 | 517 |
key := keyArray basicAt:index. |
518 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
519 |
aBlock value:(valueArray basicAt:index) |
|
520 |
]. |
|
1 | 521 |
] |
522 |
! |
|
523 |
||
3 | 524 |
keysAndValuesDo:aTwoArgBlock |
525 |
"evaluate the argument, aBlock for every element in the collection, |
|
526 |
passing both key and element as arguments." |
|
527 |
||
38 | 528 |
|key n "{ Class: SmallInteger }"| |
3 | 529 |
|
530 |
tally == 0 ifTrue:[^ self]. |
|
38 | 531 |
n := keyArray basicSize. |
532 |
1 to:n do:[:index | |
|
155 | 533 |
key := keyArray basicAt:index. |
534 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
535 |
aTwoArgBlock value:key value:(valueArray basicAt:index) |
|
536 |
]. |
|
3 | 537 |
] |
538 |
! |
|
539 |
||
609 | 540 |
keysDo:aBlock |
541 |
"perform the block for all keys in the collection." |
|
1 | 542 |
|
609 | 543 |
^ super do:aBlock |
1 | 544 |
! |
545 |
||
546 |
select:aBlock |
|
547 |
"return a new collection with all elements from the receiver, for which |
|
77 | 548 |
the argument aBlock evaluates to true. The block gets the individual values |
549 |
as its single argument." |
|
1 | 550 |
|
551 |
|newCollection| |
|
552 |
||
553 |
newCollection := self species new. |
|
77 | 554 |
self keysAndValuesDo:[:key :value | |
155 | 555 |
(aBlock value:value) ifTrue:[ |
556 |
newCollection at:key put:value |
|
557 |
] |
|
1 | 558 |
]. |
559 |
^ newCollection |
|
77 | 560 |
|
561 |
" |
|
562 |
|d| |
|
563 |
||
564 |
d := Dictionary new. |
|
565 |
d at:#foo put:#bar. |
|
566 |
d at:#bar put:#baz. |
|
567 |
d at:#baz put:#foo. |
|
568 |
||
569 |
d select:[:el | el startsWith:'b']. |
|
570 |
" |
|
1 | 571 |
! ! |
572 |
||
609 | 573 |
!Dictionary methodsFor:'inspecting'! |
574 |
||
575 |
inspectorClass |
|
576 |
"redefined to use DictionaryInspector |
|
577 |
(instead of the default Inspector)." |
|
578 |
||
579 |
^ DictionaryInspectorView |
|
580 |
! ! |
|
581 |
||
582 |
!Dictionary methodsFor:'printing & storing'! |
|
583 |
||
584 |
displayString |
|
585 |
"return a string for displaying" |
|
586 |
||
587 |
^ self stringWith:#displayString |
|
588 |
! |
|
589 |
||
590 |
printString |
|
591 |
"return a string for printing" |
|
592 |
||
593 |
^ self stringWith:#printString |
|
594 |
! |
|
595 |
||
596 |
storeOn:aStream |
|
597 |
"output a printed representation (which can be re-read) |
|
598 |
onto the argument aStream" |
|
599 |
||
600 |
|isEmpty| |
|
601 |
||
602 |
thisContext isRecursive ifTrue:[ |
|
603 |
Transcript showCr:'Error: storeOn: of self referencing collection.'. |
|
604 |
aStream nextPutAll:'#recursive'. |
|
605 |
^ self |
|
606 |
]. |
|
607 |
||
608 |
aStream nextPutAll:'('. |
|
609 |
aStream nextPutAll:(self class name). |
|
610 |
aStream nextPutAll:' new'. |
|
611 |
isEmpty := true. |
|
612 |
self keysAndValuesDo:[:key :value | |
|
613 |
aStream nextPutAll:' at:'. |
|
614 |
key storeOn:aStream. |
|
615 |
aStream nextPutAll:' put:'. |
|
616 |
value storeOn:aStream. |
|
617 |
aStream nextPutAll:'; '. |
|
618 |
isEmpty := false |
|
619 |
]. |
|
620 |
isEmpty ifFalse:[aStream nextPutAll:' yourself']. |
|
621 |
aStream nextPut:$) |
|
622 |
||
623 |
" |
|
624 |
Dictionary new storeOn:Transcript |
|
625 |
||
626 |
(Dictionary new at:1 put:'hello'; yourself) storeOn:Transcript |
|
627 |
" |
|
628 |
||
629 |
" |
|
630 |
|d| |
|
631 |
d := Dictionary new. |
|
632 |
d at:1 put:'hello'. |
|
633 |
d at:'hello' put:#world. |
|
634 |
d storeOn:Transcript |
|
635 |
" |
|
636 |
||
637 |
" |
|
638 |
|d| |
|
639 |
d := Dictionary new. |
|
640 |
d at:1 put:'hello'. |
|
641 |
d at:'hello' put:#world. |
|
642 |
d at:2 put:d. |
|
643 |
d storeOn:Transcript |
|
644 |
" |
|
645 |
! |
|
646 |
||
647 |
stringWith:aSelector |
|
648 |
"common code for printString & displayString" |
|
649 |
||
650 |
|thisString string noneYet| |
|
651 |
||
652 |
string := (self class name) , '('. |
|
653 |
noneYet := true. |
|
654 |
self associationsDo:[:element | |
|
655 |
thisString := element perform:aSelector. |
|
656 |
noneYet ifTrue:[noneYet := false] |
|
657 |
ifFalse:[thisString := ' ' , thisString]. |
|
658 |
string := string , thisString |
|
659 |
]. |
|
660 |
string := string , ')'. |
|
661 |
^string |
|
662 |
! ! |
|
663 |
||
1 | 664 |
!Dictionary methodsFor:'private'! |
665 |
||
345 | 666 |
compareSame:element1 with:element2 |
667 |
^ element1 = element2 |
|
668 |
! |
|
669 |
||
609 | 670 |
emptyCollectionForKeys |
671 |
^ Set new:(self size) |
|
1 | 672 |
! |
673 |
||
674 |
grow:newSize |
|
675 |
"grow the receiver to make space for at least newSize elements. |
|
676 |
To do this, we have to rehash into the new arrays. |
|
677 |
(which is done by re-adding all elements to a new, empty key/value array pair)." |
|
678 |
||
92 | 679 |
|key deletedEntry oldKeyArray oldValueArray n |
680 |
oldSize "{ Class:SmallInteger }" |
|
10 | 681 |
newIndex "{ Class:SmallInteger }" | |
1 | 682 |
|
10 | 683 |
oldKeyArray := keyArray. |
684 |
oldValueArray := valueArray. |
|
1 | 685 |
|
249 | 686 |
n := self class goodSizeFrom:newSize. |
687 |
oldSize := oldKeyArray size. |
|
688 |
n == oldSize ifTrue:[^ self]. |
|
689 |
||
10 | 690 |
keyArray := self keyContainerOfSize:n. |
92 | 691 |
valueArray := self valueContainerOfSize:n. |
1 | 692 |
|
249 | 693 |
|
92 | 694 |
deletedEntry := DeletedEntry. |
10 | 695 |
1 to:oldSize do:[:index | |
155 | 696 |
key := oldKeyArray basicAt:index. |
697 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
|
698 |
newIndex := self findNil:key. |
|
699 |
keyArray basicAt:newIndex put:key. |
|
700 |
valueArray basicAt:newIndex put:(oldValueArray basicAt:index). |
|
701 |
] |
|
1 | 702 |
] |
703 |
! |
|
704 |
||
705 |
rehash |
|
706 |
"rehash contents - is done by re-adding all elements to a new, empty key/value array pair)." |
|
707 |
||
2 | 708 |
| oldKeyArray oldValueArray key |
709 |
n "{ Class:SmallInteger }" |
|
710 |
newIndex "{ Class:SmallInteger }" | |
|
1 | 711 |
|
712 |
oldKeyArray := keyArray. |
|
713 |
oldValueArray := valueArray. |
|
714 |
||
715 |
n := keyArray size. |
|
10 | 716 |
keyArray := self keyContainerOfSize:n. |
92 | 717 |
valueArray := self valueContainerOfSize:n. |
1 | 718 |
|
2 | 719 |
1 to:n do:[:index | |
155 | 720 |
key := oldKeyArray basicAt:index. |
721 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
722 |
newIndex := self findNil:key. |
|
723 |
keyArray basicAt:newIndex put:key. |
|
724 |
valueArray basicAt:newIndex put:(oldValueArray basicAt:index). |
|
725 |
] |
|
1 | 726 |
] |
727 |
! |
|
728 |
||
729 |
rehashFrom:startIndex |
|
77 | 730 |
"rehash elements starting at index - after a remove. |
731 |
NOTE: this method is no longer needed; |
|
155 | 732 |
the trick using DeletedEntry avoids the need to do this time |
733 |
consuming operation, making remove pretty fast :-) |
|
77 | 734 |
" |
1 | 735 |
|
736 |
|key i length |
|
737 |
index "{ Class:SmallInteger }" | |
|
738 |
||
739 |
length := keyArray basicSize. |
|
740 |
index := startIndex. |
|
741 |
key := keyArray basicAt:index. |
|
742 |
[key notNil] whileTrue:[ |
|
155 | 743 |
key ~~ DeletedEntry ifTrue:[ |
744 |
i := self findNil:key. |
|
745 |
i == index ifTrue:[ |
|
746 |
^ self |
|
747 |
]. |
|
748 |
keyArray basicAt:i put:key. |
|
749 |
valueArray basicAt:i put:(valueArray basicAt:index). |
|
750 |
keyArray basicAt:index put:nil. |
|
751 |
valueArray basicAt:index put:nil. |
|
752 |
]. |
|
753 |
index == length ifTrue:[ |
|
754 |
index := 1 |
|
755 |
] ifFalse:[ |
|
756 |
index := index + 1. |
|
757 |
]. |
|
758 |
key := keyArray basicAt:index. |
|
1 | 759 |
] |
77 | 760 |
! |
761 |
||
609 | 762 |
setTally:count |
763 |
"initialize the contents array (for at least count slots) |
|
764 |
and set tally to zero. |
|
765 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 766 |
|
609 | 767 |
|n| |
1 | 768 |
|
609 | 769 |
n := self class goodSizeFrom:count. |
770 |
keyArray := self keyContainerOfSize:n. |
|
771 |
valueArray := self valueContainerOfSize:n. |
|
772 |
tally := 0 |
|
44 | 773 |
! |
774 |
||
609 | 775 |
valueContainerOfSize:n |
776 |
"return a container for values of size n. |
|
777 |
Extracted to make life of weak subclasses easier ..." |
|
44 | 778 |
|
609 | 779 |
^ Array basicNew:n |
780 |
! ! |
|
781 |
||
782 |
!Dictionary methodsFor:'testing'! |
|
44 | 783 |
|
609 | 784 |
includes:anObject |
785 |
"/ OLD: |
|
786 |
"/ "return true, if there is an association in the receiver with the |
|
787 |
"/ same key as the argument, anObject. |
|
788 |
"/ NOTICE: in contrast to #includesAssociation:, this compares only the key." |
|
789 |
"/ |
|
790 |
"/ ^ self includesKey:(anObject key) |
|
791 |
||
792 |
"/ NEW: |
|
793 |
"return true, if the argument, aValue is stored in the dictionary, |
|
794 |
i.e. if there is an associaten, with aValue as value. |
|
795 |
This is a slow search, since there is no fast reverse mapping; |
|
796 |
the values have to be all scanned without any hashing. |
|
797 |
You need a special collection (or two Dictionaries) to get this |
|
798 |
reverse mapping fast." |
|
799 |
||
800 |
^ self includesValue:anObject |
|
801 |
! |
|
68 | 802 |
|
609 | 803 |
includesAssociation:anAssociation |
804 |
"return true, if there is an association in the receiver with the |
|
805 |
same key and value as the argument, anAssociation. |
|
806 |
NOTICE: in contrast to #includes:, this compares both key and value." |
|
807 |
||
808 |
|val| |
|
44 | 809 |
|
609 | 810 |
val := self at:(anAssociation key) ifAbsent:[^ false]. |
811 |
^ (self compareSame:val with:anAssociation) value |
|
812 |
! |
|
813 |
||
814 |
includesKey:aKey |
|
815 |
"return true, if the argument, aKey is a key in the receiver" |
|
816 |
||
817 |
^ (self find:aKey ifAbsent:0) ~~ 0 |
|
818 |
! |
|
293 | 819 |
|
609 | 820 |
includesValue:aValue |
821 |
"return true, if the argument, aValue is stored in the dictionary, |
|
822 |
i.e. if there is an associaten, with aValue as value. |
|
823 |
This is a slow search, since there is no fast reverse mapping; |
|
824 |
the values have to be all scanned without any hashing. |
|
825 |
You need a special collection (or two Dictionaries) to get this |
|
826 |
reverse mapping fast." |
|
293 | 827 |
|
609 | 828 |
^ valueArray includes:aValue |
829 |
! |
|
293 | 830 |
|
609 | 831 |
occurrencesOf:anObject |
832 |
"count & return how often anObject is stored in the dictionary. |
|
833 |
This counts values - not keys." |
|
834 |
||
835 |
^ valueArray occurrencesOf:anObject |
|
1 | 836 |
! ! |
609 | 837 |
|
633 | 838 |
!Dictionary class methodsFor:'documentation'! |
839 |
||
840 |
version |
|
641
ef3bcf9de54b
return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
841 |
^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.31 1995-11-24 13:02:02 cg Exp $' |
633 | 842 |
! ! |