author | Claus Gittinger <cg@exept.de> |
Fri, 21 Mar 1997 10:31:23 +0100 | |
changeset 2474 | 0103fbbe2659 |
parent 2467 | 64cb4c25338d |
child 5050 | ea292b958108 |
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. |
|
1975 | 92 |
index < 16r1FFFFFFF ifTrue:[ |
93 |
index := index * 2 |
|
94 |
]. |
|
95 |
index := index \\ length + 1. |
|
609 | 96 |
startIndex := index. |
97 |
||
98 |
[true] whileTrue:[ |
|
99 |
probe := keyArray basicAt:index. |
|
100 |
probe == key ifTrue:[^ index]. |
|
101 |
||
102 |
index == length ifTrue:[ |
|
103 |
index := 1 |
|
104 |
] ifFalse:[ |
|
105 |
index := index + 1 |
|
106 |
]. |
|
107 |
(probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value] |
|
108 |
] |
|
109 |
! |
|
110 |
||
1 | 111 |
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
|
112 |
"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
|
113 |
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
|
114 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 115 |
|
116 |
|index "{ Class:SmallInteger }" |
|
375 | 117 |
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
|
118 |
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
|
119 |
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
|
120 |
|
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 |
delIndex := 0. |
1 | 122 |
|
123 |
length := keyArray basicSize. |
|
362 | 124 |
index := key identityHash. |
1975 | 125 |
index < 16r1FFFFFFF ifTrue:[ |
126 |
index := index * 2 |
|
127 |
]. |
|
128 |
index := index \\ length + 1. |
|
362 | 129 |
startIndex := index. |
1 | 130 |
|
131 |
[true] whileTrue:[ |
|
175 | 132 |
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
|
133 |
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
|
134 |
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
|
135 |
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
|
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 |
|
175 | 140 |
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
|
141 |
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
|
142 |
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
|
143 |
] |
175 | 144 |
]. |
1 | 145 |
|
175 | 146 |
index == length ifTrue:[ |
147 |
index := 1 |
|
148 |
] ifFalse:[ |
|
149 |
index := index + 1 |
|
150 |
]. |
|
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
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
^ 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
|
155 |
]. |
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
|
156 |
^ 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
|
157 |
]. |
1 | 158 |
] |
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 |
|
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
|
160 |
"Modified: 26.3.1996 / 20:00:44 / cg" |
1 | 161 |
! |
162 |
||
2460 | 163 |
hashFor:aKey |
164 |
"return an initial index given a key." |
|
165 |
||
166 |
^ aKey identityHash |
|
167 |
||
168 |
"Created: 19.3.1997 / 15:03:36 / cg" |
|
609 | 169 |
! ! |
13 | 170 |
|
609 | 171 |
!IdentityDictionary methodsFor:'testing'! |
1 | 172 |
|
609 | 173 |
includesValue:aValue |
174 |
"return true, if the argument, aValue is stored in the dictionary, |
|
175 |
Redefined to use identity compare, NOT equality compare" |
|
1 | 176 |
|
2467 | 177 |
^ (valueArray identityIndexOf:aValue startingAt:1) ~~ 0 |
178 |
||
179 |
"Modified: 20.3.1997 / 15:59:27 / cg" |
|
77 | 180 |
! |
181 |
||
609 | 182 |
occurrencesOf:anObject |
183 |
"count & return how often anObject is stored in the dictionary. |
|
184 |
This counts values - not keys. |
|
185 |
Redefined to use identity compare, NOT equality compare." |
|
186 |
||
187 |
|cnt| |
|
175 | 188 |
|
609 | 189 |
cnt := 0. |
190 |
valueArray do:[:element | |
|
191 |
element == anObject ifTrue:[cnt := cnt + 1] |
|
192 |
]. |
|
193 |
^ cnt |
|
1 | 194 |
! ! |
609 | 195 |
|
634 | 196 |
!IdentityDictionary class methodsFor:'documentation'! |
197 |
||
198 |
version |
|
2474
0103fbbe2659
can now use inherited #findNil:
Claus Gittinger <cg@exept.de>
parents:
2467
diff
changeset
|
199 |
^ '$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.26 1997-03-21 09:31:00 cg Exp $' |
634 | 200 |
! ! |