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