author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 19 Jan 2012 11:46:00 +0000 | |
branch | jv |
changeset 17911 | a99f15c5efa5 |
parent 17910 | 8d796ca8bd1d |
child 18120 | e3a375d5f6a8 |
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 |
" |
|
6233 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Dictionary subclass:#IdentityDictionary |
1256 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Unordered' |
|
1 | 19 |
! |
20 |
||
88 | 21 |
!IdentityDictionary class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
175 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
39 |
same as a Dictionary but key must be identical - not just equal. |
|
40 |
Since compare is on identical keys (using ==), hashing is also done via |
|
41 |
#identityHash instead of #hash. |
|
42 |
IdentityDictionaries are especially useful, when symbols are used as keys. |
|
1290 | 43 |
|
44 |
[author:] |
|
45 |
Claus Gittinger |
|
88 | 46 |
" |
345 | 47 |
! ! |
48 |
||
1 | 49 |
!IdentityDictionary methodsFor:'private'! |
50 |
||
345 | 51 |
compareSame:element1 with:element2 |
1256 | 52 |
"compare two elements for being the same. Here, return true if the |
53 |
elements are identical (i.e. using #==)." |
|
54 |
||
345 | 55 |
^ element1 == element2 |
1256 | 56 |
|
57 |
"Modified: 22.4.1996 / 17:34:48 / cg" |
|
345 | 58 |
! |
59 |
||
609 | 60 |
emptyCollectionForKeys |
61 |
"return an empty collection used for keys. |
|
1256 | 62 |
Here, an IdentitySet is returned. |
609 | 63 |
Made a separate method to allow redefinition for different kind of |
1256 | 64 |
containers in subclasses." |
609 | 65 |
|
66 |
^ IdentitySet new:(self size) |
|
1256 | 67 |
|
68 |
"Modified: 22.4.1996 / 17:35:40 / cg" |
|
609 | 69 |
! |
70 |
||
71 |
find:key ifAbsent:aBlock |
|
72 |
"Look for the key in the receiver. If it is found, return |
|
73 |
the index of the association containing the key, otherwise |
|
6233 | 74 |
return the value of evaluating aBlock. |
75 |
Redefined - since we inherit this code from Set-Dictionary |
|
76 |
(one of the seldom cases, where I could make use of multiple inheritance |
|
77 |
and inherit from IdentitySet ... sigh)" |
|
609 | 78 |
|
79 |
|index "{ Class:SmallInteger }" |
|
80 |
length "{ Class:SmallInteger }" |
|
6233 | 81 |
startIndex probe| |
609 | 82 |
|
83 |
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
|
84 |
|
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 |
"/ length < 10 ifTrue:[ |
6233 | 87 |
"/ "assuming, that for small dictionaries the overhead of hashing |
88 |
"/ is large ... maybe that proves wrong (if overhead of comparing |
|
89 |
"/ is high)" |
|
90 |
"/ ^ 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
|
91 |
"/ ]. |
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 |
"/ |
609 | 93 |
|
6916
1d7c11c94599
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
94 |
startIndex := index := self initialIndexForKey:key. |
609 | 95 |
|
96 |
[true] whileTrue:[ |
|
6233 | 97 |
probe := keyArray basicAt:index. |
98 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
99 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
609 | 100 |
|
6233 | 101 |
index == length ifTrue:[ |
102 |
index := 1 |
|
103 |
] ifFalse:[ |
|
104 |
index := index + 1 |
|
105 |
]. |
|
106 |
index == startIndex ifTrue:[^ aBlock value] |
|
609 | 107 |
] |
108 |
! |
|
109 |
||
1 | 110 |
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
|
111 |
"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
|
112 |
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
|
113 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 114 |
|
115 |
|index "{ Class:SmallInteger }" |
|
375 | 116 |
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
|
117 |
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
|
118 |
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
|
119 |
|
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 := 0. |
1 | 121 |
|
122 |
length := keyArray basicSize. |
|
6916
1d7c11c94599
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
123 |
startIndex := index := self initialIndexForKey:key. |
1 | 124 |
|
125 |
[true] whileTrue:[ |
|
6346 | 126 |
probe := keyArray basicAt:index. |
127 |
key == probe ifTrue:[^ index]. |
|
128 |
(self slotIsEmpty:probe) ifTrue:[ |
|
129 |
delIndex == 0 ifTrue:[^ index]. |
|
130 |
keyArray basicAt:delIndex put:nil. |
|
131 |
^ delIndex |
|
132 |
]. |
|
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 |
|
6346 | 134 |
probe == DeletedEntry ifTrue:[ |
135 |
delIndex == 0 ifTrue:[ |
|
136 |
delIndex := index |
|
137 |
] |
|
138 |
]. |
|
1 | 139 |
|
6346 | 140 |
index == length ifTrue:[ |
141 |
index := 1 |
|
142 |
] ifFalse:[ |
|
143 |
index := index + 1 |
|
144 |
]. |
|
145 |
index == startIndex ifTrue:[ |
|
146 |
delIndex ~~ 0 ifTrue:[ |
|
147 |
keyArray basicAt:delIndex put:nil. |
|
148 |
^ delIndex |
|
149 |
]. |
|
150 |
^ self grow findKeyOrNil:key |
|
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 |
||
2460 | 157 |
hashFor:aKey |
158 |
"return an initial index given a key." |
|
159 |
||
160 |
^ aKey identityHash |
|
161 |
||
162 |
"Created: 19.3.1997 / 15:03:36 / cg" |
|
609 | 163 |
! ! |
13 | 164 |
|
17865 | 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 |
|
17892 | 194 |
^ '$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.30 2002/11/29 11:08:54 cg Exp $' |
17865 | 195 |
! |
196 |
||
197 |
version_SVN |
|
17911 | 198 |
^ '$Id: IdentityDictionary.st 10761 2012-01-19 11:46:00Z vranyj1 $' |
17846 | 199 |
! ! |
17892 | 200 |
|
17910 | 201 |
|
17911 | 202 |