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