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