author | Claus Gittinger <cg@exept.de> |
Mon, 15 Jul 2019 15:39:26 +0200 | |
changeset 5054 | b01df68cba0a |
parent 4803 | 5040e2b3e0c5 |
permissions | -rw-r--r-- |
3175 | 1 |
" |
2 |
COPYRIGHT (c) 2014 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
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 |
"{ Package: 'stx:libbasic2' }" |
|
13 |
||
4021 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
3175 | 16 |
Dictionary subclass:#PluggableDictionary |
17 |
instanceVariableNames:'hashFunction compareFunction' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Unordered' |
|
21 |
! |
|
22 |
||
23 |
!PluggableDictionary class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2014 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
a dictionary where the hash- and compare functions can be provided externally. |
|
42 |
||
43 |
[author:] |
|
44 |
Claus Gittinger |
|
45 |
" |
|
46 |
! |
|
47 |
||
48 |
examples |
|
49 |
" |
|
50 |
|s| |
|
51 |
||
52 |
s := PluggableDictionary |
|
4021 | 53 |
hashWith:[:k | k asLowercase hash] |
3175 | 54 |
compareWith:[:a :b | a notNil and:[b notNil and:[a asLowercase = b asLowercase]]]. |
55 |
s at:'hello' put:123. |
|
56 |
s at:'world' put:222. |
|
57 |
s at:'abc' put:333. |
|
58 |
s at:'Hello'. |
|
59 |
s at:'heLLo'. |
|
60 |
s at:'ABC'. |
|
61 |
s at:'WORLD'. |
|
62 |
s size. |
|
63 |
s includesKey:'heLlo'. |
|
64 |
s includesKey:'wOrLd'. |
|
65 |
s includesKey:'wOrLds'. |
|
66 |
" |
|
67 |
! ! |
|
68 |
||
69 |
!PluggableDictionary class methodsFor:'instance creation'! |
|
70 |
||
71 |
hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
72 |
^ self new hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
73 |
! ! |
|
74 |
||
75 |
!PluggableDictionary methodsFor:'accessing'! |
|
76 |
||
77 |
hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
78 |
hashFunction := hashFunctionArg. |
|
79 |
compareFunction := compareFunctionArg. |
|
80 |
! ! |
|
81 |
||
82 |
!PluggableDictionary methodsFor:'private'! |
|
83 |
||
84 |
compareSame:element1 with:element2 |
|
85 |
"compare two elements for being the same. |
|
86 |
Here, return the value from compareFunction" |
|
87 |
||
88 |
^ compareFunction value:element1 value:element2 |
|
89 |
! |
|
90 |
||
91 |
find:key ifAbsent:aBlock |
|
92 |
"Look for the key in the receiver. If it is found, return |
|
93 |
the index of the association containing the key, otherwise |
|
94 |
return the value of evaluating aBlock. |
|
95 |
Redefined - since we inherit this code from Set-Dictionary |
|
96 |
(one of the seldom cases, where I could make use of multiple inheritance |
|
97 |
and inherit from IdentitySet ... sigh)" |
|
98 |
||
99 |
|index "{ Class:SmallInteger }" |
|
100 |
length "{ Class:SmallInteger }" |
|
101 |
startIndex probe| |
|
102 |
||
103 |
length := keyArray basicSize. |
|
104 |
||
105 |
"/ |
|
106 |
"/ length < 10 ifTrue:[ |
|
107 |
"/ "assuming, that for small dictionaries the overhead of hashing |
|
108 |
"/ is large ... maybe that proves wrong (if overhead of comparing |
|
109 |
"/ is high)" |
|
110 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock. |
|
111 |
"/ ]. |
|
112 |
"/ |
|
113 |
||
114 |
startIndex := index := self initialIndexForKey:key. |
|
115 |
||
4021 | 116 |
[ |
3175 | 117 |
probe := keyArray basicAt:index. |
4021 | 118 |
probe isNil ifTrue:[ |
119 |
^ aBlock value. |
|
3178
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
120 |
]. |
4021 | 121 |
|
122 |
(probe ~~ DeletedEntry |
|
123 |
and:[compareFunction value:probe value:key]) ifTrue:[ "<<<< == is different from inherited" |
|
124 |
^ index |
|
125 |
]. |
|
3175 | 126 |
|
127 |
index == length ifTrue:[ |
|
128 |
index := 1 |
|
129 |
] ifFalse:[ |
|
130 |
index := index + 1 |
|
131 |
]. |
|
132 |
index == startIndex ifTrue:[^ aBlock value] |
|
4021 | 133 |
] loop. |
3175 | 134 |
! |
135 |
||
136 |
findKeyOrNil:key |
|
137 |
"Look for the key in the receiver. |
|
138 |
If it is found, return return the index of the first unused slot. |
|
139 |
Grow the receiver, if key was not found, and no unused slots were present" |
|
140 |
||
141 |
|index "{ Class:SmallInteger }" |
|
142 |
length "{ Class:SmallInteger }" |
|
143 |
startIndex probe |
|
144 |
delIndex "{ Class:SmallInteger }" | |
|
145 |
||
146 |
delIndex := 0. |
|
147 |
||
148 |
length := keyArray basicSize. |
|
149 |
startIndex := index := self initialIndexForKey:key. |
|
150 |
||
4021 | 151 |
[ |
3175 | 152 |
probe := keyArray basicAt:index. |
4021 | 153 |
probe isNil ifTrue:[ |
3175 | 154 |
delIndex == 0 ifTrue:[^ index]. |
155 |
keyArray basicAt:delIndex put:nil. |
|
156 |
^ delIndex |
|
157 |
]. |
|
4021 | 158 |
(probe == DeletedEntry and:[delIndex == 0]) ifTrue:[ |
159 |
delIndex := index. |
|
160 |
] ifFalse:[ |
|
161 |
(compareFunction value:probe value:key) ifTrue:[^ index] |
|
3175 | 162 |
]. |
163 |
||
164 |
index == length ifTrue:[ |
|
165 |
index := 1 |
|
166 |
] ifFalse:[ |
|
167 |
index := index + 1 |
|
168 |
]. |
|
169 |
index == startIndex ifTrue:[ |
|
170 |
delIndex ~~ 0 ifTrue:[ |
|
171 |
keyArray basicAt:delIndex put:nil. |
|
172 |
^ delIndex |
|
173 |
]. |
|
4021 | 174 |
self grow. |
175 |
length := keyArray basicSize. |
|
176 |
startIndex := index := self initialIndexForKey:key. |
|
3175 | 177 |
]. |
4021 | 178 |
] loop. |
179 |
! |
|
180 |
||
181 |
findKeyOrNilOrDeletedEntry:key |
|
182 |
"Look for the key in the receiver. |
|
183 |
If it is found, return return the index of the first unused slot. |
|
184 |
Grow the receiver, if key was not found, and no unused slots were present" |
|
185 |
||
186 |
|index "{ Class:SmallInteger }" |
|
187 |
length "{ Class:SmallInteger }" |
|
188 |
startIndex probe |
|
189 |
delIndex "{ Class:SmallInteger }" | |
|
190 |
||
191 |
delIndex := 0. |
|
192 |
||
193 |
length := keyArray basicSize. |
|
194 |
startIndex := index := self initialIndexForKey:key. |
|
195 |
||
196 |
[ |
|
197 |
probe := keyArray basicAt:index. |
|
198 |
probe isNil ifTrue:[ |
|
199 |
delIndex == 0 ifTrue:[^ index]. |
|
200 |
^ delIndex |
|
201 |
]. |
|
202 |
(probe == DeletedEntry and:[delIndex == 0]) ifTrue:[ |
|
203 |
delIndex := index. |
|
204 |
] ifFalse:[ |
|
205 |
(compareFunction value:probe value:key) ifTrue:[^ index] |
|
206 |
]. |
|
207 |
||
208 |
index == length ifTrue:[ |
|
209 |
index := 1 |
|
210 |
] ifFalse:[ |
|
211 |
index := index + 1 |
|
212 |
]. |
|
213 |
index == startIndex ifTrue:[ |
|
214 |
delIndex ~~ 0 ifTrue:[ |
|
215 |
^ delIndex |
|
216 |
]. |
|
217 |
self grow. |
|
218 |
length := keyArray basicSize. |
|
219 |
startIndex := index := self initialIndexForKey:key. |
|
220 |
]. |
|
221 |
] loop. |
|
3175 | 222 |
! |
223 |
||
224 |
hashFor:aKey |
|
225 |
"return an initial index given a key." |
|
226 |
||
227 |
^ hashFunction value:aKey |
|
228 |
! ! |
|
229 |
||
4803 | 230 |
!PluggableDictionary methodsFor:'queries'! |
3175 | 231 |
|
232 |
occurrencesOf:anObject |
|
233 |
"count & return how often anObject is stored in the dictionary. |
|
234 |
This counts values - not keys. |
|
235 |
Redefined to use #== (identity compare), NOT equality compare." |
|
236 |
||
237 |
|cnt| |
|
238 |
||
239 |
anObject isNil ifTrue:[^ super occurrencesOf:anObject]. |
|
240 |
||
241 |
cnt := 0. |
|
242 |
valueArray do:[:element | |
|
243 |
(compareFunction value:element value:anObject) ifTrue:[cnt := cnt + 1] |
|
244 |
]. |
|
245 |
^ cnt |
|
246 |
! ! |
|
247 |
||
248 |
!PluggableDictionary class methodsFor:'documentation'! |
|
249 |
||
250 |
version |
|
4021 | 251 |
^ '$Header$' |
3175 | 252 |
! |
253 |
||
254 |
version_CVS |
|
4021 | 255 |
^ '$Header$' |
3175 | 256 |
! ! |
257 |