author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sat, 19 Jan 2013 01:30:00 +0000 | |
branch | jv |
changeset 18011 | deb0c3355881 |
parent 17966 | 8b5df02e171f |
parent 14656 | f5cab05e380f |
child 18037 | 4cf874da38c9 |
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 |
" |
|
5471
a57eeb01c5ab
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5335
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
a57eeb01c5ab
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5335
diff
changeset
|
13 |
|
1 | 14 |
Collection subclass:#Set |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
15 |
instanceVariableNames:'tally keyArray' |
14314 | 16 |
classVariableNames:'DeletedEntry NilEntry' |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
17 |
poolDictionaries:'' |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
18 |
category:'Collections-Unordered' |
1 | 19 |
! |
20 |
||
2272 | 21 |
Object subclass:#EmptySlot |
2259 | 22 |
instanceVariableNames:'' |
4017
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
23 |
classVariableNames:'TheOneAndOnlyInstance' |
2259 | 24 |
poolDictionaries:'' |
25 |
privateIn:Set |
|
26 |
! |
|
27 |
||
14314 | 28 |
Object subclass:#NilKey |
29 |
instanceVariableNames:'' |
|
30 |
classVariableNames:'TheOneAndOnlyInstance' |
|
31 |
poolDictionaries:'' |
|
32 |
privateIn:Set |
|
33 |
! |
|
34 |
||
2259 | 35 |
!Set class methodsFor:'documentation'! |
88 | 36 |
|
37 |
copyright |
|
38 |
" |
|
39 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
159 | 40 |
All Rights Reserved |
1 | 41 |
|
88 | 42 |
This software is furnished under a license and may be used |
43 |
only in accordance with the terms of that license and with the |
|
44 |
inclusion of the above copyright notice. This software may not |
|
45 |
be provided or otherwise made available to, or used by, any |
|
46 |
other person. No title to or ownership of the software is |
|
47 |
hereby transferred. |
|
48 |
" |
|
49 |
! |
|
1 | 50 |
|
88 | 51 |
documentation |
52 |
" |
|
53 |
a Set is a collection where each element occurs at most once. |
|
14300 | 54 |
The inclusion test is done using equality (=) for comparison; |
95 | 55 |
see IdentitySet for sets using identity compare. |
6191 | 56 |
Keep in mind, that a regular Set therefore treats 3.0 and 3 as equal |
57 |
and therefore: |
|
14300 | 58 |
(Set with:3.0) includes:3 |
6191 | 59 |
will return true (since 3.0 and 3 are equal). |
60 |
In contrast, an IdentitySet will return false, because 3.0 and 3 are not |
|
61 |
identical. |
|
62 |
||
95 | 63 |
Sets use hashing for fast access, this access is considerably faster, |
64 |
if a good hash-number is returned by the elements. |
|
362 | 65 |
|
66 |
Notice that the default hash (Object>>hash) is not perfect; due to |
|
67 |
the implementation of hash-keys in ST/X, increased hash collisions |
|
68 |
are to be expected for large sets (say: > 20000 element). |
|
69 |
If your objects are heavyly used in sets or dictionaries, and you need |
|
70 |
big collections, your instances may provide a better hash values. |
|
95 | 71 |
|
345 | 72 |
Performance hints: |
657 | 73 |
If only symbols or smallIntegers are used as keys, |
362 | 74 |
use an instance of IdentitySet for slightly better performance, |
75 |
since both hashing and comparison is faster. |
|
345 | 76 |
|
77 |
If you have a rough idea how big the set is going to grow, |
|
78 |
create it using #new: instead of #new. Even if the size given is a |
|
79 |
poor guess (say half of the real size), there is some 20-30% performance |
|
14300 | 80 |
win to expect, since many resizing operations of the set are avoided |
81 |
(resizing is expensive, as the set does a rehash). |
|
345 | 82 |
|
95 | 83 |
Examples: |
84 |
||
14300 | 85 |
|s| |
86 |
s := Set new. |
|
87 |
s add:'hello'. |
|
88 |
s add:'world'. |
|
89 |
s add:#foo. |
|
90 |
s add:1.2345678. |
|
91 |
s add:'hello'. |
|
95 | 92 |
|
14300 | 93 |
s printCR. |
94 |
's size -> ' print. s size printCR. |
|
95 |
'(s includes:''hello'') -> ' print. (s includes:'hello') printCR. |
|
96 |
'(s includes:#foo) -> ' print. (s includes:#foo) printCR. |
|
97 |
'(s includes:''foo'') -> ' print. (s includes:'foo') printCR. |
|
98 |
'(s includes:#bar) -> ' print. (s includes:#bar) printCR. |
|
1290 | 99 |
|
100 |
[author:] |
|
14300 | 101 |
Claus Gittinger |
88 | 102 |
" |
103 |
! ! |
|
1 | 104 |
|
2259 | 105 |
!Set class methodsFor:'initialization'! |
41 | 106 |
|
107 |
initialize |
|
108 |
"initialize the Set class" |
|
109 |
||
110 |
DeletedEntry isNil ifTrue:[ |
|
14314 | 111 |
DeletedEntry := EmptySlot new. |
112 |
NilEntry := NilKey new. |
|
41 | 113 |
]. |
114 |
||
14314 | 115 |
" |
116 |
Set initialize |
|
117 |
" |
|
2259 | 118 |
|
119 |
"Modified: 24.1.1997 / 21:09:00 / cg" |
|
41 | 120 |
! ! |
121 |
||
2259 | 122 |
!Set class methodsFor:'instance creation'! |
1 | 123 |
|
6083 | 124 |
decodeFromLiteralArray:anArray |
125 |
"create & return a new instance from information encoded in anArray." |
|
126 |
||
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
127 |
|set |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
128 |
sz "{ Class: SmallInteger }"| |
6083 | 129 |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
130 |
sz := anArray size. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
131 |
set := self new:sz-1. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
132 |
2 to:sz do:[:idx| set add:(anArray at:idx) decodeAsLiteralArray]. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
133 |
^ set |
6083 | 134 |
|
135 |
" |
|
136 |
(Set with:1234 |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
137 |
with:(1 @ 2) |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8606
diff
changeset
|
138 |
with:'hello' |
6083 | 139 |
) literalArrayEncoding decodeAsLiteralArray |
140 |
" |
|
141 |
! |
|
142 |
||
1 | 143 |
new |
144 |
"return a new empty Set" |
|
145 |
||
146 |
^ self new:7 |
|
147 |
! |
|
148 |
||
149 |
new:anInteger |
|
150 |
"return a new empty Set with space for anInteger elements" |
|
151 |
||
159 | 152 |
" |
153 |
make it somewhat bigger; hashing works better if fill grade is |
|
154 |
below 10% (make it 75% here ..) |
|
155 |
" |
|
156 |
^ self basicNew setTally:(anInteger * 4 // 3) |
|
157 |
! ! |
|
158 |
||
2259 | 159 |
!Set class methodsFor:'queries'! |
252 | 160 |
|
161 |
goodSizeFrom:arg |
|
162 |
"return a good array size for the given argument. |
|
163 |
Returns the next prime after arg, since prime sizes are good for hashing." |
|
164 |
||
302 | 165 |
|n| |
166 |
||
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
167 |
"/ arg <= 11 ifTrue:[^ 11]. |
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
168 |
"/ n := arg * 3 // 2. |
252 | 169 |
|
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
170 |
arg <= 7 ifTrue:[^ 7]. |
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
171 |
n := arg. |
302 | 172 |
|
252 | 173 |
" |
174 |
mhmh - this returns good numbers for collections with up-to about |
|
175 |
500k elements; if you have bigger ones, add some more primes here ... |
|
176 |
" |
|
302 | 177 |
n <= 524288 ifTrue:[ |
9318 | 178 |
"2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288" |
179 |
^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit) |
|
252 | 180 |
]. |
2461
2d65e1845502
minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents:
2459
diff
changeset
|
181 |
|
9318 | 182 |
^ n nextPrime |
252 | 183 |
! ! |
184 |
||
3048 | 185 |
!Set class methodsFor:'utilities'! |
186 |
||
187 |
rehashAllSubInstances |
|
188 |
"rehash all sets & dictionaries. |
|
189 |
Useful utility if hash/identityHash method was changed |
|
190 |
of some object which is known to be kept in a set" |
|
191 |
||
192 |
self allSubInstances do:[:s | s rehash] |
|
193 |
||
194 |
" |
|
195 |
Set rehashAllSubInstances |
|
196 |
" |
|
197 |
||
198 |
"Created: / 24.10.1997 / 23:13:44 / cg" |
|
199 |
! ! |
|
200 |
||
7268 | 201 |
!Set methodsFor:'Compatibility-ST80'! |
6991
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
202 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
203 |
initialIndexFor:hashKey boundedBy:length |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
204 |
"for ST-80 compatibility only; it is (currently) not used in this |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
205 |
implementation of sets. Therefore, in ST/X it does not make sense |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
206 |
to redefine it. (which may be a bad design decision, but slightly |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
207 |
improves performance, by avoiding an extra message send ...)" |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
208 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
209 |
|index "{ Class:SmallInteger }"| |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
210 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
211 |
index := hashKey bitAnd:16r3FFFFFFF. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
212 |
index < 16r1FFFFFFF ifTrue:[ |
6994 | 213 |
index := index * 2 |
6991
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
214 |
]. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
215 |
index := index \\ length + 1. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
216 |
^ index. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
217 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
218 |
"Modified: 1.3.1997 / 01:01:13 / cg" |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
219 |
! ! |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
220 |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
221 |
!Set methodsFor:'Compatibility-Squeak'! |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
222 |
|
11052 | 223 |
copyWithout:elementToSkip |
224 |
"return a new collection consisting of a copy of the receiver, with |
|
225 |
ALL elements equal to elementToSkip are left out. |
|
226 |
No error is reported, if elementToSkip is not in the collection." |
|
227 |
||
228 |
^ self select:[:each | each ~= elementToSkip] |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
229 |
|
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
230 |
" |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
231 |
#(1 2 3 4 5 6 7) asSet copyWithout:5 |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
232 |
" |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
233 |
! |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
234 |
|
14019 | 235 |
copyWithoutAll:elementsToSkip |
236 |
"return a new collection consisting of a copy of the receiver, with |
|
237 |
ALL elements equal to elementToSkip are left out. |
|
238 |
No error is reported, if elementsToSkip is not in the collection." |
|
239 |
||
240 |
elementsToSkip size * self size > 10000 ifTrue:[ |
|
241 |
"speed up everything" |
|
242 |
^ self \ (elementsToSkip asSet). |
|
243 |
]. |
|
244 |
^ self \ elementsToSkip |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
245 |
|
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
246 |
" |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
247 |
#(1 2 3 4 5 6 7) asSet copyWithoutAll:#(3 5 7 9) |
14019 | 248 |
#(a b c d e f) asIdentitySet copyWithoutAll:#(d e f) |
249 |
#('a' 'b' 'c' 'd' 'e' 'f') asIdentitySet copyWithoutAll:#('d' 'e' 'f') |
|
250 |
#('a' 'b' 'c' 'd' 'e' 'f') asSet copyWithoutAll:#('d' 'e' 'f') |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
251 |
" |
12342 | 252 |
! |
253 |
||
254 |
like:anObject |
|
255 |
"Answer an object in the receiver that is equal to anObject, |
|
256 |
nil if no such object is found. |
|
257 |
Relies heavily on hash properties (i.e. that two object's hashes are equal |
|
258 |
if the two object compare equal)" |
|
259 |
||
260 |
^ self elementAt:anObject ifAbsent:[ nil ] |
|
261 |
||
262 |
" |
|
263 |
(Set withAll:#(10.0 20.0 30.0 40.0)) like:20 |
|
264 |
" |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
265 |
! ! |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
266 |
|
609 | 267 |
!Set methodsFor:'accessing'! |
268 |
||
12734 | 269 |
addFirst:anObject |
270 |
"add the argument, anObject to the receiver. |
|
271 |
If the receiver is ordered, the new element will be added at the beginning. |
|
272 |
An error is raised here - it does not make sense for unordered collections" |
|
273 |
||
274 |
^ self shouldNotImplement |
|
275 |
! |
|
276 |
||
609 | 277 |
at:index |
278 |
"report an error: at: is not allowed for Sets" |
|
279 |
||
280 |
^ self errorNotKeyed |
|
281 |
! |
|
282 |
||
283 |
at:index put:anObject |
|
284 |
"report an error: at:put: is not allowed for Sets" |
|
285 |
||
286 |
^ self errorNotKeyed |
|
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
287 |
! |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
288 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
289 |
elementAt:anObject |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
290 |
"return the element, if contained in the set. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
291 |
If there is none, report an error. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
292 |
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
|
293 |
non-identitysets, to find an existing element, for a |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
294 |
given equal (but not identical) object. |
12342 | 295 |
This is the same functionality as provided by the goodies/KeyedSet goody." |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
296 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
297 |
^ self elementAt:anObject ifAbsent:[^ self errorNotFound]. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
298 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
299 |
"Modified: 20.3.1997 / 20:35:24 / cg" |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
300 |
! |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
301 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
302 |
elementAt:anObject ifAbsent:exceptionBlock |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
303 |
"return the element, if contained in the set. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
304 |
If there is none, return the result from evaluating exceptionBlock. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
305 |
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
|
306 |
non-identitysets, to find an existing element, for a |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
307 |
given equal (but not identical) object. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
308 |
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
|
309 |
goody." |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
310 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
311 |
|index "{ Class: SmallInteger }" ret| |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
312 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
313 |
index := self find:(anObject ? NilEntry)ifAbsent:0. |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
314 |
index == 0 ifTrue:[ |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
315 |
^ exceptionBlock value |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
316 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
317 |
ret := keyArray basicAt:index. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
318 |
ret == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
319 |
ret := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
320 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
321 |
^ ret. |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
322 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
323 |
"Created: 20.3.1997 / 20:34:07 / cg" |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
324 |
"Modified: 20.3.1997 / 20:35:49 / cg" |
12734 | 325 |
! |
326 |
||
327 |
removeLast |
|
328 |
"remove the last element from the receiver. |
|
329 |
Return the removed element. |
|
330 |
An error is raised here - it does not make sense for unordered collections" |
|
331 |
||
332 |
^ self shouldNotImplement |
|
333 |
! |
|
334 |
||
335 |
reverseDo:aBlock |
|
336 |
"evaluate the argument, aBlock for each element in reverse order. |
|
337 |
An error is raised here - it does not make sense for unordered collections" |
|
338 |
||
339 |
^ self shouldNotImplement |
|
609 | 340 |
! ! |
341 |
||
342 |
!Set methodsFor:'adding & removing'! |
|
343 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
344 |
add:keyArg |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
345 |
"add the argument, anObject to the receiver. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
346 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
347 |
WARNING: do not add elements while iterating over the receiver. |
10868 | 348 |
Iterate over a copy to do this." |
609 | 349 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
350 |
|key index "{ Class: SmallInteger }"| |
609 | 351 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
352 |
keyArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
353 |
key := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
354 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
355 |
key := keyArg. |
5335
fae7e0698241
added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents:
5298
diff
changeset
|
356 |
]. |
609 | 357 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
358 |
index := self findKeyOrNil:key. |
5335
fae7e0698241
added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents:
5298
diff
changeset
|
359 |
(keyArray basicAt:index) isNil ifTrue:[ |
10868 | 360 |
"/ not already there |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
361 |
keyArray basicAt:index put:key. |
10868 | 362 |
tally := tally + 1. |
5335
fae7e0698241
added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents:
5298
diff
changeset
|
363 |
|
10868 | 364 |
self fullCheck. |
609 | 365 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
366 |
^ keyArg |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
367 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
368 |
"Modified: 30.1.1997 / 14:58:08 / cg" |
609 | 369 |
! |
370 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
371 |
remove:oldObjectArg ifAbsent:exceptionBlock |
609 | 372 |
"remove oldObject from the collection and return it. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
373 |
If it was not in the collection return the value of exceptionBlock. |
6191 | 374 |
Notice, that the returned object could be non-identical to the argument |
375 |
(although it will always be equal). |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
376 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
377 |
WARNING: do not remove elements while iterating over the receiver. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
378 |
See #saveRemove: to do this." |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
379 |
|
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
380 |
|oldObject index next removedObject| |
609 | 381 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
382 |
oldObjectArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
383 |
oldObject := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
384 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
385 |
oldObject := oldObjectArg. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
386 |
]. |
3265
18d96c910653
handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents:
3261
diff
changeset
|
387 |
|
609 | 388 |
index := self find:oldObject ifAbsent:0. |
389 |
index == 0 ifTrue:[^ exceptionBlock value]. |
|
390 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
391 |
removedObject := keyArray basicAt:index. |
609 | 392 |
keyArray basicAt:index put:nil. |
393 |
tally := tally - 1. |
|
394 |
tally == 0 ifTrue:[ |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
395 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). |
609 | 396 |
] ifFalse:[ |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
397 |
index == keyArray basicSize ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
398 |
next := 1 |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
399 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
400 |
next := index + 1. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
401 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
402 |
(keyArray basicAt:next) notNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
403 |
keyArray basicAt:index put:DeletedEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
404 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
405 |
self emptyCheck |
609 | 406 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
407 |
removedObject == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
408 |
removedObject := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
409 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
410 |
^ removedObject |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
411 |
|
6191 | 412 |
"Modified: / 16.11.2001 / 10:14:24 / cg" |
609 | 413 |
! |
414 |
||
415 |
removeAll |
|
1164 | 416 |
"remove all elements from the receiver. Returns the receiver." |
609 | 417 |
|
418 |
self setTally:7. |
|
1164 | 419 |
|
420 |
"Modified: 12.4.1996 / 13:35:06 / cg" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
421 |
! |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
422 |
|
14319 | 423 |
saveRemove:oldObject |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
424 |
"remove the element, oldObject from the collection. |
6191 | 425 |
Return the element |
426 |
(could be non-identical to oldObject, since I hash on equality, not on identity). |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
427 |
If it was not in the collection return nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
428 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
429 |
In contrast to #remove:, this does not resize the underlying collection |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
430 |
and therefore does NOT rehash & change the elements order. |
14319 | 431 |
Therefor this can be used while enumerating the receiver, |
432 |
which is not possible if #remove: is used. |
|
433 |
||
434 |
WARNING: since no resizing is done, the physical amount of memory used |
|
435 |
by the container remains the same, although the logical size shrinks. |
|
436 |
You may want to manually resize the receiver using #emptyCheck. |
|
437 |
(after the loop)" |
|
438 |
||
439 |
^ self saveRemove:oldObject ifAbsent:[]. |
|
440 |
||
441 |
"Created: / 16.11.2001 / 10:23:48 / cg" |
|
442 |
"Modified: / 16.11.2001 / 10:24:03 / cg" |
|
443 |
! |
|
444 |
||
445 |
saveRemove:oldObjectArg ifAbsent:exceptionValueProvider |
|
446 |
"remove the element, oldObject from the collection. |
|
447 |
Return the element |
|
448 |
(could be non-identical to oldObject, since I hash on equality, not on identity). |
|
449 |
If it was not in the collection return the value of exceptionValueProvider. |
|
450 |
||
451 |
In contrast to #remove:, this does not resize the underlying collection |
|
452 |
and therefore does NOT rehash & change the elements order. |
|
12963 | 453 |
Therefore this can be used while enumerating the receiver, |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
454 |
which is not possible if #remove: is used. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
455 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
456 |
WARNING: since no resizing is done, the physical amount of memory used |
12963 | 457 |
by the container remains the same, although the logical size shrinks. |
458 |
You may want to manually resize the receiver using #emptyCheck. |
|
459 |
(after the loop)" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
460 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
461 |
|oldObject |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
462 |
index "{ Class:SmallInteger }" |
6191 | 463 |
next "{ Class:SmallInteger }" |
464 |
removedObject| |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
465 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
466 |
oldObjectArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
467 |
oldObject := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
468 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
469 |
oldObject := oldObjectArg. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
470 |
]. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
471 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
472 |
index := self find:oldObject ifAbsent:0. |
14319 | 473 |
index == 0 ifTrue:[^ exceptionValueProvider value]. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
474 |
|
6191 | 475 |
removedObject := keyArray basicAt:index. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
476 |
keyArray basicAt:index put:nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
477 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
478 |
tally := tally - 1. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
479 |
tally ~~ 0 ifTrue:[ |
12963 | 480 |
index == keyArray basicSize ifTrue:[ |
481 |
next := 1 |
|
482 |
] ifFalse:[ |
|
483 |
next := index + 1. |
|
484 |
]. |
|
485 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
486 |
keyArray basicAt:index put:DeletedEntry |
|
487 |
]. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
488 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
489 |
removedObject == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
490 |
removedObject := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
491 |
]. |
6191 | 492 |
^ removedObject |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
493 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
494 |
"does NOT work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
495 |
|
12963 | 496 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
497 |
|
12963 | 498 |
s := Set new. |
499 |
s add:1. |
|
500 |
s add:2. |
|
501 |
s add:3. |
|
502 |
s add:4. |
|
503 |
s add:5. |
|
504 |
s add:6. |
|
505 |
s add:7. |
|
506 |
s add:8. |
|
507 |
s add:9. |
|
508 |
s do:[:v | |
|
509 |
v odd ifTrue:[ |
|
510 |
s remove:v |
|
511 |
] |
|
512 |
]. |
|
513 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
514 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
515 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
516 |
"DOES work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
517 |
|
12963 | 518 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
519 |
|
12963 | 520 |
s := Set new. |
521 |
s add:1. |
|
522 |
s add:2. |
|
523 |
s add:3. |
|
524 |
s add:4. |
|
525 |
s add:5. |
|
526 |
s add:6. |
|
527 |
s add:7. |
|
528 |
s add:8. |
|
529 |
s add:9. |
|
530 |
s do:[:v | |
|
531 |
v odd ifTrue:[ |
|
532 |
s saveRemove:v |
|
533 |
] |
|
534 |
]. |
|
535 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
536 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
537 |
|
6191 | 538 |
"Created: / 1.3.1996 / 21:14:26 / cg" |
539 |
"Modified: / 16.11.2001 / 10:22:59 / cg" |
|
10588 | 540 |
! |
541 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
542 |
testAndAdd:keyArg |
10588 | 543 |
"add the argument, anObject to the receiver. |
544 |
Answer true, if the element did already exist in the collection, |
|
545 |
false otherwise. |
|
546 |
||
547 |
WARNING: do not add elements while iterating over the receiver. |
|
548 |
Iterate over a copy to do this." |
|
549 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
550 |
|key index "{ Class: SmallInteger }"| |
10588 | 551 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
552 |
keyArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
553 |
key := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
554 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
555 |
key := keyArg. |
10588 | 556 |
]. |
557 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
558 |
index := self findKeyOrNil:key. |
10588 | 559 |
(keyArray basicAt:index) isNil ifTrue:[ |
560 |
"/ not already there |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
561 |
keyArray basicAt:index put:key. |
10588 | 562 |
tally := tally + 1. |
563 |
||
564 |
self fullCheck. |
|
565 |
^ false. |
|
566 |
]. |
|
567 |
^ true. |
|
568 |
||
569 |
"Modified: 30.1.1997 / 14:58:08 / cg" |
|
609 | 570 |
! ! |
571 |
||
11052 | 572 |
|
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
573 |
!Set methodsFor:'comparing'! |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
574 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
575 |
= aCollection |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
576 |
"return true, if the argument is a Set containing the same elements |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
577 |
as I do" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
578 |
|
14134 | 579 |
aCollection species ~~ self species ifTrue:[^ false]. |
580 |
aCollection size ~~ self size ifTrue:[^ false]. |
|
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
581 |
"/ same number of elements; since I am a Set, all of |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
582 |
"/ of my elements must be in the other collection ... |
14134 | 583 |
self do:[:eachElement | (aCollection includes:eachElement) ifFalse:[^ false]]. |
584 |
^ true. |
|
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
585 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
586 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
587 |
#(1 2 3 4 5) asSet = #(2 3 4 5 1) asSet |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
588 |
#(nil 1 2 3 4 5) asSet = #(2 3 4 5 1) asSet´ |
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
589 |
#(1 2 3 4 5) asSet = #(2 3 4 5 1.0) asSet |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
590 |
#(1 2 3 4 5) asSet = #(2 3 4 5 'one') asSet |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
591 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
592 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
593 |
|d1 d2| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
594 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
595 |
d1 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
596 |
d2 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
597 |
d1 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
598 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
599 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
600 |
d1 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
601 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
602 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
603 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
604 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
605 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
606 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
607 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
608 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
609 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
610 |
d1 = d2 |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
611 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
612 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
613 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
614 |
|d1 d2| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
615 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
616 |
d1 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
617 |
d2 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
618 |
d1 at:1 put:'uno'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
619 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
620 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
621 |
d1 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
622 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
623 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
624 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
625 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
626 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
627 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
628 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
629 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
630 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
631 |
d1 = d2 |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
632 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
633 |
|
10090
af5b9428e8ae
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9933
diff
changeset
|
634 |
"Modified: / 13-10-2006 / 12:58:43 / cg" |
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
635 |
! |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
636 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
637 |
hash |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
638 |
"return a hash key for the receiver" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
639 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
640 |
"this hash is stupid - but for larger collections, the hashing |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
641 |
time can become much bigger than the time lost in added probing. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
642 |
Time will show ... |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
643 |
Notice & warning: |
14134 | 644 |
if the #= method is ever changed to compare non-dictionaries equal, |
645 |
the code below must be changed to assert that the same hash-value is |
|
646 |
still returned. |
|
647 |
(which may be hard to acomplish) |
|
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
648 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
649 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
650 |
|mySize| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
651 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
652 |
mySize := self size. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
653 |
mySize == 0 ifTrue:[^ 0]. |
14134 | 654 |
^ (self anElement hash times:mySize) bitAnd:16r3FFFFFFF |
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
655 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
656 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
657 |
|d| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
658 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
659 |
d := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
660 |
d at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
661 |
d at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
662 |
d at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
663 |
d at:'two' put:2. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
664 |
d hash |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
665 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
666 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
667 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
668 |
|d| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
669 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
670 |
d := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
671 |
d at:1 put:'uno'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
672 |
d at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
673 |
d at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
674 |
d at:'two' put:2. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
675 |
d hash |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
676 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
677 |
! ! |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
678 |
|
159 | 679 |
!Set methodsFor:'copying'! |
680 |
||
681 |
postCopy |
|
682 |
"have to copy the keyArray too" |
|
683 |
||
684 |
keyArray := keyArray shallowCopy |
|
1 | 685 |
! ! |
686 |
||
609 | 687 |
!Set methodsFor:'enumerating'! |
1 | 688 |
|
609 | 689 |
do:aBlock |
1056 | 690 |
"perform the block for all members in the collection. |
691 |
||
692 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
693 |
Iterate over a copy to do this." |
13 | 694 |
|
609 | 695 |
|sz "{ Class: SmallInteger }" |
696 |
element| |
|
13 | 697 |
|
609 | 698 |
sz := keyArray size. |
699 |
1 to:sz do:[:index | |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
700 |
element := keyArray at:index. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
701 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
702 |
element == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
703 |
element := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
704 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
705 |
aBlock value:element |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
706 |
] |
609 | 707 |
] |
1056 | 708 |
|
709 |
"Modified: 1.3.1996 / 21:41:13 / cg" |
|
609 | 710 |
! ! |
61 | 711 |
|
969 | 712 |
|
7169 | 713 |
!Set methodsFor:'obsolete set operations'! |
714 |
||
715 |
+ aCollection |
|
8606 | 716 |
"Kept for backward compatibility. |
717 |
Use #union: instead, to isolate arithmethic and set operations" |
|
7169 | 718 |
|
719 |
<resource: #obsolete> |
|
720 |
||
721 |
^ self union:aCollection. |
|
722 |
! |
|
723 |
||
724 |
- aCollection |
|
8606 | 725 |
"Kept for backward compatibility. |
726 |
Use #\ instead, to isolate arithmethic and set operations" |
|
7169 | 727 |
|
728 |
<resource: #obsolete> |
|
729 |
||
730 |
^ self \ aCollection |
|
731 |
! ! |
|
732 |
||
609 | 733 |
!Set methodsFor:'private'! |
2 | 734 |
|
1 | 735 |
find:key ifAbsent:aBlock |
736 |
"Look for the key in the receiver. If it is found, return |
|
737 |
the index of the slot containing the key, otherwise |
|
738 |
return the value of evaluating aBlock." |
|
739 |
||
61 | 740 |
|index "{ Class:SmallInteger }" |
741 |
length "{ Class:SmallInteger }" |
|
742 |
startIndex probe| |
|
1 | 743 |
|
13 | 744 |
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
|
745 |
|
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
|
746 |
"/ |
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
|
747 |
"/ length < 10 ifTrue:[ |
362 | 748 |
"/ "assuming, that for small collections the overhead of hashing |
749 |
"/ is larger ... maybe that proves wrong |
|
750 |
"/ (if overhead of comparing is higher)" |
|
751 |
"/ ^ 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
|
752 |
"/ ]. |
13 | 753 |
|
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
754 |
startIndex := index := self initialIndexForKey:key. |
1 | 755 |
|
756 |
[true] whileTrue:[ |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
757 |
probe := (keyArray basicAt:index). |
13279 | 758 |
(probe notNil and:[probe ~~ DeletedEntry and:[key = probe]]) ifTrue:[^ index]. |
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
759 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
1 | 760 |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
761 |
index == length ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
762 |
index := 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
763 |
] ifFalse:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
764 |
index := index + 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
765 |
]. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
766 |
index == startIndex ifTrue:[^ aBlock value]. |
1 | 767 |
] |
13279 | 768 |
|
769 |
"Modified: / 03-02-2011 / 13:53:18 / sr" |
|
1 | 770 |
! |
771 |
||
13 | 772 |
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
|
773 |
"Look for the key in the receiver. |
3082 | 774 |
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
|
775 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 776 |
|
61 | 777 |
|index "{ Class:SmallInteger }" |
778 |
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
|
779 |
startIndex probe |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
780 |
delIndex| |
1 | 781 |
|
13 | 782 |
length := keyArray basicSize. |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
783 |
startIndex := index := self initialIndexForKey:key. |
1 | 784 |
|
785 |
[true] whileTrue:[ |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
786 |
probe := keyArray basicAt:index. |
13311 | 787 |
(probe notNil and:[probe ~~ DeletedEntry and:[key = probe]]) ifTrue:[^ index]. |
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
788 |
(self slotIsEmpty:probe) ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
789 |
delIndex isNil ifTrue:[^ index]. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
790 |
keyArray basicAt:delIndex put:nil. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
791 |
^ delIndex |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
792 |
]. |
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
|
793 |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
794 |
probe == DeletedEntry ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
795 |
delIndex isNil ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
796 |
delIndex := index |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
797 |
] |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
798 |
]. |
1 | 799 |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
800 |
index == length ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
801 |
index := 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
802 |
] ifFalse:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
803 |
index := index + 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
804 |
]. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
805 |
index == startIndex ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
806 |
delIndex notNil ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
807 |
keyArray basicAt:delIndex put:nil. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
808 |
^ delIndex |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
809 |
]. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
810 |
^ self grow findKeyOrNil:key |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
811 |
]. |
1 | 812 |
] |
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
|
813 |
|
13311 | 814 |
"Modified: / 27-02-2011 / 15:30:42 / cg" |
1 | 815 |
! |
816 |
||
817 |
findNil:key |
|
2475 | 818 |
"Look for the next slot usable for key. |
819 |
WARNING: |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
820 |
This method assumes that key is not already in the receiver |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
821 |
AND that keyArray does not have previously removed entries |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
822 |
AND that there is an empty slot. |
2475 | 823 |
To be used ONLY while growing/rehashing to enter elements into a fresh |
824 |
collection - if any of the above conditions is not met, the method |
|
825 |
loops forever." |
|
1 | 826 |
|
2475 | 827 |
|index "{ Class:SmallInteger }" |
61 | 828 |
length "{ Class:SmallInteger }"| |
1 | 829 |
|
13 | 830 |
length := keyArray basicSize. |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
831 |
index := self initialIndexForKey:key. |
1 | 832 |
|
5772 | 833 |
"/ index := keyArray identityIndexOf:nil startingAt:index. |
834 |
"/ index == 0 ifTrue:[ |
|
835 |
"/ index := keyArray identityIndexOf:nil startingAt:1 endingAt:index. |
|
836 |
"/ ]. |
|
837 |
"/ that code is completely inlined by stc ... |
|
838 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
839 |
index == length ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
840 |
index := 1 |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
841 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
842 |
index := index + 1 |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
843 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
844 |
"notice: no check for no nil found - we must find one since |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
845 |
this is only called after growing" |
1 | 846 |
]. |
847 |
^ index |
|
1055 | 848 |
|
2475 | 849 |
"Modified: 21.3.1997 / 10:32:58 / cg" |
1 | 850 |
! |
851 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
852 |
hashFor:aKey |
6991
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
853 |
"return the arguments hash value. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
854 |
Redefined in subclasses, which use a different comparison (i.e. identity compare)" |
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
855 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
856 |
^ aKey hash |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
857 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
858 |
"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
|
859 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
860 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
861 |
initialIndexForKey:aKey |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
862 |
"return an initial index given a key." |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
863 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
864 |
|index "{ Class:SmallInteger }" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
865 |
length "{ Class:SmallInteger }"| |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
866 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
867 |
length := keyArray basicSize. |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
868 |
index := (self hashFor:aKey) bitAnd:16r3FFFFFFF. |
14656 | 869 |
|
870 |
"multiply by a large prime to spread the keys over the whole range. |
|
871 |
This causes maxChainLength to reduce from about 2000 to 6 for an |
|
872 |
IdentitySet of 9000 Smalltalk classes" |
|
873 |
index := index times:31415821. |
|
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
874 |
index := index \\ length + 1. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
875 |
^ index. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
876 |
|
14656 | 877 |
" |
878 |
Smalltalk allClasses maxChainLength |
|
879 |
" |
|
880 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
881 |
"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
|
882 |
"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
|
883 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
884 |
|
5845
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
885 |
invalidElementError |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
886 |
"this is send when a nil is used with a Set. |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
887 |
For now this is just an info-Message. Will become an error sometimes." |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
888 |
|
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
889 |
"/ self elementBoundError. |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
890 |
|
9086 | 891 |
InfoPrinting ifTrue:[ |
892 |
'Set [warning]: nil is not a valid element - will be an error in later versions' errorPrintCR. |
|
11472 | 893 |
Context showWhereWeCameFrom. |
6540
cf1a6a1a8645
better invalidElementError message
Claus Gittinger <cg@exept.de>
parents:
6519
diff
changeset
|
894 |
]. |
5845
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
895 |
! |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
896 |
|
3261
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
897 |
keyArray |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
898 |
^ keyArray |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
899 |
|
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
900 |
"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
|
901 |
! |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
902 |
|
609 | 903 |
keyContainerOfSize:n |
904 |
"return a container for keys of size n. |
|
905 |
Extracted to make life of weak subclasses easier ..." |
|
906 |
||
907 |
^ Array basicNew:n |
|
908 |
! |
|
909 |
||
1 | 910 |
rehash |
61 | 911 |
"rehash is done by re-adding all elements to a new empty set. |
912 |
Rehash is needed after a binaryRead, for example." |
|
1 | 913 |
|
3082 | 914 |
|element oldKeyArray newKeyArray newIdx |
95 | 915 |
n "{ Class:SmallInteger }"| |
1 | 916 |
|
95 | 917 |
oldKeyArray := keyArray. |
918 |
n := oldKeyArray size. |
|
919 |
keyArray := newKeyArray := self keyContainerOfSize:n. |
|
920 |
||
2 | 921 |
1 to:n do:[:index | |
6994 | 922 |
element := oldKeyArray at:index. |
923 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
|
924 |
"cannot be already there" |
|
925 |
newIdx := self findNil:element. |
|
926 |
newKeyArray basicAt:newIdx put:element |
|
927 |
]. |
|
1 | 928 |
] |
3082 | 929 |
|
930 |
"Modified: / 30.10.1997 / 16:04:46 / cg" |
|
1 | 931 |
! |
932 |
||
933 |
rehashFrom:startIndex |
|
61 | 934 |
"rehash elements starting at index - after a remove. |
935 |
Notice: due to the new implementation of remove, |
|
159 | 936 |
this is no longer needed" |
1 | 937 |
|
3 | 938 |
|element i "{ Class:SmallInteger }" |
939 |
length |
|
1 | 940 |
index "{ Class:SmallInteger }" | |
941 |
||
13 | 942 |
length := keyArray basicSize. |
1 | 943 |
index := startIndex. |
13 | 944 |
element := keyArray basicAt:index. |
1 | 945 |
[element notNil] whileTrue:[ |
159 | 946 |
i := self findNil:element. |
947 |
i == index ifTrue:[ |
|
948 |
^ self |
|
949 |
]. |
|
950 |
keyArray basicAt:i put:element. |
|
951 |
keyArray basicAt:index put:nil. |
|
1 | 952 |
|
159 | 953 |
index == length ifTrue:[ |
954 |
index := 1 |
|
955 |
] ifFalse:[ |
|
956 |
index := index + 1. |
|
957 |
]. |
|
958 |
element := keyArray basicAt:index. |
|
1 | 959 |
] |
960 |
! |
|
961 |
||
609 | 962 |
setTally:count |
963 |
"initialize the contents array (for at least count slots) |
|
964 |
and set tally to zero. |
|
965 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 966 |
|
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
967 |
|n| |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
968 |
|
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
969 |
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
|
970 |
n == keyArray size ifTrue:[ |
6994 | 971 |
keyArray atAllPut:nil. |
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
972 |
] ifFalse:[ |
6994 | 973 |
keyArray := self keyContainerOfSize:n. |
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
974 |
]. |
609 | 975 |
tally := 0 |
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
976 |
|
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
977 |
"Modified: / 4.8.1998 / 22:07:25 / cg" |
6233 | 978 |
! |
979 |
||
980 |
slotIsEmpty:aSlotValue |
|
981 |
"only redefined in weak subclasses, since they treat a 0-value |
|
982 |
as being empty" |
|
983 |
||
984 |
^ aSlotValue isNil |
|
1 | 985 |
! ! |
986 |
||
7260 | 987 |
!Set methodsFor:'private-grow & shrink'! |
3082 | 988 |
|
989 |
emptyCheck |
|
990 |
"check if the receiver has become too empty (after a remove) |
|
991 |
and shrink if it makes sense. |
|
992 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
993 |
||
994 |
|sz "{Class: SmallInteger}" |
|
995 |
newSize "{Class: SmallInteger}" | |
|
996 |
||
997 |
sz := keyArray basicSize. |
|
998 |
sz > 56 ifTrue:[ |
|
6994 | 999 |
" |
1000 |
shrink if too empty |
|
1001 |
" |
|
1002 |
tally < (sz // 8) ifTrue:[ |
|
1003 |
newSize := sz // 4. |
|
1004 |
self grow:newSize |
|
1005 |
] |
|
3082 | 1006 |
] |
1007 |
||
1008 |
"Modified: 19.3.1997 / 16:02:55 / cg" |
|
1009 |
! |
|
1010 |
||
1011 |
fullCheck |
|
1012 |
"check if collection is full (after an add); grow if so. |
|
1013 |
Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'" |
|
1014 |
||
1015 |
|sz "{Class: SmallInteger}" | |
|
1016 |
||
1017 |
" |
|
1018 |
grow if filled more than 75% |
|
1019 |
" |
|
1020 |
sz := keyArray basicSize. |
|
1021 |
tally > (sz * 3 // 4) ifTrue:[ |
|
1022 |
self grow |
|
1023 |
] |
|
1024 |
! |
|
1025 |
||
1026 |
grow |
|
1027 |
"change the number of element slots of the collection to a useful |
|
1028 |
new size" |
|
1029 |
||
1030 |
self grow:(keyArray basicSize * 2) |
|
1031 |
! |
|
1032 |
||
1033 |
grow:newSize |
|
1034 |
"change the number of element slots of the collection - to do this, |
|
1035 |
we have to rehash (which is done by re-adding all elements to a new |
|
1036 |
empty set)." |
|
1037 |
||
1038 |
|elem oldKeyArray newKeyArray deletedEntry |
|
1039 |
containerSize oldSize "{ Class:SmallInteger }"| |
|
1040 |
||
1041 |
oldKeyArray := keyArray. |
|
1042 |
oldSize := oldKeyArray size. |
|
1043 |
containerSize := (self class goodSizeFrom:newSize). |
|
1044 |
containerSize == oldSize ifTrue:[^ self]. |
|
1045 |
||
1046 |
keyArray := newKeyArray := self keyContainerOfSize:containerSize. |
|
1047 |
||
1048 |
deletedEntry := DeletedEntry. |
|
1049 |
1 to:oldSize do:[:srcIndex | |
|
1050 |
elem := oldKeyArray basicAt:srcIndex. |
|
1051 |
(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[ |
|
1052 |
"cannot be already there" |
|
1053 |
newKeyArray basicAt:(self findNil:elem) put:elem |
|
1054 |
]. |
|
1055 |
]. |
|
1056 |
! ! |
|
1057 |
||
5557 | 1058 |
!Set methodsFor:'queries'! |
1059 |
||
8499 | 1060 |
collisionCount |
1061 |
"return the number of key collisions in the set. |
|
1062 |
There is a collision if two keys hash to the same value." |
|
1063 |
||
1064 |
|count| |
|
1065 |
||
1066 |
count := 0. |
|
1067 |
||
1068 |
keyArray do:[:eachKey| |
|
1069 |
(eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[ |
|
1070 |
count := count + (self collisionsFor:eachKey). |
|
1071 |
] |
|
1072 |
]. |
|
1073 |
||
1074 |
^ count |
|
14654 | 1075 |
|
1076 |
" |
|
1077 |
self allSubInstances |
|
1078 |
collect:[:each| each collisionCount -> each] |
|
1079 |
thenSelect:[:each| each key > 0] |
|
1080 |
" |
|
8499 | 1081 |
! |
1082 |
||
1083 |
collisionsFor:key |
|
1084 |
"Return the number of searches - 1 required for key" |
|
1085 |
||
1086 |
|index "{ Class:SmallInteger }" |
|
14654 | 1087 |
length "{ Class:SmallInteger }" startIndex probe count| |
8499 | 1088 |
|
1089 |
length := keyArray basicSize. |
|
14654 | 1090 |
startIndex := index := self initialIndexForKey:key. |
8499 | 1091 |
|
1092 |
count := 0. |
|
1093 |
[true] whileTrue:[ |
|
14654 | 1094 |
probe := keyArray basicAt:index. |
8499 | 1095 |
(probe notNil and:[key = probe]) ifTrue:[^ count]. |
1096 |
(self slotIsEmpty:probe) ifTrue:[self error:'non existing key']. |
|
1097 |
||
1098 |
index == length ifTrue:[ |
|
1099 |
index := 1. |
|
1100 |
] ifFalse:[ |
|
1101 |
index := index + 1. |
|
1102 |
]. |
|
1103 |
count := count + 1. |
|
1104 |
index == startIndex ifTrue:[self error:'non existing key']. |
|
1105 |
] |
|
1106 |
! |
|
1107 |
||
1108 |
maxChainLength |
|
1109 |
"return the number of the maximum chain length in the set. |
|
1110 |
This is the worst case overhead when accessing a key." |
|
1111 |
||
1112 |
|chainLength key| |
|
1113 |
||
1114 |
chainLength := 0. |
|
1115 |
||
1116 |
keyArray do:[:eachKey| |t| |
|
1117 |
(eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[ |
|
1118 |
t := self collisionsFor:eachKey. |
|
1119 |
chainLength < t ifTrue:[chainLength := t. key := eachKey]. |
|
1120 |
] |
|
1121 |
]. |
|
1122 |
||
1123 |
^ (key -> chainLength) |
|
1124 |
! |
|
1125 |
||
5557 | 1126 |
size |
1127 |
"return the number of set elements" |
|
1128 |
||
1129 |
^ tally |
|
1130 |
! ! |
|
5471
a57eeb01c5ab
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5335
diff
changeset
|
1131 |
|
14654 | 1132 |
|
1 | 1133 |
!Set methodsFor:'testing'! |
1134 |
||
362 | 1135 |
capacity |
1136 |
"return the number of elements, that the receiver is |
|
1137 |
prepared to take. |
|
1138 |
Not used by the system; added for ST-80 compatibility." |
|
1139 |
||
1140 |
^ keyArray size |
|
1141 |
! |
|
1142 |
||
1 | 1143 |
includes:anObject |
1144 |
"return true if the argument anObject is in the receiver" |
|
1145 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
1146 |
^ (self find:(anObject ? NilEntry) ifAbsent:0) ~~ 0 |
1 | 1147 |
! |
1148 |
||
1149 |
isEmpty |
|
1150 |
"return true if the receiver is empty" |
|
1151 |
||
1152 |
^ tally == 0 |
|
1153 |
! |
|
1154 |
||
609 | 1155 |
isFixedSize |
1156 |
"return true if the receiver cannot grow - this will vanish once |
|
1157 |
Arrays and Strings learn how to grow ..." |
|
1158 |
||
1159 |
^ false |
|
1160 |
! |
|
1161 |
||
2383 | 1162 |
notEmpty |
1163 |
"return true if the receiver is not empty" |
|
1164 |
||
1165 |
^ tally ~~ 0 |
|
1166 |
||
1167 |
"Created: 12.2.1997 / 12:39:02 / cg" |
|
1168 |
! |
|
1169 |
||
1 | 1170 |
occurrencesOf:anObject |
5048 | 1171 |
"return the number of occurrences of anObject in the receiver. |
6191 | 1172 |
As I am a Set, this can only return 0 or 1." |
1 | 1173 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
1174 |
(self find:(anObject ? NilEntry) ifAbsent:0) == 0 ifTrue:[^ 0]. |
1 | 1175 |
^ 1 |
6191 | 1176 |
|
1177 |
"Modified: / 16.11.2001 / 10:30:14 / cg" |
|
1 | 1178 |
! |
1179 |
||
4795 | 1180 |
sameContentsAs:aCollection |
1181 |
"answer true, if all the elements in self and aCollection |
|
1182 |
are common. This is not defined as #=, since we cannot redefine #hash |
|
1183 |
for aCollection." |
|
1184 |
||
1185 |
aCollection size ~~ self size ifTrue:[ |
|
10090
af5b9428e8ae
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9933
diff
changeset
|
1186 |
^ false |
4795 | 1187 |
]. |
1188 |
||
10090
af5b9428e8ae
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9933
diff
changeset
|
1189 |
^ aCollection conform:[:e | (self includes:e)] |
4795 | 1190 |
|
1191 |
" |
|
1192 |
#(1 2 3) asSet sameContentsAs: #(1 2 3) |
|
1193 |
#(1 2 3 4) asSet sameContentsAs: #(1 2 3) |
|
1194 |
#(1 2 3) asSet sameContentsAs: #(1 2 3 3) |
|
1195 |
#(1 2 3 'aa') asSet sameContentsAs: #(1 2 3 'aa') |
|
1196 |
#(1 2 3 'aa') asIdentitySet sameContentsAs: #(1 2 3 'aa') |
|
1197 |
#(1 2 3 #aa) asIdentitySet sameContentsAs: #(1 2 3 #aa) |
|
1198 |
" |
|
1199 |
||
10090
af5b9428e8ae
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9933
diff
changeset
|
1200 |
"Modified: / 13-10-2006 / 12:59:01 / cg" |
1 | 1201 |
! ! |
1202 |
||
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1203 |
!Set methodsFor:'visiting'! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1204 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1205 |
acceptVisitor:aVisitor with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1206 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1207 |
^ aVisitor visitSet:self with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1208 |
! ! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1209 |
|
4017
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1210 |
!Set::EmptySlot class methodsFor:'instance creation'! |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1211 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1212 |
basicNew |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1213 |
TheOneAndOnlyInstance isNil ifTrue:[ |
6994 | 1214 |
TheOneAndOnlyInstance := super basicNew |
4017
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1215 |
]. |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1216 |
^ TheOneAndOnlyInstance |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1217 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1218 |
! ! |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1219 |
|
14314 | 1220 |
!Set::NilKey class methodsFor:'instance creation'! |
1221 |
||
1222 |
basicNew |
|
1223 |
TheOneAndOnlyInstance isNil ifTrue:[ |
|
1224 |
TheOneAndOnlyInstance := super basicNew |
|
1225 |
]. |
|
1226 |
^ TheOneAndOnlyInstance |
|
1227 |
||
1228 |
||
1229 |
! ! |
|
1230 |
||
2259 | 1231 |
!Set class methodsFor:'documentation'! |
635 | 1232 |
|
1233 |
version |
|
14656 | 1234 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.117 2013-01-15 17:25:54 stefan Exp $' |
12342 | 1235 |
! |
1236 |
||
1237 |
version_CVS |
|
14656 | 1238 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.117 2013-01-15 17:25:54 stefan Exp $' |
635 | 1239 |
! ! |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
1240 |
|
14654 | 1241 |
|
609 | 1242 |
Set initialize! |