author | Stefan Vogel <sv@exept.de> |
Thu, 23 Sep 1999 18:04:23 +0200 | |
changeset 4795 | 4bdb6488d5b2 |
parent 4017 | cdf103ce2e30 |
child 5048 | b1e10ce753fd |
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:'' |
4017
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
22 |
classVariableNames:'TheOneAndOnlyInstance' |
2259 | 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 |
||
3048 | 151 |
!Set class methodsFor:'utilities'! |
152 |
||
153 |
rehashAllSubInstances |
|
154 |
"rehash all sets & dictionaries. |
|
155 |
Useful utility if hash/identityHash method was changed |
|
156 |
of some object which is known to be kept in a set" |
|
157 |
||
158 |
self allSubInstances do:[:s | s rehash] |
|
159 |
||
160 |
" |
|
161 |
Set rehashAllSubInstances |
|
162 |
" |
|
163 |
||
164 |
"Created: / 24.10.1997 / 23:13:44 / cg" |
|
165 |
! ! |
|
166 |
||
609 | 167 |
!Set methodsFor:'accessing'! |
168 |
||
169 |
at:index |
|
170 |
"report an error: at: is not allowed for Sets" |
|
171 |
||
172 |
^ self errorNotKeyed |
|
173 |
! |
|
174 |
||
175 |
at:index put:anObject |
|
176 |
"report an error: at:put: is not allowed for Sets" |
|
177 |
||
178 |
^ self errorNotKeyed |
|
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
179 |
! |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
180 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
181 |
elementAt:anObject |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
182 |
"return the element, if contained in the set. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
183 |
If there is none, report an error. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
184 |
This may seem confusing at first - however, it is useful with |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
185 |
non-identitysets, to find an existing element, for a |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
186 |
given equal (but not identical) object. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
187 |
This is the same functionality as provided by the goodies/KeyedSet |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
188 |
goody." |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
189 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
190 |
^ self elementAt:anObject ifAbsent:[^ self errorNotFound]. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
191 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
192 |
"Modified: 20.3.1997 / 20:35:24 / cg" |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
193 |
! |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
194 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
195 |
elementAt:anObject ifAbsent:exceptionBlock |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
196 |
"return the element, if contained in the set. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
197 |
If there is none, return the result from evaluating exceptionBlock. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
198 |
This may seem confusing at first - however, it is useful with |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
199 |
non-identitysets, to find an existing element, for a |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
200 |
given equal (but not identical) object. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
201 |
This is the same functionality as provided by the goodies/KeyedSet |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
202 |
goody." |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
203 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
204 |
|index "{ Class: SmallInteger }" | |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
205 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
206 |
index := self find:anObject ifAbsent:0. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
207 |
index == 0 ifTrue:[ |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
208 |
^ exceptionBlock value |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
209 |
]. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
210 |
^ keyArray basicAt:index |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
211 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
212 |
"Created: 20.3.1997 / 20:34:07 / cg" |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
213 |
"Modified: 20.3.1997 / 20:35:49 / cg" |
609 | 214 |
! ! |
215 |
||
216 |
!Set methodsFor:'adding & removing'! |
|
217 |
||
218 |
add:anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
219 |
"add the argument, anObject to the receiver. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
220 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
221 |
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
|
222 |
Iterate over a copy to do this." |
609 | 223 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
224 |
|index "{ Class: SmallInteger }"| |
609 | 225 |
|
226 |
anObject notNil ifTrue:[ |
|
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
227 |
index := self findKeyOrNil:anObject. |
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
228 |
(keyArray basicAt:index) isNil ifTrue:[ |
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
229 |
"/ not already there |
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
230 |
keyArray basicAt:index put:anObject. |
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
231 |
tally := tally + 1. |
609 | 232 |
|
2323
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
233 |
self fullCheck. |
a1fb9151a986
make findKeyOrNil more idiot-proof
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
234 |
] |
609 | 235 |
]. |
236 |
^ anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
237 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
238 |
"Modified: 30.1.1997 / 14:58:08 / cg" |
609 | 239 |
! |
240 |
||
241 |
remove:oldObject ifAbsent:exceptionBlock |
|
242 |
"remove oldObject from the collection and return it. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
243 |
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
|
244 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
245 |
WARNING: do not remove elements while iterating over the receiver. |
2314 | 246 |
See #saveRemove: to do this." |
609 | 247 |
|
248 |
|index next| |
|
249 |
||
3265
18d96c910653
handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents:
3261
diff
changeset
|
250 |
oldObject isNil ifTrue:[^ exceptionBlock value]. |
18d96c910653
handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents:
3261
diff
changeset
|
251 |
|
609 | 252 |
index := self find:oldObject ifAbsent:0. |
253 |
index == 0 ifTrue:[^ exceptionBlock value]. |
|
254 |
||
255 |
keyArray basicAt:index put:nil. |
|
256 |
tally := tally - 1. |
|
257 |
tally == 0 ifTrue:[ |
|
2314 | 258 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). |
609 | 259 |
] ifFalse:[ |
2314 | 260 |
index == keyArray basicSize ifTrue:[ |
261 |
next := 1 |
|
262 |
] ifFalse:[ |
|
263 |
next := index + 1. |
|
264 |
]. |
|
265 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
266 |
keyArray basicAt:index put:DeletedEntry. |
|
267 |
]. |
|
268 |
self emptyCheck |
|
609 | 269 |
]. |
270 |
^ oldObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
271 |
|
3265
18d96c910653
handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents:
3261
diff
changeset
|
272 |
"Modified: / 2.2.1998 / 23:17:27 / cg" |
609 | 273 |
! |
274 |
||
275 |
removeAll |
|
1164 | 276 |
"remove all elements from the receiver. Returns the receiver." |
609 | 277 |
|
278 |
self setTally:7. |
|
1164 | 279 |
|
280 |
"Modified: 12.4.1996 / 13:35:06 / cg" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
281 |
! |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
282 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
283 |
saveRemove:oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
284 |
"remove the element, oldObject from the collection. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
285 |
Return the element. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
286 |
If it was not in the collection return nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
287 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
288 |
In contrast to #remove:, this does not resize the underlying collection |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
289 |
and therefore does NOT rehash & change the elements order. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
290 |
Therefor this can be used while enumerating the receiver, |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
291 |
which is not possible if #remove: is used. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
292 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
293 |
WARNING: since no resizing is done, the physical amount of memory used |
2314 | 294 |
by the container remains the same, although the logical size shrinks. |
295 |
You may want to manually resize the receiver using #emptyCheck. |
|
296 |
(after the loop)" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
297 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
298 |
|index "{ Class:SmallInteger }" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
299 |
next "{ Class:SmallInteger }"| |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
300 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
301 |
oldObject isNil ifTrue:[^ nil]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
302 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
303 |
index := self find:oldObject ifAbsent:0. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
304 |
index == 0 ifTrue:[^ nil]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
305 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
306 |
keyArray basicAt:index put:nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
307 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
308 |
tally := tally - 1. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
309 |
tally ~~ 0 ifTrue:[ |
2314 | 310 |
index == keyArray basicSize ifTrue:[ |
311 |
next := 1 |
|
312 |
] ifFalse:[ |
|
313 |
next := index + 1. |
|
314 |
]. |
|
315 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
316 |
keyArray basicAt:index put:DeletedEntry |
|
317 |
]. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
318 |
]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
319 |
^ oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
320 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
321 |
"does NOT work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
322 |
|
2314 | 323 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
324 |
|
2314 | 325 |
s := Set new. |
326 |
s add:1. |
|
327 |
s add:2. |
|
328 |
s add:3. |
|
329 |
s add:4. |
|
330 |
s add:5. |
|
331 |
s add:6. |
|
332 |
s add:7. |
|
333 |
s add:8. |
|
334 |
s add:9. |
|
335 |
s do:[:v | |
|
336 |
v odd ifTrue:[ |
|
337 |
s remove:v |
|
338 |
] |
|
339 |
]. |
|
340 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
341 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
342 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
343 |
"DOES work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
344 |
|
2314 | 345 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
346 |
|
2314 | 347 |
s := Set new. |
348 |
s add:1. |
|
349 |
s add:2. |
|
350 |
s add:3. |
|
351 |
s add:4. |
|
352 |
s add:5. |
|
353 |
s add:6. |
|
354 |
s add:7. |
|
355 |
s add:8. |
|
356 |
s add:9. |
|
357 |
s do:[:v | |
|
358 |
v odd ifTrue:[ |
|
359 |
s saveRemove:v |
|
360 |
] |
|
361 |
]. |
|
362 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
363 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
364 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
365 |
"Created: 1.3.1996 / 21:14:26 / cg" |
2314 | 366 |
"Modified: 29.1.1997 / 21:35:55 / cg" |
609 | 367 |
! ! |
368 |
||
369 |
!Set methodsFor:'binary storage'! |
|
370 |
||
371 |
readBinaryContentsFrom: stream manager: manager |
|
372 |
"must rehash after reload" |
|
373 |
||
374 |
super readBinaryContentsFrom: stream manager: manager. |
|
375 |
self rehash |
|
376 |
! ! |
|
377 |
||
159 | 378 |
!Set methodsFor:'copying'! |
379 |
||
380 |
postCopy |
|
381 |
"have to copy the keyArray too" |
|
382 |
||
383 |
keyArray := keyArray shallowCopy |
|
1 | 384 |
! ! |
385 |
||
609 | 386 |
!Set methodsFor:'enumerating'! |
1 | 387 |
|
609 | 388 |
do:aBlock |
1056 | 389 |
"perform the block for all members in the collection. |
390 |
||
391 |
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
|
392 |
Iterate over a copy to do this." |
13 | 393 |
|
609 | 394 |
|sz "{ Class: SmallInteger }" |
395 |
element| |
|
13 | 396 |
|
609 | 397 |
sz := keyArray size. |
398 |
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
|
399 |
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
|
400 |
(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
|
401 |
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
|
402 |
] |
609 | 403 |
] |
1056 | 404 |
|
405 |
"Modified: 1.3.1996 / 21:41:13 / cg" |
|
609 | 406 |
! ! |
61 | 407 |
|
969 | 408 |
!Set methodsFor:'inspecting'! |
409 |
||
410 |
inspectorClass |
|
411 |
"redefined to use SetInspector |
|
412 |
(instead of the default Inspector)." |
|
413 |
||
414 |
^ SetInspectorView |
|
415 |
! ! |
|
416 |
||
609 | 417 |
!Set methodsFor:'private'! |
2 | 418 |
|
1 | 419 |
find:key ifAbsent:aBlock |
420 |
"Look for the key in the receiver. If it is found, return |
|
421 |
the index of the slot containing the key, otherwise |
|
422 |
return the value of evaluating aBlock." |
|
423 |
||
61 | 424 |
|index "{ Class:SmallInteger }" |
425 |
length "{ Class:SmallInteger }" |
|
426 |
startIndex probe| |
|
1 | 427 |
|
13 | 428 |
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
|
429 |
|
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 |
"/ |
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 |
"/ length < 10 ifTrue:[ |
362 | 432 |
"/ "assuming, that for small collections the overhead of hashing |
433 |
"/ is larger ... maybe that proves wrong |
|
434 |
"/ (if overhead of comparing is higher)" |
|
435 |
"/ ^ 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
|
436 |
"/ ]. |
13 | 437 |
|
362 | 438 |
index := key hash. |
1975 | 439 |
index < 16r1FFFFFFF ifTrue:[ |
440 |
index := index * 2 |
|
441 |
]. |
|
442 |
index := index \\ length + 1. |
|
362 | 443 |
startIndex := index. |
1 | 444 |
|
445 |
[true] whileTrue:[ |
|
159 | 446 |
probe := (keyArray basicAt:index). |
362 | 447 |
probe isNil ifTrue:[^ aBlock value]. |
159 | 448 |
key = probe ifTrue:[^ index]. |
1 | 449 |
|
159 | 450 |
index == length ifTrue:[ |
451 |
index := 1 |
|
452 |
] ifFalse:[ |
|
453 |
index := index + 1 |
|
454 |
]. |
|
362 | 455 |
index == startIndex ifTrue:[^ aBlock value]. |
1 | 456 |
] |
457 |
! |
|
458 |
||
13 | 459 |
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
|
460 |
"Look for the key in the receiver. |
3082 | 461 |
If it is found, return the index of the first unused slot. |
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
|
462 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 463 |
|
61 | 464 |
|index "{ Class:SmallInteger }" |
465 |
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
|
466 |
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
|
467 |
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
|
468 |
|
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
|
469 |
delIndex := 0. |
1 | 470 |
|
13 | 471 |
length := keyArray basicSize. |
362 | 472 |
index := key hash. |
1975 | 473 |
index < 16r1FFFFFFF ifTrue:[ |
474 |
index := index * 2 |
|
475 |
]. |
|
476 |
index := index \\ length + 1. |
|
362 | 477 |
startIndex := index. |
1 | 478 |
|
479 |
[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
|
480 |
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
|
481 |
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
|
482 |
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
|
483 |
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
|
484 |
^ 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
|
485 |
]. |
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
|
486 |
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
|
487 |
|
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
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
] |
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
|
492 |
]. |
1 | 493 |
|
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
|
494 |
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
|
495 |
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
|
496 |
] 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
|
497 |
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
|
498 |
]. |
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
|
499 |
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
|
500 |
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
|
501 |
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
|
502 |
^ 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
|
503 |
]. |
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
|
504 |
^ 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
|
505 |
]. |
1 | 506 |
] |
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
|
507 |
|
3082 | 508 |
"Modified: / 30.10.1997 / 16:01:57 / cg" |
1 | 509 |
! |
510 |
||
511 |
findNil:key |
|
2475 | 512 |
"Look for the next slot usable for key. |
513 |
WARNING: |
|
514 |
This method assumes that key is not already in the receiver |
|
515 |
AND that keyArray does not have previously removed entries |
|
516 |
AND that there is an empty slot. |
|
517 |
To be used ONLY while growing/rehashing to enter elements into a fresh |
|
518 |
collection - if any of the above conditions is not met, the method |
|
519 |
loops forever." |
|
1 | 520 |
|
2475 | 521 |
|index "{ Class:SmallInteger }" |
61 | 522 |
length "{ Class:SmallInteger }"| |
1 | 523 |
|
13 | 524 |
length := keyArray basicSize. |
2475 | 525 |
index := self hashFor:key. |
1975 | 526 |
index < 16r1FFFFFFF ifTrue:[ |
527 |
index := index * 2 |
|
528 |
]. |
|
529 |
index := index \\ length + 1. |
|
1 | 530 |
|
13 | 531 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
1319 | 532 |
index == length ifTrue:[ |
533 |
index := 1 |
|
534 |
] ifFalse:[ |
|
535 |
index := index + 1 |
|
536 |
]. |
|
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
537 |
"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
|
538 |
this is only called after growing" |
1 | 539 |
]. |
540 |
^ index |
|
1055 | 541 |
|
2475 | 542 |
"Modified: 21.3.1997 / 10:32:58 / cg" |
1 | 543 |
! |
544 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
545 |
hashFor:aKey |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
546 |
"return an initial index given a key." |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
547 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
548 |
^ aKey hash |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
549 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
550 |
"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
|
551 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
552 |
|
609 | 553 |
initialIndexFor:hashKey boundedBy:length |
554 |
"for ST-80 compatibility only; it is (currently) not used in this |
|
555 |
implementation of sets. Therefore, in ST/X it does not make sense |
|
556 |
to redefine it. (which may be a bad design decision, but slightly |
|
557 |
improves performance, by avoiding an extra message send ...)" |
|
558 |
||
2427 | 559 |
|index| |
560 |
||
561 |
index := hashKey. |
|
562 |
index < 16r1FFFFFFF ifTrue:[ |
|
563 |
index := index * 2 |
|
564 |
]. |
|
565 |
index := index \\ length + 1. |
|
566 |
^ index. |
|
567 |
||
568 |
"Modified: 1.3.1997 / 01:01:13 / cg" |
|
609 | 569 |
! |
570 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
571 |
initialIndexForKey:aKey |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
572 |
"return an initial index given a key." |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
573 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
574 |
|index "{ Class:SmallInteger }" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
575 |
length "{ Class:SmallInteger }"| |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
576 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
577 |
length := keyArray basicSize. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
578 |
index := self hashFor:aKey. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
579 |
index < 16r1FFFFFFF ifTrue:[ |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
580 |
index := index * 2 |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
581 |
]. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
582 |
index := index \\ length + 1. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
583 |
^ index. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
584 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
585 |
"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
|
586 |
"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
|
587 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
588 |
|
3261
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
589 |
keyArray |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
590 |
^ keyArray |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
591 |
|
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
592 |
"Created: / 2.2.1998 / 14:50:55 / cg" |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
593 |
! |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
594 |
|
609 | 595 |
keyContainerOfSize:n |
596 |
"return a container for keys of size n. |
|
597 |
Extracted to make life of weak subclasses easier ..." |
|
598 |
||
599 |
^ Array basicNew:n |
|
600 |
! |
|
601 |
||
1 | 602 |
rehash |
61 | 603 |
"rehash is done by re-adding all elements to a new empty set. |
604 |
Rehash is needed after a binaryRead, for example." |
|
1 | 605 |
|
3082 | 606 |
|element oldKeyArray newKeyArray newIdx |
95 | 607 |
n "{ Class:SmallInteger }"| |
1 | 608 |
|
95 | 609 |
oldKeyArray := keyArray. |
610 |
n := oldKeyArray size. |
|
611 |
keyArray := newKeyArray := self keyContainerOfSize:n. |
|
612 |
||
2 | 613 |
1 to:n do:[:index | |
3082 | 614 |
element := oldKeyArray at:index. |
615 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
|
616 |
"cannot be already there" |
|
617 |
newIdx := self findNil:element. |
|
618 |
newKeyArray basicAt:newIdx put:element |
|
619 |
]. |
|
1 | 620 |
] |
3082 | 621 |
|
622 |
"Modified: / 30.10.1997 / 16:04:46 / cg" |
|
1 | 623 |
! |
624 |
||
625 |
rehashFrom:startIndex |
|
61 | 626 |
"rehash elements starting at index - after a remove. |
627 |
Notice: due to the new implementation of remove, |
|
159 | 628 |
this is no longer needed" |
1 | 629 |
|
3 | 630 |
|element i "{ Class:SmallInteger }" |
631 |
length |
|
1 | 632 |
index "{ Class:SmallInteger }" | |
633 |
||
13 | 634 |
length := keyArray basicSize. |
1 | 635 |
index := startIndex. |
13 | 636 |
element := keyArray basicAt:index. |
1 | 637 |
[element notNil] whileTrue:[ |
159 | 638 |
i := self findNil:element. |
639 |
i == index ifTrue:[ |
|
640 |
^ self |
|
641 |
]. |
|
642 |
keyArray basicAt:i put:element. |
|
643 |
keyArray basicAt:index put:nil. |
|
1 | 644 |
|
159 | 645 |
index == length ifTrue:[ |
646 |
index := 1 |
|
647 |
] ifFalse:[ |
|
648 |
index := index + 1. |
|
649 |
]. |
|
650 |
element := keyArray basicAt:index. |
|
1 | 651 |
] |
652 |
! |
|
653 |
||
609 | 654 |
setTally:count |
655 |
"initialize the contents array (for at least count slots) |
|
656 |
and set tally to zero. |
|
657 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 658 |
|
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
659 |
|n| |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
660 |
|
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
661 |
n := self class goodSizeFrom:count. |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
662 |
n == keyArray size ifTrue:[ |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
663 |
keyArray atAllPut:nil. |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
664 |
] ifFalse:[ |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
665 |
keyArray := self keyContainerOfSize:n. |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
666 |
]. |
609 | 667 |
tally := 0 |
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
668 |
|
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
669 |
"Modified: / 4.8.1998 / 22:07:25 / cg" |
1 | 670 |
! ! |
671 |
||
3082 | 672 |
!Set methodsFor:'private - grow & shrink'! |
673 |
||
674 |
emptyCheck |
|
675 |
"check if the receiver has become too empty (after a remove) |
|
676 |
and shrink if it makes sense. |
|
677 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
678 |
||
679 |
|sz "{Class: SmallInteger}" |
|
680 |
newSize "{Class: SmallInteger}" | |
|
681 |
||
682 |
sz := keyArray basicSize. |
|
683 |
sz > 56 ifTrue:[ |
|
684 |
" |
|
685 |
shrink if too empty |
|
686 |
" |
|
687 |
tally < (sz // 8) ifTrue:[ |
|
688 |
newSize := sz // 4. |
|
689 |
self grow:newSize |
|
690 |
] |
|
691 |
] |
|
692 |
||
693 |
"Modified: 19.3.1997 / 16:02:55 / cg" |
|
694 |
! |
|
695 |
||
696 |
fullCheck |
|
697 |
"check if collection is full (after an add); grow if so. |
|
698 |
Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'" |
|
699 |
||
700 |
|sz "{Class: SmallInteger}" | |
|
701 |
||
702 |
" |
|
703 |
grow if filled more than 75% |
|
704 |
" |
|
705 |
sz := keyArray basicSize. |
|
706 |
tally > (sz * 3 // 4) ifTrue:[ |
|
707 |
self grow |
|
708 |
] |
|
709 |
! |
|
710 |
||
711 |
grow |
|
712 |
"change the number of element slots of the collection to a useful |
|
713 |
new size" |
|
714 |
||
715 |
self grow:(keyArray basicSize * 2) |
|
716 |
! |
|
717 |
||
718 |
grow:newSize |
|
719 |
"change the number of element slots of the collection - to do this, |
|
720 |
we have to rehash (which is done by re-adding all elements to a new |
|
721 |
empty set)." |
|
722 |
||
723 |
|elem oldKeyArray newKeyArray deletedEntry |
|
724 |
containerSize oldSize "{ Class:SmallInteger }"| |
|
725 |
||
726 |
oldKeyArray := keyArray. |
|
727 |
oldSize := oldKeyArray size. |
|
728 |
containerSize := (self class goodSizeFrom:newSize). |
|
729 |
containerSize == oldSize ifTrue:[^ self]. |
|
730 |
||
731 |
keyArray := newKeyArray := self keyContainerOfSize:containerSize. |
|
732 |
||
733 |
deletedEntry := DeletedEntry. |
|
734 |
1 to:oldSize do:[:srcIndex | |
|
735 |
elem := oldKeyArray basicAt:srcIndex. |
|
736 |
(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[ |
|
737 |
"cannot be already there" |
|
738 |
newKeyArray basicAt:(self findNil:elem) put:elem |
|
739 |
]. |
|
740 |
]. |
|
741 |
! ! |
|
742 |
||
1 | 743 |
!Set methodsFor:'testing'! |
744 |
||
362 | 745 |
capacity |
746 |
"return the number of elements, that the receiver is |
|
747 |
prepared to take. |
|
748 |
Not used by the system; added for ST-80 compatibility." |
|
749 |
||
750 |
^ keyArray size |
|
751 |
! |
|
752 |
||
1 | 753 |
includes:anObject |
754 |
"return true if the argument anObject is in the receiver" |
|
755 |
||
362 | 756 |
^ (self find:anObject ifAbsent:0) ~~ 0 |
1 | 757 |
! |
758 |
||
759 |
isEmpty |
|
760 |
"return true if the receiver is empty" |
|
761 |
||
762 |
^ tally == 0 |
|
763 |
! |
|
764 |
||
609 | 765 |
isFixedSize |
766 |
"return true if the receiver cannot grow - this will vanish once |
|
767 |
Arrays and Strings learn how to grow ..." |
|
768 |
||
769 |
^ false |
|
770 |
! |
|
771 |
||
2383 | 772 |
notEmpty |
773 |
"return true if the receiver is not empty" |
|
774 |
||
775 |
^ tally ~~ 0 |
|
776 |
||
777 |
"Created: 12.2.1997 / 12:39:02 / cg" |
|
778 |
! |
|
779 |
||
1 | 780 |
occurrencesOf:anObject |
781 |
"return the number of occurrences of anObject in the receiver" |
|
782 |
||
362 | 783 |
(self find:anObject ifAbsent:0) == 0 ifTrue:[^ 0]. |
1 | 784 |
^ 1 |
785 |
! |
|
786 |
||
4795 | 787 |
sameContentsAs:aCollection |
788 |
"answer true, if all the elements in self and aCollection |
|
789 |
are common. This is not defined as #=, since we cannot redefine #hash |
|
790 |
for aCollection." |
|
791 |
||
792 |
aCollection size ~~ self size ifTrue:[ |
|
793 |
^ false |
|
794 |
]. |
|
795 |
||
796 |
aCollection do:[:e| |
|
797 |
(self includes:e) ifFalse:[ |
|
798 |
^ false. |
|
799 |
] |
|
800 |
]. |
|
801 |
||
802 |
^ true. |
|
803 |
||
804 |
||
805 |
" |
|
806 |
#(1 2 3) asSet sameContentsAs: #(1 2 3) |
|
807 |
#(1 2 3 4) asSet sameContentsAs: #(1 2 3) |
|
808 |
#(1 2 3) asSet sameContentsAs: #(1 2 3 3) |
|
809 |
#(1 2 3 'aa') asSet sameContentsAs: #(1 2 3 'aa') |
|
810 |
#(1 2 3 'aa') asIdentitySet sameContentsAs: #(1 2 3 'aa') |
|
811 |
#(1 2 3 #aa) asIdentitySet sameContentsAs: #(1 2 3 #aa) |
|
812 |
" |
|
813 |
||
814 |
||
815 |
||
816 |
||
817 |
||
818 |
! |
|
819 |
||
609 | 820 |
size |
821 |
"return the number of set elements" |
|
1 | 822 |
|
609 | 823 |
^ tally |
1 | 824 |
! ! |
825 |
||
4017
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
826 |
!Set::EmptySlot class methodsFor:'instance creation'! |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
827 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
828 |
basicNew |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
829 |
TheOneAndOnlyInstance isNil ifTrue:[ |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
830 |
TheOneAndOnlyInstance := super basicNew |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
831 |
]. |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
832 |
^ TheOneAndOnlyInstance |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
833 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
834 |
! |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
835 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
836 |
new |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
837 |
TheOneAndOnlyInstance isNil ifTrue:[ |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
838 |
TheOneAndOnlyInstance := super basicNew |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
839 |
]. |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
840 |
^ TheOneAndOnlyInstance |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
841 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
842 |
! ! |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
843 |
|
2259 | 844 |
!Set class methodsFor:'documentation'! |
635 | 845 |
|
846 |
version |
|
4795 | 847 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.59 1999-09-23 16:04:23 stefan Exp $' |
635 | 848 |
! ! |
609 | 849 |
Set initialize! |