author | Claus Gittinger <cg@exept.de> |
Fri, 21 Dec 2001 11:59:16 +0100 | |
changeset 6346 | a69610ec89a0 |
parent 6233 | 0a79cbd07543 |
child 6916 | 1d7c11c94599 |
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 |
||
6233 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
Dictionary subclass:#IdentityDictionary |
1256 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Unordered' |
|
1 | 20 |
! |
21 |
||
88 | 22 |
!IdentityDictionary class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
175 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
same as a Dictionary but key must be identical - not just equal. |
|
41 |
Since compare is on identical keys (using ==), hashing is also done via |
|
42 |
#identityHash instead of #hash. |
|
43 |
IdentityDictionaries are especially useful, when symbols are used as keys. |
|
1290 | 44 |
|
45 |
[author:] |
|
46 |
Claus Gittinger |
|
88 | 47 |
" |
345 | 48 |
! ! |
49 |
||
1 | 50 |
!IdentityDictionary methodsFor:'private'! |
51 |
||
345 | 52 |
compareSame:element1 with:element2 |
1256 | 53 |
"compare two elements for being the same. Here, return true if the |
54 |
elements are identical (i.e. using #==)." |
|
55 |
||
345 | 56 |
^ element1 == element2 |
1256 | 57 |
|
58 |
"Modified: 22.4.1996 / 17:34:48 / cg" |
|
345 | 59 |
! |
60 |
||
609 | 61 |
emptyCollectionForKeys |
62 |
"return an empty collection used for keys. |
|
1256 | 63 |
Here, an IdentitySet is returned. |
609 | 64 |
Made a separate method to allow redefinition for different kind of |
1256 | 65 |
containers in subclasses." |
609 | 66 |
|
67 |
^ IdentitySet new:(self size) |
|
1256 | 68 |
|
69 |
"Modified: 22.4.1996 / 17:35:40 / cg" |
|
609 | 70 |
! |
71 |
||
72 |
find:key ifAbsent:aBlock |
|
73 |
"Look for the key in the receiver. If it is found, return |
|
74 |
the index of the association containing the key, otherwise |
|
6233 | 75 |
return the value of evaluating aBlock. |
76 |
Redefined - since we inherit this code from Set-Dictionary |
|
77 |
(one of the seldom cases, where I could make use of multiple inheritance |
|
78 |
and inherit from IdentitySet ... sigh)" |
|
609 | 79 |
|
80 |
|index "{ Class:SmallInteger }" |
|
81 |
length "{ Class:SmallInteger }" |
|
6233 | 82 |
startIndex probe| |
609 | 83 |
|
84 |
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
|
85 |
|
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 |
"/ |
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 |
"/ length < 10 ifTrue:[ |
6233 | 88 |
"/ "assuming, that for small dictionaries the overhead of hashing |
89 |
"/ is large ... maybe that proves wrong (if overhead of comparing |
|
90 |
"/ is high)" |
|
91 |
"/ ^ 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
|
92 |
"/ ]. |
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
|
93 |
"/ |
609 | 94 |
|
95 |
index := key identityHash. |
|
1975 | 96 |
index < 16r1FFFFFFF ifTrue:[ |
6233 | 97 |
index := index * 2 |
1975 | 98 |
]. |
99 |
index := index \\ length + 1. |
|
609 | 100 |
startIndex := index. |
101 |
||
102 |
[true] whileTrue:[ |
|
6233 | 103 |
probe := keyArray basicAt:index. |
104 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
105 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
609 | 106 |
|
6233 | 107 |
index == length ifTrue:[ |
108 |
index := 1 |
|
109 |
] ifFalse:[ |
|
110 |
index := index + 1 |
|
111 |
]. |
|
112 |
index == startIndex ifTrue:[^ aBlock value] |
|
609 | 113 |
] |
114 |
! |
|
115 |
||
1 | 116 |
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
|
117 |
"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
|
118 |
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
|
119 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 120 |
|
121 |
|index "{ Class:SmallInteger }" |
|
375 | 122 |
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
|
123 |
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
|
124 |
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
|
125 |
|
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 |
delIndex := 0. |
1 | 127 |
|
128 |
length := keyArray basicSize. |
|
362 | 129 |
index := key identityHash. |
1975 | 130 |
index < 16r1FFFFFFF ifTrue:[ |
131 |
index := index * 2 |
|
132 |
]. |
|
133 |
index := index \\ length + 1. |
|
362 | 134 |
startIndex := index. |
1 | 135 |
|
136 |
[true] whileTrue:[ |
|
6346 | 137 |
probe := keyArray basicAt:index. |
138 |
key == probe ifTrue:[^ index]. |
|
139 |
(self slotIsEmpty:probe) ifTrue:[ |
|
140 |
delIndex == 0 ifTrue:[^ index]. |
|
141 |
keyArray basicAt:delIndex put:nil. |
|
142 |
^ delIndex |
|
143 |
]. |
|
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
|
144 |
|
6346 | 145 |
probe == DeletedEntry ifTrue:[ |
146 |
delIndex == 0 ifTrue:[ |
|
147 |
delIndex := index |
|
148 |
] |
|
149 |
]. |
|
1 | 150 |
|
6346 | 151 |
index == length ifTrue:[ |
152 |
index := 1 |
|
153 |
] ifFalse:[ |
|
154 |
index := index + 1 |
|
155 |
]. |
|
156 |
index == startIndex ifTrue:[ |
|
157 |
delIndex ~~ 0 ifTrue:[ |
|
158 |
keyArray basicAt:delIndex put:nil. |
|
159 |
^ delIndex |
|
160 |
]. |
|
161 |
^ self grow findKeyOrNil:key |
|
162 |
]. |
|
1 | 163 |
] |
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
|
164 |
|
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 |
"Modified: 26.3.1996 / 20:00:44 / cg" |
1 | 166 |
! |
167 |
||
2460 | 168 |
hashFor:aKey |
169 |
"return an initial index given a key." |
|
170 |
||
171 |
^ aKey identityHash |
|
172 |
||
173 |
"Created: 19.3.1997 / 15:03:36 / cg" |
|
609 | 174 |
! ! |
13 | 175 |
|
609 | 176 |
!IdentityDictionary methodsFor:'testing'! |
1 | 177 |
|
609 | 178 |
includesValue:aValue |
179 |
"return true, if the argument, aValue is stored in the dictionary, |
|
180 |
Redefined to use identity compare, NOT equality compare" |
|
1 | 181 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
182 |
^ self includesIdenticalValue:aValue |
77 | 183 |
! |
184 |
||
609 | 185 |
occurrencesOf:anObject |
186 |
"count & return how often anObject is stored in the dictionary. |
|
187 |
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
|
188 |
Redefined to use #== (identity compare), NOT equality compare." |
609 | 189 |
|
190 |
|cnt| |
|
175 | 191 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
192 |
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
|
193 |
|
609 | 194 |
cnt := 0. |
195 |
valueArray do:[:element | |
|
196 |
element == anObject ifTrue:[cnt := cnt + 1] |
|
197 |
]. |
|
198 |
^ cnt |
|
1 | 199 |
! ! |
609 | 200 |
|
634 | 201 |
!IdentityDictionary class methodsFor:'documentation'! |
202 |
||
203 |
version |
|
6346 | 204 |
^ '$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.29 2001-12-21 10:59:16 cg Exp $' |
634 | 205 |
! ! |