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