author | Stefan Vogel <sv@exept.de> |
Wed, 09 Nov 2005 16:56:10 +0100 | |
changeset 8979 | 869510c58b9c |
parent 7310 | 8865e8d343cd |
child 10091 | ad0bf1206ce0 |
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 |
" |
|
12 |
||
5559 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
Set subclass:#IdentitySet |
1290 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Unordered' |
|
1 | 20 |
! |
21 |
||
88 | 22 |
!IdentitySet class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
250 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
same as a Set but compares elements using == |
|
41 |
(i.e. they must be identical - not just equal in structure). |
|
42 |
Since compare is on identity (using ==), hashing is also done via |
|
43 |
#identityHash instead of #hash. |
|
1290 | 44 |
|
45 |
[author:] |
|
46 |
Claus Gittinger |
|
88 | 47 |
" |
48 |
! ! |
|
1 | 49 |
|
7310
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
50 |
!IdentitySet methodsFor:'Compatibility-Squeak'! |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
51 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
52 |
copyWithout:anElement |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
53 |
^ self select:[:each | each ~~ anElement] |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
54 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
55 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
56 |
#(1 2 3 4 5 6 7) asSet copyWithout:5 |
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 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
60 |
copyWithoutAll:aCollection |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
61 |
^ self select:[:each | (aCollection includesIdentical:each)] |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
62 |
|
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
63 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
64 |
#(1 2 3 4 5 6 7) asSet copyWithoutAll:#(3 5 7 9) |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
65 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
66 |
! ! |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
67 |
|
4625 | 68 |
!IdentitySet methodsFor:'adding & removing'! |
69 |
||
70 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
71 |
^ super remove:anObject ifAbsent:exceptionBlock |
|
72 |
! ! |
|
73 |
||
1 | 74 |
!IdentitySet methodsFor:'private'! |
75 |
||
76 |
find:key ifAbsent:aBlock |
|
77 |
"Look for the key in the receiver. If it is found, return |
|
78 |
the index of the slot containing the key, otherwise |
|
79 |
return the value of evaluating aBlock. |
|
80 |
Redefined to compare for identity instead of equality" |
|
81 |
||
6233 | 82 |
|index "{ Class:SmallInteger }" |
83 |
length "{ Class:SmallInteger }" |
|
84 |
startIndex probe | |
|
1 | 85 |
|
13 | 86 |
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
|
87 |
|
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 |
"/ length < 10 ifTrue:[ |
6233 | 90 |
"/ "assuming, that for small sets the overhead of hashing |
91 |
"/ is large ..." |
|
92 |
"/ ^ 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
|
93 |
"/ ]. |
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
|
94 |
"/ |
10 | 95 |
|
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
96 |
startIndex := index := self initialIndexForKey:key. |
1 | 97 |
|
98 |
[true] whileTrue:[ |
|
6233 | 99 |
probe := keyArray basicAt:index. |
100 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
101 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
1 | 102 |
|
6233 | 103 |
index == length ifTrue:[ |
104 |
index := 1 |
|
105 |
] ifFalse:[ |
|
106 |
index := index + 1 |
|
107 |
]. |
|
108 |
index == startIndex ifTrue:[ |
|
109 |
^ aBlock value |
|
110 |
] |
|
1 | 111 |
] |
112 |
! |
|
113 |
||
13 | 114 |
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
|
115 |
"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
|
116 |
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
|
117 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 118 |
|
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
|
119 |
|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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
|
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
|
124 |
delIndex := 0. |
1 | 125 |
|
13 | 126 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
127 |
startIndex := index := self initialIndexForKey:key. |
1 | 128 |
|
129 |
[true] whileTrue:[ |
|
6346 | 130 |
probe := keyArray basicAt:index. |
131 |
key == probe ifTrue:[^ index]. |
|
132 |
(self slotIsEmpty:probe) ifTrue:[ |
|
133 |
delIndex == 0 ifTrue:[^ index]. |
|
134 |
keyArray basicAt:delIndex put:nil. |
|
135 |
^ delIndex |
|
136 |
]. |
|
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
|
137 |
|
6346 | 138 |
probe == DeletedEntry ifTrue:[ |
139 |
delIndex == 0 ifTrue:[ |
|
140 |
delIndex := index |
|
141 |
] |
|
142 |
]. |
|
1 | 143 |
|
6346 | 144 |
index == length ifTrue:[ |
145 |
index := 1 |
|
146 |
] ifFalse:[ |
|
147 |
index := index + 1 |
|
148 |
]. |
|
149 |
index == startIndex ifTrue:[ |
|
150 |
delIndex ~~ 0 ifTrue:[ |
|
151 |
keyArray basicAt:delIndex put:nil. |
|
152 |
^ delIndex |
|
153 |
]. |
|
154 |
^ self grow findKeyOrNil:key |
|
155 |
]. |
|
1 | 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 |
|
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 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 159 |
! |
160 |
||
2460 | 161 |
hashFor:aKey |
162 |
"return an initial index given a key." |
|
163 |
||
164 |
^ aKey identityHash |
|
165 |
||
166 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 167 |
! ! |
609 | 168 |
|
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
169 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
170 |
|
5559 | 171 |
identicalContentsAs:aCollection |
172 |
"return true if the receiver and aCollection represent collections |
|
173 |
with identical contents (but not caring for order)." |
|
174 |
||
175 |
aCollection size == self size ifFalse:[^ false]. |
|
176 |
aCollection do:[:eachElement | |
|
177 |
(self includesIdentical:eachElement) ifFalse:[^ false] |
|
178 |
]. |
|
179 |
^ true |
|
180 |
||
181 |
" |
|
182 |
|col| |
|
183 |
||
184 |
col := #('aaa' 'bbb' 'ccc' 'ddd'). |
|
185 |
col identicalContentsAs:(col asIdentitySet). |
|
186 |
col identicalContentsAs:(col copy asIdentitySet). |
|
187 |
col identicalContentsAs:(col deepCopy asIdentitySet). |
|
188 |
" |
|
189 |
! |
|
190 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
191 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
192 |
"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
|
193 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
194 |
^ self includes:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
195 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
196 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
197 |
! ! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
198 |
|
634 | 199 |
!IdentitySet class methodsFor:'documentation'! |
200 |
||
201 |
version |
|
7310
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
202 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.29 2003-05-19 18:36:05 cg Exp $' |
634 | 203 |
! ! |