author | Claus Gittinger <cg@exept.de> |
Thu, 30 Jan 1997 14:59:44 +0100 | |
changeset 2328 | 0fd1d715e5a9 |
parent 2323 | a1fb9151a986 |
child 2383 | 8b2957f5ec90 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1991 by Claus Gittinger |
159 | 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 |
" |
|
12 |
||
13 |
Collection subclass:#Set |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
14 |
instanceVariableNames:'tally keyArray' |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
15 |
classVariableNames:'DeletedEntry' |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
16 |
poolDictionaries:'' |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
17 |
category:'Collections-Unordered' |
1 | 18 |
! |
19 |
||
2272 | 20 |
Object subclass:#EmptySlot |
2259 | 21 |
instanceVariableNames:'' |
22 |
classVariableNames:'' |
|
23 |
poolDictionaries:'' |
|
24 |
privateIn:Set |
|
25 |
! |
|
26 |
||
27 |
!Set class methodsFor:'documentation'! |
|
88 | 28 |
|
29 |
copyright |
|
30 |
" |
|
31 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
159 | 32 |
All Rights Reserved |
1 | 33 |
|
88 | 34 |
This software is furnished under a license and may be used |
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
! |
|
1 | 42 |
|
88 | 43 |
documentation |
44 |
" |
|
45 |
a Set is a collection where each element occurs at most once. |
|
95 | 46 |
The inclusion test is done using = for comparison; |
47 |
see IdentitySet for sets using identity compare. |
|
48 |
Sets use hashing for fast access, this access is considerably faster, |
|
49 |
if a good hash-number is returned by the elements. |
|
362 | 50 |
|
51 |
Notice that the default hash (Object>>hash) is not perfect; due to |
|
52 |
the implementation of hash-keys in ST/X, increased hash collisions |
|
53 |
are to be expected for large sets (say: > 20000 element). |
|
54 |
If your objects are heavyly used in sets or dictionaries, and you need |
|
55 |
big collections, your instances may provide a better hash values. |
|
95 | 56 |
|
345 | 57 |
Performance hints: |
657 | 58 |
If only symbols or smallIntegers are used as keys, |
362 | 59 |
use an instance of IdentitySet for slightly better performance, |
60 |
since both hashing and comparison is faster. |
|
345 | 61 |
|
62 |
If you have a rough idea how big the set is going to grow, |
|
63 |
create it using #new: instead of #new. Even if the size given is a |
|
64 |
poor guess (say half of the real size), there is some 20-30% performance |
|
65 |
win to expect, since many resizing operations of the set are avoided. |
|
66 |
||
95 | 67 |
Examples: |
68 |
||
1290 | 69 |
|s| |
70 |
s := Set new. |
|
71 |
s add:'hello'. |
|
72 |
s add:'world'. |
|
73 |
s add:#foo. |
|
74 |
s add:1.2345678. |
|
75 |
s add:'hello'. |
|
95 | 76 |
|
1290 | 77 |
s printNL. |
78 |
's size -> ' print. s size printNL. |
|
79 |
'(s includes:''hello'') -> ' print. (s includes:'hello') printNL. |
|
80 |
'(s includes:#foo) -> ' print. (s includes:#foo) printNL. |
|
81 |
'(s includes:''foo'') -> ' print. (s includes:'foo') printNL. |
|
82 |
'(s includes:#bar) -> ' print. (s includes:#bar) printNL. |
|
83 |
||
84 |
[author:] |
|
85 |
Claus Gittinger |
|
88 | 86 |
" |
87 |
! ! |
|
1 | 88 |
|
2259 | 89 |
!Set class methodsFor:'initialization'! |
41 | 90 |
|
91 |
initialize |
|
92 |
"initialize the Set class" |
|
93 |
||
94 |
DeletedEntry isNil ifTrue:[ |
|
2272 | 95 |
DeletedEntry := EmptySlot new |
41 | 96 |
]. |
97 |
||
98 |
"Set initialize" |
|
2259 | 99 |
|
100 |
"Modified: 24.1.1997 / 21:09:00 / cg" |
|
41 | 101 |
! ! |
102 |
||
2259 | 103 |
!Set class methodsFor:'instance creation'! |
1 | 104 |
|
105 |
new |
|
106 |
"return a new empty Set" |
|
107 |
||
108 |
^ self new:7 |
|
109 |
! |
|
110 |
||
111 |
new:anInteger |
|
112 |
"return a new empty Set with space for anInteger elements" |
|
113 |
||
159 | 114 |
" |
115 |
make it somewhat bigger; hashing works better if fill grade is |
|
116 |
below 10% (make it 75% here ..) |
|
117 |
" |
|
118 |
^ self basicNew setTally:(anInteger * 4 // 3) |
|
119 |
! ! |
|
120 |
||
2259 | 121 |
!Set class methodsFor:'queries'! |
252 | 122 |
|
123 |
goodSizeFrom:arg |
|
124 |
"return a good array size for the given argument. |
|
125 |
Returns the next prime after arg, since prime sizes are good for hashing." |
|
126 |
||
302 | 127 |
|n| |
128 |
||
252 | 129 |
arg <= 11 ifTrue:[^ 11]. |
130 |
||
302 | 131 |
n := arg * 3 // 2. |
132 |
||
252 | 133 |
" |
134 |
mhmh - this returns good numbers for collections with up-to about |
|
135 |
500k elements; if you have bigger ones, add some more primes here ... |
|
136 |
" |
|
302 | 137 |
n <= 524288 ifTrue:[ |
252 | 138 |
"2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288" |
302 | 139 |
^ #(11 11 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit) |
252 | 140 |
]. |
141 |
" |
|
142 |
make it odd - at least |
|
143 |
" |
|
302 | 144 |
^ n bitOr:1 |
252 | 145 |
! ! |
146 |
||
609 | 147 |
!Set methodsFor:'accessing'! |
148 |
||
149 |
at:index |
|
150 |
"report an error: at: is not allowed for Sets" |
|
151 |
||
152 |
^ self errorNotKeyed |
|
153 |
! |
|
154 |
||
155 |
at:index put:anObject |
|
156 |
"report an error: at:put: is not allowed for Sets" |
|
157 |
||
158 |
^ self errorNotKeyed |
|
159 |
! ! |
|
160 |
||
161 |
!Set methodsFor:'adding & removing'! |
|
162 |
||
163 |
add:anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
164 |
"add the argument, anObject to the receiver. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
165 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
166 |
WARNING: do not add elements while iterating over the receiver. |
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
167 |
Iterate over a copy to do this." |
609 | 168 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
169 |
|index "{ Class: SmallInteger }"| |
609 | 170 |
|
171 |
anObject notNil ifTrue:[ |
|
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
172 |
index := self findKeyOrNil:anObject. |
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
173 |
(keyArray basicAt:index) isNil ifTrue:[ |
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
174 |
"/ not already there |
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
175 |
keyArray basicAt:index put:anObject. |
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
176 |
tally := tally + 1. |
609 | 177 |
|
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
178 |
self fullCheck. |
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
179 |
] |
609 | 180 |
]. |
181 |
^ anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
182 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
183 |
"Modified: 30.1.1997 / 14:58:08 / cg" |
609 | 184 |
! |
185 |
||
186 |
remove:oldObject ifAbsent:exceptionBlock |
|
187 |
"remove oldObject from the collection and return it. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
188 |
If it was not in the collection return the value of exceptionBlock. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
189 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
190 |
WARNING: do not remove elements while iterating over the receiver. |
2314 | 191 |
See #saveRemove: to do this." |
609 | 192 |
|
193 |
|index next| |
|
194 |
||
195 |
index := self find:oldObject ifAbsent:0. |
|
196 |
index == 0 ifTrue:[^ exceptionBlock value]. |
|
197 |
||
198 |
keyArray basicAt:index put:nil. |
|
199 |
tally := tally - 1. |
|
200 |
tally == 0 ifTrue:[ |
|
2314 | 201 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). |
609 | 202 |
] ifFalse:[ |
2314 | 203 |
index == keyArray basicSize ifTrue:[ |
204 |
next := 1 |
|
205 |
] ifFalse:[ |
|
206 |
next := index + 1. |
|
207 |
]. |
|
208 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
209 |
keyArray basicAt:index put:DeletedEntry. |
|
210 |
]. |
|
211 |
self emptyCheck |
|
609 | 212 |
]. |
213 |
^ oldObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
214 |
|
2314 | 215 |
"Modified: 29.1.1997 / 21:34:33 / cg" |
609 | 216 |
! |
217 |
||
218 |
removeAll |
|
1164 | 219 |
"remove all elements from the receiver. Returns the receiver." |
609 | 220 |
|
221 |
self setTally:7. |
|
1164 | 222 |
|
223 |
"Modified: 12.4.1996 / 13:35:06 / cg" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
224 |
! |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
225 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
226 |
saveRemove:oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
227 |
"remove the element, oldObject from the collection. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
228 |
Return the element. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
229 |
If it was not in the collection return nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
230 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
231 |
In contrast to #remove:, this does not resize the underlying collection |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
232 |
and therefore does NOT rehash & change the elements order. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
233 |
Therefor this can be used while enumerating the receiver, |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
234 |
which is not possible if #remove: is used. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
235 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
236 |
WARNING: since no resizing is done, the physical amount of memory used |
2314 | 237 |
by the container remains the same, although the logical size shrinks. |
238 |
You may want to manually resize the receiver using #emptyCheck. |
|
239 |
(after the loop)" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
240 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
241 |
|index "{ Class:SmallInteger }" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
242 |
next "{ Class:SmallInteger }"| |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
243 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
244 |
oldObject isNil ifTrue:[^ nil]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
245 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
246 |
index := self find:oldObject ifAbsent:0. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
247 |
index == 0 ifTrue:[^ nil]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
248 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
249 |
keyArray basicAt:index put:nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
250 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
251 |
tally := tally - 1. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
252 |
tally ~~ 0 ifTrue:[ |
2314 | 253 |
index == keyArray basicSize ifTrue:[ |
254 |
next := 1 |
|
255 |
] ifFalse:[ |
|
256 |
next := index + 1. |
|
257 |
]. |
|
258 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
259 |
keyArray basicAt:index put:DeletedEntry |
|
260 |
]. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
261 |
]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
262 |
^ oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
263 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
264 |
"does NOT work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
265 |
|
2314 | 266 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
267 |
|
2314 | 268 |
s := Set new. |
269 |
s add:1. |
|
270 |
s add:2. |
|
271 |
s add:3. |
|
272 |
s add:4. |
|
273 |
s add:5. |
|
274 |
s add:6. |
|
275 |
s add:7. |
|
276 |
s add:8. |
|
277 |
s add:9. |
|
278 |
s do:[:v | |
|
279 |
v odd ifTrue:[ |
|
280 |
s remove:v |
|
281 |
] |
|
282 |
]. |
|
283 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
284 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
285 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
286 |
"DOES work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
287 |
|
2314 | 288 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
289 |
|
2314 | 290 |
s := Set new. |
291 |
s add:1. |
|
292 |
s add:2. |
|
293 |
s add:3. |
|
294 |
s add:4. |
|
295 |
s add:5. |
|
296 |
s add:6. |
|
297 |
s add:7. |
|
298 |
s add:8. |
|
299 |
s add:9. |
|
300 |
s do:[:v | |
|
301 |
v odd ifTrue:[ |
|
302 |
s saveRemove:v |
|
303 |
] |
|
304 |
]. |
|
305 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
306 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
307 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
308 |
"Created: 1.3.1996 / 21:14:26 / cg" |
2314 | 309 |
"Modified: 29.1.1997 / 21:35:55 / cg" |
609 | 310 |
! ! |
311 |
||
312 |
!Set methodsFor:'binary storage'! |
|
313 |
||
314 |
readBinaryContentsFrom: stream manager: manager |
|
315 |
"must rehash after reload" |
|
316 |
||
317 |
super readBinaryContentsFrom: stream manager: manager. |
|
318 |
self rehash |
|
319 |
! ! |
|
320 |
||
159 | 321 |
!Set methodsFor:'copying'! |
322 |
||
323 |
postCopy |
|
324 |
"have to copy the keyArray too" |
|
325 |
||
326 |
keyArray := keyArray shallowCopy |
|
1 | 327 |
! ! |
328 |
||
609 | 329 |
!Set methodsFor:'enumerating'! |
1 | 330 |
|
609 | 331 |
do:aBlock |
1056 | 332 |
"perform the block for all members in the collection. |
333 |
||
334 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
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:
1056
diff
changeset
|
335 |
Iterate over a copy to do this." |
13 | 336 |
|
609 | 337 |
|sz "{ Class: SmallInteger }" |
338 |
element| |
|
13 | 339 |
|
609 | 340 |
sz := keyArray size. |
341 |
1 to:sz do:[:index | |
|
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:
1056
diff
changeset
|
342 |
element := keyArray at:index. |
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:
1056
diff
changeset
|
343 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
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:
1056
diff
changeset
|
344 |
aBlock value:element |
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:
1056
diff
changeset
|
345 |
] |
609 | 346 |
] |
1056 | 347 |
|
348 |
"Modified: 1.3.1996 / 21:41:13 / cg" |
|
609 | 349 |
! ! |
61 | 350 |
|
969 | 351 |
!Set methodsFor:'inspecting'! |
352 |
||
353 |
inspectorClass |
|
354 |
"redefined to use SetInspector |
|
355 |
(instead of the default Inspector)." |
|
356 |
||
357 |
^ SetInspectorView |
|
358 |
! ! |
|
359 |
||
609 | 360 |
!Set methodsFor:'private'! |
2 | 361 |
|
41 | 362 |
emptyCheck |
252 | 363 |
"check if the receiver has become too empty (after a remove) |
324 | 364 |
and shrink if it makes sense. |
365 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
41 | 366 |
|
61 | 367 |
|sz "{Class: SmallInteger}" |
368 |
newSize "{Class: SmallInteger}" | |
|
41 | 369 |
|
61 | 370 |
sz := keyArray basicSize. |
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
371 |
sz > 56 ifTrue:[ |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
372 |
" |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
373 |
shrink if too empty |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
374 |
" |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
375 |
tally < (sz // 8) ifTrue:[ |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
376 |
newSize := sz // 7. |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
377 |
self grow:newSize |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
378 |
] |
41 | 379 |
] |
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
380 |
|
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
381 |
"Modified: 4.9.1996 / 14:39:53 / cg" |
41 | 382 |
! |
383 |
||
1 | 384 |
find:key ifAbsent:aBlock |
385 |
"Look for the key in the receiver. If it is found, return |
|
386 |
the index of the slot containing the key, otherwise |
|
387 |
return the value of evaluating aBlock." |
|
388 |
||
61 | 389 |
|index "{ Class:SmallInteger }" |
390 |
length "{ Class:SmallInteger }" |
|
391 |
startIndex probe| |
|
1 | 392 |
|
13 | 393 |
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:
1056
diff
changeset
|
394 |
|
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:
1056
diff
changeset
|
395 |
"/ |
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:
1056
diff
changeset
|
396 |
"/ length < 10 ifTrue:[ |
362 | 397 |
"/ "assuming, that for small collections the overhead of hashing |
398 |
"/ is larger ... maybe that proves wrong |
|
399 |
"/ (if overhead of comparing is higher)" |
|
400 |
"/ ^ keyArray indexOf: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:
1056
diff
changeset
|
401 |
"/ ]. |
13 | 402 |
|
362 | 403 |
index := key hash. |
1975 | 404 |
index < 16r1FFFFFFF ifTrue:[ |
405 |
index := index * 2 |
|
406 |
]. |
|
407 |
index := index \\ length + 1. |
|
362 | 408 |
startIndex := index. |
1 | 409 |
|
410 |
[true] whileTrue:[ |
|
159 | 411 |
probe := (keyArray basicAt:index). |
362 | 412 |
probe isNil ifTrue:[^ aBlock value]. |
159 | 413 |
key = probe ifTrue:[^ index]. |
1 | 414 |
|
159 | 415 |
index == length ifTrue:[ |
416 |
index := 1 |
|
417 |
] ifFalse:[ |
|
418 |
index := index + 1 |
|
419 |
]. |
|
362 | 420 |
index == startIndex ifTrue:[^ aBlock value]. |
1 | 421 |
] |
422 |
! |
|
423 |
||
13 | 424 |
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:
1056
diff
changeset
|
425 |
"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:
1056
diff
changeset
|
426 |
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:
1056
diff
changeset
|
427 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 428 |
|
61 | 429 |
|index "{ Class:SmallInteger }" |
430 |
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:
1056
diff
changeset
|
431 |
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:
1056
diff
changeset
|
432 |
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:
1056
diff
changeset
|
433 |
|
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:
1056
diff
changeset
|
434 |
delIndex := 0. |
1 | 435 |
|
13 | 436 |
length := keyArray basicSize. |
362 | 437 |
index := key hash. |
1975 | 438 |
index < 16r1FFFFFFF ifTrue:[ |
439 |
index := index * 2 |
|
440 |
]. |
|
441 |
index := index \\ length + 1. |
|
362 | 442 |
startIndex := index. |
1 | 443 |
|
444 |
[true] whileTrue:[ |
|
1450
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
445 |
probe := keyArray basicAt:index. |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
446 |
probe isNil ifTrue:[ |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
447 |
delIndex == 0 ifTrue:[^ index]. |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
448 |
keyArray basicAt:delIndex put:nil. |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
449 |
^ delIndex |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
450 |
]. |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
451 |
key = probe ifTrue:[^ index]. |
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:
1056
diff
changeset
|
452 |
|
1450
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
453 |
probe == DeletedEntry ifTrue:[ |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
454 |
delIndex == 0 ifTrue:[ |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
455 |
delIndex := index |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
456 |
] |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
457 |
]. |
1 | 458 |
|
1450
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
459 |
index == length ifTrue:[ |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
460 |
index := 1 |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
461 |
] ifFalse:[ |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
462 |
index := index + 1 |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
463 |
]. |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
464 |
index == startIndex ifTrue:[ |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
465 |
delIndex ~~ 0 ifTrue:[ |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
466 |
keyArray basicAt:delIndex put:nil. |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
467 |
^ delIndex |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
468 |
]. |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
469 |
^ self grow findKeyOrNil:key |
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
470 |
]. |
1 | 471 |
] |
1450
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
472 |
|
18c2a1da8be2
first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
473 |
"Modified: 4.6.1996 / 11:15:16 / cg" |
1 | 474 |
! |
475 |
||
476 |
findNil:key |
|
477 |
"Look for the next slot usable for key. This method assumes that |
|
1055 | 478 |
key is not already in the receiver and that keyArray does not have |
479 |
previously removed entries. |
|
480 |
To be used only while growing/rehashing to enter elements into a fresh |
|
481 |
collection." |
|
1 | 482 |
|
1319 | 483 |
|startIndex |
484 |
index "{ Class:SmallInteger }" |
|
61 | 485 |
length "{ Class:SmallInteger }"| |
1 | 486 |
|
13 | 487 |
length := keyArray basicSize. |
362 | 488 |
index := key hash. |
1975 | 489 |
index < 16r1FFFFFFF ifTrue:[ |
490 |
index := index * 2 |
|
491 |
]. |
|
492 |
index := index \\ length + 1. |
|
1319 | 493 |
startIndex := index. |
1 | 494 |
|
13 | 495 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
1319 | 496 |
index == length ifTrue:[ |
497 |
index := 1 |
|
498 |
] ifFalse:[ |
|
499 |
index := index + 1 |
|
500 |
]. |
|
501 |
index == startIndex ifTrue:[ |
|
502 |
"notice: should not be reached |
|
503 |
- we must find one since this is only to be |
|
504 |
called after growing" |
|
505 |
^ 0 |
|
506 |
]. |
|
1 | 507 |
]. |
508 |
^ index |
|
1055 | 509 |
|
1319 | 510 |
"Modified: 30.4.1996 / 14:15:20 / cg" |
1 | 511 |
! |
512 |
||
609 | 513 |
fullCheck |
514 |
"check if collection is full (after an add); grow if so. |
|
515 |
Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'" |
|
516 |
||
517 |
|sz "{Class: SmallInteger}" | |
|
518 |
||
519 |
" |
|
520 |
grow if filled more than 75% |
|
521 |
" |
|
522 |
sz := keyArray basicSize. |
|
523 |
tally > (sz * 3 // 4) ifTrue:[ |
|
524 |
self grow |
|
525 |
] |
|
526 |
! |
|
527 |
||
1 | 528 |
grow |
529 |
"change the number of element slots of the collection to a useful |
|
530 |
new size" |
|
531 |
||
13 | 532 |
self grow:(keyArray basicSize * 2) |
1 | 533 |
! |
534 |
||
535 |
grow:newSize |
|
536 |
"change the number of element slots of the collection - to do this, |
|
537 |
we have to rehash (which is done by re-adding all elements to a new |
|
538 |
empty set)." |
|
539 |
||
95 | 540 |
|elem oldKeyArray newKeyArray deletedEntry |
252 | 541 |
containerSize oldSize "{ Class:SmallInteger }"| |
1 | 542 |
|
13 | 543 |
oldKeyArray := keyArray. |
252 | 544 |
oldSize := oldKeyArray size. |
545 |
containerSize := (self class goodSizeFrom:newSize). |
|
546 |
containerSize == oldSize ifTrue:[^ self]. |
|
1 | 547 |
|
252 | 548 |
keyArray := newKeyArray := self keyContainerOfSize:containerSize. |
549 |
||
95 | 550 |
deletedEntry := DeletedEntry. |
13 | 551 |
1 to:oldSize do:[:srcIndex | |
159 | 552 |
elem := oldKeyArray basicAt:srcIndex. |
553 |
(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[ |
|
554 |
"cannot be already there" |
|
555 |
newKeyArray basicAt:(self findNil:elem) put:elem |
|
556 |
]. |
|
95 | 557 |
]. |
1 | 558 |
! |
559 |
||
609 | 560 |
initialIndexFor:hashKey boundedBy:length |
561 |
"for ST-80 compatibility only; it is (currently) not used in this |
|
562 |
implementation of sets. Therefore, in ST/X it does not make sense |
|
563 |
to redefine it. (which may be a bad design decision, but slightly |
|
564 |
improves performance, by avoiding an extra message send ...)" |
|
565 |
||
1972 | 566 |
^ (hashKey * 2 \\ length) + 1. |
609 | 567 |
! |
568 |
||
569 |
keyContainerOfSize:n |
|
570 |
"return a container for keys of size n. |
|
571 |
Extracted to make life of weak subclasses easier ..." |
|
572 |
||
573 |
^ Array basicNew:n |
|
574 |
! |
|
575 |
||
1 | 576 |
rehash |
61 | 577 |
"rehash is done by re-adding all elements to a new empty set. |
578 |
Rehash is needed after a binaryRead, for example." |
|
1 | 579 |
|
95 | 580 |
|element oldKeyArray newKeyArray |
581 |
n "{ Class:SmallInteger }"| |
|
1 | 582 |
|
95 | 583 |
oldKeyArray := keyArray. |
584 |
n := oldKeyArray size. |
|
585 |
keyArray := newKeyArray := self keyContainerOfSize:n. |
|
586 |
||
2 | 587 |
1 to:n do:[:index | |
159 | 588 |
element := oldKeyArray at:index. |
589 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
|
590 |
"cannot be already there" |
|
591 |
newKeyArray basicAt:(self findNil:element) put:element |
|
592 |
]. |
|
1 | 593 |
] |
594 |
! |
|
595 |
||
596 |
rehashFrom:startIndex |
|
61 | 597 |
"rehash elements starting at index - after a remove. |
598 |
Notice: due to the new implementation of remove, |
|
159 | 599 |
this is no longer needed" |
1 | 600 |
|
3 | 601 |
|element i "{ Class:SmallInteger }" |
602 |
length |
|
1 | 603 |
index "{ Class:SmallInteger }" | |
604 |
||
13 | 605 |
length := keyArray basicSize. |
1 | 606 |
index := startIndex. |
13 | 607 |
element := keyArray basicAt:index. |
1 | 608 |
[element notNil] whileTrue:[ |
159 | 609 |
i := self findNil:element. |
610 |
i == index ifTrue:[ |
|
611 |
^ self |
|
612 |
]. |
|
613 |
keyArray basicAt:i put:element. |
|
614 |
keyArray basicAt:index put:nil. |
|
1 | 615 |
|
159 | 616 |
index == length ifTrue:[ |
617 |
index := 1 |
|
618 |
] ifFalse:[ |
|
619 |
index := index + 1. |
|
620 |
]. |
|
621 |
element := keyArray basicAt:index. |
|
1 | 622 |
] |
623 |
! |
|
624 |
||
609 | 625 |
setTally:count |
626 |
"initialize the contents array (for at least count slots) |
|
627 |
and set tally to zero. |
|
628 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 629 |
|
609 | 630 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:count). |
631 |
tally := 0 |
|
1 | 632 |
! ! |
633 |
||
634 |
!Set methodsFor:'testing'! |
|
635 |
||
362 | 636 |
capacity |
637 |
"return the number of elements, that the receiver is |
|
638 |
prepared to take. |
|
639 |
Not used by the system; added for ST-80 compatibility." |
|
640 |
||
641 |
^ keyArray size |
|
642 |
! |
|
643 |
||
1 | 644 |
includes:anObject |
645 |
"return true if the argument anObject is in the receiver" |
|
646 |
||
362 | 647 |
^ (self find:anObject ifAbsent:0) ~~ 0 |
1 | 648 |
! |
649 |
||
650 |
isEmpty |
|
651 |
"return true if the receiver is empty" |
|
652 |
||
653 |
^ tally == 0 |
|
654 |
! |
|
655 |
||
609 | 656 |
isFixedSize |
657 |
"return true if the receiver cannot grow - this will vanish once |
|
658 |
Arrays and Strings learn how to grow ..." |
|
659 |
||
660 |
^ false |
|
661 |
! |
|
662 |
||
1 | 663 |
occurrencesOf:anObject |
664 |
"return the number of occurrences of anObject in the receiver" |
|
665 |
||
362 | 666 |
(self find:anObject ifAbsent:0) == 0 ifTrue:[^ 0]. |
1 | 667 |
^ 1 |
668 |
! |
|
669 |
||
609 | 670 |
size |
671 |
"return the number of set elements" |
|
1 | 672 |
|
609 | 673 |
^ tally |
1 | 674 |
! ! |
675 |
||
2259 | 676 |
!Set class methodsFor:'documentation'! |
635 | 677 |
|
678 |
version |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
679 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.46 1997-01-30 13:59:44 cg Exp $' |
635 | 680 |
! ! |
609 | 681 |
Set initialize! |