1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
1
|
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 |
|
|
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
|
|
22 |
All Rights Reserved
|
|
23 |
'!
|
1
|
24 |
|
88
|
25 |
!Set class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
1
|
30 |
All Rights Reserved
|
|
31 |
|
88
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
1
|
40 |
|
88
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic/Set.st,v 1.10 1994-06-02 16:21:59 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
|
47 |
history
|
|
48 |
"
|
1
|
49 |
written jun 91 by claus
|
|
50 |
jan 93 claus: changed to use hashing
|
41
|
51 |
jan 94 claus: recoded to mark empty slots instead of rehashing
|
88
|
52 |
"
|
|
53 |
!
|
|
54 |
|
|
55 |
documentation
|
|
56 |
"
|
|
57 |
a Set is a collection where each element occurs at most once.
|
|
58 |
The inclusion test is done using = for comparison; see IdentitySet
|
|
59 |
for sets using identity compare.
|
|
60 |
Sets use hashing for fast access.
|
|
61 |
"
|
|
62 |
! !
|
1
|
63 |
|
41
|
64 |
!Set class methodsFor:'initialization'!
|
|
65 |
|
|
66 |
initialize
|
|
67 |
"initialize the Set class"
|
|
68 |
|
|
69 |
DeletedEntry isNil ifTrue:[
|
|
70 |
DeletedEntry := Object new
|
|
71 |
].
|
|
72 |
|
|
73 |
"Set initialize"
|
|
74 |
! !
|
|
75 |
|
1
|
76 |
!Set class methodsFor:'instance creation'!
|
|
77 |
|
|
78 |
new
|
|
79 |
"return a new empty Set"
|
|
80 |
|
|
81 |
^ self new:7
|
|
82 |
!
|
|
83 |
|
|
84 |
new:anInteger
|
|
85 |
"return a new empty Set with space for anInteger elements"
|
|
86 |
|
|
87 |
^ self basicNew setTally:anInteger
|
|
88 |
! !
|
|
89 |
|
|
90 |
!Set methodsFor:'private'!
|
|
91 |
|
13
|
92 |
keyContainerOfSize:n
|
|
93 |
"return a container for keys of size n.
|
|
94 |
Extracted to make life of weak subclasses easier ..."
|
|
95 |
|
|
96 |
^ Array new:n
|
|
97 |
!
|
|
98 |
|
2
|
99 |
fullCheck
|
41
|
100 |
"check if collection is full, grow if so.
|
77
|
101 |
Definition of full is currently:'filled more than 75%'"
|
2
|
102 |
|
61
|
103 |
|sz "{Class: SmallInteger}" |
|
|
104 |
|
77
|
105 |
"grow if filled more than 75% "
|
61
|
106 |
sz := keyArray basicSize.
|
77
|
107 |
tally > (sz * 3 // 4) ifTrue:[
|
2
|
108 |
self grow
|
|
109 |
]
|
|
110 |
!
|
|
111 |
|
41
|
112 |
emptyCheck
|
|
113 |
"check if set has become too empty
|
61
|
114 |
- shrink if filled less than 12.5%"
|
41
|
115 |
|
61
|
116 |
|sz "{Class: SmallInteger}"
|
|
117 |
newSize "{Class: SmallInteger}" |
|
41
|
118 |
|
61
|
119 |
sz := keyArray basicSize.
|
77
|
120 |
sz > 60 ifTrue:[
|
|
121 |
tally < (sz // 8) ifTrue:[
|
|
122 |
newSize := sz // 7.
|
41
|
123 |
self grow:newSize
|
|
124 |
]
|
|
125 |
]
|
|
126 |
!
|
|
127 |
|
1
|
128 |
goodSizeFor:arg
|
|
129 |
"return a good array size for the given argument.
|
|
130 |
Returns the next prime after arg"
|
|
131 |
|
|
132 |
arg <= 7 ifTrue:[^ 7].
|
61
|
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 |
arg <= 524288 ifTrue:[
|
|
137 |
"2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288"
|
|
138 |
^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(arg highBit)
|
1
|
139 |
].
|
|
140 |
^ arg bitOr:1
|
|
141 |
!
|
|
142 |
|
|
143 |
setTally:count
|
|
144 |
"initialize the contents array (for at least count slots)
|
|
145 |
and set tally to zero.
|
|
146 |
The size is increased to the next prime for better hashing behavior."
|
|
147 |
|
13
|
148 |
keyArray := Array new:(self goodSizeFor:count).
|
1
|
149 |
tally := 0
|
|
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 |
|
61
|
157 |
|index "{ Class:SmallInteger }"
|
|
158 |
length "{ Class:SmallInteger }"
|
|
159 |
startIndex probe|
|
1
|
160 |
|
13
|
161 |
length := keyArray basicSize.
|
|
162 |
length < 10 ifTrue:[
|
|
163 |
"assuming, that for small collections the overhead of hashing
|
|
164 |
is large ... maybe that proves wrong (if overhead of comparing
|
|
165 |
is high)"
|
|
166 |
^ keyArray indexOf:key ifAbsent:aBlock
|
|
167 |
].
|
|
168 |
|
1
|
169 |
startIndex := key hash \\ length + 1.
|
|
170 |
index := startIndex.
|
|
171 |
|
|
172 |
[true] whileTrue:[
|
13
|
173 |
probe := (keyArray basicAt:index).
|
1
|
174 |
key = probe ifTrue:[^ index].
|
|
175 |
|
|
176 |
index == length ifTrue:[
|
|
177 |
index := 1
|
|
178 |
] ifFalse:[
|
|
179 |
index := index + 1
|
|
180 |
].
|
13
|
181 |
((probe isNil) or:[index == startIndex]) ifTrue:[^ aBlock value].
|
1
|
182 |
]
|
|
183 |
!
|
|
184 |
|
13
|
185 |
findKeyOrNil:key
|
1
|
186 |
"Look for the key in the receiver. If it is found, return
|
|
187 |
the index of the slot containing the key, otherwise
|
|
188 |
return the index of the first unused slot. Grow the receiver,
|
|
189 |
if key was not found, and no unused slots where present"
|
|
190 |
|
61
|
191 |
|index "{ Class:SmallInteger }"
|
|
192 |
length "{ Class:SmallInteger }"
|
|
193 |
startIndex probe|
|
1
|
194 |
|
13
|
195 |
length := keyArray basicSize.
|
1
|
196 |
startIndex := key hash \\ length + 1.
|
|
197 |
index := startIndex.
|
|
198 |
|
|
199 |
[true] whileTrue:[
|
13
|
200 |
probe := keyArray basicAt:index.
|
77
|
201 |
(probe isNil or: [key = probe]) ifTrue:[^ index].
|
41
|
202 |
probe == DeletedEntry ifTrue:[
|
|
203 |
keyArray basicAt:index put:nil.
|
|
204 |
^ index
|
|
205 |
].
|
1
|
206 |
|
|
207 |
index == length ifTrue:[
|
|
208 |
index := 1
|
|
209 |
] ifFalse:[
|
|
210 |
index := index + 1
|
|
211 |
].
|
13
|
212 |
index == startIndex ifTrue:[^ self grow findKeyOrNil:key].
|
1
|
213 |
]
|
|
214 |
!
|
|
215 |
|
|
216 |
findNil:key
|
|
217 |
"Look for the next slot usable for key. This method assumes that
|
|
218 |
key is not already in the receiver - used only while growing/rehashing"
|
|
219 |
|
|
220 |
|index "{ Class:SmallInteger }"
|
61
|
221 |
length "{ Class:SmallInteger }"|
|
1
|
222 |
|
13
|
223 |
length := keyArray basicSize.
|
1
|
224 |
index := key hash \\ length + 1.
|
|
225 |
|
13
|
226 |
[(keyArray basicAt:index) notNil] whileTrue:[
|
1
|
227 |
index == length ifTrue:[
|
|
228 |
index := 1
|
|
229 |
] ifFalse:[
|
|
230 |
index := index + 1
|
|
231 |
].
|
|
232 |
"notice: no check for no nil found - we must find one since
|
|
233 |
this is only called after growing"
|
|
234 |
].
|
|
235 |
^ index
|
|
236 |
!
|
|
237 |
|
|
238 |
grow
|
|
239 |
"change the number of element slots of the collection to a useful
|
|
240 |
new size"
|
|
241 |
|
13
|
242 |
self grow:(keyArray basicSize * 2)
|
1
|
243 |
!
|
|
244 |
|
|
245 |
grow:newSize
|
|
246 |
"change the number of element slots of the collection - to do this,
|
|
247 |
we have to rehash (which is done by re-adding all elements to a new
|
|
248 |
empty set)."
|
|
249 |
|
13
|
250 |
|oldKeyArray elem
|
|
251 |
oldSize "{ Class:SmallInteger }" |
|
1
|
252 |
|
13
|
253 |
oldKeyArray := keyArray.
|
2
|
254 |
|
13
|
255 |
keyArray := Array new:(self goodSizeFor:newSize).
|
1
|
256 |
|
13
|
257 |
oldSize := oldKeyArray size.
|
|
258 |
1 to:oldSize do:[:srcIndex |
|
|
259 |
elem := oldKeyArray basicAt:srcIndex.
|
41
|
260 |
(elem notNil and:[elem ~~ DeletedEntry]) ifTrue:[
|
1
|
261 |
"cannot be already there"
|
13
|
262 |
keyArray basicAt:(self findNil:elem) put:elem
|
1
|
263 |
].
|
13
|
264 |
]
|
1
|
265 |
!
|
|
266 |
|
|
267 |
rehash
|
61
|
268 |
"rehash is done by re-adding all elements to a new empty set.
|
|
269 |
Rehash is needed after a binaryRead, for example."
|
1
|
270 |
|
2
|
271 |
| oldArray element
|
|
272 |
n "{ Class:SmallInteger }" |
|
1
|
273 |
|
13
|
274 |
oldArray := keyArray.
|
2
|
275 |
n := oldArray size.
|
13
|
276 |
keyArray := Array new:n.
|
2
|
277 |
1 to:n do:[:index |
|
|
278 |
element := oldArray at:index.
|
41
|
279 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
|
1
|
280 |
"cannot be already there"
|
13
|
281 |
keyArray basicAt:(self findNil:element) put:element
|
1
|
282 |
].
|
|
283 |
]
|
|
284 |
!
|
|
285 |
|
|
286 |
rehashFrom:startIndex
|
61
|
287 |
"rehash elements starting at index - after a remove.
|
|
288 |
Notice: due to the new implementation of remove,
|
|
289 |
this is no longer needed"
|
1
|
290 |
|
3
|
291 |
|element i "{ Class:SmallInteger }"
|
|
292 |
length
|
1
|
293 |
index "{ Class:SmallInteger }" |
|
|
294 |
|
13
|
295 |
length := keyArray basicSize.
|
1
|
296 |
index := startIndex.
|
13
|
297 |
element := keyArray basicAt:index.
|
1
|
298 |
[element notNil] whileTrue:[
|
|
299 |
i := self findNil:element.
|
|
300 |
i == index ifTrue:[
|
|
301 |
^ self
|
|
302 |
].
|
13
|
303 |
keyArray basicAt:i put:element.
|
|
304 |
keyArray basicAt:index put:nil.
|
1
|
305 |
|
|
306 |
index == length ifTrue:[
|
|
307 |
index := 1
|
|
308 |
] ifFalse:[
|
|
309 |
index := index + 1.
|
|
310 |
].
|
13
|
311 |
element := keyArray basicAt:index.
|
1
|
312 |
]
|
|
313 |
! !
|
|
314 |
|
|
315 |
!Set methodsFor:'accessing'!
|
|
316 |
|
|
317 |
at:index
|
|
318 |
"report an error: at: is not allowed for Sets"
|
|
319 |
|
|
320 |
^ self errorNotKeyed
|
|
321 |
!
|
|
322 |
|
|
323 |
at:index put:anObject
|
|
324 |
"report an error: at:put: is not allowed for Sets"
|
|
325 |
|
|
326 |
^ self errorNotKeyed
|
|
327 |
! !
|
|
328 |
|
|
329 |
!Set methodsFor:'testing'!
|
|
330 |
|
|
331 |
size
|
|
332 |
"return the number of set elements"
|
|
333 |
|
|
334 |
^ tally
|
|
335 |
!
|
|
336 |
|
|
337 |
includes:anObject
|
|
338 |
"return true if the argument anObject is in the receiver"
|
|
339 |
|
|
340 |
^ (self find:anObject ifAbsent:[0]) ~~ 0
|
|
341 |
!
|
|
342 |
|
|
343 |
isEmpty
|
|
344 |
"return true if the receiver is empty"
|
|
345 |
|
|
346 |
^ tally == 0
|
|
347 |
!
|
|
348 |
|
|
349 |
occurrencesOf:anObject
|
|
350 |
"return the number of occurrences of anObject in the receiver"
|
|
351 |
|
|
352 |
(self find:anObject ifAbsent:[0]) == 0 ifTrue:[^ 0].
|
|
353 |
^ 1
|
|
354 |
!
|
|
355 |
|
|
356 |
isFixedSize
|
|
357 |
"return true if the receiver cannot grow - this will vanish once
|
|
358 |
Arrays and Strings learn how to grow ..."
|
|
359 |
|
|
360 |
^ false
|
|
361 |
! !
|
|
362 |
|
|
363 |
!Set methodsFor:'adding & removing'!
|
|
364 |
|
|
365 |
add:anObject
|
|
366 |
"add the argument, anObject to the receiver"
|
|
367 |
|
|
368 |
|index|
|
|
369 |
|
|
370 |
anObject notNil ifTrue:[
|
13
|
371 |
index := self findKeyOrNil:anObject.
|
|
372 |
(keyArray basicAt:index) isNil ifTrue:[
|
|
373 |
keyArray basicAt:index put:anObject.
|
1
|
374 |
tally := tally + 1.
|
|
375 |
|
2
|
376 |
self fullCheck.
|
1
|
377 |
]
|
|
378 |
].
|
|
379 |
^ anObject
|
|
380 |
!
|
|
381 |
|
|
382 |
remove:oldObject ifAbsent:exceptionBlock
|
|
383 |
"remove oldObject from the collection and return it.
|
|
384 |
If it was not in the collection return the value of exceptionBlock."
|
|
385 |
|
|
386 |
|index next|
|
|
387 |
|
50
|
388 |
"this is actually the same as:
|
|
389 |
|
1
|
390 |
index := self find:oldObject ifAbsent:[^ exceptionBlock value].
|
50
|
391 |
|
|
392 |
but cheaper, since there will be no new block to create
|
|
393 |
(remember: [0] blocks are super-cheap)
|
|
394 |
"
|
|
395 |
index := self find:oldObject ifAbsent:[0].
|
|
396 |
index == 0 ifTrue:[^ exceptionBlock value].
|
|
397 |
|
13
|
398 |
keyArray basicAt:index put:nil.
|
1
|
399 |
tally := tally - 1.
|
|
400 |
tally == 0 ifTrue:[
|
13
|
401 |
keyArray := Array new:(self goodSizeFor:0).
|
1
|
402 |
] ifFalse:[
|
13
|
403 |
index == keyArray basicSize ifTrue:[
|
1
|
404 |
next := 1
|
|
405 |
] ifFalse:[
|
|
406 |
next := index + 1.
|
|
407 |
].
|
13
|
408 |
(keyArray basicAt:next) notNil ifTrue:[
|
41
|
409 |
keyArray basicAt:index put:DeletedEntry.
|
|
410 |
].
|
|
411 |
self emptyCheck
|
1
|
412 |
].
|
|
413 |
^ oldObject
|
|
414 |
! !
|
|
415 |
|
|
416 |
!Set methodsFor:'enumerating'!
|
|
417 |
|
|
418 |
do:aBlock
|
|
419 |
"perform the block for all members in the collection."
|
|
420 |
|
50
|
421 |
|sz "{ Class: SmallInteger }"
|
|
422 |
element|
|
|
423 |
|
|
424 |
sz := keyArray size.
|
|
425 |
1 to:sz do:[:index |
|
|
426 |
element := keyArray at:index.
|
|
427 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
|
|
428 |
aBlock value:element
|
1
|
429 |
]
|
|
430 |
]
|
|
431 |
! !
|
2
|
432 |
|
|
433 |
!Set methodsFor: 'binary storage'!
|
|
434 |
|
|
435 |
readBinaryContentsFrom: stream manager: manager
|
|
436 |
"must rehash after reload"
|
|
437 |
|
|
438 |
super readBinaryContentsFrom: stream manager: manager.
|
|
439 |
self rehash
|
|
440 |
! !
|