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