author | Claus Gittinger <cg@exept.de> |
Fri, 08 Nov 1996 16:27:05 +0100 | |
changeset 1938 | e1a56370a833 |
parent 1290 | 15ba3221b89b |
child 1972 | f9bfcad48b64 |
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 |
" |
|
12 |
||
13 |
Dictionary subclass:#IdentityDictionary |
|
1256 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Unordered' |
|
1 | 18 |
! |
19 |
||
88 | 20 |
!IdentityDictionary class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
175 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 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 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
same as a Dictionary but key must be identical - not just equal. |
|
39 |
Since compare is on identical keys (using ==), hashing is also done via |
|
40 |
#identityHash instead of #hash. |
|
41 |
IdentityDictionaries are especially useful, when symbols are used as keys. |
|
1290 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
88 | 45 |
" |
345 | 46 |
! ! |
47 |
||
1 | 48 |
!IdentityDictionary methodsFor:'private'! |
49 |
||
345 | 50 |
compareSame:element1 with:element2 |
1256 | 51 |
"compare two elements for being the same. Here, return true if the |
52 |
elements are identical (i.e. using #==)." |
|
53 |
||
345 | 54 |
^ element1 == element2 |
1256 | 55 |
|
56 |
"Modified: 22.4.1996 / 17:34:48 / cg" |
|
345 | 57 |
! |
58 |
||
609 | 59 |
emptyCollectionForKeys |
60 |
"return an empty collection used for keys. |
|
1256 | 61 |
Here, an IdentitySet is returned. |
609 | 62 |
Made a separate method to allow redefinition for different kind of |
1256 | 63 |
containers in subclasses." |
609 | 64 |
|
65 |
^ IdentitySet new:(self size) |
|
1256 | 66 |
|
67 |
"Modified: 22.4.1996 / 17:35:40 / cg" |
|
609 | 68 |
! |
69 |
||
70 |
find:key ifAbsent:aBlock |
|
71 |
"Look for the key in the receiver. If it is found, return |
|
72 |
the index of the association containing the key, otherwise |
|
73 |
return the value of evaluating aBlock." |
|
74 |
||
75 |
|index "{ Class:SmallInteger }" |
|
76 |
length "{ Class:SmallInteger }" |
|
77 |
startIndex |
|
78 |
probe| |
|
79 |
||
80 |
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
|
81 |
|
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
|
82 |
"/ |
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
|
83 |
"/ length < 10 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:
634
diff
changeset
|
84 |
"/ "assuming, that for small dictionaries the overhead of hashing |
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
|
85 |
"/ is large ... maybe that proves wrong (if overhead of comparing |
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
|
86 |
"/ is high)" |
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 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock. |
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 |
"/ |
609 | 90 |
|
91 |
index := key identityHash. |
|
92 |
index := index \\ length + 1. |
|
93 |
startIndex := index. |
|
94 |
||
95 |
[true] whileTrue:[ |
|
96 |
probe := keyArray basicAt:index. |
|
97 |
probe == key ifTrue:[^ index]. |
|
98 |
||
99 |
index == length ifTrue:[ |
|
100 |
index := 1 |
|
101 |
] ifFalse:[ |
|
102 |
index := index + 1 |
|
103 |
]. |
|
104 |
(probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value] |
|
105 |
] |
|
106 |
! |
|
107 |
||
1 | 108 |
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
|
109 |
"Look for the key in the receiver. |
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
|
110 |
If it is found, return return the index of the first unused slot. |
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
|
111 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 112 |
|
113 |
|index "{ Class:SmallInteger }" |
|
375 | 114 |
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
|
115 |
startIndex probe |
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
|
116 |
delIndex "{ Class:SmallInteger }" | |
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
|
117 |
|
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
|
118 |
delIndex := 0. |
1 | 119 |
|
120 |
length := keyArray basicSize. |
|
362 | 121 |
index := key identityHash. |
122 |
index := index \\ length + 1. |
|
123 |
startIndex := index. |
|
1 | 124 |
|
125 |
[true] whileTrue:[ |
|
175 | 126 |
probe := keyArray basicAt:index. |
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
|
127 |
key == probe ifTrue:[^ 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:
634
diff
changeset
|
128 |
probe isNil 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:
634
diff
changeset
|
129 |
delIndex == 0 ifTrue:[^ 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:
634
diff
changeset
|
130 |
keyArray basicAt:delIndex put: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:
634
diff
changeset
|
131 |
^ delIndex |
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 |
]. |
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 |
|
175 | 134 |
probe == DeletedEntry ifTrue:[ |
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
|
135 |
delIndex == 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:
634
diff
changeset
|
136 |
delIndex := 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:
634
diff
changeset
|
137 |
] |
175 | 138 |
]. |
1 | 139 |
|
175 | 140 |
index == length ifTrue:[ |
141 |
index := 1 |
|
142 |
] ifFalse:[ |
|
143 |
index := index + 1 |
|
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 |
index == startIndex 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:
634
diff
changeset
|
146 |
delIndex ~~ 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:
634
diff
changeset
|
147 |
keyArray basicAt:delIndex put: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:
634
diff
changeset
|
148 |
^ delIndex |
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
|
149 |
]. |
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
|
150 |
^ self grow findKeyOrNil: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:
634
diff
changeset
|
151 |
]. |
1 | 152 |
] |
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
|
153 |
|
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
|
154 |
"Modified: 26.3.1996 / 20:00:44 / cg" |
1 | 155 |
! |
156 |
||
13 | 157 |
findNil:key |
158 |
"Look for the next slot usable for key. This method assumes that |
|
159 |
key is not already in the receiver - used only while growing/rehashing" |
|
160 |
||
161 |
|index "{ Class:SmallInteger }" |
|
375 | 162 |
length "{ Class:SmallInteger }"| |
13 | 163 |
|
164 |
length := keyArray basicSize. |
|
362 | 165 |
index := key identityHash. |
166 |
index := index \\ length + 1. |
|
13 | 167 |
|
168 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
|
175 | 169 |
index == length ifTrue:[ |
170 |
index := 1 |
|
171 |
] ifFalse:[ |
|
172 |
index := index + 1 |
|
173 |
]. |
|
174 |
"notice: no check for no nil found - we must find one since |
|
175 |
this is only called after growing" |
|
13 | 176 |
]. |
177 |
^ index |
|
609 | 178 |
! ! |
13 | 179 |
|
609 | 180 |
!IdentityDictionary methodsFor:'testing'! |
1 | 181 |
|
609 | 182 |
includesValue:aValue |
183 |
"return true, if the argument, aValue is stored in the dictionary, |
|
184 |
Redefined to use identity compare, NOT equality compare" |
|
1 | 185 |
|
609 | 186 |
^ (valueArray identityIndexOf:aValue) ~~ 0 |
77 | 187 |
! |
188 |
||
609 | 189 |
occurrencesOf:anObject |
190 |
"count & return how often anObject is stored in the dictionary. |
|
191 |
This counts values - not keys. |
|
192 |
Redefined to use identity compare, NOT equality compare." |
|
193 |
||
194 |
|cnt| |
|
175 | 195 |
|
609 | 196 |
cnt := 0. |
197 |
valueArray do:[:element | |
|
198 |
element == anObject ifTrue:[cnt := cnt + 1] |
|
199 |
]. |
|
200 |
^ cnt |
|
1 | 201 |
! ! |
609 | 202 |
|
634 | 203 |
!IdentityDictionary class methodsFor:'documentation'! |
204 |
||
205 |
version |
|
1290 | 206 |
^ '$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.21 1996-04-25 16:18:54 cg Exp $' |
634 | 207 |
! ! |