author | Claus Gittinger <cg@exept.de> |
Mon, 15 Jul 2019 15:39:26 +0200 | |
changeset 5054 | b01df68cba0a |
parent 4836 | 2024ae7bafc8 |
permissions | -rw-r--r-- |
3176 | 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 |
||
4022 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
3176 | 16 |
Set subclass:#PluggableSet |
17 |
instanceVariableNames:'hashFunction compareFunction' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Unordered' |
|
21 |
! |
|
22 |
||
23 |
!PluggableSet 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 set 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 := PluggableSet |
|
53 |
hashWith:[:k | k size] |
|
54 |
compareWith:[:a :b | a notNil and:[b notNil and:[a asLowercase = b asLowercase]]]. |
|
55 |
s add:'hello'. |
|
56 |
s add:'world'. |
|
57 |
s add:'abc'. |
|
58 |
s add:'Hello'. |
|
59 |
s add:'heLLo'. |
|
60 |
s add:'ABC'. |
|
61 |
s add:'WORLD'. |
|
62 |
s size. |
|
63 |
s includes:'heLlo'. |
|
64 |
s includes:'wOrLd'. |
|
65 |
s includes:'wOrLds'. |
|
66 |
" |
|
67 |
! ! |
|
68 |
||
69 |
!PluggableSet class methodsFor:'instance creation'! |
|
70 |
||
71 |
hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
72 |
^ self new hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
73 |
! ! |
|
74 |
||
75 |
!PluggableSet methodsFor:'accessing'! |
|
76 |
||
77 |
hashWith:hashFunctionArg compareWith:compareFunctionArg |
|
78 |
hashFunction := hashFunctionArg. |
|
79 |
compareFunction := compareFunctionArg. |
|
80 |
! ! |
|
81 |
||
82 |
!PluggableSet methodsFor:'converting'! |
|
83 |
||
84 |
asIdentitySet |
|
85 |
"return the receiver as an IdentitySet" |
|
86 |
||
87 |
"could be an instance of a subclass..." |
|
88 |
self class == IdentitySet ifTrue:[ |
|
89 |
^ self |
|
90 |
]. |
|
91 |
^ super asIdentitySet |
|
92 |
! |
|
93 |
||
94 |
asNewIdentitySet |
|
95 |
"make sure to return myself as a unique new IdentitySet" |
|
96 |
||
97 |
"could be an instance of a subclass..." |
|
98 |
self class == IdentitySet ifTrue:[ |
|
99 |
^ self copy |
|
100 |
]. |
|
101 |
^ super asIdentitySet |
|
102 |
||
103 |
" |
|
104 |
|s| |
|
105 |
s := #(1 2 3 4) asIdentitySet. |
|
106 |
self assert:(s ~~ s asNewIdentitySet). |
|
107 |
self assert:(s = s asNewIdentitySet). |
|
108 |
" |
|
109 |
! ! |
|
110 |
||
4836 | 111 |
!PluggableSet methodsFor:'copying'! |
112 |
||
113 |
copyWithout:anElement |
|
114 |
"return a new collection consisting of a copy of the receiver, with |
|
115 |
ALL elements equal to elementToSkip are left out. |
|
116 |
No error is reported, if elementToSkip is not in the collection." |
|
117 |
||
118 |
^ self reject:[:each | compareFunction value:each value:anElement] |
|
119 |
||
120 |
" |
|
121 |
#(1 2 3 4 5 6 7) asSet copyWithout:5 |
|
122 |
" |
|
123 |
! ! |
|
124 |
||
3176 | 125 |
!PluggableSet methodsFor:'private'! |
126 |
||
127 |
collisionsFor:key |
|
128 |
"Return the number of searches - 1 required for key" |
|
129 |
||
130 |
|index "{ Class:SmallInteger }" |
|
131 |
length "{ Class:SmallInteger }" startIndex probe count| |
|
132 |
||
133 |
length := keyArray basicSize. |
|
134 |
startIndex := index := self initialIndexForKey:key. |
|
135 |
||
136 |
count := 0. |
|
137 |
[true] whileTrue:[ |
|
138 |
probe := keyArray basicAt:index. |
|
3179
3c3bf4781f2e
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3176
diff
changeset
|
139 |
(probe notNil and:[compareFunction value:key value:probe]) ifTrue:[^ count]. |
3176 | 140 |
(self slotIsEmpty:probe) ifTrue:[self error:'non existing key']. |
141 |
||
142 |
index == length ifTrue:[ |
|
143 |
index := 1. |
|
144 |
] ifFalse:[ |
|
145 |
index := index + 1. |
|
146 |
]. |
|
147 |
count := count + 1. |
|
148 |
index == startIndex ifTrue:[self error:'non existing key']. |
|
149 |
] |
|
150 |
! |
|
151 |
||
152 |
find:key ifAbsent:aBlock |
|
153 |
"Look for the key in the receiver. If it is found, return |
|
154 |
the index of the slot containing the key, otherwise |
|
155 |
return the value of evaluating aBlock. |
|
156 |
Redefined to compare for identity instead of equality" |
|
157 |
||
158 |
|index "{ Class:SmallInteger }" |
|
159 |
length "{ Class:SmallInteger }" |
|
160 |
startIndex probe | |
|
161 |
||
162 |
length := keyArray basicSize. |
|
163 |
||
164 |
"/ |
|
165 |
"/ length < 10 ifTrue:[ |
|
166 |
"/ "assuming, that for small sets the overhead of hashing |
|
167 |
"/ is large ..." |
|
168 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock. |
|
169 |
"/ ]. |
|
170 |
"/ |
|
171 |
||
172 |
startIndex := index := self initialIndexForKey:key. |
|
173 |
||
174 |
[true] whileTrue:[ |
|
175 |
probe := keyArray basicAt:index. |
|
4022 | 176 |
probe isNil ifTrue:[ |
177 |
^ aBlock value. |
|
3179
3c3bf4781f2e
don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents:
3176
diff
changeset
|
178 |
]. |
4022 | 179 |
|
180 |
(probe ~~ DeletedEntry |
|
4107 | 181 |
and:[compareFunction value:probe value:key]) ifTrue:[ " <<<< == is different from inherited" |
4022 | 182 |
^ index |
4107 | 183 |
]. |
3176 | 184 |
|
185 |
index == length ifTrue:[ |
|
186 |
index := 1 |
|
187 |
] ifFalse:[ |
|
188 |
index := index + 1 |
|
189 |
]. |
|
190 |
index == startIndex ifTrue:[ |
|
191 |
^ aBlock value |
|
192 |
] |
|
193 |
] |
|
194 |
! |
|
195 |
||
196 |
findKeyOrNil:key |
|
197 |
"Look for the key in the receiver. |
|
198 |
If it is found, return return the index of the first unused slot. |
|
199 |
Grow the receiver, if key was not found, and no unused slots were present" |
|
200 |
||
201 |
|index "{ Class:SmallInteger }" |
|
202 |
length "{ Class:SmallInteger }" |
|
203 |
startIndex probe |
|
204 |
delIndex "{ Class:SmallInteger }" | |
|
205 |
||
206 |
delIndex := 0. |
|
207 |
||
208 |
length := keyArray basicSize. |
|
209 |
startIndex := index := self initialIndexForKey:key. |
|
210 |
||
4022 | 211 |
[ |
3176 | 212 |
probe := keyArray basicAt:index. |
4022 | 213 |
probe isNil ifTrue:[ |
3176 | 214 |
delIndex == 0 ifTrue:[^ index]. |
215 |
keyArray basicAt:delIndex put:nil. |
|
216 |
^ delIndex |
|
217 |
]. |
|
4022 | 218 |
(probe == DeletedEntry and:[delIndex == 0]) ifTrue:[ |
219 |
delIndex := index. |
|
220 |
] ifFalse:[ |
|
221 |
(compareFunction value:probe value:key) ifTrue:[^ index] |
|
3176 | 222 |
]. |
223 |
||
224 |
index == length ifTrue:[ |
|
225 |
index := 1 |
|
226 |
] ifFalse:[ |
|
227 |
index := index + 1 |
|
228 |
]. |
|
229 |
index == startIndex ifTrue:[ |
|
230 |
delIndex ~~ 0 ifTrue:[ |
|
231 |
keyArray basicAt:delIndex put:nil. |
|
232 |
^ delIndex |
|
233 |
]. |
|
4022 | 234 |
self grow. |
235 |
length := keyArray basicSize. |
|
236 |
startIndex := index := self initialIndexForKey:key. |
|
3176 | 237 |
]. |
4022 | 238 |
] loop. |
239 |
||
240 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
|
241 |
! |
|
242 |
||
243 |
findKeyOrNilOrDeletedEntry:key |
|
244 |
"Look for the key in the receiver. |
|
245 |
If it is found, return return the index of the first unused slot. |
|
246 |
Grow the receiver, if key was not found, and no unused slots were present" |
|
247 |
||
248 |
|index "{ Class:SmallInteger }" |
|
249 |
length "{ Class:SmallInteger }" |
|
250 |
startIndex probe |
|
251 |
delIndex "{ Class:SmallInteger }" | |
|
252 |
||
253 |
delIndex := 0. |
|
254 |
||
255 |
length := keyArray basicSize. |
|
256 |
startIndex := index := self initialIndexForKey:key. |
|
257 |
||
258 |
[ |
|
259 |
probe := keyArray basicAt:index. |
|
260 |
probe isNil ifTrue:[ |
|
261 |
delIndex == 0 ifTrue:[^ index]. |
|
262 |
^ delIndex |
|
263 |
]. |
|
264 |
(probe == DeletedEntry and:[delIndex == 0]) ifTrue:[ |
|
265 |
delIndex := index. |
|
266 |
] ifFalse:[ |
|
267 |
(compareFunction value:probe value:key) ifTrue:[^ index] |
|
268 |
]. |
|
269 |
||
270 |
index == length ifTrue:[ |
|
271 |
index := 1 |
|
272 |
] ifFalse:[ |
|
273 |
index := index + 1 |
|
274 |
]. |
|
275 |
index == startIndex ifTrue:[ |
|
276 |
delIndex ~~ 0 ifTrue:[ |
|
277 |
^ delIndex |
|
278 |
]. |
|
279 |
self grow. |
|
280 |
length := keyArray basicSize. |
|
281 |
startIndex := index := self initialIndexForKey:key. |
|
282 |
]. |
|
283 |
] loop. |
|
3176 | 284 |
|
285 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
|
286 |
! |
|
287 |
||
288 |
hashFor:aKey |
|
289 |
"return an initial index given a key." |
|
290 |
||
291 |
^ hashFunction value:aKey |
|
292 |
! ! |
|
293 |
||
294 |
!PluggableSet class methodsFor:'documentation'! |
|
295 |
||
296 |
version |
|
4022 | 297 |
^ '$Header$' |
3176 | 298 |
! |
299 |
||
300 |
version_CVS |
|
4022 | 301 |
^ '$Header$' |
3176 | 302 |
! ! |
303 |