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
|
13
|
14 |
instanceVariableNames:'tally keyArray'
|
41
|
15 |
classVariableNames:'DeletedEntry'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
Set comment:'
|
88
|
21 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
95
|
23 |
|
375
|
24 |
$Header: /cvs/stx/stx/libbasic/Set.st,v 1.20 1995-08-08 00:48:53 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!Set class methodsFor:'documentation'!
|
|
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 |
version
|
|
44 |
"
|
375
|
45 |
$Header: /cvs/stx/stx/libbasic/Set.st,v 1.20 1995-08-08 00:48:53 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
a Set is a collection where each element occurs at most once.
|
95
|
52 |
The inclusion test is done using = for comparison;
|
|
53 |
see IdentitySet for sets using identity compare.
|
|
54 |
Sets use hashing for fast access, this access is considerably faster,
|
|
55 |
if a good hash-number is returned by the elements.
|
362
|
56 |
|
|
57 |
Notice that the default hash (Object>>hash) is not perfect; due to
|
|
58 |
the implementation of hash-keys in ST/X, increased hash collisions
|
|
59 |
are to be expected for large sets (say: > 20000 element).
|
|
60 |
If your objects are heavyly used in sets or dictionaries, and you need
|
|
61 |
big collections, your instances may provide a better hash values.
|
95
|
62 |
|
345
|
63 |
Performance hints:
|
362
|
64 |
If only symbols or smallIntegers are entered into a set,
|
|
65 |
use an instance of IdentitySet for slightly better performance,
|
|
66 |
since both hashing and comparison is faster.
|
345
|
67 |
|
|
68 |
If you have a rough idea how big the set is going to grow,
|
|
69 |
create it using #new: instead of #new. Even if the size given is a
|
|
70 |
poor guess (say half of the real size), there is some 20-30% performance
|
|
71 |
win to expect, since many resizing operations of the set are avoided.
|
|
72 |
|
95
|
73 |
Examples:
|
|
74 |
|
159
|
75 |
|s|
|
|
76 |
s := Set new.
|
|
77 |
s add:'hello'.
|
|
78 |
s add:'world'.
|
|
79 |
s add:#foo.
|
|
80 |
s add:1.2345678.
|
|
81 |
s add:'hello'.
|
95
|
82 |
|
159
|
83 |
s printNL.
|
|
84 |
's size -> ' print. s size printNL.
|
362
|
85 |
'(s includes:''hello'') -> ' print. (s includes:'hello') printNL.
|
|
86 |
'(s includes:#foo) -> ' print. (s includes:#foo) printNL.
|
|
87 |
'(s includes:''foo'') -> ' print. (s includes:'foo') printNL.
|
|
88 |
'(s includes:#bar) -> ' print. (s includes:#bar) printNL.
|
88
|
89 |
"
|
|
90 |
! !
|
1
|
91 |
|
41
|
92 |
!Set class methodsFor:'initialization'!
|
|
93 |
|
|
94 |
initialize
|
|
95 |
"initialize the Set class"
|
|
96 |
|
|
97 |
DeletedEntry isNil ifTrue:[
|
159
|
98 |
DeletedEntry := Object new
|
41
|
99 |
].
|
|
100 |
|
|
101 |
"Set initialize"
|
|
102 |
! !
|
|
103 |
|
1
|
104 |
!Set class methodsFor:'instance creation'!
|
|
105 |
|
|
106 |
new
|
|
107 |
"return a new empty Set"
|
|
108 |
|
|
109 |
^ self new:7
|
|
110 |
!
|
|
111 |
|
|
112 |
new:anInteger
|
|
113 |
"return a new empty Set with space for anInteger elements"
|
|
114 |
|
159
|
115 |
"
|
|
116 |
make it somewhat bigger; hashing works better if fill grade is
|
|
117 |
below 10% (make it 75% here ..)
|
|
118 |
"
|
|
119 |
^ self basicNew setTally:(anInteger * 4 // 3)
|
|
120 |
! !
|
|
121 |
|
252
|
122 |
!Set class methodsFor:'queries'!
|
|
123 |
|
|
124 |
goodSizeFrom:arg
|
|
125 |
"return a good array size for the given argument.
|
|
126 |
Returns the next prime after arg, since prime sizes are good for hashing."
|
|
127 |
|
302
|
128 |
|n|
|
|
129 |
|
252
|
130 |
arg <= 11 ifTrue:[^ 11].
|
|
131 |
|
302
|
132 |
n := arg * 3 // 2.
|
|
133 |
|
252
|
134 |
"
|
|
135 |
mhmh - this returns good numbers for collections with up-to about
|
|
136 |
500k elements; if you have bigger ones, add some more primes here ...
|
|
137 |
"
|
302
|
138 |
n <= 524288 ifTrue:[
|
252
|
139 |
"2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288"
|
302
|
140 |
^ #(11 11 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit)
|
252
|
141 |
].
|
|
142 |
"
|
|
143 |
make it odd - at least
|
|
144 |
"
|
302
|
145 |
^ n bitOr:1
|
252
|
146 |
! !
|
|
147 |
|
159
|
148 |
!Set methodsFor:'copying'!
|
|
149 |
|
|
150 |
postCopy
|
|
151 |
"have to copy the keyArray too"
|
|
152 |
|
|
153 |
keyArray := keyArray shallowCopy
|
1
|
154 |
! !
|
|
155 |
|
|
156 |
!Set methodsFor:'private'!
|
|
157 |
|
13
|
158 |
keyContainerOfSize:n
|
|
159 |
"return a container for keys of size n.
|
|
160 |
Extracted to make life of weak subclasses easier ..."
|
|
161 |
|
359
|
162 |
^ Array basicNew:n
|
13
|
163 |
!
|
|
164 |
|
2
|
165 |
fullCheck
|
95
|
166 |
"check if collection is full (after an add); grow if so.
|
362
|
167 |
Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'"
|
2
|
168 |
|
61
|
169 |
|sz "{Class: SmallInteger}" |
|
|
170 |
|
324
|
171 |
"
|
|
172 |
grow if filled more than 75%
|
|
173 |
"
|
61
|
174 |
sz := keyArray basicSize.
|
77
|
175 |
tally > (sz * 3 // 4) ifTrue:[
|
2
|
176 |
self grow
|
|
177 |
]
|
|
178 |
!
|
|
179 |
|
41
|
180 |
emptyCheck
|
252
|
181 |
"check if the receiver has become too empty (after a remove)
|
324
|
182 |
and shrink if it makes sense.
|
|
183 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'"
|
41
|
184 |
|
61
|
185 |
|sz "{Class: SmallInteger}"
|
|
186 |
newSize "{Class: SmallInteger}" |
|
41
|
187 |
|
61
|
188 |
sz := keyArray basicSize.
|
95
|
189 |
sz > 30 ifTrue:[
|
324
|
190 |
"
|
|
191 |
shrink if too empty
|
|
192 |
"
|
159
|
193 |
tally < (sz // 8) ifTrue:[
|
|
194 |
newSize := sz // 7.
|
|
195 |
self grow:newSize
|
|
196 |
]
|
41
|
197 |
]
|
|
198 |
!
|
|
199 |
|
324
|
200 |
initialIndexFor:hashKey boundedBy:length
|
|
201 |
"for ST-80 compatibility only; it is (currently) not used in this
|
|
202 |
implementation of sets. Therefore, in ST/X it does not make sense
|
|
203 |
to redefine it. (which may be a bad design decision, but slightly
|
|
204 |
improves performance, by avoiding an extra message send ...)"
|
|
205 |
|
375
|
206 |
^ (hashKey \\ length) + 1.
|
324
|
207 |
!
|
|
208 |
|
1
|
209 |
setTally:count
|
|
210 |
"initialize the contents array (for at least count slots)
|
|
211 |
and set tally to zero.
|
|
212 |
The size is increased to the next prime for better hashing behavior."
|
|
213 |
|
252
|
214 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:count).
|
1
|
215 |
tally := 0
|
|
216 |
!
|
|
217 |
|
|
218 |
find:key ifAbsent:aBlock
|
|
219 |
"Look for the key in the receiver. If it is found, return
|
|
220 |
the index of the slot containing the key, otherwise
|
|
221 |
return the value of evaluating aBlock."
|
|
222 |
|
61
|
223 |
|index "{ Class:SmallInteger }"
|
|
224 |
length "{ Class:SmallInteger }"
|
|
225 |
startIndex probe|
|
1
|
226 |
|
13
|
227 |
length := keyArray basicSize.
|
362
|
228 |
"/ length < 10 ifTrue:[
|
|
229 |
"/ "assuming, that for small collections the overhead of hashing
|
|
230 |
"/ is larger ... maybe that proves wrong
|
|
231 |
"/ (if overhead of comparing is higher)"
|
|
232 |
"/ ^ keyArray indexOf:key ifAbsent:aBlock
|
|
233 |
"/ ].
|
13
|
234 |
|
362
|
235 |
index := key hash.
|
|
236 |
index := index \\ length + 1.
|
|
237 |
startIndex := index.
|
1
|
238 |
|
|
239 |
[true] whileTrue:[
|
159
|
240 |
probe := (keyArray basicAt:index).
|
362
|
241 |
probe isNil ifTrue:[^ aBlock value].
|
159
|
242 |
key = probe ifTrue:[^ index].
|
1
|
243 |
|
159
|
244 |
index == length ifTrue:[
|
|
245 |
index := 1
|
|
246 |
] ifFalse:[
|
|
247 |
index := index + 1
|
|
248 |
].
|
362
|
249 |
index == startIndex ifTrue:[^ aBlock value].
|
1
|
250 |
]
|
|
251 |
!
|
|
252 |
|
13
|
253 |
findKeyOrNil:key
|
1
|
254 |
"Look for the key in the receiver. If it is found, return
|
|
255 |
the index of the slot containing the key, otherwise
|
|
256 |
return the index of the first unused slot. Grow the receiver,
|
|
257 |
if key was not found, and no unused slots where present"
|
|
258 |
|
61
|
259 |
|index "{ Class:SmallInteger }"
|
|
260 |
length "{ Class:SmallInteger }"
|
|
261 |
startIndex probe|
|
1
|
262 |
|
13
|
263 |
length := keyArray basicSize.
|
362
|
264 |
index := key hash.
|
|
265 |
index := index \\ length + 1.
|
|
266 |
startIndex := index.
|
1
|
267 |
|
|
268 |
[true] whileTrue:[
|
159
|
269 |
probe := keyArray basicAt:index.
|
|
270 |
(probe isNil or: [key = probe]) ifTrue:[^ index].
|
|
271 |
probe == DeletedEntry ifTrue:[
|
|
272 |
keyArray basicAt:index put:nil.
|
|
273 |
^ index
|
|
274 |
].
|
1
|
275 |
|
159
|
276 |
index == length ifTrue:[
|
|
277 |
index := 1
|
|
278 |
] ifFalse:[
|
|
279 |
index := index + 1
|
|
280 |
].
|
|
281 |
index == startIndex ifTrue:[^ self grow findKeyOrNil:key].
|
1
|
282 |
]
|
|
283 |
!
|
|
284 |
|
|
285 |
findNil:key
|
|
286 |
"Look for the next slot usable for key. This method assumes that
|
|
287 |
key is not already in the receiver - used only while growing/rehashing"
|
|
288 |
|
|
289 |
|index "{ Class:SmallInteger }"
|
61
|
290 |
length "{ Class:SmallInteger }"|
|
1
|
291 |
|
13
|
292 |
length := keyArray basicSize.
|
362
|
293 |
index := key hash.
|
|
294 |
index := index \\ length + 1.
|
1
|
295 |
|
13
|
296 |
[(keyArray basicAt:index) notNil] whileTrue:[
|
159
|
297 |
index == length ifTrue:[
|
|
298 |
index := 1
|
|
299 |
] ifFalse:[
|
|
300 |
index := index + 1
|
|
301 |
].
|
|
302 |
"notice: no check for no nil found - we must find one since
|
|
303 |
this is only called after growing"
|
1
|
304 |
].
|
|
305 |
^ index
|
|
306 |
!
|
|
307 |
|
|
308 |
grow
|
|
309 |
"change the number of element slots of the collection to a useful
|
|
310 |
new size"
|
|
311 |
|
13
|
312 |
self grow:(keyArray basicSize * 2)
|
1
|
313 |
!
|
|
314 |
|
|
315 |
grow:newSize
|
|
316 |
"change the number of element slots of the collection - to do this,
|
|
317 |
we have to rehash (which is done by re-adding all elements to a new
|
|
318 |
empty set)."
|
|
319 |
|
95
|
320 |
|elem oldKeyArray newKeyArray deletedEntry
|
252
|
321 |
containerSize oldSize "{ Class:SmallInteger }"|
|
1
|
322 |
|
13
|
323 |
oldKeyArray := keyArray.
|
252
|
324 |
oldSize := oldKeyArray size.
|
|
325 |
containerSize := (self class goodSizeFrom:newSize).
|
|
326 |
containerSize == oldSize ifTrue:[^ self].
|
1
|
327 |
|
252
|
328 |
keyArray := newKeyArray := self keyContainerOfSize:containerSize.
|
|
329 |
|
95
|
330 |
deletedEntry := DeletedEntry.
|
13
|
331 |
1 to:oldSize do:[:srcIndex |
|
159
|
332 |
elem := oldKeyArray basicAt:srcIndex.
|
|
333 |
(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[
|
|
334 |
"cannot be already there"
|
|
335 |
newKeyArray basicAt:(self findNil:elem) put:elem
|
|
336 |
].
|
95
|
337 |
].
|
1
|
338 |
!
|
|
339 |
|
|
340 |
rehash
|
61
|
341 |
"rehash is done by re-adding all elements to a new empty set.
|
|
342 |
Rehash is needed after a binaryRead, for example."
|
1
|
343 |
|
95
|
344 |
|element oldKeyArray newKeyArray
|
|
345 |
n "{ Class:SmallInteger }"|
|
1
|
346 |
|
95
|
347 |
oldKeyArray := keyArray.
|
|
348 |
n := oldKeyArray size.
|
|
349 |
keyArray := newKeyArray := self keyContainerOfSize:n.
|
|
350 |
|
2
|
351 |
1 to:n do:[:index |
|
159
|
352 |
element := oldKeyArray at:index.
|
|
353 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
|
|
354 |
"cannot be already there"
|
|
355 |
newKeyArray basicAt:(self findNil:element) put:element
|
|
356 |
].
|
1
|
357 |
]
|
|
358 |
!
|
|
359 |
|
|
360 |
rehashFrom:startIndex
|
61
|
361 |
"rehash elements starting at index - after a remove.
|
|
362 |
Notice: due to the new implementation of remove,
|
159
|
363 |
this is no longer needed"
|
1
|
364 |
|
3
|
365 |
|element i "{ Class:SmallInteger }"
|
|
366 |
length
|
1
|
367 |
index "{ Class:SmallInteger }" |
|
|
368 |
|
13
|
369 |
length := keyArray basicSize.
|
1
|
370 |
index := startIndex.
|
13
|
371 |
element := keyArray basicAt:index.
|
1
|
372 |
[element notNil] whileTrue:[
|
159
|
373 |
i := self findNil:element.
|
|
374 |
i == index ifTrue:[
|
|
375 |
^ self
|
|
376 |
].
|
|
377 |
keyArray basicAt:i put:element.
|
|
378 |
keyArray basicAt:index put:nil.
|
1
|
379 |
|
159
|
380 |
index == length ifTrue:[
|
|
381 |
index := 1
|
|
382 |
] ifFalse:[
|
|
383 |
index := index + 1.
|
|
384 |
].
|
|
385 |
element := keyArray basicAt:index.
|
1
|
386 |
]
|
|
387 |
! !
|
|
388 |
|
|
389 |
!Set methodsFor:'accessing'!
|
|
390 |
|
|
391 |
at:index
|
|
392 |
"report an error: at: is not allowed for Sets"
|
|
393 |
|
|
394 |
^ self errorNotKeyed
|
|
395 |
!
|
|
396 |
|
|
397 |
at:index put:anObject
|
|
398 |
"report an error: at:put: is not allowed for Sets"
|
|
399 |
|
|
400 |
^ self errorNotKeyed
|
|
401 |
! !
|
|
402 |
|
|
403 |
!Set methodsFor:'testing'!
|
|
404 |
|
|
405 |
size
|
|
406 |
"return the number of set elements"
|
|
407 |
|
|
408 |
^ tally
|
|
409 |
!
|
|
410 |
|
362
|
411 |
capacity
|
|
412 |
"return the number of elements, that the receiver is
|
|
413 |
prepared to take.
|
|
414 |
Not used by the system; added for ST-80 compatibility."
|
|
415 |
|
|
416 |
^ keyArray size
|
|
417 |
!
|
|
418 |
|
1
|
419 |
includes:anObject
|
|
420 |
"return true if the argument anObject is in the receiver"
|
|
421 |
|
362
|
422 |
^ (self find:anObject ifAbsent:0) ~~ 0
|
1
|
423 |
!
|
|
424 |
|
|
425 |
isEmpty
|
|
426 |
"return true if the receiver is empty"
|
|
427 |
|
|
428 |
^ tally == 0
|
|
429 |
!
|
|
430 |
|
|
431 |
occurrencesOf:anObject
|
|
432 |
"return the number of occurrences of anObject in the receiver"
|
|
433 |
|
362
|
434 |
(self find:anObject ifAbsent:0) == 0 ifTrue:[^ 0].
|
1
|
435 |
^ 1
|
|
436 |
!
|
|
437 |
|
|
438 |
isFixedSize
|
|
439 |
"return true if the receiver cannot grow - this will vanish once
|
|
440 |
Arrays and Strings learn how to grow ..."
|
|
441 |
|
|
442 |
^ false
|
|
443 |
! !
|
|
444 |
|
|
445 |
!Set methodsFor:'adding & removing'!
|
|
446 |
|
|
447 |
add:anObject
|
|
448 |
"add the argument, anObject to the receiver"
|
|
449 |
|
95
|
450 |
|index "{ Class: SmallInteger }"|
|
1
|
451 |
|
|
452 |
anObject notNil ifTrue:[
|
159
|
453 |
index := self findKeyOrNil:anObject.
|
|
454 |
(keyArray basicAt:index) isNil ifTrue:[
|
|
455 |
keyArray basicAt:index put:anObject.
|
|
456 |
tally := tally + 1.
|
1
|
457 |
|
159
|
458 |
self fullCheck.
|
|
459 |
]
|
1
|
460 |
].
|
|
461 |
^ anObject
|
|
462 |
!
|
|
463 |
|
|
464 |
remove:oldObject ifAbsent:exceptionBlock
|
|
465 |
"remove oldObject from the collection and return it.
|
|
466 |
If it was not in the collection return the value of exceptionBlock."
|
|
467 |
|
|
468 |
|index next|
|
|
469 |
|
362
|
470 |
"/ code below is actually the same as:
|
|
471 |
"/
|
|
472 |
"/ index := self find:oldObject ifAbsent:[^ exceptionBlock value].
|
|
473 |
"/
|
|
474 |
"/ but cheaper, since there will be no new block to create
|
|
475 |
"/ (remember: [0] blocks are super-cheap)
|
50
|
476 |
|
362
|
477 |
index := self find:oldObject ifAbsent:0.
|
50
|
478 |
index == 0 ifTrue:[^ exceptionBlock value].
|
|
479 |
|
13
|
480 |
keyArray basicAt:index put:nil.
|
1
|
481 |
tally := tally - 1.
|
|
482 |
tally == 0 ifTrue:[
|
252
|
483 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0).
|
1
|
484 |
] ifFalse:[
|
159
|
485 |
index == keyArray basicSize ifTrue:[
|
|
486 |
next := 1
|
|
487 |
] ifFalse:[
|
|
488 |
next := index + 1.
|
|
489 |
].
|
|
490 |
(keyArray basicAt:next) notNil ifTrue:[
|
|
491 |
keyArray basicAt:index put:DeletedEntry.
|
|
492 |
].
|
|
493 |
self emptyCheck
|
1
|
494 |
].
|
|
495 |
^ oldObject
|
119
|
496 |
!
|
|
497 |
|
|
498 |
removeAll
|
|
499 |
"remove all elements from the receiver."
|
|
500 |
|
|
501 |
self setTally:7.
|
1
|
502 |
! !
|
|
503 |
|
|
504 |
!Set methodsFor:'enumerating'!
|
|
505 |
|
|
506 |
do:aBlock
|
|
507 |
"perform the block for all members in the collection."
|
|
508 |
|
50
|
509 |
|sz "{ Class: SmallInteger }"
|
|
510 |
element|
|
|
511 |
|
|
512 |
sz := keyArray size.
|
|
513 |
1 to:sz do:[:index |
|
159
|
514 |
element := keyArray at:index.
|
|
515 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
|
|
516 |
aBlock value:element
|
|
517 |
]
|
1
|
518 |
]
|
|
519 |
! !
|
2
|
520 |
|
|
521 |
!Set methodsFor: 'binary storage'!
|
|
522 |
|
|
523 |
readBinaryContentsFrom: stream manager: manager
|
|
524 |
"must rehash after reload"
|
|
525 |
|
|
526 |
super readBinaryContentsFrom: stream manager: manager.
|
|
527 |
self rehash
|
|
528 |
! !
|