author | Claus Gittinger <cg@exept.de> |
Tue, 12 Nov 1996 16:13:57 +0100 | |
changeset 1975 | c94a8e0b0251 |
parent 1972 | f9bfcad48b64 |
child 2460 | 1d7caef5f5ed |
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 |
|
48 |
!IdentitySet methodsFor:'private'! |
|
49 |
||
50 |
find:key ifAbsent:aBlock |
|
51 |
"Look for the key in the receiver. If it is found, return |
|
52 |
the index of the slot containing the key, otherwise |
|
53 |
return the value of evaluating aBlock. |
|
54 |
Redefined to compare for identity instead of equality" |
|
55 |
||
56 |
|index "{ Class:SmallInteger }" |
|
3 | 57 |
length startIndex probe | |
1 | 58 |
|
13 | 59 |
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
|
60 |
|
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
|
61 |
"/ |
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
|
62 |
"/ 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
|
63 |
"/ "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
|
64 |
"/ 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
|
65 |
"/ ^ 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
|
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 |
"/ |
10 | 68 |
|
362 | 69 |
index := key identityHash. |
1975 | 70 |
index < 16r1FFFFFFF ifTrue:[ |
71 |
index := index * 2 |
|
72 |
]. |
|
73 |
index := index \\ length + 1. |
|
362 | 74 |
startIndex := index. |
1 | 75 |
|
76 |
[true] whileTrue:[ |
|
250 | 77 |
probe := (keyArray basicAt:index). |
78 |
probe == key ifTrue:[^ index]. |
|
79 |
probe isNil ifTrue:[^ aBlock value]. |
|
1 | 80 |
|
250 | 81 |
index == length ifTrue:[ |
82 |
index := 1 |
|
83 |
] ifFalse:[ |
|
84 |
index := index + 1 |
|
85 |
]. |
|
86 |
index == startIndex ifTrue:[ |
|
87 |
^ aBlock value |
|
88 |
] |
|
1 | 89 |
] |
90 |
! |
|
91 |
||
13 | 92 |
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
|
93 |
"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
|
94 |
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
|
95 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 96 |
|
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
|
97 |
|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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
|
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 |
delIndex := 0. |
1 | 103 |
|
13 | 104 |
length := keyArray basicSize. |
362 | 105 |
index := key identityHash. |
1975 | 106 |
index < 16r1FFFFFFF ifTrue:[ |
107 |
index := index * 2 |
|
108 |
]. |
|
109 |
||
110 |
index := index \\ length + 1. |
|
362 | 111 |
startIndex := index. |
1 | 112 |
|
113 |
[true] whileTrue:[ |
|
250 | 114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
^ 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
|
120 |
]. |
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 |
|
250 | 122 |
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
|
123 |
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
|
124 |
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
|
125 |
] |
250 | 126 |
]. |
1 | 127 |
|
250 | 128 |
index == length ifTrue:[ |
129 |
index := 1 |
|
130 |
] ifFalse:[ |
|
131 |
index := index + 1 |
|
132 |
]. |
|
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
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
^ 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
|
137 |
]. |
250 | 138 |
^ 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
|
139 |
]. |
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 |
|
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 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 143 |
! |
144 |
||
145 |
findNil:key |
|
146 |
"Look for the next slot usable for key. This method assumes that |
|
147 |
key is not already in the receiver - used only while growing/rehashing" |
|
148 |
||
149 |
|index "{ Class:SmallInteger }" |
|
150 |
length| |
|
151 |
||
13 | 152 |
length := keyArray basicSize. |
362 | 153 |
index := key identityHash. |
1975 | 154 |
index < 16r1FFFFFFF ifTrue:[ |
155 |
index := index * 2 |
|
156 |
]. |
|
157 |
index := index \\ length + 1. |
|
10 | 158 |
|
13 | 159 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
250 | 160 |
index == length ifTrue:[ |
161 |
index := 1 |
|
162 |
] ifFalse:[ |
|
163 |
index := index + 1 |
|
164 |
]. |
|
165 |
"notice: no check for no nil found - we must find one since |
|
166 |
this is only called after growing" |
|
10 | 167 |
]. |
168 |
^ index |
|
169 |
||
1 | 170 |
! ! |
609 | 171 |
|
634 | 172 |
!IdentitySet class methodsFor:'documentation'! |
173 |
||
174 |
version |
|
1975 | 175 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.20 1996-11-12 15:13:55 cg Exp $' |
634 | 176 |
! ! |