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'
|
1
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
Set comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
a Set is a collection where each element occurs at most once.
|
|
26 |
|
13
|
27 |
$Header: /cvs/stx/stx/libbasic/Set.st,v 1.5 1993-12-11 00:56:26 claus Exp $
|
1
|
28 |
written jun 91 by claus
|
|
29 |
jan 93 claus: changed to use hashing
|
|
30 |
'!
|
|
31 |
|
|
32 |
!Set class methodsFor:'instance creation'!
|
|
33 |
|
|
34 |
new
|
|
35 |
"return a new empty Set"
|
|
36 |
|
|
37 |
^ self new:7
|
|
38 |
!
|
|
39 |
|
|
40 |
new:anInteger
|
|
41 |
"return a new empty Set with space for anInteger elements"
|
|
42 |
|
|
43 |
^ self basicNew setTally:anInteger
|
|
44 |
! !
|
|
45 |
|
|
46 |
!Set methodsFor:'private'!
|
|
47 |
|
13
|
48 |
keyContainerOfSize:n
|
|
49 |
"return a container for keys of size n.
|
|
50 |
Extracted to make life of weak subclasses easier ..."
|
|
51 |
|
|
52 |
^ Array new:n
|
|
53 |
!
|
|
54 |
|
2
|
55 |
fullCheck
|
|
56 |
"check if dictionary is full, grow if so.
|
|
57 |
Definition of full is currently:'filled more than 70%'"
|
|
58 |
|
|
59 |
"grow if filled more than 70% "
|
13
|
60 |
tally > (keyArray basicSize * 7 // 10) ifTrue:[
|
2
|
61 |
self grow
|
|
62 |
]
|
|
63 |
!
|
|
64 |
|
1
|
65 |
goodSizeFor:arg
|
|
66 |
"return a good array size for the given argument.
|
|
67 |
Returns the next prime after arg"
|
|
68 |
|
|
69 |
arg <= 7 ifTrue:[^ 7].
|
2
|
70 |
arg <= 131072 ifTrue:[
|
|
71 |
"2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072"
|
|
72 |
^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101) at:(arg highBit)
|
1
|
73 |
].
|
|
74 |
^ arg bitOr:1
|
|
75 |
!
|
|
76 |
|
|
77 |
setTally:count
|
|
78 |
"initialize the contents array (for at least count slots)
|
|
79 |
and set tally to zero.
|
|
80 |
The size is increased to the next prime for better hashing behavior."
|
|
81 |
|
13
|
82 |
keyArray := Array new:(self goodSizeFor:count).
|
1
|
83 |
tally := 0
|
|
84 |
!
|
|
85 |
|
|
86 |
find:key ifAbsent:aBlock
|
|
87 |
"Look for the key in the receiver. If it is found, return
|
|
88 |
the index of the slot containing the key, otherwise
|
|
89 |
return the value of evaluating aBlock."
|
|
90 |
|
|
91 |
|index "{ Class:SmallInteger }"
|
3
|
92 |
length startIndex probe|
|
1
|
93 |
|
13
|
94 |
length := keyArray basicSize.
|
|
95 |
length < 10 ifTrue:[
|
|
96 |
"assuming, that for small collections the overhead of hashing
|
|
97 |
is large ... maybe that proves wrong (if overhead of comparing
|
|
98 |
is high)"
|
|
99 |
^ keyArray indexOf:key ifAbsent:aBlock
|
|
100 |
].
|
|
101 |
|
1
|
102 |
startIndex := key hash \\ length + 1.
|
|
103 |
index := startIndex.
|
|
104 |
|
|
105 |
[true] whileTrue:[
|
13
|
106 |
probe := (keyArray basicAt:index).
|
1
|
107 |
key = probe ifTrue:[^ index].
|
|
108 |
|
|
109 |
index == length ifTrue:[
|
|
110 |
index := 1
|
|
111 |
] ifFalse:[
|
|
112 |
index := index + 1
|
|
113 |
].
|
13
|
114 |
((probe isNil) or:[index == startIndex]) ifTrue:[^ aBlock value].
|
1
|
115 |
]
|
|
116 |
!
|
|
117 |
|
13
|
118 |
findKeyOrNil:key
|
1
|
119 |
"Look for the key in the receiver. If it is found, return
|
|
120 |
the index of the slot containing the key, otherwise
|
|
121 |
return the index of the first unused slot. Grow the receiver,
|
|
122 |
if key was not found, and no unused slots where present"
|
|
123 |
|
|
124 |
|index "{ Class:SmallInteger }"
|
3
|
125 |
length startIndex probe|
|
1
|
126 |
|
13
|
127 |
length := keyArray basicSize.
|
1
|
128 |
startIndex := key hash \\ length + 1.
|
|
129 |
index := startIndex.
|
|
130 |
|
|
131 |
[true] whileTrue:[
|
13
|
132 |
probe := keyArray basicAt:index.
|
1
|
133 |
(probe isNil or: [key = probe]) ifTrue:[^ index].
|
|
134 |
|
|
135 |
index == length ifTrue:[
|
|
136 |
index := 1
|
|
137 |
] ifFalse:[
|
|
138 |
index := index + 1
|
|
139 |
].
|
13
|
140 |
index == startIndex ifTrue:[^ self grow findKeyOrNil:key].
|
1
|
141 |
]
|
|
142 |
!
|
|
143 |
|
|
144 |
findNil:key
|
|
145 |
"Look for the next slot usable for key. This method assumes that
|
|
146 |
key is not already in the receiver - used only while growing/rehashing"
|
|
147 |
|
|
148 |
|index "{ Class:SmallInteger }"
|
3
|
149 |
length|
|
1
|
150 |
|
13
|
151 |
length := keyArray basicSize.
|
1
|
152 |
index := key hash \\ length + 1.
|
|
153 |
|
13
|
154 |
[(keyArray basicAt:index) notNil] whileTrue:[
|
1
|
155 |
index == length ifTrue:[
|
|
156 |
index := 1
|
|
157 |
] ifFalse:[
|
|
158 |
index := index + 1
|
|
159 |
].
|
|
160 |
"notice: no check for no nil found - we must find one since
|
|
161 |
this is only called after growing"
|
|
162 |
].
|
|
163 |
^ index
|
|
164 |
!
|
|
165 |
|
|
166 |
grow
|
|
167 |
"change the number of element slots of the collection to a useful
|
|
168 |
new size"
|
|
169 |
|
13
|
170 |
self grow:(keyArray basicSize * 2)
|
1
|
171 |
!
|
|
172 |
|
|
173 |
grow:newSize
|
|
174 |
"change the number of element slots of the collection - to do this,
|
|
175 |
we have to rehash (which is done by re-adding all elements to a new
|
|
176 |
empty set)."
|
|
177 |
|
13
|
178 |
|oldKeyArray elem
|
|
179 |
oldSize "{ Class:SmallInteger }" |
|
1
|
180 |
|
13
|
181 |
oldKeyArray := keyArray.
|
2
|
182 |
|
13
|
183 |
keyArray := Array new:(self goodSizeFor:newSize).
|
1
|
184 |
|
13
|
185 |
oldSize := oldKeyArray size.
|
|
186 |
1 to:oldSize do:[:srcIndex |
|
|
187 |
elem := oldKeyArray basicAt:srcIndex.
|
1
|
188 |
elem notNil ifTrue:[
|
|
189 |
"cannot be already there"
|
13
|
190 |
keyArray basicAt:(self findNil:elem) put:elem
|
1
|
191 |
].
|
13
|
192 |
]
|
1
|
193 |
!
|
|
194 |
|
|
195 |
rehash
|
|
196 |
"rehash is done by re-adding all elements to a new empty set."
|
|
197 |
|
2
|
198 |
| oldArray element
|
|
199 |
n "{ Class:SmallInteger }" |
|
1
|
200 |
|
13
|
201 |
oldArray := keyArray.
|
2
|
202 |
n := oldArray size.
|
13
|
203 |
keyArray := Array new:n.
|
2
|
204 |
1 to:n do:[:index |
|
|
205 |
element := oldArray at:index.
|
1
|
206 |
element notNil ifTrue:[
|
|
207 |
"cannot be already there"
|
13
|
208 |
keyArray basicAt:(self findNil:element) put:element
|
1
|
209 |
].
|
|
210 |
]
|
|
211 |
!
|
|
212 |
|
|
213 |
rehashFrom:startIndex
|
|
214 |
"rehash elements starting at index - after a remove"
|
|
215 |
|
3
|
216 |
|element i "{ Class:SmallInteger }"
|
|
217 |
length
|
1
|
218 |
index "{ Class:SmallInteger }" |
|
|
219 |
|
13
|
220 |
length := keyArray basicSize.
|
1
|
221 |
index := startIndex.
|
13
|
222 |
element := keyArray basicAt:index.
|
1
|
223 |
[element notNil] whileTrue:[
|
|
224 |
i := self findNil:element.
|
|
225 |
i == index ifTrue:[
|
|
226 |
^ self
|
|
227 |
].
|
13
|
228 |
keyArray basicAt:i put:element.
|
|
229 |
keyArray basicAt:index put:nil.
|
1
|
230 |
|
|
231 |
index == length ifTrue:[
|
|
232 |
index := 1
|
|
233 |
] ifFalse:[
|
|
234 |
index := index + 1.
|
|
235 |
].
|
13
|
236 |
element := keyArray basicAt:index.
|
1
|
237 |
]
|
|
238 |
! !
|
|
239 |
|
|
240 |
!Set methodsFor:'accessing'!
|
|
241 |
|
|
242 |
at:index
|
|
243 |
"report an error: at: is not allowed for Sets"
|
|
244 |
|
|
245 |
^ self errorNotKeyed
|
|
246 |
!
|
|
247 |
|
|
248 |
at:index put:anObject
|
|
249 |
"report an error: at:put: is not allowed for Sets"
|
|
250 |
|
|
251 |
^ self errorNotKeyed
|
|
252 |
! !
|
|
253 |
|
|
254 |
!Set methodsFor:'testing'!
|
|
255 |
|
|
256 |
size
|
|
257 |
"return the number of set elements"
|
|
258 |
|
|
259 |
^ tally
|
|
260 |
!
|
|
261 |
|
|
262 |
includes:anObject
|
|
263 |
"return true if the argument anObject is in the receiver"
|
|
264 |
|
|
265 |
^ (self find:anObject ifAbsent:[0]) ~~ 0
|
|
266 |
!
|
|
267 |
|
|
268 |
isEmpty
|
|
269 |
"return true if the receiver is empty"
|
|
270 |
|
|
271 |
^ tally == 0
|
|
272 |
!
|
|
273 |
|
|
274 |
occurrencesOf:anObject
|
|
275 |
"return the number of occurrences of anObject in the receiver"
|
|
276 |
|
|
277 |
(self find:anObject ifAbsent:[0]) == 0 ifTrue:[^ 0].
|
|
278 |
^ 1
|
|
279 |
!
|
|
280 |
|
|
281 |
isFixedSize
|
|
282 |
"return true if the receiver cannot grow - this will vanish once
|
|
283 |
Arrays and Strings learn how to grow ..."
|
|
284 |
|
|
285 |
^ false
|
|
286 |
! !
|
|
287 |
|
|
288 |
!Set methodsFor:'adding & removing'!
|
|
289 |
|
|
290 |
add:anObject
|
|
291 |
"add the argument, anObject to the receiver"
|
|
292 |
|
|
293 |
|index|
|
|
294 |
|
|
295 |
anObject notNil ifTrue:[
|
13
|
296 |
index := self findKeyOrNil:anObject.
|
|
297 |
(keyArray basicAt:index) isNil ifTrue:[
|
|
298 |
keyArray basicAt:index put:anObject.
|
1
|
299 |
tally := tally + 1.
|
|
300 |
|
2
|
301 |
self fullCheck.
|
1
|
302 |
]
|
|
303 |
].
|
|
304 |
^ anObject
|
|
305 |
!
|
|
306 |
|
|
307 |
remove:oldObject ifAbsent:exceptionBlock
|
|
308 |
"remove oldObject from the collection and return it.
|
|
309 |
If it was not in the collection return the value of exceptionBlock."
|
|
310 |
|
|
311 |
|index next|
|
|
312 |
|
|
313 |
index := self find:oldObject ifAbsent:[^ exceptionBlock value].
|
13
|
314 |
keyArray basicAt:index put:nil.
|
1
|
315 |
tally := tally - 1.
|
|
316 |
tally == 0 ifTrue:[
|
13
|
317 |
keyArray := Array new:(self goodSizeFor:0).
|
1
|
318 |
] ifFalse:[
|
13
|
319 |
index == keyArray basicSize ifTrue:[
|
1
|
320 |
next := 1
|
|
321 |
] ifFalse:[
|
|
322 |
next := index + 1.
|
|
323 |
].
|
|
324 |
"this check is redundant - is also done in rehashFrom:,
|
|
325 |
however, since there is some probability that the next
|
|
326 |
element is nil, this saves a send sometimes
|
|
327 |
"
|
13
|
328 |
(keyArray basicAt:next) notNil ifTrue:[
|
1
|
329 |
self rehashFrom:next.
|
|
330 |
]
|
|
331 |
].
|
|
332 |
^ oldObject
|
|
333 |
! !
|
|
334 |
|
|
335 |
!Set methodsFor:'enumerating'!
|
|
336 |
|
|
337 |
do:aBlock
|
|
338 |
"perform the block for all members in the collection."
|
|
339 |
|
13
|
340 |
keyArray do:[:each |
|
1
|
341 |
each notNil ifTrue:[
|
|
342 |
aBlock value:each
|
|
343 |
]
|
|
344 |
]
|
|
345 |
! !
|
2
|
346 |
|
|
347 |
!Set methodsFor: 'binary storage'!
|
|
348 |
|
|
349 |
readBinaryContentsFrom: stream manager: manager
|
|
350 |
"must rehash after reload"
|
|
351 |
|
|
352 |
super readBinaryContentsFrom: stream manager: manager.
|
|
353 |
self rehash
|
|
354 |
! !
|