author | Claus Gittinger <cg@exept.de> |
Tue, 12 Nov 1996 16:13:57 +0100 | |
changeset 1975 | c94a8e0b0251 |
parent 1972 | f9bfcad48b64 |
child 2259 | 685b96fb41b6 |
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 |
||
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
20 |
!Set class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
a Set is a collection where each element occurs at most once. |
|
95 | 39 |
The inclusion test is done using = for comparison; |
40 |
see IdentitySet for sets using identity compare. |
|
41 |
Sets use hashing for fast access, this access is considerably faster, |
|
42 |
if a good hash-number is returned by the elements. |
|
362 | 43 |
|
44 |
Notice that the default hash (Object>>hash) is not perfect; due to |
|
45 |
the implementation of hash-keys in ST/X, increased hash collisions |
|
46 |
are to be expected for large sets (say: > 20000 element). |
|
47 |
If your objects are heavyly used in sets or dictionaries, and you need |
|
48 |
big collections, your instances may provide a better hash values. |
|
95 | 49 |
|
345 | 50 |
Performance hints: |
657 | 51 |
If only symbols or smallIntegers are used as keys, |
362 | 52 |
use an instance of IdentitySet for slightly better performance, |
53 |
since both hashing and comparison is faster. |
|
345 | 54 |
|
55 |
If you have a rough idea how big the set is going to grow, |
|
56 |
create it using #new: instead of #new. Even if the size given is a |
|
57 |
poor guess (say half of the real size), there is some 20-30% performance |
|
58 |
win to expect, since many resizing operations of the set are avoided. |
|
59 |
||
95 | 60 |
Examples: |
61 |
||
1290 | 62 |
|s| |
63 |
s := Set new. |
|
64 |
s add:'hello'. |
|
65 |
s add:'world'. |
|
66 |
s add:#foo. |
|
67 |
s add:1.2345678. |
|
68 |
s add:'hello'. |
|
95 | 69 |
|
1290 | 70 |
s printNL. |
71 |
's size -> ' print. s size printNL. |
|
72 |
'(s includes:''hello'') -> ' print. (s includes:'hello') printNL. |
|
73 |
'(s includes:#foo) -> ' print. (s includes:#foo) printNL. |
|
74 |
'(s includes:''foo'') -> ' print. (s includes:'foo') printNL. |
|
75 |
'(s includes:#bar) -> ' print. (s includes:#bar) printNL. |
|
76 |
||
77 |
[author:] |
|
78 |
Claus Gittinger |
|
88 | 79 |
" |
80 |
! ! |
|
1 | 81 |
|
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
82 |
!Set class methodsFor:'initialization'! |
41 | 83 |
|
84 |
initialize |
|
85 |
"initialize the Set class" |
|
86 |
||
87 |
DeletedEntry isNil ifTrue:[ |
|
159 | 88 |
DeletedEntry := Object new |
41 | 89 |
]. |
90 |
||
91 |
"Set initialize" |
|
92 |
! ! |
|
93 |
||
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
94 |
!Set class methodsFor:'instance creation'! |
1 | 95 |
|
96 |
new |
|
97 |
"return a new empty Set" |
|
98 |
||
99 |
^ self new:7 |
|
100 |
! |
|
101 |
||
102 |
new:anInteger |
|
103 |
"return a new empty Set with space for anInteger elements" |
|
104 |
||
159 | 105 |
" |
106 |
make it somewhat bigger; hashing works better if fill grade is |
|
107 |
below 10% (make it 75% here ..) |
|
108 |
" |
|
109 |
^ self basicNew setTally:(anInteger * 4 // 3) |
|
110 |
! ! |
|
111 |
||
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
112 |
!Set class methodsFor:'queries'! |
252 | 113 |
|
114 |
goodSizeFrom:arg |
|
115 |
"return a good array size for the given argument. |
|
116 |
Returns the next prime after arg, since prime sizes are good for hashing." |
|
117 |
||
302 | 118 |
|n| |
119 |
||
252 | 120 |
arg <= 11 ifTrue:[^ 11]. |
121 |
||
302 | 122 |
n := arg * 3 // 2. |
123 |
||
252 | 124 |
" |
125 |
mhmh - this returns good numbers for collections with up-to about |
|
126 |
500k elements; if you have bigger ones, add some more primes here ... |
|
127 |
" |
|
302 | 128 |
n <= 524288 ifTrue:[ |
252 | 129 |
"2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288" |
302 | 130 |
^ #(11 11 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit) |
252 | 131 |
]. |
132 |
" |
|
133 |
make it odd - at least |
|
134 |
" |
|
302 | 135 |
^ n bitOr:1 |
252 | 136 |
! ! |
137 |
||
609 | 138 |
!Set methodsFor:'accessing'! |
139 |
||
140 |
at:index |
|
141 |
"report an error: at: is not allowed for Sets" |
|
142 |
||
143 |
^ self errorNotKeyed |
|
144 |
! |
|
145 |
||
146 |
at:index put:anObject |
|
147 |
"report an error: at:put: is not allowed for Sets" |
|
148 |
||
149 |
^ self errorNotKeyed |
|
150 |
! ! |
|
151 |
||
152 |
!Set methodsFor:'adding & removing'! |
|
153 |
||
154 |
add:anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
155 |
"add the argument, anObject to the receiver. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
156 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
157 |
WARNING: do not add 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
|
158 |
Iterate over a copy to do this." |
609 | 159 |
|
160 |
|index "{ Class: SmallInteger }"| |
|
161 |
||
162 |
anObject notNil ifTrue:[ |
|
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
|
163 |
index := self findKeyOrNil:anObject. |
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
|
164 |
(keyArray basicAt:index) isNil 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
|
165 |
keyArray basicAt:index put:anObject. |
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
|
166 |
tally := tally + 1. |
609 | 167 |
|
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
|
168 |
self fullCheck. |
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
|
169 |
] |
609 | 170 |
]. |
171 |
^ anObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
172 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
173 |
"Modified: 1.3.1996 / 21:24:26 / cg" |
609 | 174 |
! |
175 |
||
176 |
remove:oldObject ifAbsent:exceptionBlock |
|
177 |
"remove oldObject from the collection and return it. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
178 |
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
|
179 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
180 |
WARNING: do not 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
|
181 |
See #saveRemove: to do this." |
609 | 182 |
|
183 |
|index next| |
|
184 |
||
185 |
"/ code below is actually the same as: |
|
186 |
"/ |
|
187 |
"/ index := self find:oldObject ifAbsent:[^ exceptionBlock value]. |
|
188 |
"/ |
|
1144 | 189 |
"/ but cheaper, since it avoids a block creation, |
190 |
"/ making the good case a bit faster. |
|
609 | 191 |
|
192 |
index := self find:oldObject ifAbsent:0. |
|
193 |
index == 0 ifTrue:[^ exceptionBlock value]. |
|
194 |
||
195 |
keyArray basicAt:index put:nil. |
|
196 |
tally := tally - 1. |
|
197 |
tally == 0 ifTrue:[ |
|
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
|
198 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). |
609 | 199 |
] ifFalse:[ |
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
|
200 |
index == keyArray basicSize 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
|
201 |
next := 1 |
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
|
202 |
] ifFalse:[ |
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
|
203 |
next := index + 1. |
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
|
204 |
]. |
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
|
205 |
(keyArray basicAt:next) notNil 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
|
206 |
keyArray basicAt:index put:DeletedEntry. |
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
|
207 |
]. |
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
|
208 |
self emptyCheck |
609 | 209 |
]. |
210 |
^ oldObject |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
211 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
212 |
"Modified: 1.3.1996 / 21:24:45 / cg" |
609 | 213 |
! |
214 |
||
215 |
removeAll |
|
1164 | 216 |
"remove all elements from the receiver. Returns the receiver." |
609 | 217 |
|
218 |
self setTally:7. |
|
1164 | 219 |
|
220 |
"Modified: 12.4.1996 / 13:35:06 / cg" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
221 |
! |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
222 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
223 |
saveRemove:oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
224 |
"remove the element, oldObject from the collection. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
225 |
Return the element. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
226 |
If it was not in the collection return nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
227 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
228 |
In contrast to #remove:, this does not resize the underlying collection |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
229 |
and therefore does NOT rehash & change the elements order. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
230 |
Therefor this can be used while enumerating the receiver, |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
231 |
which is not possible if #remove: is used. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
232 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
233 |
WARNING: since no resizing is done, the physical amount of memory used |
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
|
234 |
by the container remains the same, although the logical size shrinks. |
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
|
235 |
You may want to manually resize the receiver using #emptyCheck." |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
236 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
237 |
|index "{ Class:SmallInteger }" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
238 |
next "{ Class:SmallInteger }"| |
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 |
oldObject isNil ifTrue:[^ nil]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
241 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
242 |
index := self find:oldObject ifAbsent:0. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
243 |
index == 0 ifTrue:[^ nil]. |
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 |
keyArray basicAt:index put:nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
246 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
247 |
tally := tally - 1. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
248 |
tally ~~ 0 ifTrue:[ |
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
|
249 |
index == keyArray basicSize 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
|
250 |
next := 1 |
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
|
251 |
] ifFalse:[ |
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
|
252 |
next := index + 1. |
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
|
253 |
]. |
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
|
254 |
(keyArray basicAt:next) notNil 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
|
255 |
keyArray basicAt:index put:DeletedEntry |
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
|
256 |
]. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
257 |
]. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
258 |
^ oldObject |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
259 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
260 |
"does NOT work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
261 |
|
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
|
262 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
263 |
|
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
|
264 |
s := Set new. |
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
|
265 |
s add:1. |
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
|
266 |
s add:2. |
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
|
267 |
s add:3. |
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
|
268 |
s add:4. |
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
|
269 |
s add:5. |
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
|
270 |
s add:6. |
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
|
271 |
s add:7. |
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
|
272 |
s add:8. |
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
|
273 |
s add:9. |
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
|
274 |
s do:[:v | |
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
|
275 |
v odd 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
|
276 |
s remove:v |
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
|
277 |
] |
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
|
278 |
]. |
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
|
279 |
s inspect |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
280 |
" |
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 |
"DOES work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
283 |
|
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
|
284 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
285 |
|
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
|
286 |
s := Set new. |
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
|
287 |
s add:1. |
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
|
288 |
s add:2. |
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
|
289 |
s add:3. |
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
|
290 |
s add:4. |
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
|
291 |
s add:5. |
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
|
292 |
s add:6. |
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
|
293 |
s add:7. |
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
|
294 |
s add:8. |
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
|
295 |
s add:9. |
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
|
296 |
s do:[:v | |
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
|
297 |
v odd 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
|
298 |
s saveRemove:v |
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
|
299 |
] |
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
|
300 |
]. |
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
|
301 |
s inspect |
1052
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 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
304 |
"Created: 1.3.1996 / 21:14:26 / cg" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
305 |
"Modified: 1.3.1996 / 21:15:27 / cg" |
609 | 306 |
! ! |
307 |
||
308 |
!Set methodsFor:'binary storage'! |
|
309 |
||
310 |
readBinaryContentsFrom: stream manager: manager |
|
311 |
"must rehash after reload" |
|
312 |
||
313 |
super readBinaryContentsFrom: stream manager: manager. |
|
314 |
self rehash |
|
315 |
! ! |
|
316 |
||
159 | 317 |
!Set methodsFor:'copying'! |
318 |
||
319 |
postCopy |
|
320 |
"have to copy the keyArray too" |
|
321 |
||
322 |
keyArray := keyArray shallowCopy |
|
1 | 323 |
! ! |
324 |
||
609 | 325 |
!Set methodsFor:'enumerating'! |
1 | 326 |
|
609 | 327 |
do:aBlock |
1056 | 328 |
"perform the block for all members in the collection. |
329 |
||
330 |
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
|
331 |
Iterate over a copy to do this." |
13 | 332 |
|
609 | 333 |
|sz "{ Class: SmallInteger }" |
334 |
element| |
|
13 | 335 |
|
609 | 336 |
sz := keyArray size. |
337 |
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
|
338 |
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
|
339 |
(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
|
340 |
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
|
341 |
] |
609 | 342 |
] |
1056 | 343 |
|
344 |
"Modified: 1.3.1996 / 21:41:13 / cg" |
|
609 | 345 |
! ! |
61 | 346 |
|
969 | 347 |
!Set methodsFor:'inspecting'! |
348 |
||
349 |
inspectorClass |
|
350 |
"redefined to use SetInspector |
|
351 |
(instead of the default Inspector)." |
|
352 |
||
353 |
^ SetInspectorView |
|
354 |
! ! |
|
355 |
||
609 | 356 |
!Set methodsFor:'private'! |
2 | 357 |
|
41 | 358 |
emptyCheck |
252 | 359 |
"check if the receiver has become too empty (after a remove) |
324 | 360 |
and shrink if it makes sense. |
361 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
41 | 362 |
|
61 | 363 |
|sz "{Class: SmallInteger}" |
364 |
newSize "{Class: SmallInteger}" | |
|
41 | 365 |
|
61 | 366 |
sz := keyArray basicSize. |
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
367 |
sz > 56 ifTrue:[ |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
368 |
" |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
369 |
shrink if too empty |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
370 |
" |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
371 |
tally < (sz // 8) ifTrue:[ |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
372 |
newSize := sz // 7. |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
373 |
self grow:newSize |
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
374 |
] |
41 | 375 |
] |
1643
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 |
"Modified: 4.9.1996 / 14:39:53 / cg" |
41 | 378 |
! |
379 |
||
1 | 380 |
find:key ifAbsent:aBlock |
381 |
"Look for the key in the receiver. If it is found, return |
|
382 |
the index of the slot containing the key, otherwise |
|
383 |
return the value of evaluating aBlock." |
|
384 |
||
61 | 385 |
|index "{ Class:SmallInteger }" |
386 |
length "{ Class:SmallInteger }" |
|
387 |
startIndex probe| |
|
1 | 388 |
|
13 | 389 |
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
|
390 |
|
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
|
391 |
"/ |
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 |
"/ length < 10 ifTrue:[ |
362 | 393 |
"/ "assuming, that for small collections the overhead of hashing |
394 |
"/ is larger ... maybe that proves wrong |
|
395 |
"/ (if overhead of comparing is higher)" |
|
396 |
"/ ^ 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
|
397 |
"/ ]. |
13 | 398 |
|
362 | 399 |
index := key hash. |
1975 | 400 |
index < 16r1FFFFFFF ifTrue:[ |
401 |
index := index * 2 |
|
402 |
]. |
|
403 |
index := index \\ length + 1. |
|
362 | 404 |
startIndex := index. |
1 | 405 |
|
406 |
[true] whileTrue:[ |
|
159 | 407 |
probe := (keyArray basicAt:index). |
362 | 408 |
probe isNil ifTrue:[^ aBlock value]. |
159 | 409 |
key = probe ifTrue:[^ index]. |
1 | 410 |
|
159 | 411 |
index == length ifTrue:[ |
412 |
index := 1 |
|
413 |
] ifFalse:[ |
|
414 |
index := index + 1 |
|
415 |
]. |
|
362 | 416 |
index == startIndex ifTrue:[^ aBlock value]. |
1 | 417 |
] |
418 |
! |
|
419 |
||
13 | 420 |
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
|
421 |
"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
|
422 |
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
|
423 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 424 |
|
61 | 425 |
|index "{ Class:SmallInteger }" |
426 |
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
|
427 |
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
|
428 |
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
|
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 |
delIndex := 0. |
1 | 431 |
|
13 | 432 |
length := keyArray basicSize. |
362 | 433 |
index := key hash. |
1975 | 434 |
index < 16r1FFFFFFF ifTrue:[ |
435 |
index := index * 2 |
|
436 |
]. |
|
437 |
index := index \\ length + 1. |
|
362 | 438 |
startIndex := index. |
1 | 439 |
|
440 |
[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
|
441 |
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
|
442 |
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
|
443 |
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
|
444 |
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
|
445 |
^ 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
|
446 |
]. |
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
|
447 |
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
|
448 |
|
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 == 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
|
450 |
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
|
451 |
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
|
452 |
] |
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 |
]. |
1 | 454 |
|
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
|
455 |
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
|
456 |
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
|
457 |
] 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
|
458 |
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
|
459 |
]. |
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 |
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
|
461 |
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
|
462 |
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
|
463 |
^ 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
|
464 |
]. |
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 |
^ 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
|
466 |
]. |
1 | 467 |
] |
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
|
468 |
|
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 |
"Modified: 4.6.1996 / 11:15:16 / cg" |
1 | 470 |
! |
471 |
||
472 |
findNil:key |
|
473 |
"Look for the next slot usable for key. This method assumes that |
|
1055 | 474 |
key is not already in the receiver and that keyArray does not have |
475 |
previously removed entries. |
|
476 |
To be used only while growing/rehashing to enter elements into a fresh |
|
477 |
collection." |
|
1 | 478 |
|
1319 | 479 |
|startIndex |
480 |
index "{ Class:SmallInteger }" |
|
61 | 481 |
length "{ Class:SmallInteger }"| |
1 | 482 |
|
13 | 483 |
length := keyArray basicSize. |
362 | 484 |
index := key hash. |
1975 | 485 |
index < 16r1FFFFFFF ifTrue:[ |
486 |
index := index * 2 |
|
487 |
]. |
|
488 |
index := index \\ length + 1. |
|
1319 | 489 |
startIndex := index. |
1 | 490 |
|
13 | 491 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
1319 | 492 |
index == length ifTrue:[ |
493 |
index := 1 |
|
494 |
] ifFalse:[ |
|
495 |
index := index + 1 |
|
496 |
]. |
|
497 |
index == startIndex ifTrue:[ |
|
498 |
"notice: should not be reached |
|
499 |
- we must find one since this is only to be |
|
500 |
called after growing" |
|
501 |
^ 0 |
|
502 |
]. |
|
1 | 503 |
]. |
504 |
^ index |
|
1055 | 505 |
|
1319 | 506 |
"Modified: 30.4.1996 / 14:15:20 / cg" |
1 | 507 |
! |
508 |
||
609 | 509 |
fullCheck |
510 |
"check if collection is full (after an add); grow if so. |
|
511 |
Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'" |
|
512 |
||
513 |
|sz "{Class: SmallInteger}" | |
|
514 |
||
515 |
" |
|
516 |
grow if filled more than 75% |
|
517 |
" |
|
518 |
sz := keyArray basicSize. |
|
519 |
tally > (sz * 3 // 4) ifTrue:[ |
|
520 |
self grow |
|
521 |
] |
|
522 |
! |
|
523 |
||
1 | 524 |
grow |
525 |
"change the number of element slots of the collection to a useful |
|
526 |
new size" |
|
527 |
||
13 | 528 |
self grow:(keyArray basicSize * 2) |
1 | 529 |
! |
530 |
||
531 |
grow:newSize |
|
532 |
"change the number of element slots of the collection - to do this, |
|
533 |
we have to rehash (which is done by re-adding all elements to a new |
|
534 |
empty set)." |
|
535 |
||
95 | 536 |
|elem oldKeyArray newKeyArray deletedEntry |
252 | 537 |
containerSize oldSize "{ Class:SmallInteger }"| |
1 | 538 |
|
13 | 539 |
oldKeyArray := keyArray. |
252 | 540 |
oldSize := oldKeyArray size. |
541 |
containerSize := (self class goodSizeFrom:newSize). |
|
542 |
containerSize == oldSize ifTrue:[^ self]. |
|
1 | 543 |
|
252 | 544 |
keyArray := newKeyArray := self keyContainerOfSize:containerSize. |
545 |
||
95 | 546 |
deletedEntry := DeletedEntry. |
13 | 547 |
1 to:oldSize do:[:srcIndex | |
159 | 548 |
elem := oldKeyArray basicAt:srcIndex. |
549 |
(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[ |
|
550 |
"cannot be already there" |
|
551 |
newKeyArray basicAt:(self findNil:elem) put:elem |
|
552 |
]. |
|
95 | 553 |
]. |
1 | 554 |
! |
555 |
||
609 | 556 |
initialIndexFor:hashKey boundedBy:length |
557 |
"for ST-80 compatibility only; it is (currently) not used in this |
|
558 |
implementation of sets. Therefore, in ST/X it does not make sense |
|
559 |
to redefine it. (which may be a bad design decision, but slightly |
|
560 |
improves performance, by avoiding an extra message send ...)" |
|
561 |
||
1972 | 562 |
^ (hashKey * 2 \\ length) + 1. |
609 | 563 |
! |
564 |
||
565 |
keyContainerOfSize:n |
|
566 |
"return a container for keys of size n. |
|
567 |
Extracted to make life of weak subclasses easier ..." |
|
568 |
||
569 |
^ Array basicNew:n |
|
570 |
! |
|
571 |
||
1 | 572 |
rehash |
61 | 573 |
"rehash is done by re-adding all elements to a new empty set. |
574 |
Rehash is needed after a binaryRead, for example." |
|
1 | 575 |
|
95 | 576 |
|element oldKeyArray newKeyArray |
577 |
n "{ Class:SmallInteger }"| |
|
1 | 578 |
|
95 | 579 |
oldKeyArray := keyArray. |
580 |
n := oldKeyArray size. |
|
581 |
keyArray := newKeyArray := self keyContainerOfSize:n. |
|
582 |
||
2 | 583 |
1 to:n do:[:index | |
159 | 584 |
element := oldKeyArray at:index. |
585 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
|
586 |
"cannot be already there" |
|
587 |
newKeyArray basicAt:(self findNil:element) put:element |
|
588 |
]. |
|
1 | 589 |
] |
590 |
! |
|
591 |
||
592 |
rehashFrom:startIndex |
|
61 | 593 |
"rehash elements starting at index - after a remove. |
594 |
Notice: due to the new implementation of remove, |
|
159 | 595 |
this is no longer needed" |
1 | 596 |
|
3 | 597 |
|element i "{ Class:SmallInteger }" |
598 |
length |
|
1 | 599 |
index "{ Class:SmallInteger }" | |
600 |
||
13 | 601 |
length := keyArray basicSize. |
1 | 602 |
index := startIndex. |
13 | 603 |
element := keyArray basicAt:index. |
1 | 604 |
[element notNil] whileTrue:[ |
159 | 605 |
i := self findNil:element. |
606 |
i == index ifTrue:[ |
|
607 |
^ self |
|
608 |
]. |
|
609 |
keyArray basicAt:i put:element. |
|
610 |
keyArray basicAt:index put:nil. |
|
1 | 611 |
|
159 | 612 |
index == length ifTrue:[ |
613 |
index := 1 |
|
614 |
] ifFalse:[ |
|
615 |
index := index + 1. |
|
616 |
]. |
|
617 |
element := keyArray basicAt:index. |
|
1 | 618 |
] |
619 |
! |
|
620 |
||
609 | 621 |
setTally:count |
622 |
"initialize the contents array (for at least count slots) |
|
623 |
and set tally to zero. |
|
624 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 625 |
|
609 | 626 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:count). |
627 |
tally := 0 |
|
1 | 628 |
! ! |
629 |
||
630 |
!Set methodsFor:'testing'! |
|
631 |
||
362 | 632 |
capacity |
633 |
"return the number of elements, that the receiver is |
|
634 |
prepared to take. |
|
635 |
Not used by the system; added for ST-80 compatibility." |
|
636 |
||
637 |
^ keyArray size |
|
638 |
! |
|
639 |
||
1 | 640 |
includes:anObject |
641 |
"return true if the argument anObject is in the receiver" |
|
642 |
||
362 | 643 |
^ (self find:anObject ifAbsent:0) ~~ 0 |
1 | 644 |
! |
645 |
||
646 |
isEmpty |
|
647 |
"return true if the receiver is empty" |
|
648 |
||
649 |
^ tally == 0 |
|
650 |
! |
|
651 |
||
609 | 652 |
isFixedSize |
653 |
"return true if the receiver cannot grow - this will vanish once |
|
654 |
Arrays and Strings learn how to grow ..." |
|
655 |
||
656 |
^ false |
|
657 |
! |
|
658 |
||
1 | 659 |
occurrencesOf:anObject |
660 |
"return the number of occurrences of anObject in the receiver" |
|
661 |
||
362 | 662 |
(self find:anObject ifAbsent:0) == 0 ifTrue:[^ 0]. |
1 | 663 |
^ 1 |
664 |
! |
|
665 |
||
609 | 666 |
size |
667 |
"return the number of set elements" |
|
1 | 668 |
|
609 | 669 |
^ tally |
1 | 670 |
! ! |
671 |
||
1643
577e35f5a046
oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents:
1450
diff
changeset
|
672 |
!Set class methodsFor:'documentation'! |
635 | 673 |
|
674 |
version |
|
1975 | 675 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.40 1996-11-12 15:13:57 cg Exp $' |
635 | 676 |
! ! |
609 | 677 |
Set initialize! |