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