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