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