author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 6916 | 1d7c11c94599 |
child 17761 | b0e5971141bc |
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 |
|
6916
1d7c11c94599
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
95 |
startIndex := index := self initialIndexForKey:key. |
609 | 96 |
|
97 |
[true] whileTrue:[ |
|
6233 | 98 |
probe := keyArray basicAt:index. |
99 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
100 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
609 | 101 |
|
6233 | 102 |
index == length ifTrue:[ |
103 |
index := 1 |
|
104 |
] ifFalse:[ |
|
105 |
index := index + 1 |
|
106 |
]. |
|
107 |
index == startIndex ifTrue:[^ aBlock value] |
|
609 | 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. |
|
6916
1d7c11c94599
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
124 |
startIndex := index := self initialIndexForKey:key. |
1 | 125 |
|
126 |
[true] whileTrue:[ |
|
6346 | 127 |
probe := keyArray basicAt:index. |
128 |
key == probe ifTrue:[^ index]. |
|
129 |
(self slotIsEmpty:probe) ifTrue:[ |
|
130 |
delIndex == 0 ifTrue:[^ index]. |
|
131 |
keyArray basicAt:delIndex put:nil. |
|
132 |
^ delIndex |
|
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 |
|
6346 | 135 |
probe == DeletedEntry ifTrue:[ |
136 |
delIndex == 0 ifTrue:[ |
|
137 |
delIndex := index |
|
138 |
] |
|
139 |
]. |
|
1 | 140 |
|
6346 | 141 |
index == length ifTrue:[ |
142 |
index := 1 |
|
143 |
] ifFalse:[ |
|
144 |
index := index + 1 |
|
145 |
]. |
|
146 |
index == startIndex ifTrue:[ |
|
147 |
delIndex ~~ 0 ifTrue:[ |
|
148 |
keyArray basicAt:delIndex put:nil. |
|
149 |
^ delIndex |
|
150 |
]. |
|
151 |
^ self grow findKeyOrNil:key |
|
152 |
]. |
|
1 | 153 |
] |
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
|
154 |
|
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 |
"Modified: 26.3.1996 / 20:00:44 / cg" |
1 | 156 |
! |
157 |
||
2460 | 158 |
hashFor:aKey |
159 |
"return an initial index given a key." |
|
160 |
||
161 |
^ aKey identityHash |
|
162 |
||
163 |
"Created: 19.3.1997 / 15:03:36 / cg" |
|
609 | 164 |
! ! |
13 | 165 |
|
609 | 166 |
!IdentityDictionary methodsFor:'testing'! |
1 | 167 |
|
609 | 168 |
includesValue:aValue |
169 |
"return true, if the argument, aValue is stored in the dictionary, |
|
170 |
Redefined to use identity compare, NOT equality compare" |
|
1 | 171 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
172 |
^ self includesIdenticalValue:aValue |
77 | 173 |
! |
174 |
||
609 | 175 |
occurrencesOf:anObject |
176 |
"count & return how often anObject is stored in the dictionary. |
|
177 |
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
|
178 |
Redefined to use #== (identity compare), NOT equality compare." |
609 | 179 |
|
180 |
|cnt| |
|
175 | 181 |
|
5050
ea292b958108
allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
182 |
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
|
183 |
|
609 | 184 |
cnt := 0. |
185 |
valueArray do:[:element | |
|
186 |
element == anObject ifTrue:[cnt := cnt + 1] |
|
187 |
]. |
|
188 |
^ cnt |
|
1 | 189 |
! ! |
609 | 190 |
|
634 | 191 |
!IdentityDictionary class methodsFor:'documentation'! |
192 |
||
193 |
version |
|
17711 | 194 |
^ '$Id: IdentityDictionary.st 10447 2009-06-14 13:09:55Z vranyj1 $' |
634 | 195 |
! ! |