author | Claus Gittinger <cg@exept.de> |
Wed, 19 Mar 1997 16:08:46 +0100 | |
changeset 2461 | 2d65e1845502 |
parent 2459 | 0a38e8602aac |
child 2469 | ecca86d50852 |
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 |
||
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
129 |
"/ arg <= 11 ifTrue:[^ 11]. |
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
130 |
"/ n := arg * 3 // 2. |
252 | 131 |
|
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
132 |
arg <= 7 ifTrue:[^ 7]. |
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
133 |
n := arg. |
302 | 134 |
|
252 | 135 |
" |
136 |
mhmh - this returns good numbers for collections with up-to about |
|
137 |
500k elements; if you have bigger ones, add some more primes here ... |
|
138 |
" |
|
302 | 139 |
n <= 524288 ifTrue:[ |
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
140 |
"2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288" |
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
141 |
^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit) |
252 | 142 |
]. |
143 |
" |
|
144 |
make it odd - at least |
|
145 |
" |
|
302 | 146 |
^ n bitOr:1 |
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
147 |
|
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
148 |
"Modified: 19.3.1997 / 16:07:59 / cg" |
252 | 149 |
! ! |
150 |
||
609 | 151 |
!Set methodsFor:'accessing'! |
152 |
||
153 |
at:index |
|
154 |
"report an error: at: is not allowed for Sets" |
|
155 |
||
156 |
^ self errorNotKeyed |
|
157 |
! |
|
158 |
||
159 |
at:index put:anObject |
|
160 |
"report an error: at:put: is not allowed for Sets" |
|
161 |
||
162 |
^ self errorNotKeyed |
|
163 |
! ! |
|
164 |
||
165 |
!Set methodsFor:'adding & removing'! |
|
166 |
||
167 |
add:anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
168 |
"add the argument, anObject to the receiver. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
169 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
170 |
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
|
171 |
Iterate over a copy to do this." |
609 | 172 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
173 |
|index "{ Class: SmallInteger }"| |
609 | 174 |
|
175 |
anObject notNil ifTrue:[ |
|
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
176 |
index := self findKeyOrNil:anObject. |
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
177 |
(keyArray basicAt:index) isNil ifTrue:[ |
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
178 |
"/ not already there |
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
179 |
keyArray basicAt:index put:anObject. |
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
180 |
tally := tally + 1. |
609 | 181 |
|
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
182 |
self fullCheck. |
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
183 |
] |
609 | 184 |
]. |
185 |
^ anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
186 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
187 |
"Modified: 30.1.1997 / 14:58:08 / cg" |
609 | 188 |
! |
189 |
||
190 |
remove:oldObject ifAbsent:exceptionBlock |
|
191 |
"remove oldObject from the collection and return it. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
192 |
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
|
193 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
194 |
WARNING: do not remove elements while iterating over the receiver. |
2314 | 195 |
See #saveRemove: to do this." |
609 | 196 |
|
197 |
|index next| |
|
198 |
||
199 |
index := self find:oldObject ifAbsent:0. |
|
200 |
index == 0 ifTrue:[^ exceptionBlock value]. |
|
201 |
||
202 |
keyArray basicAt:index put:nil. |
|
203 |
tally := tally - 1. |
|
204 |
tally == 0 ifTrue:[ |
|
2314 | 205 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). |
609 | 206 |
] ifFalse:[ |
2314 | 207 |
index == keyArray basicSize ifTrue:[ |
208 |
next := 1 |
|
209 |
] ifFalse:[ |
|
210 |
next := index + 1. |
|
211 |
]. |
|
212 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
213 |
keyArray basicAt:index put:DeletedEntry. |
|
214 |
]. |
|
215 |
self emptyCheck |
|
609 | 216 |
]. |
217 |
^ oldObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
218 |
|
2314 | 219 |
"Modified: 29.1.1997 / 21:34:33 / cg" |
609 | 220 |
! |
221 |
||
222 |
removeAll |
|
1164 | 223 |
"remove all elements from the receiver. Returns the receiver." |
609 | 224 |
|
225 |
self setTally:7. |
|
1164 | 226 |
|
227 |
"Modified: 12.4.1996 / 13:35:06 / cg" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
228 |
! |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
229 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
230 |
saveRemove:oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
231 |
"remove the element, oldObject from the collection. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
232 |
Return the element. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
233 |
If it was not in the collection return nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
234 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
235 |
In contrast to #remove:, this does not resize the underlying collection |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
236 |
and therefore does NOT rehash & change the elements order. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
237 |
Therefor this can be used while enumerating the receiver, |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
238 |
which is not possible if #remove: is used. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
239 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
240 |
WARNING: since no resizing is done, the physical amount of memory used |
2314 | 241 |
by the container remains the same, although the logical size shrinks. |
242 |
You may want to manually resize the receiver using #emptyCheck. |
|
243 |
(after the loop)" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
244 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
245 |
|index "{ Class:SmallInteger }" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
246 |
next "{ Class:SmallInteger }"| |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
247 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
248 |
oldObject isNil ifTrue:[^ nil]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
249 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
250 |
index := self find:oldObject ifAbsent:0. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
251 |
index == 0 ifTrue:[^ nil]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
252 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
253 |
keyArray basicAt:index put:nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
254 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
255 |
tally := tally - 1. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
256 |
tally ~~ 0 ifTrue:[ |
2314 | 257 |
index == keyArray basicSize ifTrue:[ |
258 |
next := 1 |
|
259 |
] ifFalse:[ |
|
260 |
next := index + 1. |
|
261 |
]. |
|
262 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
263 |
keyArray basicAt:index put:DeletedEntry |
|
264 |
]. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
265 |
]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
266 |
^ oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
267 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
268 |
"does NOT work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
269 |
|
2314 | 270 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
271 |
|
2314 | 272 |
s := Set new. |
273 |
s add:1. |
|
274 |
s add:2. |
|
275 |
s add:3. |
|
276 |
s add:4. |
|
277 |
s add:5. |
|
278 |
s add:6. |
|
279 |
s add:7. |
|
280 |
s add:8. |
|
281 |
s add:9. |
|
282 |
s do:[:v | |
|
283 |
v odd ifTrue:[ |
|
284 |
s remove:v |
|
285 |
] |
|
286 |
]. |
|
287 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
288 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
289 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
290 |
"DOES work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
291 |
|
2314 | 292 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
293 |
|
2314 | 294 |
s := Set new. |
295 |
s add:1. |
|
296 |
s add:2. |
|
297 |
s add:3. |
|
298 |
s add:4. |
|
299 |
s add:5. |
|
300 |
s add:6. |
|
301 |
s add:7. |
|
302 |
s add:8. |
|
303 |
s add:9. |
|
304 |
s do:[:v | |
|
305 |
v odd ifTrue:[ |
|
306 |
s saveRemove:v |
|
307 |
] |
|
308 |
]. |
|
309 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
310 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
311 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
312 |
"Created: 1.3.1996 / 21:14:26 / cg" |
2314 | 313 |
"Modified: 29.1.1997 / 21:35:55 / cg" |
609 | 314 |
! ! |
315 |
||
316 |
!Set methodsFor:'binary storage'! |
|
317 |
||
318 |
readBinaryContentsFrom: stream manager: manager |
|
319 |
"must rehash after reload" |
|
320 |
||
321 |
super readBinaryContentsFrom: stream manager: manager. |
|
322 |
self rehash |
|
323 |
! ! |
|
324 |
||
159 | 325 |
!Set methodsFor:'copying'! |
326 |
||
327 |
postCopy |
|
328 |
"have to copy the keyArray too" |
|
329 |
||
330 |
keyArray := keyArray shallowCopy |
|
1 | 331 |
! ! |
332 |
||
609 | 333 |
!Set methodsFor:'enumerating'! |
1 | 334 |
|
609 | 335 |
do:aBlock |
1056 | 336 |
"perform the block for all members in the collection. |
337 |
||
338 |
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
|
339 |
Iterate over a copy to do this." |
13 | 340 |
|
609 | 341 |
|sz "{ Class: SmallInteger }" |
342 |
element| |
|
13 | 343 |
|
609 | 344 |
sz := keyArray size. |
345 |
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
|
346 |
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
|
347 |
(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
|
348 |
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
|
349 |
] |
609 | 350 |
] |
1056 | 351 |
|
352 |
"Modified: 1.3.1996 / 21:41:13 / cg" |
|
609 | 353 |
! ! |
61 | 354 |
|
969 | 355 |
!Set methodsFor:'inspecting'! |
356 |
||
357 |
inspectorClass |
|
358 |
"redefined to use SetInspector |
|
359 |
(instead of the default Inspector)." |
|
360 |
||
361 |
^ SetInspectorView |
|
362 |
! ! |
|
363 |
||
609 | 364 |
!Set methodsFor:'private'! |
2 | 365 |
|
41 | 366 |
emptyCheck |
252 | 367 |
"check if the receiver has become too empty (after a remove) |
324 | 368 |
and shrink if it makes sense. |
369 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
41 | 370 |
|
61 | 371 |
|sz "{Class: SmallInteger}" |
372 |
newSize "{Class: SmallInteger}" | |
|
41 | 373 |
|
61 | 374 |
sz := keyArray basicSize. |
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
375 |
sz > 56 ifTrue:[ |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
376 |
" |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
377 |
shrink if too empty |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
378 |
" |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
379 |
tally < (sz // 8) ifTrue:[ |
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
380 |
newSize := sz // 4. |
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
381 |
self grow:newSize |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
382 |
] |
41 | 383 |
] |
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
384 |
|
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
385 |
"Modified: 19.3.1997 / 16:02:55 / cg" |
41 | 386 |
! |
387 |
||
1 | 388 |
find:key ifAbsent:aBlock |
389 |
"Look for the key in the receiver. If it is found, return |
|
390 |
the index of the slot containing the key, otherwise |
|
391 |
return the value of evaluating aBlock." |
|
392 |
||
61 | 393 |
|index "{ Class:SmallInteger }" |
394 |
length "{ Class:SmallInteger }" |
|
395 |
startIndex probe| |
|
1 | 396 |
|
13 | 397 |
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
|
398 |
|
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
|
399 |
"/ |
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
|
400 |
"/ length < 10 ifTrue:[ |
362 | 401 |
"/ "assuming, that for small collections the overhead of hashing |
402 |
"/ is larger ... maybe that proves wrong |
|
403 |
"/ (if overhead of comparing is higher)" |
|
404 |
"/ ^ 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
|
405 |
"/ ]. |
13 | 406 |
|
362 | 407 |
index := key hash. |
1975 | 408 |
index < 16r1FFFFFFF ifTrue:[ |
409 |
index := index * 2 |
|
410 |
]. |
|
411 |
index := index \\ length + 1. |
|
362 | 412 |
startIndex := index. |
1 | 413 |
|
414 |
[true] whileTrue:[ |
|
159 | 415 |
probe := (keyArray basicAt:index). |
362 | 416 |
probe isNil ifTrue:[^ aBlock value]. |
159 | 417 |
key = probe ifTrue:[^ index]. |
1 | 418 |
|
159 | 419 |
index == length ifTrue:[ |
420 |
index := 1 |
|
421 |
] ifFalse:[ |
|
422 |
index := index + 1 |
|
423 |
]. |
|
362 | 424 |
index == startIndex ifTrue:[^ aBlock value]. |
1 | 425 |
] |
426 |
! |
|
427 |
||
13 | 428 |
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
|
429 |
"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
|
430 |
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
|
431 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 432 |
|
61 | 433 |
|index "{ Class:SmallInteger }" |
434 |
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
|
435 |
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
|
436 |
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
|
437 |
|
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
|
438 |
delIndex := 0. |
1 | 439 |
|
13 | 440 |
length := keyArray basicSize. |
362 | 441 |
index := key hash. |
1975 | 442 |
index < 16r1FFFFFFF ifTrue:[ |
443 |
index := index * 2 |
|
444 |
]. |
|
445 |
index := index \\ length + 1. |
|
362 | 446 |
startIndex := index. |
1 | 447 |
|
448 |
[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
|
449 |
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
|
450 |
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
|
451 |
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
|
452 |
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
|
453 |
^ 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
|
454 |
]. |
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 |
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
|
456 |
|
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
|
457 |
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
|
458 |
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
|
459 |
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
|
460 |
] |
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 |
]. |
1 | 462 |
|
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
|
463 |
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
|
464 |
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
|
465 |
] 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
|
466 |
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
|
467 |
]. |
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 |
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
|
469 |
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
|
470 |
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
|
471 |
^ 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
|
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 |
^ 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
|
474 |
]. |
1 | 475 |
] |
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
|
476 |
|
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
|
477 |
"Modified: 4.6.1996 / 11:15:16 / cg" |
1 | 478 |
! |
479 |
||
480 |
findNil:key |
|
481 |
"Look for the next slot usable for key. This method assumes that |
|
1055 | 482 |
key is not already in the receiver and that keyArray does not have |
483 |
previously removed entries. |
|
484 |
To be used only while growing/rehashing to enter elements into a fresh |
|
485 |
collection." |
|
1 | 486 |
|
1319 | 487 |
|startIndex |
488 |
index "{ Class:SmallInteger }" |
|
61 | 489 |
length "{ Class:SmallInteger }"| |
1 | 490 |
|
13 | 491 |
length := keyArray basicSize. |
362 | 492 |
index := key hash. |
1975 | 493 |
index < 16r1FFFFFFF ifTrue:[ |
494 |
index := index * 2 |
|
495 |
]. |
|
496 |
index := index \\ length + 1. |
|
1319 | 497 |
startIndex := index. |
1 | 498 |
|
13 | 499 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
1319 | 500 |
index == length ifTrue:[ |
501 |
index := 1 |
|
502 |
] ifFalse:[ |
|
503 |
index := index + 1 |
|
504 |
]. |
|
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
505 |
"notice: no check for no nil found - we must find one since |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
506 |
this is only called after growing" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
507 |
"/ index == startIndex ifTrue:[ |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
508 |
"/ "notice: should not be reached |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
509 |
"/ - we must find one since this is only to be |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
510 |
"/ called after growing" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
511 |
"/ ^ 0 |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
512 |
"/ ]. |
1 | 513 |
]. |
514 |
^ index |
|
1055 | 515 |
|
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
516 |
"Modified: 19.3.1997 / 14:59:38 / cg" |
1 | 517 |
! |
518 |
||
609 | 519 |
fullCheck |
520 |
"check if collection is full (after an add); grow if so. |
|
521 |
Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'" |
|
522 |
||
523 |
|sz "{Class: SmallInteger}" | |
|
524 |
||
525 |
" |
|
526 |
grow if filled more than 75% |
|
527 |
" |
|
528 |
sz := keyArray basicSize. |
|
529 |
tally > (sz * 3 // 4) ifTrue:[ |
|
530 |
self grow |
|
531 |
] |
|
532 |
! |
|
533 |
||
1 | 534 |
grow |
535 |
"change the number of element slots of the collection to a useful |
|
536 |
new size" |
|
537 |
||
13 | 538 |
self grow:(keyArray basicSize * 2) |
1 | 539 |
! |
540 |
||
541 |
grow:newSize |
|
542 |
"change the number of element slots of the collection - to do this, |
|
543 |
we have to rehash (which is done by re-adding all elements to a new |
|
544 |
empty set)." |
|
545 |
||
95 | 546 |
|elem oldKeyArray newKeyArray deletedEntry |
252 | 547 |
containerSize oldSize "{ Class:SmallInteger }"| |
1 | 548 |
|
13 | 549 |
oldKeyArray := keyArray. |
252 | 550 |
oldSize := oldKeyArray size. |
551 |
containerSize := (self class goodSizeFrom:newSize). |
|
552 |
containerSize == oldSize ifTrue:[^ self]. |
|
1 | 553 |
|
252 | 554 |
keyArray := newKeyArray := self keyContainerOfSize:containerSize. |
555 |
||
95 | 556 |
deletedEntry := DeletedEntry. |
13 | 557 |
1 to:oldSize do:[:srcIndex | |
159 | 558 |
elem := oldKeyArray basicAt:srcIndex. |
559 |
(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[ |
|
560 |
"cannot be already there" |
|
561 |
newKeyArray basicAt:(self findNil:elem) put:elem |
|
562 |
]. |
|
95 | 563 |
]. |
1 | 564 |
! |
565 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
566 |
hashFor:aKey |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
567 |
"return an initial index given a key." |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
568 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
569 |
^ aKey hash |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
570 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
571 |
"Created: 19.3.1997 / 15:03:03 / cg" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
572 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
573 |
|
609 | 574 |
initialIndexFor:hashKey boundedBy:length |
575 |
"for ST-80 compatibility only; it is (currently) not used in this |
|
576 |
implementation of sets. Therefore, in ST/X it does not make sense |
|
577 |
to redefine it. (which may be a bad design decision, but slightly |
|
578 |
improves performance, by avoiding an extra message send ...)" |
|
579 |
||
2427 | 580 |
|index| |
581 |
||
582 |
index := hashKey. |
|
583 |
index < 16r1FFFFFFF ifTrue:[ |
|
584 |
index := index * 2 |
|
585 |
]. |
|
586 |
index := index \\ length + 1. |
|
587 |
^ index. |
|
588 |
||
589 |
"Modified: 1.3.1997 / 01:01:13 / cg" |
|
609 | 590 |
! |
591 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
592 |
initialIndexForKey:aKey |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
593 |
"return an initial index given a key." |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
594 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
595 |
|index "{ Class:SmallInteger }" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
596 |
length "{ Class:SmallInteger }"| |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
597 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
598 |
length := keyArray basicSize. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
599 |
index := self hashFor:aKey. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
600 |
index < 16r1FFFFFFF ifTrue:[ |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
601 |
index := index * 2 |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
602 |
]. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
603 |
index := index \\ length + 1. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
604 |
^ index. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
605 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
606 |
"Modified: 1.3.1997 / 01:01:13 / cg" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
607 |
"Created: 19.3.1997 / 15:02:41 / cg" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
608 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
609 |
|
609 | 610 |
keyContainerOfSize:n |
611 |
"return a container for keys of size n. |
|
612 |
Extracted to make life of weak subclasses easier ..." |
|
613 |
||
614 |
^ Array basicNew:n |
|
615 |
! |
|
616 |
||
1 | 617 |
rehash |
61 | 618 |
"rehash is done by re-adding all elements to a new empty set. |
619 |
Rehash is needed after a binaryRead, for example." |
|
1 | 620 |
|
95 | 621 |
|element oldKeyArray newKeyArray |
622 |
n "{ Class:SmallInteger }"| |
|
1 | 623 |
|
95 | 624 |
oldKeyArray := keyArray. |
625 |
n := oldKeyArray size. |
|
626 |
keyArray := newKeyArray := self keyContainerOfSize:n. |
|
627 |
||
2 | 628 |
1 to:n do:[:index | |
159 | 629 |
element := oldKeyArray at:index. |
630 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
|
631 |
"cannot be already there" |
|
632 |
newKeyArray basicAt:(self findNil:element) put:element |
|
633 |
]. |
|
1 | 634 |
] |
635 |
! |
|
636 |
||
637 |
rehashFrom:startIndex |
|
61 | 638 |
"rehash elements starting at index - after a remove. |
639 |
Notice: due to the new implementation of remove, |
|
159 | 640 |
this is no longer needed" |
1 | 641 |
|
3 | 642 |
|element i "{ Class:SmallInteger }" |
643 |
length |
|
1 | 644 |
index "{ Class:SmallInteger }" | |
645 |
||
13 | 646 |
length := keyArray basicSize. |
1 | 647 |
index := startIndex. |
13 | 648 |
element := keyArray basicAt:index. |
1 | 649 |
[element notNil] whileTrue:[ |
159 | 650 |
i := self findNil:element. |
651 |
i == index ifTrue:[ |
|
652 |
^ self |
|
653 |
]. |
|
654 |
keyArray basicAt:i put:element. |
|
655 |
keyArray basicAt:index put:nil. |
|
1 | 656 |
|
159 | 657 |
index == length ifTrue:[ |
658 |
index := 1 |
|
659 |
] ifFalse:[ |
|
660 |
index := index + 1. |
|
661 |
]. |
|
662 |
element := keyArray basicAt:index. |
|
1 | 663 |
] |
664 |
! |
|
665 |
||
609 | 666 |
setTally:count |
667 |
"initialize the contents array (for at least count slots) |
|
668 |
and set tally to zero. |
|
669 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 670 |
|
609 | 671 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:count). |
672 |
tally := 0 |
|
1 | 673 |
! ! |
674 |
||
675 |
!Set methodsFor:'testing'! |
|
676 |
||
362 | 677 |
capacity |
678 |
"return the number of elements, that the receiver is |
|
679 |
prepared to take. |
|
680 |
Not used by the system; added for ST-80 compatibility." |
|
681 |
||
682 |
^ keyArray size |
|
683 |
! |
|
684 |
||
1 | 685 |
includes:anObject |
686 |
"return true if the argument anObject is in the receiver" |
|
687 |
||
362 | 688 |
^ (self find:anObject ifAbsent:0) ~~ 0 |
1 | 689 |
! |
690 |
||
691 |
isEmpty |
|
692 |
"return true if the receiver is empty" |
|
693 |
||
694 |
^ tally == 0 |
|
695 |
! |
|
696 |
||
609 | 697 |
isFixedSize |
698 |
"return true if the receiver cannot grow - this will vanish once |
|
699 |
Arrays and Strings learn how to grow ..." |
|
700 |
||
701 |
^ false |
|
702 |
! |
|
703 |
||
2383 | 704 |
notEmpty |
705 |
"return true if the receiver is not empty" |
|
706 |
||
707 |
^ tally ~~ 0 |
|
708 |
||
709 |
"Created: 12.2.1997 / 12:39:02 / cg" |
|
710 |
! |
|
711 |
||
1 | 712 |
occurrencesOf:anObject |
713 |
"return the number of occurrences of anObject in the receiver" |
|
714 |
||
362 | 715 |
(self find:anObject ifAbsent:0) == 0 ifTrue:[^ 0]. |
1 | 716 |
^ 1 |
717 |
! |
|
718 |
||
609 | 719 |
size |
720 |
"return the number of set elements" |
|
1 | 721 |
|
609 | 722 |
^ tally |
1 | 723 |
! ! |
724 |
||
2259 | 725 |
!Set class methodsFor:'documentation'! |
635 | 726 |
|
727 |
version |
|
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
728 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.50 1997-03-19 15:08:46 cg Exp $' |
635 | 729 |
! ! |
609 | 730 |
Set initialize! |