author | Claus Gittinger <cg@exept.de> |
Fri, 08 Nov 1996 16:27:05 +0100 | |
changeset 1938 | e1a56370a833 |
parent 1290 | 15ba3221b89b |
child 1972 | f9bfcad48b64 |
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. |
70 |
index := index \\ length + 1. |
|
71 |
startIndex := index. |
|
1 | 72 |
|
73 |
[true] whileTrue:[ |
|
250 | 74 |
probe := (keyArray basicAt:index). |
75 |
probe == key ifTrue:[^ index]. |
|
76 |
probe isNil ifTrue:[^ aBlock value]. |
|
1 | 77 |
|
250 | 78 |
index == length ifTrue:[ |
79 |
index := 1 |
|
80 |
] ifFalse:[ |
|
81 |
index := index + 1 |
|
82 |
]. |
|
83 |
index == startIndex ifTrue:[ |
|
84 |
^ aBlock value |
|
85 |
] |
|
1 | 86 |
] |
87 |
! |
|
88 |
||
13 | 89 |
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
|
90 |
"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
|
91 |
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
|
92 |
Grow the receiver, if key was not found, and no unused slots were present" |
1 | 93 |
|
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
|
94 |
|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
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
|
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 |
delIndex := 0. |
1 | 100 |
|
13 | 101 |
length := keyArray basicSize. |
362 | 102 |
index := key identityHash. |
103 |
index := index \\ length + 1. |
|
104 |
startIndex := index. |
|
1 | 105 |
|
106 |
[true] whileTrue:[ |
|
250 | 107 |
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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
^ 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
|
113 |
]. |
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 |
|
250 | 115 |
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
|
116 |
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
|
117 |
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
|
118 |
] |
250 | 119 |
]. |
1 | 120 |
|
250 | 121 |
index == length ifTrue:[ |
122 |
index := 1 |
|
123 |
] ifFalse:[ |
|
124 |
index := index + 1 |
|
125 |
]. |
|
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
|
126 |
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
|
127 |
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
|
128 |
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
|
129 |
^ 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
|
130 |
]. |
250 | 131 |
^ 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
|
132 |
]. |
1 | 133 |
] |
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
|
134 |
|
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 |
"Modified: 26.3.1996 / 20:00:42 / cg" |
10 | 136 |
! |
137 |
||
138 |
findNil:key |
|
139 |
"Look for the next slot usable for key. This method assumes that |
|
140 |
key is not already in the receiver - used only while growing/rehashing" |
|
141 |
||
142 |
|index "{ Class:SmallInteger }" |
|
143 |
length| |
|
144 |
||
13 | 145 |
length := keyArray basicSize. |
362 | 146 |
index := key identityHash. |
147 |
index := index \\ length + 1. |
|
10 | 148 |
|
13 | 149 |
[(keyArray basicAt:index) notNil] whileTrue:[ |
250 | 150 |
index == length ifTrue:[ |
151 |
index := 1 |
|
152 |
] ifFalse:[ |
|
153 |
index := index + 1 |
|
154 |
]. |
|
155 |
"notice: no check for no nil found - we must find one since |
|
156 |
this is only called after growing" |
|
10 | 157 |
]. |
158 |
^ index |
|
159 |
||
1 | 160 |
! ! |
609 | 161 |
|
634 | 162 |
!IdentitySet class methodsFor:'documentation'! |
163 |
||
164 |
version |
|
1290 | 165 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/IdSet.st,v 1.18 1996-04-25 16:18:39 cg Exp $' |
634 | 166 |
! ! |