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