author | Claus Gittinger <cg@exept.de> |
Fri, 09 Nov 2001 16:18:46 +0100 | |
changeset 6172 | a3c88ea5efe9 |
parent 5559 | 426dee57de44 |
child 6233 | 0a79cbd07543 |
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 |
|
4625 | 50 |
!IdentitySet methodsFor:'adding & removing'! |
51 |
||
52 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
53 |
^ super remove:anObject ifAbsent:exceptionBlock |
|
54 |
! ! |
|
55 |
||
1 | 56 |
!IdentitySet methodsFor:'private'! |
57 |
||
58 |
find:key ifAbsent:aBlock |
|
59 |
"Look for the key in the receiver. If it is found, return |
|
60 |
the index of the slot containing the key, otherwise |
|
61 |
return the value of evaluating aBlock. |
|
62 |
Redefined to compare for identity instead of equality" |
|
63 |
||
64 |
|index "{ Class:SmallInteger }" |
|
3 | 65 |
length startIndex probe | |
1 | 66 |
|
13 | 67 |
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
|
68 |
|
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
|
69 |
"/ |
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
|
70 |
"/ length < 10 ifTrue:[ |
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
|
71 |
"/ "assuming, that for small sets the overhead of hashing |
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
|
72 |
"/ is large ..." |
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
|
73 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock. |
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
|
74 |
"/ ]. |
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
|
75 |
"/ |
10 | 76 |
|
362 | 77 |
index := key identityHash. |
1975 | 78 |
index < 16r1FFFFFFF ifTrue:[ |
79 |
index := index * 2 |
|
80 |
]. |
|
81 |
index := index \\ length + 1. |
|
362 | 82 |
startIndex := index. |
1 | 83 |
|
84 |
[true] whileTrue:[ |
|
250 | 85 |
probe := (keyArray basicAt:index). |
86 |
probe == key ifTrue:[^ index]. |
|
87 |
probe isNil ifTrue:[^ aBlock value]. |
|
1 | 88 |
|
250 | 89 |
index == length ifTrue:[ |
90 |
index := 1 |
|
91 |
] ifFalse:[ |
|
92 |
index := index + 1 |
|
93 |
]. |
|
94 |
index == startIndex ifTrue:[ |
|
95 |
^ aBlock value |
|
96 |
] |
|
1 | 97 |
] |
98 |
! |
|
99 |
||
13 | 100 |
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
|
101 |
"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
|
102 |
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
|
103 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 104 |
|
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
|
105 |
|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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
|
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 |
delIndex := 0. |
1 | 111 |
|
13 | 112 |
length := keyArray basicSize. |
362 | 113 |
index := key identityHash. |
1975 | 114 |
index < 16r1FFFFFFF ifTrue:[ |
115 |
index := index * 2 |
|
116 |
]. |
|
117 |
||
118 |
index := index \\ length + 1. |
|
362 | 119 |
startIndex := index. |
1 | 120 |
|
121 |
[true] whileTrue:[ |
|
250 | 122 |
probe := keyArray basicAt:index. |
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
|
123 |
key == probe ifTrue:[^ index]. |
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 |
probe isNil ifTrue:[ |
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
|
125 |
delIndex == 0 ifTrue:[^ index]. |
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
|
126 |
keyArray basicAt:delIndex put:nil. |
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
|
127 |
^ delIndex |
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
|
128 |
]. |
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
|
129 |
|
250 | 130 |
probe == DeletedEntry ifTrue:[ |
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
|
131 |
delIndex == 0 ifTrue:[ |
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 |
delIndex := index |
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
|
133 |
] |
250 | 134 |
]. |
1 | 135 |
|
250 | 136 |
index == length ifTrue:[ |
137 |
index := 1 |
|
138 |
] ifFalse:[ |
|
139 |
index := index + 1 |
|
140 |
]. |
|
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
|
141 |
index == startIndex ifTrue:[ |
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 ~~ 0 ifTrue:[ |
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 |
keyArray basicAt:delIndex put:nil. |
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 |
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
|
145 |
]. |
250 | 146 |
^ self grow 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
|
147 |
]. |
1 | 148 |
] |
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
|
149 |
|
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
|
150 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 151 |
! |
152 |
||
2460 | 153 |
hashFor:aKey |
154 |
"return an initial index given a key." |
|
155 |
||
156 |
^ aKey identityHash |
|
157 |
||
158 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 159 |
! ! |
609 | 160 |
|
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
161 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
162 |
|
5559 | 163 |
identicalContentsAs:aCollection |
164 |
"return true if the receiver and aCollection represent collections |
|
165 |
with identical contents (but not caring for order)." |
|
166 |
||
167 |
aCollection size == self size ifFalse:[^ false]. |
|
168 |
aCollection do:[:eachElement | |
|
169 |
(self includesIdentical:eachElement) ifFalse:[^ false] |
|
170 |
]. |
|
171 |
^ true |
|
172 |
||
173 |
" |
|
174 |
|col| |
|
175 |
||
176 |
col := #('aaa' 'bbb' 'ccc' 'ddd'). |
|
177 |
col identicalContentsAs:(col asIdentitySet). |
|
178 |
col identicalContentsAs:(col copy asIdentitySet). |
|
179 |
col identicalContentsAs:(col deepCopy asIdentitySet). |
|
180 |
" |
|
181 |
! |
|
182 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
183 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
184 |
"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
|
185 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
186 |
^ self includes:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
187 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
188 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
189 |
! ! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
190 |
|
634 | 191 |
!IdentitySet class methodsFor:'documentation'! |
192 |
||
193 |
version |
|
5559 | 194 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.25 2000-08-22 19:52:01 cg Exp $' |
634 | 195 |
! ! |