author | Claus Gittinger <cg@exept.de> |
Thu, 27 Jun 2019 16:34:40 +0200 | |
changeset 24379 | 852be153b0ec |
parent 23844 | 77d509832234 |
permissions | -rw-r--r-- |
23844 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
4 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
175 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
6233 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
19547 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
Dictionary subclass:#IdentityDictionary |
1256 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Unordered' |
|
1 | 23 |
! |
24 |
||
88 | 25 |
!IdentityDictionary class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
175 | 30 |
All Rights Reserved |
1 | 31 |
|
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
1 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
20087 | 43 |
Same as a Dictionary, but key must be identical - not just equal. |
88 | 44 |
Since compare is on identical keys (using ==), hashing is also done via |
45 |
#identityHash instead of #hash. |
|
20087 | 46 |
IdentityDictionaries are especially useful when symbols are used as keys. |
1290 | 47 |
|
48 |
[author:] |
|
49 |
Claus Gittinger |
|
88 | 50 |
" |
345 | 51 |
! ! |
52 |
||
23844 | 53 |
!IdentityDictionary methodsFor:'copying'! |
54 |
||
55 |
copyWithout:anAssociation |
|
56 |
"Return a copy of the dictionary that is 1 smaller than the receiver and |
|
57 |
does not include the argument, anAssociation |
|
58 |
No error is reported, if elementToSkip is not in the collection." |
|
59 |
||
60 |
|newDict keyToIgnore valueToIgnore| |
|
61 |
||
62 |
newDict := self species new:self size - 1. |
|
63 |
||
64 |
keyToIgnore := anAssociation key. |
|
65 |
valueToIgnore := anAssociation value. |
|
66 |
self keysAndValuesDo:[:k :v | |
|
67 |
(keyToIgnore == k and:[valueToIgnore = v]) ifFalse:[ |
|
68 |
newDict at:k put:v |
|
69 |
] |
|
70 |
]. |
|
71 |
^ newDict. |
|
72 |
||
73 |
" |
|
74 |
|d d2| |
|
75 |
||
76 |
d := IdentityDictionary new |
|
77 |
at:1 put:'1'; |
|
78 |
at:2 put:'2'; |
|
79 |
at:3 put:'3'; |
|
80 |
at:4 put:'4'; |
|
81 |
at:4.0 put:'4'; |
|
82 |
at:5 put:'5'; |
|
83 |
yourself. |
|
84 |
d2 := d copyWithout:(4->'4'). |
|
85 |
d2 |
|
86 |
" |
|
87 |
||
88 |
"Created: / 05-03-2019 / 12:45:38 / Stefan Vogel" |
|
89 |
! ! |
|
90 |
||
1 | 91 |
!IdentityDictionary methodsFor:'private'! |
92 |
||
345 | 93 |
compareSame:element1 with:element2 |
1256 | 94 |
"compare two elements for being the same. Here, return true if the |
95 |
elements are identical (i.e. using #==)." |
|
96 |
||
345 | 97 |
^ element1 == element2 |
1256 | 98 |
|
99 |
"Modified: 22.4.1996 / 17:34:48 / cg" |
|
345 | 100 |
! |
101 |
||
609 | 102 |
emptyCollectionForKeys |
103 |
"return an empty collection used for keys. |
|
1256 | 104 |
Here, an IdentitySet is returned. |
609 | 105 |
Made a separate method to allow redefinition for different kind of |
1256 | 106 |
containers in subclasses." |
609 | 107 |
|
108 |
^ IdentitySet new:(self size) |
|
1256 | 109 |
|
110 |
"Modified: 22.4.1996 / 17:35:40 / cg" |
|
609 | 111 |
! |
112 |
||
113 |
find:key ifAbsent:aBlock |
|
114 |
"Look for the key in the receiver. If it is found, return |
|
115 |
the index of the association containing the key, otherwise |
|
6233 | 116 |
return the value of evaluating aBlock. |
117 |
Redefined - since we inherit this code from Set-Dictionary |
|
118 |
(one of the seldom cases, where I could make use of multiple inheritance |
|
119 |
and inherit from IdentitySet ... sigh)" |
|
609 | 120 |
|
121 |
|index "{ Class:SmallInteger }" |
|
122 |
length "{ Class:SmallInteger }" |
|
6233 | 123 |
startIndex probe| |
609 | 124 |
|
125 |
length := keyArray basicSize. |
|
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:
634
diff
changeset
|
126 |
|
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:
634
diff
changeset
|
127 |
"/ |
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:
634
diff
changeset
|
128 |
"/ length < 10 ifTrue:[ |
6233 | 129 |
"/ "assuming, that for small dictionaries the overhead of hashing |
130 |
"/ is large ... maybe that proves wrong (if overhead of comparing |
|
131 |
"/ is high)" |
|
132 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock. |
|
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:
634
diff
changeset
|
133 |
"/ ]. |
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:
634
diff
changeset
|
134 |
"/ |
609 | 135 |
|
6916
1d7c11c94599
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
136 |
startIndex := index := self initialIndexForKey:key. |
609 | 137 |
|
16246 | 138 |
[ |
6233 | 139 |
probe := keyArray basicAt:index. |
19999 | 140 |
probe == key ifTrue:[^ index]. "<--- == is different from inherited" |
19547 | 141 |
probe isNil ifTrue:[^ aBlock value]. |
609 | 142 |
|
6233 | 143 |
index == length ifTrue:[ |
144 |
index := 1 |
|
145 |
] ifFalse:[ |
|
146 |
index := index + 1 |
|
147 |
]. |
|
148 |
index == startIndex ifTrue:[^ aBlock value] |
|
16246 | 149 |
] loop. |
609 | 150 |
! |
151 |
||
20232 | 152 |
findIdentical:key ifAbsent:aBlock |
153 |
"IdentityDictionary does identity compare anyway..." |
|
154 |
||
155 |
^ self find:key ifAbsent:aBlock |
|
156 |
! |
|
157 |
||
1 | 158 |
findKeyOrNil:key |
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:
634
diff
changeset
|
159 |
"Look for the key in the receiver. |
19547 | 160 |
If it is found, return the index of the first unused slot. |
161 |
Grow the receiver, if key was not found, and no unused slots were present |
|
162 |
||
163 |
Warning: an empty slot MUST be filled by the sender - it is only to be sent |
|
164 |
by at:put: / add: - like methods." |
|
1 | 165 |
|
166 |
|index "{ Class:SmallInteger }" |
|
375 | 167 |
length "{ Class:SmallInteger }" |
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:
634
diff
changeset
|
168 |
startIndex probe |
19547 | 169 |
delIndex "{ Class:SmallInteger }"| |
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:
634
diff
changeset
|
170 |
|
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:
634
diff
changeset
|
171 |
delIndex := 0. |
1 | 172 |
|
173 |
length := keyArray basicSize. |
|
6916
1d7c11c94599
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
174 |
startIndex := index := self initialIndexForKey:key. |
1 | 175 |
|
16246 | 176 |
[ |
6346 | 177 |
probe := keyArray basicAt:index. |
19999 | 178 |
key == probe ifTrue:[^ index]. "<--- == is different from inherited" |
19547 | 179 |
probe isNil ifTrue:[ |
6346 | 180 |
delIndex == 0 ifTrue:[^ index]. |
181 |
keyArray basicAt:delIndex put:nil. |
|
182 |
^ delIndex |
|
183 |
]. |
|
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:
634
diff
changeset
|
184 |
|
20039 | 185 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
186 |
delIndex := index |
|
6346 | 187 |
]. |
1 | 188 |
|
6346 | 189 |
index == length ifTrue:[ |
190 |
index := 1 |
|
191 |
] ifFalse:[ |
|
192 |
index := index + 1 |
|
193 |
]. |
|
194 |
index == startIndex ifTrue:[ |
|
195 |
delIndex ~~ 0 ifTrue:[ |
|
196 |
keyArray basicAt:delIndex put:nil. |
|
197 |
^ delIndex |
|
198 |
]. |
|
20039 | 199 |
self grow. |
200 |
length := keyArray basicSize. |
|
201 |
startIndex := index := self initialIndexForKey:key. |
|
6346 | 202 |
]. |
16246 | 203 |
] loop. |
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:
634
diff
changeset
|
204 |
|
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:
634
diff
changeset
|
205 |
"Modified: 26.3.1996 / 20:00:44 / cg" |
1 | 206 |
! |
207 |
||
20233 | 208 |
findKeyOrNilOrDeletedEntry:key |
209 |
"Look for the key in the receiver. |
|
210 |
If it is found, return the index of the first unused slot. |
|
211 |
Grow the receiver, if key was not found, and no unused slots were present." |
|
212 |
||
213 |
|index "{ Class:SmallInteger }" |
|
214 |
length "{ Class:SmallInteger }" |
|
215 |
startIndex probe |
|
216 |
delIndex "{ Class:SmallInteger }"| |
|
217 |
||
218 |
delIndex := 0. |
|
219 |
||
220 |
length := keyArray basicSize. |
|
221 |
startIndex := index := self initialIndexForKey:key. |
|
222 |
||
223 |
[ |
|
224 |
probe := keyArray basicAt:index. |
|
225 |
key == probe ifTrue:[^ index]. "<--- == is different from inherited" |
|
226 |
probe isNil ifTrue:[ |
|
227 |
delIndex == 0 ifTrue:[^ index]. |
|
228 |
^ delIndex |
|
229 |
]. |
|
230 |
||
231 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
|
232 |
delIndex := index |
|
233 |
]. |
|
234 |
||
235 |
index == length ifTrue:[ |
|
236 |
index := 1 |
|
237 |
] ifFalse:[ |
|
238 |
index := index + 1 |
|
239 |
]. |
|
240 |
index == startIndex ifTrue:[ |
|
241 |
delIndex ~~ 0 ifTrue:[ |
|
242 |
^ delIndex |
|
243 |
]. |
|
244 |
self grow. |
|
245 |
length := keyArray basicSize. |
|
246 |
startIndex := index := self initialIndexForKey:key. |
|
247 |
]. |
|
248 |
] loop. |
|
249 |
||
250 |
"Modified: 26.3.1996 / 20:00:44 / cg" |
|
251 |
! |
|
252 |
||
2460 | 253 |
hashFor:aKey |
254 |
"return an initial index given a key." |
|
255 |
||
256 |
^ aKey identityHash |
|
257 |
||
258 |
"Created: 19.3.1997 / 15:03:36 / cg" |
|
609 | 259 |
! ! |
13 | 260 |
|
23698 | 261 |
!IdentityDictionary methodsFor:'queries'! |
1 | 262 |
|
609 | 263 |
includesValue:aValue |
264 |
"return true, if the argument, aValue is stored in the dictionary, |
|
265 |
Redefined to use identity compare, NOT equality compare" |
|
1 | 266 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
267 |
^ self includesIdenticalValue:aValue |
77 | 268 |
! |
269 |
||
609 | 270 |
occurrencesOf:anObject |
271 |
"count & return how often anObject is stored in the dictionary. |
|
272 |
This counts values - not keys. |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
273 |
Redefined to use #== (identity compare), NOT equality compare." |
609 | 274 |
|
275 |
|cnt| |
|
175 | 276 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
277 |
anObject isNil ifTrue:[^ super occurrencesOf:anObject]. |
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
278 |
|
609 | 279 |
cnt := 0. |
280 |
valueArray do:[:element | |
|
281 |
element == anObject ifTrue:[cnt := cnt + 1] |
|
282 |
]. |
|
283 |
^ cnt |
|
1 | 284 |
! ! |
609 | 285 |
|
23698 | 286 |
|
634 | 287 |
!IdentityDictionary class methodsFor:'documentation'! |
288 |
||
289 |
version |
|
19547 | 290 |
^ '$Header$' |
20233 | 291 |
! |
292 |
||
293 |
version_CVS |
|
294 |
^ '$Header$' |
|
634 | 295 |
! ! |
16246 | 296 |