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