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
|
250
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
1
|
18 |
!
|
|
19 |
|
|
20 |
IdentitySet comment:'
|
88
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
250
|
22 |
All Rights Reserved
|
92
|
23 |
|
379
|
24 |
$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.12 1995-08-10 12:28:32 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!IdentitySet class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
250
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
379
|
45 |
$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.12 1995-08-10 12:28:32 claus Exp $
|
|
46 |
$Revision: 1.12 $
|
88
|
47 |
"
|
|
48 |
!
|
1
|
49 |
|
88
|
50 |
documentation
|
|
51 |
"
|
|
52 |
same as a Set but compares elements using ==
|
|
53 |
(i.e. they must be identical - not just equal in structure).
|
|
54 |
Since compare is on identity (using ==), hashing is also done via
|
|
55 |
#identityHash instead of #hash.
|
|
56 |
"
|
|
57 |
! !
|
1
|
58 |
|
|
59 |
!IdentitySet methodsFor:'private'!
|
|
60 |
|
|
61 |
find:key ifAbsent:aBlock
|
|
62 |
"Look for the key in the receiver. If it is found, return
|
|
63 |
the index of the slot containing the key, otherwise
|
|
64 |
return the value of evaluating aBlock.
|
|
65 |
Redefined to compare for identity instead of equality"
|
|
66 |
|
|
67 |
|index "{ Class:SmallInteger }"
|
3
|
68 |
length startIndex probe |
|
1
|
69 |
|
13
|
70 |
length := keyArray basicSize.
|
10
|
71 |
length < 10 ifTrue:[
|
250
|
72 |
"assuming, that for small sets the overhead of hashing
|
|
73 |
is large ..."
|
|
74 |
^ keyArray identityIndexOf:key ifAbsent:aBlock.
|
10
|
75 |
].
|
|
76 |
|
362
|
77 |
index := key identityHash.
|
|
78 |
index := index \\ length + 1.
|
|
79 |
startIndex := index.
|
1
|
80 |
|
|
81 |
[true] whileTrue:[
|
250
|
82 |
probe := (keyArray basicAt:index).
|
|
83 |
probe == key ifTrue:[^ index].
|
|
84 |
probe isNil ifTrue:[^ aBlock value].
|
1
|
85 |
|
250
|
86 |
index == length ifTrue:[
|
|
87 |
index := 1
|
|
88 |
] ifFalse:[
|
|
89 |
index := index + 1
|
|
90 |
].
|
|
91 |
index == startIndex ifTrue:[
|
|
92 |
^ aBlock value
|
|
93 |
]
|
1
|
94 |
]
|
|
95 |
!
|
|
96 |
|
13
|
97 |
findKeyOrNil:key
|
250
|
98 |
"Look for the key in the receiver. If it is found, return
|
|
99 |
the index of the slot containing the key, otherwise
|
|
100 |
return the index of the first unused slot. Grow the receiver,
|
|
101 |
if key was not found, and no unused slots where present.
|
|
102 |
Redefined to compare for identity instead of equality"
|
1
|
103 |
|
|
104 |
|index "{ Class:SmallInteger }"
|
3
|
105 |
length startIndex probe |
|
1
|
106 |
|
13
|
107 |
length := keyArray basicSize.
|
362
|
108 |
index := key identityHash.
|
|
109 |
index := index \\ length + 1.
|
|
110 |
startIndex := index.
|
1
|
111 |
|
|
112 |
[true] whileTrue:[
|
250
|
113 |
probe := keyArray basicAt:index.
|
|
114 |
(probe isNil or: [key == probe]) ifTrue:[^ index].
|
|
115 |
probe == DeletedEntry ifTrue:[
|
|
116 |
keyArray basicAt:index put:nil.
|
|
117 |
^ index
|
|
118 |
].
|
1
|
119 |
|
250
|
120 |
index == length ifTrue:[
|
|
121 |
index := 1
|
|
122 |
] ifFalse:[
|
|
123 |
index := index + 1
|
|
124 |
].
|
|
125 |
index == startIndex ifTrue: [
|
|
126 |
^ self grow findKeyOrNil:key
|
|
127 |
]
|
1
|
128 |
]
|
10
|
129 |
!
|
|
130 |
|
|
131 |
findNil:key
|
|
132 |
"Look for the next slot usable for key. This method assumes that
|
|
133 |
key is not already in the receiver - used only while growing/rehashing"
|
|
134 |
|
|
135 |
|index "{ Class:SmallInteger }"
|
|
136 |
length|
|
|
137 |
|
13
|
138 |
length := keyArray basicSize.
|
362
|
139 |
index := key identityHash.
|
|
140 |
index := index \\ length + 1.
|
10
|
141 |
|
13
|
142 |
[(keyArray basicAt:index) notNil] whileTrue:[
|
250
|
143 |
index == length ifTrue:[
|
|
144 |
index := 1
|
|
145 |
] ifFalse:[
|
|
146 |
index := index + 1
|
|
147 |
].
|
|
148 |
"notice: no check for no nil found - we must find one since
|
|
149 |
this is only called after growing"
|
10
|
150 |
].
|
|
151 |
^ index
|
|
152 |
|
1
|
153 |
! !
|