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