author | Claus Gittinger <cg@exept.de> |
Tue, 24 Aug 1999 12:49:24 +0200 | |
changeset 4625 | 1ebb8edfbd0d |
parent 3944 | 541580143566 |
child 5559 | 426dee57de44 |
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 |
||
13 |
Set subclass:#IdentitySet |
|
1290 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Unordered' |
|
1 | 18 |
! |
19 |
||
88 | 20 |
!IdentitySet class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
250 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
same as a Set but compares elements using == |
|
39 |
(i.e. they must be identical - not just equal in structure). |
|
40 |
Since compare is on identity (using ==), hashing is also done via |
|
41 |
#identityHash instead of #hash. |
|
1290 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
88 | 45 |
" |
46 |
! ! |
|
1 | 47 |
|
4625 | 48 |
!IdentitySet methodsFor:'adding & removing'! |
49 |
||
50 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
51 |
^ super remove:anObject ifAbsent:exceptionBlock |
|
52 |
! ! |
|
53 |
||
1 | 54 |
!IdentitySet methodsFor:'private'! |
55 |
||
56 |
find:key ifAbsent:aBlock |
|
57 |
"Look for the key in the receiver. If it is found, return |
|
58 |
the index of the slot containing the key, otherwise |
|
59 |
return the value of evaluating aBlock. |
|
60 |
Redefined to compare for identity instead of equality" |
|
61 |
||
62 |
|index "{ Class:SmallInteger }" |
|
3 | 63 |
length startIndex probe | |
1 | 64 |
|
13 | 65 |
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
|
66 |
|
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
|
67 |
"/ |
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 |
"/ 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
|
69 |
"/ "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
|
70 |
"/ 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
|
71 |
"/ ^ 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
|
72 |
"/ ]. |
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 |
"/ |
10 | 74 |
|
362 | 75 |
index := key identityHash. |
1975 | 76 |
index < 16r1FFFFFFF ifTrue:[ |
77 |
index := index * 2 |
|
78 |
]. |
|
79 |
index := index \\ length + 1. |
|
362 | 80 |
startIndex := index. |
1 | 81 |
|
82 |
[true] whileTrue:[ |
|
250 | 83 |
probe := (keyArray basicAt:index). |
84 |
probe == key ifTrue:[^ index]. |
|
85 |
probe isNil ifTrue:[^ aBlock value]. |
|
1 | 86 |
|
250 | 87 |
index == length ifTrue:[ |
88 |
index := 1 |
|
89 |
] ifFalse:[ |
|
90 |
index := index + 1 |
|
91 |
]. |
|
92 |
index == startIndex ifTrue:[ |
|
93 |
^ aBlock value |
|
94 |
] |
|
1 | 95 |
] |
96 |
! |
|
97 |
||
13 | 98 |
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
|
99 |
"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
|
100 |
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
|
101 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 102 |
|
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
|
103 |
|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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
|
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 := 0. |
1 | 109 |
|
13 | 110 |
length := keyArray basicSize. |
362 | 111 |
index := key identityHash. |
1975 | 112 |
index < 16r1FFFFFFF ifTrue:[ |
113 |
index := index * 2 |
|
114 |
]. |
|
115 |
||
116 |
index := index \\ length + 1. |
|
362 | 117 |
startIndex := index. |
1 | 118 |
|
119 |
[true] whileTrue:[ |
|
250 | 120 |
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
|
121 |
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
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
^ 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
|
126 |
]. |
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 |
|
250 | 128 |
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
|
129 |
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
|
130 |
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
|
131 |
] |
250 | 132 |
]. |
1 | 133 |
|
250 | 134 |
index == length ifTrue:[ |
135 |
index := 1 |
|
136 |
] ifFalse:[ |
|
137 |
index := index + 1 |
|
138 |
]. |
|
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
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
^ 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
|
143 |
]. |
250 | 144 |
^ 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
|
145 |
]. |
1 | 146 |
] |
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 |
|
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
|
148 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 149 |
! |
150 |
||
2460 | 151 |
hashFor:aKey |
152 |
"return an initial index given a key." |
|
153 |
||
154 |
^ aKey identityHash |
|
155 |
||
156 |
"Created: 19.3.1997 / 15:18:59 / cg" |
|
1 | 157 |
! ! |
609 | 158 |
|
3944
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
159 |
!IdentitySet methodsFor:'testing'! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
160 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
161 |
includesIdentical:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
162 |
"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
|
163 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
164 |
^ self includes:anObject |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
165 |
|
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
166 |
"Created: / 11.12.1998 / 20:01:12 / cg" |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
167 |
! ! |
541580143566
#includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents:
2474
diff
changeset
|
168 |
|
634 | 169 |
!IdentitySet class methodsFor:'documentation'! |
170 |
||
171 |
version |
|
4625 | 172 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.24 1999-08-24 10:49:24 cg Exp $' |
634 | 173 |
! ! |