author | Stefan Vogel <sv@exept.de> |
Fri, 29 Mar 2013 00:19:39 +0100 | |
changeset 14998 | 6bea48d14676 |
parent 14655 | 1d2b0650e086 |
child 15037 | 893c7b5f5776 |
child 18043 | 03660093fe98 |
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 |
||
65 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
66 |
^ super remove:anObject ifAbsent:exceptionBlock |
|
67 |
! ! |
|
68 |
||
14998 | 69 |
!IdentitySet methodsFor:'converting'! |
70 |
||
71 |
asNewIdentitySet |
|
72 |
"make sure to return myself as a unique new set" |
|
73 |
||
74 |
"could be an instance of a subclass..." |
|
75 |
self class == IdentitySet ifTrue:[ |
|
76 |
^ self copy |
|
77 |
]. |
|
78 |
^ super asIdentitySet |
|
79 |
||
80 |
" |
|
81 |
|s| |
|
82 |
s := #(1 2 3 4) asIdentitySet. |
|
83 |
self assert:(s ~~ s asNewIdentitySet). |
|
84 |
self assert:(s = s asNewIdentitySet). |
|
85 |
" |
|
86 |
! ! |
|
87 |
||
1 | 88 |
!IdentitySet methodsFor:'private'! |
89 |
||
14655 | 90 |
collisionsFor:key |
91 |
"Return the number of searches - 1 required for key" |
|
92 |
||
93 |
|index "{ Class:SmallInteger }" |
|
94 |
length "{ Class:SmallInteger }" startIndex probe count| |
|
95 |
||
96 |
length := keyArray basicSize. |
|
97 |
startIndex := index := self initialIndexForKey:key. |
|
98 |
||
99 |
count := 0. |
|
100 |
[true] whileTrue:[ |
|
101 |
probe := keyArray basicAt:index. |
|
102 |
(probe notNil and:[key == probe]) ifTrue:[^ count]. |
|
103 |
(self slotIsEmpty:probe) ifTrue:[self error:'non existing key']. |
|
104 |
||
105 |
index == length ifTrue:[ |
|
106 |
index := 1. |
|
107 |
] ifFalse:[ |
|
108 |
index := index + 1. |
|
109 |
]. |
|
110 |
count := count + 1. |
|
111 |
index == startIndex ifTrue:[self error:'non existing key']. |
|
112 |
] |
|
113 |
! |
|
114 |
||
1 | 115 |
find:key ifAbsent:aBlock |
116 |
"Look for the key in the receiver. If it is found, return |
|
117 |
the index of the slot containing the key, otherwise |
|
118 |
return the value of evaluating aBlock. |
|
119 |
Redefined to compare for identity instead of equality" |
|
120 |
||
6233 | 121 |
|index "{ Class:SmallInteger }" |
122 |
length "{ Class:SmallInteger }" |
|
123 |
startIndex probe | |
|
1 | 124 |
|
13 | 125 |
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
|
126 |
|
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
|
127 |
"/ |
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
|
128 |
"/ length < 10 ifTrue:[ |
6233 | 129 |
"/ "assuming, that for small sets the overhead of hashing |
130 |
"/ is large ..." |
|
131 |
"/ ^ 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
|
132 |
"/ ]. |
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
|
133 |
"/ |
10 | 134 |
|
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
135 |
startIndex := index := self initialIndexForKey:key. |
1 | 136 |
|
137 |
[true] whileTrue:[ |
|
6233 | 138 |
probe := keyArray basicAt:index. |
139 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
140 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
1 | 141 |
|
6233 | 142 |
index == length ifTrue:[ |
143 |
index := 1 |
|
144 |
] ifFalse:[ |
|
145 |
index := index + 1 |
|
146 |
]. |
|
147 |
index == startIndex ifTrue:[ |
|
148 |
^ aBlock value |
|
149 |
] |
|
1 | 150 |
] |
151 |
! |
|
152 |
||
13 | 153 |
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
|
154 |
"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
|
155 |
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
|
156 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 157 |
|
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
|
158 |
|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
|
159 |
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
|
160 |
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
|
161 |
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
|
162 |
|
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
|
163 |
delIndex := 0. |
1 | 164 |
|
13 | 165 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
166 |
startIndex := index := self initialIndexForKey:key. |
1 | 167 |
|
168 |
[true] whileTrue:[ |
|
6346 | 169 |
probe := keyArray basicAt:index. |
170 |
key == probe ifTrue:[^ index]. |
|
171 |
(self slotIsEmpty:probe) ifTrue:[ |
|
172 |
delIndex == 0 ifTrue:[^ index]. |
|
173 |
keyArray basicAt:delIndex put:nil. |
|
174 |
^ delIndex |
|
175 |
]. |
|
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 |
|
6346 | 177 |
probe == DeletedEntry ifTrue:[ |
178 |
delIndex == 0 ifTrue:[ |
|
179 |
delIndex := index |
|
180 |
] |
|
181 |
]. |
|
1 | 182 |
|
6346 | 183 |
index == length ifTrue:[ |
184 |
index := 1 |
|
185 |
] ifFalse:[ |
|
186 |
index := index + 1 |
|
187 |
]. |
|
188 |
index == startIndex ifTrue:[ |
|
189 |
delIndex ~~ 0 ifTrue:[ |
|
190 |
keyArray basicAt:delIndex put:nil. |
|
191 |
^ delIndex |
|
192 |
]. |
|
193 |
^ self grow findKeyOrNil:key |
|
194 |
]. |
|
1 | 195 |
] |
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
|
196 |
|
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
|
197 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 198 |
! |
199 |
||
2460 | 200 |
hashFor:aKey |
201 |
"return an initial index given a key." |
|
202 |
||
203 |
^ aKey identityHash |
|
204 |
||
205 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 206 |
! ! |
609 | 207 |
|
14018 | 208 |
!IdentitySet methodsFor:'set operations'! |
209 |
||
210 |
\ aCollection |
|
211 |
"return a new set containing all elements of the receiver, |
|
212 |
which are NOT also contained in the aCollection |
|
213 |
For large collections you better use a Set for aCollection. |
|
214 |
Redefined here to do identity comparison." |
|
215 |
||
216 |
|newCollection| |
|
217 |
||
218 |
newCollection := self speciesForAdding new. |
|
219 |
self do:[:element | |
|
220 |
(aCollection includesIdentical:element) ifFalse:[ |
|
221 |
newCollection add:element |
|
222 |
] |
|
223 |
]. |
|
224 |
^ newCollection |
|
225 |
||
226 |
" |
|
227 |
#(0 1 2 3 4 5 6 7 8 9) asIdentitySet \ #(1 2 3) asSet |
|
228 |
#(0 1 2 3 4 5 6 7 8 9) asIdentitySet \ #(1 2 3) |
|
229 |
" |
|
230 |
! ! |
|
231 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
232 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
233 |
|
5559 | 234 |
identicalContentsAs:aCollection |
235 |
"return true if the receiver and aCollection represent collections |
|
236 |
with identical contents (but not caring for order)." |
|
237 |
||
238 |
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
|
239 |
^ aCollection conform:[:eachElement | (self includesIdentical:eachElement)]. |
5559 | 240 |
|
241 |
" |
|
242 |
|col| |
|
243 |
||
244 |
col := #('aaa' 'bbb' 'ccc' 'ddd'). |
|
245 |
col identicalContentsAs:(col asIdentitySet). |
|
246 |
col identicalContentsAs:(col copy asIdentitySet). |
|
247 |
col identicalContentsAs:(col deepCopy asIdentitySet). |
|
248 |
" |
|
10091
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
249 |
|
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
250 |
"Modified: / 13-10-2006 / 12:55:43 / cg" |
5559 | 251 |
! |
252 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
253 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
254 |
"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
|
255 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
256 |
^ self includes:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
257 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
258 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
259 |
! ! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
260 |
|
634 | 261 |
!IdentitySet class methodsFor:'documentation'! |
262 |
||
263 |
version |
|
14998 | 264 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.35 2013-03-28 23:19:39 stefan Exp $' |
634 | 265 |
! ! |
14655 | 266 |