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 |
|
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.
|
|
42 |
"
|
|
43 |
! !
|
1
|
44 |
|
|
45 |
!IdentitySet methodsFor:'private'!
|
|
46 |
|
|
47 |
find:key ifAbsent:aBlock
|
|
48 |
"Look for the key in the receiver. If it is found, return
|
|
49 |
the index of the slot containing the key, otherwise
|
|
50 |
return the value of evaluating aBlock.
|
|
51 |
Redefined to compare for identity instead of equality"
|
|
52 |
|
|
53 |
|index "{ Class:SmallInteger }"
|
3
|
54 |
length startIndex probe |
|
1
|
55 |
|
13
|
56 |
length := keyArray basicSize.
|
10
|
57 |
length < 10 ifTrue:[
|
250
|
58 |
"assuming, that for small sets the overhead of hashing
|
|
59 |
is large ..."
|
|
60 |
^ keyArray identityIndexOf:key ifAbsent:aBlock.
|
10
|
61 |
].
|
|
62 |
|
362
|
63 |
index := key identityHash.
|
|
64 |
index := index \\ length + 1.
|
|
65 |
startIndex := index.
|
1
|
66 |
|
|
67 |
[true] whileTrue:[
|
250
|
68 |
probe := (keyArray basicAt:index).
|
|
69 |
probe == key ifTrue:[^ index].
|
|
70 |
probe isNil ifTrue:[^ aBlock value].
|
1
|
71 |
|
250
|
72 |
index == length ifTrue:[
|
|
73 |
index := 1
|
|
74 |
] ifFalse:[
|
|
75 |
index := index + 1
|
|
76 |
].
|
|
77 |
index == startIndex ifTrue:[
|
|
78 |
^ aBlock value
|
|
79 |
]
|
1
|
80 |
]
|
|
81 |
!
|
|
82 |
|
13
|
83 |
findKeyOrNil:key
|
250
|
84 |
"Look for the key in the receiver. If it is found, return
|
|
85 |
the index of the slot containing the key, otherwise
|
|
86 |
return the index of the first unused slot. Grow the receiver,
|
|
87 |
if key was not found, and no unused slots where present.
|
|
88 |
Redefined to compare for identity instead of equality"
|
1
|
89 |
|
|
90 |
|index "{ Class:SmallInteger }"
|
3
|
91 |
length startIndex probe |
|
1
|
92 |
|
13
|
93 |
length := keyArray basicSize.
|
362
|
94 |
index := key identityHash.
|
|
95 |
index := index \\ length + 1.
|
|
96 |
startIndex := index.
|
1
|
97 |
|
|
98 |
[true] whileTrue:[
|
250
|
99 |
probe := keyArray basicAt:index.
|
|
100 |
(probe isNil or: [key == probe]) ifTrue:[^ index].
|
|
101 |
probe == DeletedEntry ifTrue:[
|
|
102 |
keyArray basicAt:index put:nil.
|
|
103 |
^ index
|
|
104 |
].
|
1
|
105 |
|
250
|
106 |
index == length ifTrue:[
|
|
107 |
index := 1
|
|
108 |
] ifFalse:[
|
|
109 |
index := index + 1
|
|
110 |
].
|
|
111 |
index == startIndex ifTrue: [
|
|
112 |
^ self grow findKeyOrNil:key
|
|
113 |
]
|
1
|
114 |
]
|
10
|
115 |
!
|
|
116 |
|
|
117 |
findNil:key
|
|
118 |
"Look for the next slot usable for key. This method assumes that
|
|
119 |
key is not already in the receiver - used only while growing/rehashing"
|
|
120 |
|
|
121 |
|index "{ Class:SmallInteger }"
|
|
122 |
length|
|
|
123 |
|
13
|
124 |
length := keyArray basicSize.
|
362
|
125 |
index := key identityHash.
|
|
126 |
index := index \\ length + 1.
|
10
|
127 |
|
13
|
128 |
[(keyArray basicAt:index) notNil] whileTrue:[
|
250
|
129 |
index == length ifTrue:[
|
|
130 |
index := 1
|
|
131 |
] ifFalse:[
|
|
132 |
index := index + 1
|
|
133 |
].
|
|
134 |
"notice: no check for no nil found - we must find one since
|
|
135 |
this is only called after growing"
|
10
|
136 |
].
|
|
137 |
^ index
|
|
138 |
|
1
|
139 |
! !
|
609
|
140 |
|
634
|
141 |
!IdentitySet class methodsFor:'documentation'!
|
|
142 |
|
|
143 |
version
|
|
144 |
^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.16 1995-11-23 17:31:42 cg Exp $'
|
|
145 |
! !
|