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