author | Claus Gittinger <cg@exept.de> |
Fri, 19 Apr 1996 12:47:25 +0200 | |
changeset 1221 | 46d72af387e9 |
parent 1126 | 497de696dff0 |
child 1256 | 249fd57abee5 |
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 |
|
609 | 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. |
|
42 |
" |
|
345 | 43 |
! ! |
44 |
||
1 | 45 |
!IdentityDictionary methodsFor:'private'! |
46 |
||
345 | 47 |
compareSame:element1 with:element2 |
48 |
^ element1 == element2 |
|
49 |
! |
|
50 |
||
609 | 51 |
emptyCollectionForKeys |
52 |
"return an empty collection used for keys. |
|
53 |
Made a separate method to allow redefinition for different kind of |
|
54 |
containers in subclasses" |
|
55 |
||
56 |
^ IdentitySet new:(self size) |
|
57 |
! |
|
58 |
||
59 |
find:key ifAbsent:aBlock |
|
60 |
"Look for the key in the receiver. If it is found, return |
|
61 |
the index of the association containing the key, otherwise |
|
62 |
return the value of evaluating aBlock." |
|
63 |
||
64 |
|index "{ Class:SmallInteger }" |
|
65 |
length "{ Class:SmallInteger }" |
|
66 |
startIndex |
|
67 |
probe| |
|
68 |
||
69 |
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
|
70 |
|
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
|
71 |
"/ |
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
|
72 |
"/ 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
|
73 |
"/ "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
|
74 |
"/ 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
|
75 |
"/ 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
|
76 |
"/ ^ 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
|
77 |
"/ ]. |
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
|
78 |
"/ |
609 | 79 |
|
80 |
index := key identityHash. |
|
81 |
index := index \\ length + 1. |
|
82 |
startIndex := index. |
|
83 |
||
84 |
[true] whileTrue:[ |
|
85 |
probe := keyArray basicAt:index. |
|
86 |
probe == key ifTrue:[^ index]. |
|
87 |
||
88 |
index == length ifTrue:[ |
|
89 |
index := 1 |
|
90 |
] ifFalse:[ |
|
91 |
index := index + 1 |
|
92 |
]. |
|
93 |
(probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value] |
|
94 |
] |
|
95 |
! |
|
96 |
||
1 | 97 |
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
|
98 |
"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
|
99 |
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
|
100 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 101 |
|
102 |
|index "{ Class:SmallInteger }" |
|
375 | 103 |
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
|
104 |
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
|
105 |
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
|
106 |
|
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
|
107 |
delIndex := 0. |
1 | 108 |
|
109 |
length := keyArray basicSize. |
|
362 | 110 |
index := key identityHash. |
111 |
index := index \\ length + 1. |
|
112 |
startIndex := index. |
|
1 | 113 |
|
114 |
[true] whileTrue:[ |
|
175 | 115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
^ 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
|
121 |
]. |
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
|
122 |
|
175 | 123 |
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
|
124 |
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
|
125 |
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
|
126 |
] |
175 | 127 |
]. |
1 | 128 |
|
175 | 129 |
index == length ifTrue:[ |
130 |
index := 1 |
|
131 |
] ifFalse:[ |
|
132 |
index := index + 1 |
|
133 |
]. |
|
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
|
134 |
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
|
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 |
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
|
137 |
^ 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
|
138 |
]. |
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
|
139 |
^ 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
|
140 |
]. |
1 | 141 |
] |
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
|
142 |
|
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
|
143 |
"Modified: 26.3.1996 / 20:00:44 / cg" |
1 | 144 |
! |
145 |
||
13 | 146 |
findNil:key |
147 |
"Look for the next slot usable for key. This method assumes that |
|
148 |
key is not already in the receiver - used only while growing/rehashing" |
|
149 |
||
150 |
|index "{ Class:SmallInteger }" |
|
375 | 151 |
length "{ Class:SmallInteger }"| |
13 | 152 |
|
153 |
length := keyArray basicSize. |
|
362 | 154 |
index := key identityHash. |
155 |
index := index \\ length + 1. |
|
13 | 156 |
|
157 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
|
175 | 158 |
index == length ifTrue:[ |
159 |
index := 1 |
|
160 |
] ifFalse:[ |
|
161 |
index := index + 1 |
|
162 |
]. |
|
163 |
"notice: no check for no nil found - we must find one since |
|
164 |
this is only called after growing" |
|
13 | 165 |
]. |
166 |
^ index |
|
609 | 167 |
! ! |
13 | 168 |
|
609 | 169 |
!IdentityDictionary methodsFor:'testing'! |
1 | 170 |
|
609 | 171 |
includesValue:aValue |
172 |
"return true, if the argument, aValue is stored in the dictionary, |
|
173 |
Redefined to use identity compare, NOT equality compare" |
|
1 | 174 |
|
609 | 175 |
^ (valueArray identityIndexOf:aValue) ~~ 0 |
77 | 176 |
! |
177 |
||
609 | 178 |
occurrencesOf:anObject |
179 |
"count & return how often anObject is stored in the dictionary. |
|
180 |
This counts values - not keys. |
|
181 |
Redefined to use identity compare, NOT equality compare." |
|
182 |
||
183 |
|cnt| |
|
175 | 184 |
|
609 | 185 |
cnt := 0. |
186 |
valueArray do:[:element | |
|
187 |
element == anObject ifTrue:[cnt := cnt + 1] |
|
188 |
]. |
|
189 |
^ cnt |
|
1 | 190 |
! ! |
609 | 191 |
|
634 | 192 |
!IdentityDictionary class methodsFor:'documentation'! |
193 |
||
194 |
version |
|
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
|
195 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/IdDict.st,v 1.19 1996-03-26 20:45:04 cg Exp $' |
634 | 196 |
! ! |