author | Claus Gittinger <cg@exept.de> |
Fri, 21 Dec 2001 11:59:16 +0100 | |
changeset 6346 | a69610ec89a0 |
parent 6233 | 0a79cbd07543 |
child 6953 | 6b3a197638f0 |
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 |
||
6233 | 64 |
|index "{ Class:SmallInteger }" |
65 |
length "{ Class:SmallInteger }" |
|
66 |
startIndex probe | |
|
1 | 67 |
|
13 | 68 |
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
|
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 |
"/ |
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 |
"/ length < 10 ifTrue:[ |
6233 | 72 |
"/ "assuming, that for small sets the overhead of hashing |
73 |
"/ is large ..." |
|
74 |
"/ ^ 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
|
75 |
"/ ]. |
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
|
76 |
"/ |
10 | 77 |
|
362 | 78 |
index := key identityHash. |
1975 | 79 |
index < 16r1FFFFFFF ifTrue:[ |
80 |
index := index * 2 |
|
81 |
]. |
|
82 |
index := index \\ length + 1. |
|
362 | 83 |
startIndex := index. |
1 | 84 |
|
85 |
[true] whileTrue:[ |
|
6233 | 86 |
probe := keyArray basicAt:index. |
87 |
probe == key ifTrue:[^ index]. "<<<< == is different from inherited" |
|
88 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value]. |
|
1 | 89 |
|
6233 | 90 |
index == length ifTrue:[ |
91 |
index := 1 |
|
92 |
] ifFalse:[ |
|
93 |
index := index + 1 |
|
94 |
]. |
|
95 |
index == startIndex ifTrue:[ |
|
96 |
^ aBlock value |
|
97 |
] |
|
1 | 98 |
] |
99 |
! |
|
100 |
||
13 | 101 |
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
|
102 |
"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
|
103 |
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
|
104 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 105 |
|
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
|
106 |
|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
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
|
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 |
delIndex := 0. |
1 | 112 |
|
13 | 113 |
length := keyArray basicSize. |
362 | 114 |
index := key identityHash. |
1975 | 115 |
index < 16r1FFFFFFF ifTrue:[ |
116 |
index := index * 2 |
|
117 |
]. |
|
118 |
||
119 |
index := index \\ length + 1. |
|
362 | 120 |
startIndex := index. |
1 | 121 |
|
122 |
[true] whileTrue:[ |
|
6346 | 123 |
probe := keyArray basicAt:index. |
124 |
key == probe ifTrue:[^ index]. |
|
125 |
(self slotIsEmpty:probe) ifTrue:[ |
|
126 |
delIndex == 0 ifTrue:[^ index]. |
|
127 |
keyArray basicAt:delIndex put:nil. |
|
128 |
^ delIndex |
|
129 |
]. |
|
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
|
130 |
|
6346 | 131 |
probe == DeletedEntry ifTrue:[ |
132 |
delIndex == 0 ifTrue:[ |
|
133 |
delIndex := index |
|
134 |
] |
|
135 |
]. |
|
1 | 136 |
|
6346 | 137 |
index == length ifTrue:[ |
138 |
index := 1 |
|
139 |
] ifFalse:[ |
|
140 |
index := index + 1 |
|
141 |
]. |
|
142 |
index == startIndex ifTrue:[ |
|
143 |
delIndex ~~ 0 ifTrue:[ |
|
144 |
keyArray basicAt:delIndex put:nil. |
|
145 |
^ delIndex |
|
146 |
]. |
|
147 |
^ self grow findKeyOrNil:key |
|
148 |
]. |
|
1 | 149 |
] |
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
|
150 |
|
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
|
151 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 152 |
! |
153 |
||
2460 | 154 |
hashFor:aKey |
155 |
"return an initial index given a key." |
|
156 |
||
157 |
^ aKey identityHash |
|
158 |
||
159 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 160 |
! ! |
609 | 161 |
|
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
162 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
163 |
|
5559 | 164 |
identicalContentsAs:aCollection |
165 |
"return true if the receiver and aCollection represent collections |
|
166 |
with identical contents (but not caring for order)." |
|
167 |
||
168 |
aCollection size == self size ifFalse:[^ false]. |
|
169 |
aCollection do:[:eachElement | |
|
170 |
(self includesIdentical:eachElement) ifFalse:[^ false] |
|
171 |
]. |
|
172 |
^ true |
|
173 |
||
174 |
" |
|
175 |
|col| |
|
176 |
||
177 |
col := #('aaa' 'bbb' 'ccc' 'ddd'). |
|
178 |
col identicalContentsAs:(col asIdentitySet). |
|
179 |
col identicalContentsAs:(col copy asIdentitySet). |
|
180 |
col identicalContentsAs:(col deepCopy asIdentitySet). |
|
181 |
" |
|
182 |
! |
|
183 |
||
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
184 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
185 |
"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
|
186 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
187 |
^ self includes:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
188 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
189 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
190 |
! ! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
191 |
|
634 | 192 |
!IdentitySet class methodsFor:'documentation'! |
193 |
||
194 |
version |
|
6346 | 195 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.27 2001-12-21 10:59:02 cg Exp $' |
634 | 196 |
! ! |