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