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