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