author | Stefan Vogel <sv@exept.de> |
Tue, 12 Nov 2013 19:14:08 +0100 | |
changeset 15806 | 62bc6058e34f |
parent 15036 | f86ace435ad0 |
child 15815 | 886aab04286d |
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 |
||
15806 | 201 |
|
7268 | 202 |
!Set methodsFor:'Compatibility-ST80'! |
6991
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
203 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
204 |
initialIndexFor:hashKey boundedBy:length |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
205 |
"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
|
206 |
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
|
207 |
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
|
208 |
improves performance, by avoiding an extra message send ...)" |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
209 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
210 |
|index "{ Class:SmallInteger }"| |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
211 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
212 |
index := hashKey bitAnd:16r3FFFFFFF. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
213 |
index < 16r1FFFFFFF ifTrue:[ |
6994 | 214 |
index := index * 2 |
6991
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
215 |
]. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
216 |
index := index \\ length + 1. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
217 |
^ index. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
218 |
|
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
219 |
"Modified: 1.3.1997 / 01:01:13 / cg" |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
220 |
! ! |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
221 |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
222 |
!Set methodsFor:'Compatibility-Squeak'! |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
223 |
|
11052 | 224 |
copyWithout:elementToSkip |
225 |
"return a new collection consisting of a copy of the receiver, with |
|
226 |
ALL elements equal to elementToSkip are left out. |
|
227 |
No error is reported, if elementToSkip is not in the collection." |
|
228 |
||
229 |
^ self select:[:each | each ~= elementToSkip] |
|
7311
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 |
" |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
232 |
#(1 2 3 4 5 6 7) asSet copyWithout:5 |
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 |
! |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
235 |
|
14019 | 236 |
copyWithoutAll:elementsToSkip |
237 |
"return a new collection consisting of a copy of the receiver, with |
|
238 |
ALL elements equal to elementToSkip are left out. |
|
239 |
No error is reported, if elementsToSkip is not in the collection." |
|
240 |
||
241 |
elementsToSkip size * self size > 10000 ifTrue:[ |
|
242 |
"speed up everything" |
|
243 |
^ self \ (elementsToSkip asSet). |
|
244 |
]. |
|
245 |
^ self \ elementsToSkip |
|
7311
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 |
" |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
248 |
#(1 2 3 4 5 6 7) asSet copyWithoutAll:#(3 5 7 9) |
14019 | 249 |
#(a b c d e f) asIdentitySet copyWithoutAll:#(d e f) |
250 |
#('a' 'b' 'c' 'd' 'e' 'f') asIdentitySet copyWithoutAll:#('d' 'e' 'f') |
|
251 |
#('a' 'b' 'c' 'd' 'e' 'f') asSet copyWithoutAll:#('d' 'e' 'f') |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
252 |
" |
12342 | 253 |
! |
254 |
||
255 |
like:anObject |
|
256 |
"Answer an object in the receiver that is equal to anObject, |
|
257 |
nil if no such object is found. |
|
258 |
Relies heavily on hash properties (i.e. that two object's hashes are equal |
|
259 |
if the two object compare equal)" |
|
260 |
||
261 |
^ self elementAt:anObject ifAbsent:[ nil ] |
|
262 |
||
263 |
" |
|
264 |
(Set withAll:#(10.0 20.0 30.0 40.0)) like:20 |
|
265 |
" |
|
7311
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
266 |
! ! |
6878116b0a27
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
7268
diff
changeset
|
267 |
|
609 | 268 |
!Set methodsFor:'accessing'! |
269 |
||
12734 | 270 |
addFirst:anObject |
271 |
"add the argument, anObject to the receiver. |
|
272 |
If the receiver is ordered, the new element will be added at the beginning. |
|
273 |
An error is raised here - it does not make sense for unordered collections" |
|
274 |
||
275 |
^ self shouldNotImplement |
|
276 |
! |
|
277 |
||
609 | 278 |
at:index |
279 |
"report an error: at: is not allowed for Sets" |
|
280 |
||
281 |
^ self errorNotKeyed |
|
282 |
! |
|
283 |
||
284 |
at:index put:anObject |
|
285 |
"report an error: at:put: is not allowed for Sets" |
|
286 |
||
287 |
^ self errorNotKeyed |
|
2469
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 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
290 |
elementAt:anObject |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
291 |
"return the element, if contained in the set. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
292 |
If there is none, report an error. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
293 |
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
|
294 |
non-identitysets, to find an existing element, for a |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
295 |
given equal (but not identical) object. |
12342 | 296 |
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
|
297 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
298 |
^ self elementAt:anObject ifAbsent:[^ self errorNotFound]. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
299 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
300 |
"Modified: 20.3.1997 / 20:35:24 / cg" |
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 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
303 |
elementAt:anObject ifAbsent:exceptionBlock |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
304 |
"return the element, if contained in the set. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
305 |
If there is none, return the result from evaluating exceptionBlock. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
306 |
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
|
307 |
non-identitysets, to find an existing element, for a |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
308 |
given equal (but not identical) object. |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
309 |
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
|
310 |
goody." |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
311 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
312 |
|index "{ Class: SmallInteger }" ret| |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
313 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
314 |
index := self find:(anObject ? NilEntry)ifAbsent:0. |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
315 |
index == 0 ifTrue:[ |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
316 |
^ exceptionBlock value |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
317 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
318 |
ret := keyArray basicAt:index. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
319 |
ret == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
320 |
ret := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
321 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
322 |
^ ret. |
2469
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
323 |
|
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
324 |
"Created: 20.3.1997 / 20:34:07 / cg" |
ecca86d50852
added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2461
diff
changeset
|
325 |
"Modified: 20.3.1997 / 20:35:49 / cg" |
12734 | 326 |
! |
327 |
||
328 |
removeLast |
|
329 |
"remove the last element from the receiver. |
|
330 |
Return the removed element. |
|
331 |
An error is raised here - it does not make sense for unordered collections" |
|
332 |
||
333 |
^ self shouldNotImplement |
|
334 |
! |
|
335 |
||
336 |
reverseDo:aBlock |
|
337 |
"evaluate the argument, aBlock for each element in reverse order. |
|
338 |
An error is raised here - it does not make sense for unordered collections" |
|
339 |
||
340 |
^ self shouldNotImplement |
|
609 | 341 |
! ! |
342 |
||
343 |
!Set methodsFor:'adding & removing'! |
|
344 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
345 |
add:keyArg |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
346 |
"add the argument, anObject to the receiver. |
14895 | 347 |
Return the added element. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
348 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
349 |
WARNING: do not add elements while iterating over the receiver. |
10868 | 350 |
Iterate over a copy to do this." |
609 | 351 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
352 |
|key index "{ Class: SmallInteger }"| |
609 | 353 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
354 |
keyArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
355 |
key := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
356 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
357 |
key := keyArg. |
5335
fae7e0698241
added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents:
5298
diff
changeset
|
358 |
]. |
609 | 359 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
360 |
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
|
361 |
(keyArray basicAt:index) isNil ifTrue:[ |
10868 | 362 |
"/ not already there |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
363 |
keyArray basicAt:index put:key. |
10868 | 364 |
tally := tally + 1. |
5335
fae7e0698241
added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents:
5298
diff
changeset
|
365 |
|
10868 | 366 |
self fullCheck. |
609 | 367 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
368 |
^ keyArg |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
369 |
|
2328
0fd1d715e5a9
oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents:
2323
diff
changeset
|
370 |
"Modified: 30.1.1997 / 14:58:08 / cg" |
609 | 371 |
! |
372 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
373 |
remove:oldObjectArg ifAbsent:exceptionBlock |
609 | 374 |
"remove oldObject from the collection and return it. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
375 |
If it was not in the collection return the value of exceptionBlock. |
6191 | 376 |
Notice, that the returned object could be non-identical to the argument |
377 |
(although it will always be equal). |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
378 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
379 |
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
|
380 |
See #saveRemove: to do this." |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
381 |
|
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
382 |
|oldObject index next removedObject| |
609 | 383 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
384 |
oldObjectArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
385 |
oldObject := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
386 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
387 |
oldObject := oldObjectArg. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
388 |
]. |
3265
18d96c910653
handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents:
3261
diff
changeset
|
389 |
|
609 | 390 |
index := self find:oldObject ifAbsent:0. |
391 |
index == 0 ifTrue:[^ exceptionBlock value]. |
|
392 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
393 |
removedObject := keyArray basicAt:index. |
609 | 394 |
keyArray basicAt:index put:nil. |
395 |
tally := tally - 1. |
|
396 |
tally == 0 ifTrue:[ |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
397 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). |
609 | 398 |
] ifFalse:[ |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
399 |
index == keyArray basicSize ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
400 |
next := 1 |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
401 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
402 |
next := index + 1. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
403 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
404 |
(keyArray basicAt:next) notNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
405 |
keyArray basicAt:index put:DeletedEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
406 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
407 |
self emptyCheck |
609 | 408 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
409 |
removedObject == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
410 |
removedObject := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
411 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
412 |
^ removedObject |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
413 |
|
6191 | 414 |
"Modified: / 16.11.2001 / 10:14:24 / cg" |
609 | 415 |
! |
416 |
||
417 |
removeAll |
|
1164 | 418 |
"remove all elements from the receiver. Returns the receiver." |
609 | 419 |
|
420 |
self setTally:7. |
|
1164 | 421 |
|
422 |
"Modified: 12.4.1996 / 13:35:06 / cg" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
423 |
! |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
424 |
|
15806 | 425 |
removeIdentical:oldObjectArg ifAbsent:exceptionBlock |
426 |
"remove oldObject from the collection and return it. |
|
427 |
If it was not in the collection return the value of exceptionBlock. |
|
428 |
Uses identity compare (==) to search for an occurrence. |
|
429 |
||
430 |
WARNING: do not remove elements while iterating over the receiver. |
|
431 |
See #saveRemove: to do this." |
|
432 |
||
433 |
|oldObject index next| |
|
434 |
||
435 |
oldObjectArg isNil ifTrue:[ |
|
436 |
oldObject := NilEntry. |
|
437 |
] ifFalse:[ |
|
438 |
oldObject := oldObjectArg. |
|
439 |
]. |
|
440 |
||
441 |
"first a quick check. |
|
442 |
There is a high possibility that objects, which are |
|
443 |
equal are also identical" |
|
444 |
index := self find:oldObject ifAbsent:0. |
|
445 |
index ~~ 0 ifTrue:[ |
|
446 |
oldObject ~~ (keyArray basicAt:index) ifTrue:[ |
|
447 |
index := 0. |
|
448 |
] |
|
449 |
]. |
|
450 |
index == 0 ifTrue:[ |
|
451 |
"have to go the long and hard path..." |
|
452 |
index := self findIdentical:oldObject ifAbsent:0. |
|
453 |
index == 0 ifTrue:[^ exceptionBlock value]. |
|
454 |
]. |
|
455 |
||
456 |
keyArray basicAt:index put:nil. |
|
457 |
tally := tally - 1. |
|
458 |
tally == 0 ifTrue:[ |
|
459 |
keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). |
|
460 |
] ifFalse:[ |
|
461 |
index == keyArray basicSize ifTrue:[ |
|
462 |
next := 1 |
|
463 |
] ifFalse:[ |
|
464 |
next := index + 1. |
|
465 |
]. |
|
466 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
467 |
keyArray basicAt:index put:DeletedEntry. |
|
468 |
]. |
|
469 |
self emptyCheck |
|
470 |
]. |
|
471 |
^ oldObjectArg |
|
472 |
! |
|
473 |
||
14319 | 474 |
saveRemove:oldObject |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
475 |
"remove the element, oldObject from the collection. |
6191 | 476 |
Return the element |
477 |
(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
|
478 |
If it was not in the collection return nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
479 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
480 |
In contrast to #remove:, this does not resize the underlying collection |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
481 |
and therefore does NOT rehash & change the elements order. |
14319 | 482 |
Therefor this can be used while enumerating the receiver, |
483 |
which is not possible if #remove: is used. |
|
484 |
||
485 |
WARNING: since no resizing is done, the physical amount of memory used |
|
486 |
by the container remains the same, although the logical size shrinks. |
|
487 |
You may want to manually resize the receiver using #emptyCheck. |
|
488 |
(after the loop)" |
|
489 |
||
490 |
^ self saveRemove:oldObject ifAbsent:[]. |
|
491 |
||
492 |
"Created: / 16.11.2001 / 10:23:48 / cg" |
|
493 |
"Modified: / 16.11.2001 / 10:24:03 / cg" |
|
494 |
! |
|
495 |
||
496 |
saveRemove:oldObjectArg ifAbsent:exceptionValueProvider |
|
497 |
"remove the element, oldObject from the collection. |
|
498 |
Return the element |
|
499 |
(could be non-identical to oldObject, since I hash on equality, not on identity). |
|
500 |
If it was not in the collection return the value of exceptionValueProvider. |
|
501 |
||
502 |
In contrast to #remove:, this does not resize the underlying collection |
|
503 |
and therefore does NOT rehash & change the elements order. |
|
12963 | 504 |
Therefore this can be used while enumerating the receiver, |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
505 |
which is not possible if #remove: is used. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
506 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
507 |
WARNING: since no resizing is done, the physical amount of memory used |
12963 | 508 |
by the container remains the same, although the logical size shrinks. |
509 |
You may want to manually resize the receiver using #emptyCheck. |
|
510 |
(after the loop)" |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
511 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
512 |
|oldObject |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
513 |
index "{ Class:SmallInteger }" |
6191 | 514 |
next "{ Class:SmallInteger }" |
515 |
removedObject| |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
516 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
517 |
oldObjectArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
518 |
oldObject := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
519 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
520 |
oldObject := oldObjectArg. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
521 |
]. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
522 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
523 |
index := self find:oldObject ifAbsent:0. |
14319 | 524 |
index == 0 ifTrue:[^ exceptionValueProvider value]. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
525 |
|
6191 | 526 |
removedObject := keyArray basicAt:index. |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
527 |
keyArray basicAt:index put:nil. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
528 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
529 |
tally := tally - 1. |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
530 |
tally ~~ 0 ifTrue:[ |
12963 | 531 |
index == keyArray basicSize ifTrue:[ |
532 |
next := 1 |
|
533 |
] ifFalse:[ |
|
534 |
next := index + 1. |
|
535 |
]. |
|
536 |
(keyArray basicAt:next) notNil ifTrue:[ |
|
537 |
keyArray basicAt:index put:DeletedEntry |
|
538 |
]. |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
539 |
]. |
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
540 |
removedObject == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
541 |
removedObject := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
542 |
]. |
6191 | 543 |
^ removedObject |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
544 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
545 |
"does NOT work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
546 |
|
12963 | 547 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
548 |
|
12963 | 549 |
s := Set new. |
550 |
s add:1. |
|
551 |
s add:2. |
|
552 |
s add:3. |
|
553 |
s add:4. |
|
554 |
s add:5. |
|
555 |
s add:6. |
|
556 |
s add:7. |
|
557 |
s add:8. |
|
558 |
s add:9. |
|
559 |
s do:[:v | |
|
560 |
v odd ifTrue:[ |
|
561 |
s remove:v |
|
562 |
] |
|
563 |
]. |
|
564 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
565 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
566 |
|
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
567 |
"DOES work: |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
568 |
|
12963 | 569 |
|s| |
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
570 |
|
12963 | 571 |
s := Set new. |
572 |
s add:1. |
|
573 |
s add:2. |
|
574 |
s add:3. |
|
575 |
s add:4. |
|
576 |
s add:5. |
|
577 |
s add:6. |
|
578 |
s add:7. |
|
579 |
s add:8. |
|
580 |
s add:9. |
|
581 |
s do:[:v | |
|
582 |
v odd ifTrue:[ |
|
583 |
s saveRemove:v |
|
584 |
] |
|
585 |
]. |
|
586 |
s inspect |
|
1052
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
587 |
" |
7d43187f7e11
added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
588 |
|
6191 | 589 |
"Created: / 1.3.1996 / 21:14:26 / cg" |
590 |
"Modified: / 16.11.2001 / 10:22:59 / cg" |
|
10588 | 591 |
! |
592 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
593 |
testAndAdd:keyArg |
10588 | 594 |
"add the argument, anObject to the receiver. |
595 |
Answer true, if the element did already exist in the collection, |
|
596 |
false otherwise. |
|
597 |
||
598 |
WARNING: do not add elements while iterating over the receiver. |
|
599 |
Iterate over a copy to do this." |
|
600 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
601 |
|key index "{ Class: SmallInteger }"| |
10588 | 602 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
603 |
keyArg isNil ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
604 |
key := NilEntry. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
605 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
606 |
key := keyArg. |
10588 | 607 |
]. |
608 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
609 |
index := self findKeyOrNil:key. |
10588 | 610 |
(keyArray basicAt:index) isNil ifTrue:[ |
611 |
"/ not already there |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
612 |
keyArray basicAt:index put:key. |
10588 | 613 |
tally := tally + 1. |
614 |
||
615 |
self fullCheck. |
|
616 |
^ false. |
|
617 |
]. |
|
618 |
^ true. |
|
619 |
||
620 |
"Modified: 30.1.1997 / 14:58:08 / cg" |
|
609 | 621 |
! ! |
622 |
||
11052 | 623 |
|
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
624 |
!Set methodsFor:'comparing'! |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
625 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
626 |
= aCollection |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
627 |
"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
|
628 |
as I do" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
629 |
|
14134 | 630 |
aCollection species ~~ self species ifTrue:[^ false]. |
631 |
aCollection size ~~ self size ifTrue:[^ false]. |
|
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
632 |
"/ same number of elements; since I am a Set, all of |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
633 |
"/ of my elements must be in the other collection ... |
14134 | 634 |
self do:[:eachElement | (aCollection includes:eachElement) ifFalse:[^ false]]. |
635 |
^ true. |
|
5280
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 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
638 |
#(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
|
639 |
#(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
|
640 |
#(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
|
641 |
#(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
|
642 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
643 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
644 |
|d1 d2| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
645 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
646 |
d1 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
647 |
d2 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
648 |
d1 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
649 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
650 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
651 |
d1 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
652 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
653 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
654 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
655 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
656 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
657 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
658 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
659 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
660 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
661 |
d1 = d2 |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
662 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
663 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
664 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
665 |
|d1 d2| |
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 |
d1 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
668 |
d2 := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
669 |
d1 at:1 put:'uno'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
670 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
671 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
672 |
d1 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
673 |
d1 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
674 |
d1 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
675 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
676 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
677 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
678 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
679 |
d2 at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
680 |
d2 at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
681 |
d2 at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
682 |
d1 = d2 |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
683 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
684 |
|
10090
af5b9428e8ae
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9933
diff
changeset
|
685 |
"Modified: / 13-10-2006 / 12:58:43 / cg" |
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
686 |
! |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
687 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
688 |
hash |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
689 |
"return a hash key for the receiver" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
690 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
691 |
"this hash is stupid - but for larger collections, the hashing |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
692 |
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
|
693 |
Time will show ... |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
694 |
Notice & warning: |
14134 | 695 |
if the #= method is ever changed to compare non-dictionaries equal, |
696 |
the code below must be changed to assert that the same hash-value is |
|
697 |
still returned. |
|
698 |
(which may be hard to acomplish) |
|
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
699 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
700 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
701 |
|mySize| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
702 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
703 |
mySize := self size. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
704 |
mySize == 0 ifTrue:[^ 0]. |
14134 | 705 |
^ (self anElement hash times:mySize) bitAnd:16r3FFFFFFF |
5280
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
706 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
707 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
708 |
|d| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
709 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
710 |
d := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
711 |
d at:1 put:'one'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
712 |
d at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
713 |
d at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
714 |
d at:'two' put:2. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
715 |
d hash |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
716 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
717 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
718 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
719 |
|d| |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
720 |
|
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
721 |
d := Dictionary new. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
722 |
d at:1 put:'uno'. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
723 |
d at:'one' put:1. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
724 |
d at:2 put:#two. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
725 |
d at:'two' put:2. |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
726 |
d hash |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
727 |
" |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
728 |
! ! |
ad806c9b992e
added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
729 |
|
15000 | 730 |
!Set methodsFor:'converting'! |
731 |
||
732 |
asNewSet |
|
733 |
"make sure to return a unique new set" |
|
734 |
||
735 |
"could be an instance of a subclass..." |
|
736 |
self class == Set ifTrue:[ |
|
737 |
^ self copy |
|
738 |
]. |
|
739 |
^ super asSet |
|
740 |
||
741 |
" |
|
742 |
|s| |
|
743 |
s := #(1 2 3 4) asSet. |
|
744 |
self assert:(s ~~ s asNewSet). |
|
745 |
self assert:(s = s asNewSet). |
|
746 |
" |
|
15036 | 747 |
! |
748 |
||
749 |
asSet |
|
750 |
"return the receiver as a Set" |
|
751 |
||
752 |
"could be an instance of a subclass..." |
|
753 |
self class == Set ifTrue:[ |
|
754 |
^ self |
|
755 |
]. |
|
756 |
^ super asSet |
|
15000 | 757 |
! ! |
758 |
||
159 | 759 |
!Set methodsFor:'copying'! |
760 |
||
761 |
postCopy |
|
762 |
"have to copy the keyArray too" |
|
763 |
||
764 |
keyArray := keyArray shallowCopy |
|
1 | 765 |
! ! |
766 |
||
609 | 767 |
!Set methodsFor:'enumerating'! |
1 | 768 |
|
609 | 769 |
do:aBlock |
1056 | 770 |
"perform the block for all members in the collection. |
771 |
||
772 |
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
|
773 |
Iterate over a copy to do this." |
13 | 774 |
|
609 | 775 |
|sz "{ Class: SmallInteger }" |
776 |
element| |
|
13 | 777 |
|
609 | 778 |
sz := keyArray size. |
779 |
1 to:sz do:[:index | |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
780 |
element := keyArray at:index. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
781 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
782 |
element == NilEntry ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
783 |
element := nil. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
784 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
785 |
aBlock value:element |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
786 |
] |
609 | 787 |
] |
1056 | 788 |
|
789 |
"Modified: 1.3.1996 / 21:41:13 / cg" |
|
609 | 790 |
! ! |
61 | 791 |
|
969 | 792 |
|
15000 | 793 |
|
7169 | 794 |
!Set methodsFor:'obsolete set operations'! |
795 |
||
796 |
+ aCollection |
|
8606 | 797 |
"Kept for backward compatibility. |
798 |
Use #union: instead, to isolate arithmethic and set operations" |
|
7169 | 799 |
|
800 |
<resource: #obsolete> |
|
801 |
||
802 |
^ self union:aCollection. |
|
803 |
! |
|
804 |
||
805 |
- aCollection |
|
8606 | 806 |
"Kept for backward compatibility. |
807 |
Use #\ instead, to isolate arithmethic and set operations" |
|
7169 | 808 |
|
809 |
<resource: #obsolete> |
|
810 |
||
811 |
^ self \ aCollection |
|
812 |
! ! |
|
813 |
||
609 | 814 |
!Set methodsFor:'private'! |
2 | 815 |
|
1 | 816 |
find:key ifAbsent:aBlock |
817 |
"Look for the key in the receiver. If it is found, return |
|
818 |
the index of the slot containing the key, otherwise |
|
819 |
return the value of evaluating aBlock." |
|
820 |
||
61 | 821 |
|index "{ Class:SmallInteger }" |
822 |
length "{ Class:SmallInteger }" |
|
823 |
startIndex probe| |
|
1 | 824 |
|
13 | 825 |
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
|
826 |
|
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
|
827 |
"/ |
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
|
828 |
"/ length < 10 ifTrue:[ |
362 | 829 |
"/ "assuming, that for small collections the overhead of hashing |
830 |
"/ is larger ... maybe that proves wrong |
|
831 |
"/ (if overhead of comparing is higher)" |
|
832 |
"/ ^ 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
|
833 |
"/ ]. |
13 | 834 |
|
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
835 |
startIndex := index := self initialIndexForKey:key. |
1 | 836 |
|
837 |
[true] whileTrue:[ |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
838 |
probe := (keyArray basicAt:index). |
13279 | 839 |
(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
|
840 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
1 | 841 |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
842 |
index == length ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
843 |
index := 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
844 |
] ifFalse:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
845 |
index := index + 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
846 |
]. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
847 |
index == startIndex ifTrue:[^ aBlock value]. |
1 | 848 |
] |
13279 | 849 |
|
850 |
"Modified: / 03-02-2011 / 13:53:18 / sr" |
|
1 | 851 |
! |
852 |
||
15806 | 853 |
findIdentical:key ifAbsent:aBlock |
854 |
"Look for the key in the receiver. If it is found, return |
|
855 |
the index of the slot containing the key, otherwise |
|
856 |
return the value of evaluating aBlock." |
|
857 |
||
858 |
^ keyArray identityIndexOf:key ifAbsent:aBlock |
|
859 |
! |
|
860 |
||
13 | 861 |
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
|
862 |
"Look for the key in the receiver. |
3082 | 863 |
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
|
864 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 865 |
|
61 | 866 |
|index "{ Class:SmallInteger }" |
867 |
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
|
868 |
startIndex probe |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
869 |
delIndex| |
1 | 870 |
|
13 | 871 |
length := keyArray basicSize. |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
872 |
startIndex := index := self initialIndexForKey:key. |
1 | 873 |
|
874 |
[true] whileTrue:[ |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
875 |
probe := keyArray basicAt:index. |
13311 | 876 |
(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
|
877 |
(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
|
878 |
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
|
879 |
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
|
880 |
^ delIndex |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
881 |
]. |
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
|
882 |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
883 |
probe == DeletedEntry ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
884 |
delIndex isNil ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
885 |
delIndex := index |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
886 |
] |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
887 |
]. |
1 | 888 |
|
7654
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
889 |
index == length ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
890 |
index := 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
891 |
] ifFalse:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
892 |
index := index + 1 |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
893 |
]. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
894 |
index == startIndex ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
895 |
delIndex notNil ifTrue:[ |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
896 |
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
|
897 |
^ delIndex |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
898 |
]. |
41441d702cfc
avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents:
7598
diff
changeset
|
899 |
^ 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
|
900 |
]. |
1 | 901 |
] |
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
|
902 |
|
13311 | 903 |
"Modified: / 27-02-2011 / 15:30:42 / cg" |
1 | 904 |
! |
905 |
||
906 |
findNil:key |
|
2475 | 907 |
"Look for the next slot usable for key. |
908 |
WARNING: |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
909 |
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
|
910 |
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
|
911 |
AND that there is an empty slot. |
2475 | 912 |
To be used ONLY while growing/rehashing to enter elements into a fresh |
913 |
collection - if any of the above conditions is not met, the method |
|
914 |
loops forever." |
|
1 | 915 |
|
2475 | 916 |
|index "{ Class:SmallInteger }" |
61 | 917 |
length "{ Class:SmallInteger }"| |
1 | 918 |
|
13 | 919 |
length := keyArray basicSize. |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
920 |
index := self initialIndexForKey:key. |
1 | 921 |
|
5772 | 922 |
"/ index := keyArray identityIndexOf:nil startingAt:index. |
923 |
"/ index == 0 ifTrue:[ |
|
924 |
"/ index := keyArray identityIndexOf:nil startingAt:1 endingAt:index. |
|
925 |
"/ ]. |
|
926 |
"/ that code is completely inlined by stc ... |
|
927 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
928 |
index == length ifTrue:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
929 |
index := 1 |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
930 |
] ifFalse:[ |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
931 |
index := index + 1 |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
932 |
]. |
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
933 |
"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
|
934 |
this is only called after growing" |
1 | 935 |
]. |
936 |
^ index |
|
1055 | 937 |
|
2475 | 938 |
"Modified: 21.3.1997 / 10:32:58 / cg" |
1 | 939 |
! |
940 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
941 |
hashFor:aKey |
6991
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
942 |
"return the arguments hash value. |
e95313d7c780
comment and category change only
Claus Gittinger <cg@exept.de>
parents:
6917
diff
changeset
|
943 |
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
|
944 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
945 |
^ aKey hash |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
946 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
947 |
"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
|
948 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
949 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
950 |
initialIndexForKey:aKey |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
951 |
"return an initial index given a key." |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
952 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
953 |
|index "{ Class:SmallInteger }" |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
954 |
length "{ Class:SmallInteger }"| |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
955 |
|
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
956 |
length := keyArray basicSize. |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
957 |
index := (self hashFor:aKey) bitAnd:16r3FFFFFFF. |
14656 | 958 |
|
959 |
"multiply by a large prime to spread the keys over the whole range. |
|
960 |
This causes maxChainLength to reduce from about 2000 to 6 for an |
|
961 |
IdentitySet of 9000 Smalltalk classes" |
|
962 |
index := index times:31415821. |
|
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
963 |
index := index \\ length + 1. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
964 |
^ index. |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
965 |
|
14656 | 966 |
" |
967 |
Smalltalk allClasses maxChainLength |
|
968 |
" |
|
969 |
||
2459
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
970 |
"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
|
971 |
"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
|
972 |
! |
0a38e8602aac
findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
973 |
|
5845
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
974 |
invalidElementError |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
975 |
"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
|
976 |
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
|
977 |
|
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
978 |
"/ self elementBoundError. |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
979 |
|
9086 | 980 |
InfoPrinting ifTrue:[ |
981 |
'Set [warning]: nil is not a valid element - will be an error in later versions' errorPrintCR. |
|
11472 | 982 |
Context showWhereWeCameFrom. |
6540
cf1a6a1a8645
better invalidElementError message
Claus Gittinger <cg@exept.de>
parents:
6519
diff
changeset
|
983 |
]. |
5845
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
984 |
! |
64a385f14970
Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents:
5834
diff
changeset
|
985 |
|
3261
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
986 |
keyArray |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
987 |
^ keyArray |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
988 |
|
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
989 |
"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
|
990 |
! |
7e4e57c041df
added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents:
3082
diff
changeset
|
991 |
|
609 | 992 |
keyContainerOfSize:n |
993 |
"return a container for keys of size n. |
|
994 |
Extracted to make life of weak subclasses easier ..." |
|
995 |
||
996 |
^ Array basicNew:n |
|
997 |
! |
|
998 |
||
1 | 999 |
rehash |
61 | 1000 |
"rehash is done by re-adding all elements to a new empty set. |
1001 |
Rehash is needed after a binaryRead, for example." |
|
1 | 1002 |
|
3082 | 1003 |
|element oldKeyArray newKeyArray newIdx |
95 | 1004 |
n "{ Class:SmallInteger }"| |
1 | 1005 |
|
95 | 1006 |
oldKeyArray := keyArray. |
1007 |
n := oldKeyArray size. |
|
1008 |
keyArray := newKeyArray := self keyContainerOfSize:n. |
|
1009 |
||
2 | 1010 |
1 to:n do:[:index | |
6994 | 1011 |
element := oldKeyArray at:index. |
1012 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
|
1013 |
"cannot be already there" |
|
1014 |
newIdx := self findNil:element. |
|
1015 |
newKeyArray basicAt:newIdx put:element |
|
1016 |
]. |
|
1 | 1017 |
] |
3082 | 1018 |
|
1019 |
"Modified: / 30.10.1997 / 16:04:46 / cg" |
|
1 | 1020 |
! |
1021 |
||
1022 |
rehashFrom:startIndex |
|
61 | 1023 |
"rehash elements starting at index - after a remove. |
1024 |
Notice: due to the new implementation of remove, |
|
159 | 1025 |
this is no longer needed" |
1 | 1026 |
|
3 | 1027 |
|element i "{ Class:SmallInteger }" |
1028 |
length |
|
1 | 1029 |
index "{ Class:SmallInteger }" | |
1030 |
||
13 | 1031 |
length := keyArray basicSize. |
1 | 1032 |
index := startIndex. |
13 | 1033 |
element := keyArray basicAt:index. |
1 | 1034 |
[element notNil] whileTrue:[ |
159 | 1035 |
i := self findNil:element. |
1036 |
i == index ifTrue:[ |
|
1037 |
^ self |
|
1038 |
]. |
|
1039 |
keyArray basicAt:i put:element. |
|
1040 |
keyArray basicAt:index put:nil. |
|
1 | 1041 |
|
159 | 1042 |
index == length ifTrue:[ |
1043 |
index := 1 |
|
1044 |
] ifFalse:[ |
|
1045 |
index := index + 1. |
|
1046 |
]. |
|
1047 |
element := keyArray basicAt:index. |
|
1 | 1048 |
] |
1049 |
! |
|
1050 |
||
609 | 1051 |
setTally:count |
1052 |
"initialize the contents array (for at least count slots) |
|
1053 |
and set tally to zero. |
|
1054 |
The size is increased to the next prime for better hashing behavior." |
|
1 | 1055 |
|
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
1056 |
|n| |
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
1057 |
|
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
1058 |
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
|
1059 |
n == keyArray size ifTrue:[ |
6994 | 1060 |
keyArray atAllPut:nil. |
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
1061 |
] ifFalse:[ |
6994 | 1062 |
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
|
1063 |
]. |
609 | 1064 |
tally := 0 |
3726
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
1065 |
|
cf3eafa152a4
try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents:
3265
diff
changeset
|
1066 |
"Modified: / 4.8.1998 / 22:07:25 / cg" |
6233 | 1067 |
! |
1068 |
||
1069 |
slotIsEmpty:aSlotValue |
|
1070 |
"only redefined in weak subclasses, since they treat a 0-value |
|
1071 |
as being empty" |
|
1072 |
||
1073 |
^ aSlotValue isNil |
|
1 | 1074 |
! ! |
1075 |
||
7260 | 1076 |
!Set methodsFor:'private-grow & shrink'! |
3082 | 1077 |
|
1078 |
emptyCheck |
|
1079 |
"check if the receiver has become too empty (after a remove) |
|
1080 |
and shrink if it makes sense. |
|
1081 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
1082 |
||
1083 |
|sz "{Class: SmallInteger}" |
|
1084 |
newSize "{Class: SmallInteger}" | |
|
1085 |
||
1086 |
sz := keyArray basicSize. |
|
1087 |
sz > 56 ifTrue:[ |
|
6994 | 1088 |
" |
1089 |
shrink if too empty |
|
1090 |
" |
|
1091 |
tally < (sz // 8) ifTrue:[ |
|
1092 |
newSize := sz // 4. |
|
1093 |
self grow:newSize |
|
1094 |
] |
|
3082 | 1095 |
] |
1096 |
||
1097 |
"Modified: 19.3.1997 / 16:02:55 / cg" |
|
1098 |
! |
|
1099 |
||
1100 |
fullCheck |
|
1101 |
"check if collection is full (after an add); grow if so. |
|
1102 |
Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'" |
|
1103 |
||
1104 |
|sz "{Class: SmallInteger}" | |
|
1105 |
||
1106 |
" |
|
1107 |
grow if filled more than 75% |
|
1108 |
" |
|
1109 |
sz := keyArray basicSize. |
|
1110 |
tally > (sz * 3 // 4) ifTrue:[ |
|
1111 |
self grow |
|
1112 |
] |
|
1113 |
! |
|
1114 |
||
1115 |
grow |
|
1116 |
"change the number of element slots of the collection to a useful |
|
1117 |
new size" |
|
1118 |
||
1119 |
self grow:(keyArray basicSize * 2) |
|
1120 |
! |
|
1121 |
||
1122 |
grow:newSize |
|
1123 |
"change the number of element slots of the collection - to do this, |
|
1124 |
we have to rehash (which is done by re-adding all elements to a new |
|
1125 |
empty set)." |
|
1126 |
||
1127 |
|elem oldKeyArray newKeyArray deletedEntry |
|
1128 |
containerSize oldSize "{ Class:SmallInteger }"| |
|
1129 |
||
1130 |
oldKeyArray := keyArray. |
|
1131 |
oldSize := oldKeyArray size. |
|
1132 |
containerSize := (self class goodSizeFrom:newSize). |
|
1133 |
containerSize == oldSize ifTrue:[^ self]. |
|
1134 |
||
1135 |
keyArray := newKeyArray := self keyContainerOfSize:containerSize. |
|
1136 |
||
1137 |
deletedEntry := DeletedEntry. |
|
1138 |
1 to:oldSize do:[:srcIndex | |
|
1139 |
elem := oldKeyArray basicAt:srcIndex. |
|
1140 |
(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[ |
|
1141 |
"cannot be already there" |
|
1142 |
newKeyArray basicAt:(self findNil:elem) put:elem |
|
1143 |
]. |
|
1144 |
]. |
|
1145 |
! ! |
|
1146 |
||
5557 | 1147 |
!Set methodsFor:'queries'! |
1148 |
||
8499 | 1149 |
collisionCount |
1150 |
"return the number of key collisions in the set. |
|
1151 |
There is a collision if two keys hash to the same value." |
|
1152 |
||
1153 |
|count| |
|
1154 |
||
1155 |
count := 0. |
|
1156 |
||
1157 |
keyArray do:[:eachKey| |
|
1158 |
(eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[ |
|
1159 |
count := count + (self collisionsFor:eachKey). |
|
1160 |
] |
|
1161 |
]. |
|
1162 |
||
1163 |
^ count |
|
14654 | 1164 |
|
1165 |
" |
|
1166 |
self allSubInstances |
|
1167 |
collect:[:each| each collisionCount -> each] |
|
1168 |
thenSelect:[:each| each key > 0] |
|
1169 |
" |
|
8499 | 1170 |
! |
1171 |
||
1172 |
collisionsFor:key |
|
1173 |
"Return the number of searches - 1 required for key" |
|
1174 |
||
1175 |
|index "{ Class:SmallInteger }" |
|
14654 | 1176 |
length "{ Class:SmallInteger }" startIndex probe count| |
8499 | 1177 |
|
1178 |
length := keyArray basicSize. |
|
14654 | 1179 |
startIndex := index := self initialIndexForKey:key. |
8499 | 1180 |
|
1181 |
count := 0. |
|
1182 |
[true] whileTrue:[ |
|
14654 | 1183 |
probe := keyArray basicAt:index. |
8499 | 1184 |
(probe notNil and:[key = probe]) ifTrue:[^ count]. |
1185 |
(self slotIsEmpty:probe) ifTrue:[self error:'non existing key']. |
|
1186 |
||
1187 |
index == length ifTrue:[ |
|
1188 |
index := 1. |
|
1189 |
] ifFalse:[ |
|
1190 |
index := index + 1. |
|
1191 |
]. |
|
1192 |
count := count + 1. |
|
1193 |
index == startIndex ifTrue:[self error:'non existing key']. |
|
1194 |
] |
|
1195 |
! |
|
1196 |
||
1197 |
maxChainLength |
|
1198 |
"return the number of the maximum chain length in the set. |
|
1199 |
This is the worst case overhead when accessing a key." |
|
1200 |
||
1201 |
|chainLength key| |
|
1202 |
||
1203 |
chainLength := 0. |
|
1204 |
||
1205 |
keyArray do:[:eachKey| |t| |
|
1206 |
(eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[ |
|
1207 |
t := self collisionsFor:eachKey. |
|
1208 |
chainLength < t ifTrue:[chainLength := t. key := eachKey]. |
|
1209 |
] |
|
1210 |
]. |
|
1211 |
||
1212 |
^ (key -> chainLength) |
|
1213 |
! |
|
1214 |
||
5557 | 1215 |
size |
1216 |
"return the number of set elements" |
|
1217 |
||
1218 |
^ tally |
|
1219 |
! ! |
|
5471
a57eeb01c5ab
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5335
diff
changeset
|
1220 |
|
15000 | 1221 |
|
1 | 1222 |
!Set methodsFor:'testing'! |
1223 |
||
362 | 1224 |
capacity |
1225 |
"return the number of elements, that the receiver is |
|
1226 |
prepared to take. |
|
1227 |
Not used by the system; added for ST-80 compatibility." |
|
1228 |
||
1229 |
^ keyArray size |
|
1230 |
! |
|
1231 |
||
1 | 1232 |
includes:anObject |
1233 |
"return true if the argument anObject is in the receiver" |
|
1234 |
||
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
1235 |
^ (self find:(anObject ? NilEntry) ifAbsent:0) ~~ 0 |
1 | 1236 |
! |
1237 |
||
1238 |
isEmpty |
|
1239 |
"return true if the receiver is empty" |
|
1240 |
||
1241 |
^ tally == 0 |
|
1242 |
! |
|
1243 |
||
609 | 1244 |
isFixedSize |
1245 |
"return true if the receiver cannot grow - this will vanish once |
|
1246 |
Arrays and Strings learn how to grow ..." |
|
1247 |
||
1248 |
^ false |
|
1249 |
! |
|
1250 |
||
2383 | 1251 |
notEmpty |
1252 |
"return true if the receiver is not empty" |
|
1253 |
||
1254 |
^ tally ~~ 0 |
|
1255 |
||
1256 |
"Created: 12.2.1997 / 12:39:02 / cg" |
|
1257 |
! |
|
1258 |
||
1 | 1259 |
occurrencesOf:anObject |
5048 | 1260 |
"return the number of occurrences of anObject in the receiver. |
6191 | 1261 |
As I am a Set, this can only return 0 or 1." |
1 | 1262 |
|
14315
f75e7698ccfe
Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents:
14314
diff
changeset
|
1263 |
(self find:(anObject ? NilEntry) ifAbsent:0) == 0 ifTrue:[^ 0]. |
1 | 1264 |
^ 1 |
6191 | 1265 |
|
1266 |
"Modified: / 16.11.2001 / 10:30:14 / cg" |
|
1 | 1267 |
! |
1268 |
||
4795 | 1269 |
sameContentsAs:aCollection |
1270 |
"answer true, if all the elements in self and aCollection |
|
1271 |
are common. This is not defined as #=, since we cannot redefine #hash |
|
1272 |
for aCollection." |
|
1273 |
||
1274 |
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
|
1275 |
^ false |
4795 | 1276 |
]. |
1277 |
||
10090
af5b9428e8ae
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9933
diff
changeset
|
1278 |
^ aCollection conform:[:e | (self includes:e)] |
4795 | 1279 |
|
1280 |
" |
|
1281 |
#(1 2 3) asSet sameContentsAs: #(1 2 3) |
|
1282 |
#(1 2 3 4) asSet sameContentsAs: #(1 2 3) |
|
1283 |
#(1 2 3) asSet sameContentsAs: #(1 2 3 3) |
|
1284 |
#(1 2 3 'aa') asSet sameContentsAs: #(1 2 3 'aa') |
|
1285 |
#(1 2 3 'aa') asIdentitySet sameContentsAs: #(1 2 3 'aa') |
|
1286 |
#(1 2 3 #aa) asIdentitySet sameContentsAs: #(1 2 3 #aa) |
|
1287 |
" |
|
1288 |
||
10090
af5b9428e8ae
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9933
diff
changeset
|
1289 |
"Modified: / 13-10-2006 / 12:59:01 / cg" |
1 | 1290 |
! ! |
1291 |
||
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1292 |
!Set methodsFor:'visiting'! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1293 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1294 |
acceptVisitor:aVisitor with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1295 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1296 |
^ aVisitor visitSet:self with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1297 |
! ! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7654
diff
changeset
|
1298 |
|
4017
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1299 |
!Set::EmptySlot class methodsFor:'instance creation'! |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1300 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1301 |
basicNew |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1302 |
TheOneAndOnlyInstance isNil ifTrue:[ |
6994 | 1303 |
TheOneAndOnlyInstance := super basicNew |
4017
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1304 |
]. |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1305 |
^ TheOneAndOnlyInstance |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1306 |
|
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1307 |
! ! |
cdf103ce2e30
there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents:
3726
diff
changeset
|
1308 |
|
14314 | 1309 |
!Set::NilKey class methodsFor:'instance creation'! |
1310 |
||
1311 |
basicNew |
|
1312 |
TheOneAndOnlyInstance isNil ifTrue:[ |
|
1313 |
TheOneAndOnlyInstance := super basicNew |
|
1314 |
]. |
|
1315 |
^ TheOneAndOnlyInstance |
|
1316 |
||
1317 |
||
1318 |
! ! |
|
1319 |
||
2259 | 1320 |
!Set class methodsFor:'documentation'! |
635 | 1321 |
|
1322 |
version |
|
15806 | 1323 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.121 2013-11-12 18:14:08 stefan Exp $' |
12342 | 1324 |
! |
1325 |
||
1326 |
version_CVS |
|
15806 | 1327 |
^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.121 2013-11-12 18:14:08 stefan Exp $' |
635 | 1328 |
! ! |
6917
979bfb9267e3
refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents:
6577
diff
changeset
|
1329 |
|
14654 | 1330 |
|
609 | 1331 |
Set initialize! |