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