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