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