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