author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:36:55 +0200 | |
changeset 3898 | c90424dba938 |
parent 3178 | 5d24b4312240 |
child 4021 | 12a732f6d443 |
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 |
||
14 |
Dictionary subclass:#PluggableDictionary |
|
15 |
instanceVariableNames:'hashFunction compareFunction' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Unordered' |
|
19 |
! |
|
20 |
||
21 |
!PluggableDictionary class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 2014 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
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 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
a dictionary where the hash- and compare functions can be provided externally. |
|
40 |
||
41 |
[author:] |
|
42 |
Claus Gittinger |
|
43 |
" |
|
44 |
! |
|
45 |
||
46 |
examples |
|
47 |
" |
|
48 |
|s| |
|
49 |
||
50 |
s := PluggableDictionary |
|
51 |
hashWith:[:k | k size] |
|
52 |
compareWith:[:a :b | a notNil and:[b notNil and:[a asLowercase = b asLowercase]]]. |
|
53 |
s at:'hello' put:123. |
|
54 |
s at:'world' put:222. |
|
55 |
s at:'abc' put:333. |
|
56 |
s at:'Hello'. |
|
57 |
s at:'heLLo'. |
|
58 |
s at:'ABC'. |
|
59 |
s at:'WORLD'. |
|
60 |
s size. |
|
61 |
s includesKey:'heLlo'. |
|
62 |
s includesKey:'wOrLd'. |
|
63 |
s includesKey:'wOrLds'. |
|
64 |
" |
|
65 |
! ! |
|
66 |
||
67 |
!PluggableDictionary class methodsFor:'instance creation'! |
|
68 |
||
69 |
hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
70 |
^ self new hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
71 |
! ! |
|
72 |
||
73 |
!PluggableDictionary methodsFor:'accessing'! |
|
74 |
||
75 |
hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
76 |
hashFunction := hashFunctionArg. |
|
77 |
compareFunction := compareFunctionArg. |
|
78 |
! ! |
|
79 |
||
80 |
!PluggableDictionary methodsFor:'private'! |
|
81 |
||
82 |
compareSame:element1 with:element2 |
|
83 |
"compare two elements for being the same. |
|
84 |
Here, return the value from compareFunction" |
|
85 |
||
86 |
^ compareFunction value:element1 value:element2 |
|
87 |
! |
|
88 |
||
89 |
emptyCollectionForKeys |
|
90 |
"return an empty collection used for keys. |
|
91 |
Here, an IdentitySet is returned. |
|
92 |
Made a separate method to allow redefinition for different kind of |
|
93 |
containers in subclasses." |
|
94 |
||
95 |
^ PluggableSet new:(self size) hashWith:hashFunction compareWith:compareFunction |
|
96 |
! |
|
97 |
||
98 |
find:key ifAbsent:aBlock |
|
99 |
"Look for the key in the receiver. If it is found, return |
|
100 |
the index of the association containing the key, otherwise |
|
101 |
return the value of evaluating aBlock. |
|
102 |
Redefined - since we inherit this code from Set-Dictionary |
|
103 |
(one of the seldom cases, where I could make use of multiple inheritance |
|
104 |
and inherit from IdentitySet ... sigh)" |
|
105 |
||
106 |
|index "{ Class:SmallInteger }" |
|
107 |
length "{ Class:SmallInteger }" |
|
108 |
startIndex probe| |
|
109 |
||
110 |
length := keyArray basicSize. |
|
111 |
||
112 |
"/ |
|
113 |
"/ length < 10 ifTrue:[ |
|
114 |
"/ "assuming, that for small dictionaries the overhead of hashing |
|
115 |
"/ is large ... maybe that proves wrong (if overhead of comparing |
|
116 |
"/ is high)" |
|
117 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock. |
|
118 |
"/ ]. |
|
119 |
"/ |
|
120 |
||
121 |
startIndex := index := self initialIndexForKey:key. |
|
122 |
||
123 |
[true] whileTrue:[ |
|
124 |
probe := keyArray basicAt:index. |
|
3178
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
125 |
probe notNil ifTrue:[ |
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
126 |
(compareFunction value:probe value:key) ifTrue:[^ index]. "<<<< == is different from inherited" |
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
127 |
]. |
3175 | 128 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
129 |
||
130 |
index == length ifTrue:[ |
|
131 |
index := 1 |
|
132 |
] ifFalse:[ |
|
133 |
index := index + 1 |
|
134 |
]. |
|
135 |
index == startIndex ifTrue:[^ aBlock value] |
|
136 |
] |
|
137 |
! |
|
138 |
||
139 |
findKeyOrNil:key |
|
140 |
"Look for the key in the receiver. |
|
141 |
If it is found, return return the index of the first unused slot. |
|
142 |
Grow the receiver, if key was not found, and no unused slots were present" |
|
143 |
||
144 |
|index "{ Class:SmallInteger }" |
|
145 |
length "{ Class:SmallInteger }" |
|
146 |
startIndex probe |
|
147 |
delIndex "{ Class:SmallInteger }" | |
|
148 |
||
149 |
delIndex := 0. |
|
150 |
||
151 |
length := keyArray basicSize. |
|
152 |
startIndex := index := self initialIndexForKey:key. |
|
153 |
||
154 |
[true] whileTrue:[ |
|
155 |
probe := keyArray basicAt:index. |
|
3178
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
156 |
probe notNil ifTrue:[ |
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
157 |
(compareFunction value:probe value:key) ifTrue:[^ index]. |
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
158 |
]. |
3175 | 159 |
(self slotIsEmpty:probe) ifTrue:[ |
160 |
delIndex == 0 ifTrue:[^ index]. |
|
161 |
keyArray basicAt:delIndex put:nil. |
|
162 |
^ delIndex |
|
163 |
]. |
|
164 |
||
165 |
probe == DeletedEntry ifTrue:[ |
|
166 |
delIndex == 0 ifTrue:[ |
|
167 |
delIndex := index |
|
168 |
] |
|
169 |
]. |
|
170 |
||
171 |
index == length ifTrue:[ |
|
172 |
index := 1 |
|
173 |
] ifFalse:[ |
|
174 |
index := index + 1 |
|
175 |
]. |
|
176 |
index == startIndex ifTrue:[ |
|
177 |
delIndex ~~ 0 ifTrue:[ |
|
178 |
keyArray basicAt:delIndex put:nil. |
|
179 |
^ delIndex |
|
180 |
]. |
|
181 |
^ self grow findKeyOrNil:key |
|
182 |
]. |
|
183 |
] |
|
184 |
! |
|
185 |
||
186 |
hashFor:aKey |
|
187 |
"return an initial index given a key." |
|
188 |
||
189 |
^ hashFunction value:aKey |
|
190 |
! ! |
|
191 |
||
192 |
!PluggableDictionary methodsFor:'testing'! |
|
193 |
||
194 |
occurrencesOf:anObject |
|
195 |
"count & return how often anObject is stored in the dictionary. |
|
196 |
This counts values - not keys. |
|
197 |
Redefined to use #== (identity compare), NOT equality compare." |
|
198 |
||
199 |
|cnt| |
|
200 |
||
201 |
anObject isNil ifTrue:[^ super occurrencesOf:anObject]. |
|
202 |
||
203 |
cnt := 0. |
|
204 |
valueArray do:[:element | |
|
205 |
(compareFunction value:element value:anObject) ifTrue:[cnt := cnt + 1] |
|
206 |
]. |
|
207 |
^ cnt |
|
208 |
! ! |
|
209 |
||
210 |
!PluggableDictionary class methodsFor:'documentation'! |
|
211 |
||
212 |
version |
|
3178
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
213 |
^ '$Header: /cvs/stx/stx/libbasic2/PluggableDictionary.st,v 1.2 2014-02-18 21:21:01 cg Exp $' |
3175 | 214 |
! |
215 |
||
216 |
version_CVS |
|
3178
5d24b4312240
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3175
diff
changeset
|
217 |
^ '$Header: /cvs/stx/stx/libbasic2/PluggableDictionary.st,v 1.2 2014-02-18 21:21:01 cg Exp $' |
3175 | 218 |
! ! |
219 |