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