author | Claus Gittinger <cg@exept.de> |
Sun, 05 Aug 2018 13:55:10 +0200 | |
changeset 23262 | c0fce9983f8f |
parent 22498 | f5e8366a9fe5 |
child 23695 | 59ae587a1107 |
permissions | -rw-r--r-- |
22498 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
250 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5559 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
19546 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
Set subclass:#IdentitySet |
1290 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Unordered' |
|
1 | 23 |
! |
24 |
||
88 | 25 |
!IdentitySet class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
250 | 30 |
All Rights Reserved |
1 | 31 |
|
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
1 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
43 |
same as a Set but compares elements using == |
|
44 |
(i.e. they must be identical - not just equal in structure). |
|
45 |
Since compare is on identity (using ==), hashing is also done via |
|
46 |
#identityHash instead of #hash. |
|
1290 | 47 |
|
48 |
[author:] |
|
49 |
Claus Gittinger |
|
88 | 50 |
" |
51 |
! ! |
|
1 | 52 |
|
7310
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
53 |
!IdentitySet methodsFor:'Compatibility-Squeak'! |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
54 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
55 |
copyWithout:anElement |
11051 | 56 |
"return a new collection consisting of a copy of the receiver, with |
57 |
ALL elements equal to elementToSkip are left out. |
|
58 |
No error is reported, if elementToSkip is not in the collection." |
|
59 |
||
7310
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
60 |
^ self select:[:each | each ~~ anElement] |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
61 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
62 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
63 |
#(1 2 3 4 5 6 7) asSet copyWithout:5 |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
64 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
65 |
! ! |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
66 |
|
4625 | 67 |
!IdentitySet methodsFor:'adding & removing'! |
68 |
||
15805 | 69 |
removeIdentical:oldObject ifAbsent:exceptionBlock |
70 |
"remove oldObject from the collection and return it. |
|
71 |
If it was not in the collection return the value of exceptionBlock. |
|
72 |
Uses identity compare (==) to search for an occurrence. |
|
73 |
||
74 |
WARNING: do not remove elements while iterating over the receiver." |
|
75 |
||
76 |
^ self remove:oldObject ifAbsent:exceptionBlock |
|
4625 | 77 |
! ! |
78 |
||
14998 | 79 |
!IdentitySet methodsFor:'converting'! |
80 |
||
15037 | 81 |
asIdentitySet |
82 |
"return the receiver as an IdentitySet" |
|
83 |
||
84 |
"could be an instance of a subclass..." |
|
85 |
self class == IdentitySet ifTrue:[ |
|
86 |
^ self |
|
87 |
]. |
|
88 |
^ super asIdentitySet |
|
89 |
! |
|
90 |
||
14998 | 91 |
asNewIdentitySet |
15037 | 92 |
"make sure to return myself as a unique new IdentitySet" |
14998 | 93 |
|
94 |
"could be an instance of a subclass..." |
|
95 |
self class == IdentitySet ifTrue:[ |
|
96 |
^ self copy |
|
97 |
]. |
|
98 |
^ super asIdentitySet |
|
99 |
||
100 |
" |
|
101 |
|s| |
|
102 |
s := #(1 2 3 4) asIdentitySet. |
|
103 |
self assert:(s ~~ s asNewIdentitySet). |
|
104 |
self assert:(s = s asNewIdentitySet). |
|
105 |
" |
|
106 |
! ! |
|
107 |
||
1 | 108 |
!IdentitySet methodsFor:'private'! |
109 |
||
14655 | 110 |
collisionsFor:key |
111 |
"Return the number of searches - 1 required for key" |
|
112 |
||
113 |
|index "{ Class:SmallInteger }" |
|
114 |
length "{ Class:SmallInteger }" startIndex probe count| |
|
115 |
||
116 |
length := keyArray basicSize. |
|
117 |
startIndex := index := self initialIndexForKey:key. |
|
118 |
||
119 |
count := 0. |
|
16240 | 120 |
[ |
14655 | 121 |
probe := keyArray basicAt:index. |
19546 | 122 |
probe isNil ifTrue:[self error:'non existing key']. |
123 |
key == probe ifTrue:[^ count]. |
|
14655 | 124 |
|
125 |
index == length ifTrue:[ |
|
126 |
index := 1. |
|
127 |
] ifFalse:[ |
|
128 |
index := index + 1. |
|
129 |
]. |
|
130 |
count := count + 1. |
|
131 |
index == startIndex ifTrue:[self error:'non existing key']. |
|
16240 | 132 |
] loop. |
14655 | 133 |
! |
134 |
||
1 | 135 |
find:key ifAbsent:aBlock |
136 |
"Look for the key in the receiver. If it is found, return |
|
137 |
the index of the slot containing the key, otherwise |
|
138 |
return the value of evaluating aBlock. |
|
139 |
Redefined to compare for identity instead of equality" |
|
140 |
||
6233 | 141 |
|index "{ Class:SmallInteger }" |
142 |
length "{ Class:SmallInteger }" |
|
143 |
startIndex probe | |
|
1 | 144 |
|
13 | 145 |
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:
634
diff
changeset
|
146 |
|
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:
634
diff
changeset
|
147 |
"/ |
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:
634
diff
changeset
|
148 |
"/ length < 10 ifTrue:[ |
6233 | 149 |
"/ "assuming, that for small sets the overhead of hashing |
150 |
"/ is large ..." |
|
151 |
"/ ^ keyArray identityIndexOf: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:
634
diff
changeset
|
152 |
"/ ]. |
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:
634
diff
changeset
|
153 |
"/ |
10 | 154 |
|
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
155 |
startIndex := index := self initialIndexForKey:key. |
1 | 156 |
|
16240 | 157 |
[ |
6233 | 158 |
probe := keyArray basicAt:index. |
22498 | 159 |
probe == key ifTrue:[^ index]. "<-- is different from inherited" |
19546 | 160 |
probe isNil ifTrue:[^ aBlock value]. |
1 | 161 |
|
6233 | 162 |
index == length ifTrue:[ |
163 |
index := 1 |
|
164 |
] ifFalse:[ |
|
165 |
index := index + 1 |
|
166 |
]. |
|
167 |
index == startIndex ifTrue:[ |
|
168 |
^ aBlock value |
|
169 |
] |
|
16240 | 170 |
] loop. |
1 | 171 |
! |
172 |
||
15805 | 173 |
findIdentical:key ifAbsent:aBlock |
174 |
"IdentitySet does identity compare anyway..." |
|
175 |
||
176 |
^ self find:key ifAbsent:aBlock |
|
177 |
! |
|
178 |
||
13 | 179 |
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:
634
diff
changeset
|
180 |
"Look for the key in the receiver. |
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:
634
diff
changeset
|
181 |
If it is found, return return the index of the first unused slot. |
19546 | 182 |
Grow the receiver, if key was not found, and no unused slots were present |
183 |
||
184 |
Warning: an empty slot MUST be filled by the sender - it is only to be sent |
|
185 |
by at:put: / add: - like methods." |
|
1 | 186 |
|
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:
634
diff
changeset
|
187 |
|index "{ 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:
634
diff
changeset
|
188 |
length "{ 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:
634
diff
changeset
|
189 |
startIndex probe |
19546 | 190 |
delIndex "{ 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:
634
diff
changeset
|
191 |
|
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:
634
diff
changeset
|
192 |
delIndex := 0. |
1 | 193 |
|
13 | 194 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
195 |
startIndex := index := self initialIndexForKey:key. |
1 | 196 |
|
16240 | 197 |
[ |
6346 | 198 |
probe := keyArray basicAt:index. |
199 |
key == probe ifTrue:[^ index]. |
|
19546 | 200 |
probe isNil ifTrue:[ |
6346 | 201 |
delIndex == 0 ifTrue:[^ index]. |
202 |
keyArray basicAt:delIndex put:nil. |
|
203 |
^ delIndex |
|
204 |
]. |
|
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:
634
diff
changeset
|
205 |
|
6346 | 206 |
probe == DeletedEntry ifTrue:[ |
207 |
delIndex == 0 ifTrue:[ |
|
208 |
delIndex := index |
|
209 |
] |
|
210 |
]. |
|
1 | 211 |
|
6346 | 212 |
index == length ifTrue:[ |
213 |
index := 1 |
|
214 |
] ifFalse:[ |
|
215 |
index := index + 1 |
|
216 |
]. |
|
217 |
index == startIndex ifTrue:[ |
|
218 |
delIndex ~~ 0 ifTrue:[ |
|
219 |
keyArray basicAt:delIndex put:nil. |
|
220 |
^ delIndex |
|
221 |
]. |
|
20234 | 222 |
self grow. |
223 |
length := keyArray basicSize. |
|
224 |
startIndex := index := self initialIndexForKey:key. |
|
225 |
]. |
|
226 |
] loop. |
|
227 |
||
228 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
|
229 |
! |
|
230 |
||
231 |
findKeyOrNilOrDeletedEntry:key |
|
232 |
"Look for the key in the receiver. |
|
233 |
If it is found, return return the index of the first unused slot. |
|
234 |
Grow the receiver, if key was not found, and no unused slots were present" |
|
235 |
||
236 |
|index "{ Class:SmallInteger }" |
|
237 |
length "{ Class:SmallInteger }" |
|
238 |
startIndex probe |
|
239 |
delIndex "{ Class:SmallInteger }"| |
|
240 |
||
241 |
delIndex := 0. |
|
242 |
||
243 |
length := keyArray basicSize. |
|
244 |
startIndex := index := self initialIndexForKey:key. |
|
245 |
||
246 |
[ |
|
247 |
probe := keyArray basicAt:index. |
|
248 |
key == probe ifTrue:[^ index]. |
|
249 |
probe isNil ifTrue:[ |
|
250 |
delIndex == 0 ifTrue:[^ index]. |
|
251 |
^ delIndex |
|
252 |
]. |
|
253 |
||
254 |
probe == DeletedEntry ifTrue:[ |
|
255 |
delIndex == 0 ifTrue:[ |
|
256 |
delIndex := index |
|
257 |
] |
|
258 |
]. |
|
259 |
||
260 |
index == length ifTrue:[ |
|
261 |
index := 1 |
|
262 |
] ifFalse:[ |
|
263 |
index := index + 1 |
|
264 |
]. |
|
265 |
index == startIndex ifTrue:[ |
|
266 |
delIndex ~~ 0 ifTrue:[ |
|
267 |
^ delIndex |
|
268 |
]. |
|
269 |
self grow. |
|
270 |
length := keyArray basicSize. |
|
271 |
startIndex := index := self initialIndexForKey:key. |
|
6346 | 272 |
]. |
16240 | 273 |
] loop. |
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:
634
diff
changeset
|
274 |
|
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:
634
diff
changeset
|
275 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 276 |
! |
277 |
||
2460 | 278 |
hashFor:aKey |
279 |
"return an initial index given a key." |
|
280 |
||
281 |
^ aKey identityHash |
|
282 |
||
283 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 284 |
! ! |
609 | 285 |
|
14018 | 286 |
!IdentitySet methodsFor:'set operations'! |
287 |
||
288 |
\ aCollection |
|
289 |
"return a new set containing all elements of the receiver, |
|
290 |
which are NOT also contained in the aCollection |
|
291 |
For large collections you better use a Set for aCollection. |
|
292 |
Redefined here to do identity comparison." |
|
293 |
||
294 |
|newCollection| |
|
295 |
||
296 |
newCollection := self speciesForAdding new. |
|
297 |
self do:[:element | |
|
298 |
(aCollection includesIdentical:element) ifFalse:[ |
|
299 |
newCollection add:element |
|
300 |
] |
|
301 |
]. |
|
302 |
^ newCollection |
|
303 |
||
304 |
" |
|
305 |
#(0 1 2 3 4 5 6 7 8 9) asIdentitySet \ #(1 2 3) asSet |
|
306 |
#(0 1 2 3 4 5 6 7 8 9) asIdentitySet \ #(1 2 3) |
|
307 |
" |
|
308 |
! ! |
|
309 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
310 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
311 |
|
5559 | 312 |
identicalContentsAs:aCollection |
313 |
"return true if the receiver and aCollection represent collections |
|
314 |
with identical contents (but not caring for order)." |
|
315 |
||
316 |
aCollection size == self size ifFalse:[^ false]. |
|
10091
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
317 |
^ aCollection conform:[:eachElement | (self includesIdentical:eachElement)]. |
5559 | 318 |
|
319 |
" |
|
320 |
|col| |
|
321 |
||
322 |
col := #('aaa' 'bbb' 'ccc' 'ddd'). |
|
323 |
col identicalContentsAs:(col asIdentitySet). |
|
324 |
col identicalContentsAs:(col copy asIdentitySet). |
|
325 |
col identicalContentsAs:(col deepCopy asIdentitySet). |
|
326 |
" |
|
10091
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
327 |
|
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
328 |
"Modified: / 13-10-2006 / 12:55:43 / cg" |
5559 | 329 |
! |
330 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
331 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
332 |
"for identitySet, the #includes: test already tests for identity" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
333 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
334 |
^ self includes:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
335 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
336 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
337 |
! ! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
338 |
|
634 | 339 |
!IdentitySet class methodsFor:'documentation'! |
340 |
||
341 |
version |
|
19546 | 342 |
^ '$Header$' |
634 | 343 |
! ! |
14655 | 344 |