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