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