author | Claus Gittinger <cg@exept.de> |
Fri, 13 Oct 2006 13:04:26 +0200 | |
changeset 10091 | ad0bf1206ce0 |
parent 7310 | 8865e8d343cd |
child 11051 | 029e6b331d96 |
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 |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
52 |
^ self select:[:each | each ~~ anElement] |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
53 |
|
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 |
#(1 2 3 4 5 6 7) asSet copyWithout:5 |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
56 |
" |
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 |
copyWithoutAll:aCollection |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
60 |
^ self select:[:each | (aCollection includesIdentical:each)] |
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 |
" |
8865e8d343cd
+copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents:
6953
diff
changeset
|
63 |
#(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
|
64 |
" |
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 |
|
4625 | 67 |
!IdentitySet methodsFor:'adding & removing'! |
68 |
||
69 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
70 |
^ super remove:anObject ifAbsent:exceptionBlock |
|
71 |
! ! |
|
72 |
||
1 | 73 |
!IdentitySet methodsFor:'private'! |
74 |
||
75 |
find:key ifAbsent:aBlock |
|
76 |
"Look for the key in the receiver. If it is found, return |
|
77 |
the index of the slot containing the key, otherwise |
|
78 |
return the value of evaluating aBlock. |
|
79 |
Redefined to compare for identity instead of equality" |
|
80 |
||
6233 | 81 |
|index "{ Class:SmallInteger }" |
82 |
length "{ Class:SmallInteger }" |
|
83 |
startIndex probe | |
|
1 | 84 |
|
13 | 85 |
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
|
86 |
|
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 |
"/ length < 10 ifTrue:[ |
6233 | 89 |
"/ "assuming, that for small sets the overhead of hashing |
90 |
"/ is large ..." |
|
91 |
"/ ^ 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
|
92 |
"/ ]. |
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 |
"/ |
10 | 94 |
|
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
95 |
startIndex := index := self initialIndexForKey:key. |
1 | 96 |
|
97 |
[true] whileTrue:[ |
|
6233 | 98 |
probe := keyArray basicAt:index. |
99 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
100 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
1 | 101 |
|
6233 | 102 |
index == length ifTrue:[ |
103 |
index := 1 |
|
104 |
] ifFalse:[ |
|
105 |
index := index + 1 |
|
106 |
]. |
|
107 |
index == startIndex ifTrue:[ |
|
108 |
^ aBlock value |
|
109 |
] |
|
1 | 110 |
] |
111 |
! |
|
112 |
||
13 | 113 |
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
|
114 |
"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
|
115 |
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
|
116 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 117 |
|
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
|
118 |
|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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
|
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 |
delIndex := 0. |
1 | 124 |
|
13 | 125 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6346
diff
changeset
|
126 |
startIndex := index := self initialIndexForKey:key. |
1 | 127 |
|
128 |
[true] whileTrue:[ |
|
6346 | 129 |
probe := keyArray basicAt:index. |
130 |
key == probe ifTrue:[^ index]. |
|
131 |
(self slotIsEmpty:probe) ifTrue:[ |
|
132 |
delIndex == 0 ifTrue:[^ index]. |
|
133 |
keyArray basicAt:delIndex put:nil. |
|
134 |
^ delIndex |
|
135 |
]. |
|
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 |
|
6346 | 137 |
probe == DeletedEntry ifTrue:[ |
138 |
delIndex == 0 ifTrue:[ |
|
139 |
delIndex := index |
|
140 |
] |
|
141 |
]. |
|
1 | 142 |
|
6346 | 143 |
index == length ifTrue:[ |
144 |
index := 1 |
|
145 |
] ifFalse:[ |
|
146 |
index := index + 1 |
|
147 |
]. |
|
148 |
index == startIndex ifTrue:[ |
|
149 |
delIndex ~~ 0 ifTrue:[ |
|
150 |
keyArray basicAt:delIndex put:nil. |
|
151 |
^ delIndex |
|
152 |
]. |
|
153 |
^ self grow findKeyOrNil:key |
|
154 |
]. |
|
1 | 155 |
] |
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
|
156 |
|
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 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 158 |
! |
159 |
||
2460 | 160 |
hashFor:aKey |
161 |
"return an initial index given a key." |
|
162 |
||
163 |
^ aKey identityHash |
|
164 |
||
165 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 166 |
! ! |
609 | 167 |
|
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
168 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
169 |
|
5559 | 170 |
identicalContentsAs:aCollection |
171 |
"return true if the receiver and aCollection represent collections |
|
172 |
with identical contents (but not caring for order)." |
|
173 |
||
174 |
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
|
175 |
^ aCollection conform:[:eachElement | (self includesIdentical:eachElement)]. |
5559 | 176 |
|
177 |
" |
|
178 |
|col| |
|
179 |
||
180 |
col := #('aaa' 'bbb' 'ccc' 'ddd'). |
|
181 |
col identicalContentsAs:(col asIdentitySet). |
|
182 |
col identicalContentsAs:(col copy asIdentitySet). |
|
183 |
col identicalContentsAs:(col deepCopy asIdentitySet). |
|
184 |
" |
|
10091
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
185 |
|
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
186 |
"Modified: / 13-10-2006 / 12:55:43 / cg" |
5559 | 187 |
! |
188 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
189 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
190 |
"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
|
191 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
192 |
^ self includes:anObject |
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 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
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 |
|
634 | 197 |
!IdentitySet class methodsFor:'documentation'! |
198 |
||
199 |
version |
|
10091
ad0bf1206ce0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
7310
diff
changeset
|
200 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.30 2006-10-13 11:04:26 cg Exp $' |
634 | 201 |
! ! |