1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
175
|
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 |
Dictionary subclass:#IdentityDictionary
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
IdentityDictionary comment:'
|
88
|
21 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
175
|
22 |
All Rights Reserved
|
92
|
23 |
|
345
|
24 |
$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.11 1995-05-16 17:07:15 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!IdentityDictionary class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
175
|
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 |
"
|
345
|
45 |
$Header: /cvs/stx/stx/libbasic/IdentityDictionary.st,v 1.11 1995-05-16 17:07:15 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
1
|
48 |
|
88
|
49 |
documentation
|
|
50 |
"
|
|
51 |
same as a Dictionary but key must be identical - not just equal.
|
|
52 |
Since compare is on identical keys (using ==), hashing is also done via
|
|
53 |
#identityHash instead of #hash.
|
|
54 |
IdentityDictionaries are especially useful, when symbols are used as keys.
|
|
55 |
"
|
|
56 |
! !
|
1
|
57 |
|
345
|
58 |
!IdentityDictionary methodsFor:'testing'!
|
|
59 |
|
|
60 |
includesValue:aValue
|
|
61 |
"return true, if the argument, aValue is stored in the dictionary,
|
|
62 |
Redefined to use identity compare, NOT equality compare"
|
|
63 |
|
|
64 |
^ (valueArray identityIndexOf:aValue) ~~ 0
|
|
65 |
!
|
|
66 |
|
|
67 |
occurrencesOf:anObject
|
|
68 |
"count & return how often anObject is stored in the dictionary.
|
|
69 |
This counts values - not keys.
|
|
70 |
Redefined to use identity compare, NOT equality compare."
|
|
71 |
|
|
72 |
|cnt|
|
|
73 |
|
|
74 |
cnt := 0.
|
|
75 |
valueArray do:[:element |
|
|
76 |
element == anObject ifTrue:[cnt := cnt + 1]
|
|
77 |
].
|
|
78 |
^ cnt
|
|
79 |
! !
|
|
80 |
|
1
|
81 |
!IdentityDictionary methodsFor:'private'!
|
|
82 |
|
345
|
83 |
compareSame:element1 with:element2
|
|
84 |
^ element1 == element2
|
|
85 |
!
|
|
86 |
|
1
|
87 |
findKeyOrNil:key
|
|
88 |
"Look for the key in the receiver. If it is found, return
|
|
89 |
the index of the association containing the key, otherwise
|
|
90 |
return the index of the first unused slot. Grow the receiver,
|
|
91 |
if key was not found, and no unused slots where present"
|
|
92 |
|
|
93 |
|index "{ Class:SmallInteger }"
|
3
|
94 |
length startIndex probe |
|
1
|
95 |
|
|
96 |
length := keyArray basicSize.
|
|
97 |
startIndex := key identityHash \\ length + 1.
|
|
98 |
|
|
99 |
index := startIndex.
|
|
100 |
[true] whileTrue:[
|
175
|
101 |
probe := keyArray basicAt:index.
|
|
102 |
(probe isNil or: [probe == key]) ifTrue:[^ index].
|
|
103 |
probe == DeletedEntry ifTrue:[
|
|
104 |
keyArray basicAt:index put:nil.
|
|
105 |
^ index
|
|
106 |
].
|
1
|
107 |
|
175
|
108 |
index == length ifTrue:[
|
|
109 |
index := 1
|
|
110 |
] ifFalse:[
|
|
111 |
index := index + 1
|
|
112 |
].
|
|
113 |
index == startIndex ifTrue:[^ self grow findKeyOrNil:key].
|
1
|
114 |
]
|
|
115 |
!
|
|
116 |
|
13
|
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 |
|
|
124 |
length := keyArray basicSize.
|
|
125 |
index := key identityHash \\ length + 1.
|
|
126 |
|
|
127 |
[(keyArray basicAt:index) notNil] whileTrue:[
|
175
|
128 |
index == length ifTrue:[
|
|
129 |
index := 1
|
|
130 |
] ifFalse:[
|
|
131 |
index := index + 1
|
|
132 |
].
|
|
133 |
"notice: no check for no nil found - we must find one since
|
|
134 |
this is only called after growing"
|
13
|
135 |
].
|
|
136 |
^ index
|
|
137 |
|
|
138 |
!
|
|
139 |
|
|
140 |
find:key ifAbsent:aBlock
|
1
|
141 |
"Look for the key in the receiver. If it is found, return
|
|
142 |
the index of the association containing the key, otherwise
|
|
143 |
return the value of evaluating aBlock."
|
|
144 |
|
|
145 |
|index "{ Class:SmallInteger }"
|
|
146 |
length "{ Class:SmallInteger }"
|
|
147 |
startIndex
|
|
148 |
probe|
|
|
149 |
|
|
150 |
length := keyArray basicSize.
|
|
151 |
length < 10 ifTrue:[
|
175
|
152 |
"assuming, that for small dictionaries the overhead of hashing
|
|
153 |
is large ... maybe that proves wrong (if overhead of comparing
|
|
154 |
is high)"
|
|
155 |
^ keyArray identityIndexOf:key ifAbsent:aBlock.
|
1
|
156 |
].
|
|
157 |
|
|
158 |
startIndex := key identityHash \\ length + 1.
|
|
159 |
|
|
160 |
index := startIndex.
|
|
161 |
[true] whileTrue:[
|
175
|
162 |
probe := keyArray basicAt:index.
|
|
163 |
probe == key ifTrue:[^ index].
|
1
|
164 |
|
175
|
165 |
index == length ifTrue:[
|
|
166 |
index := 1
|
|
167 |
] ifFalse:[
|
|
168 |
index := index + 1
|
|
169 |
].
|
|
170 |
(probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value]
|
1
|
171 |
]
|
77
|
172 |
!
|
|
173 |
|
|
174 |
emptyCollectionForKeys
|
175
|
175 |
"return an empty collection used for keys.
|
|
176 |
Made a separate method to allow redefinition for different kind of
|
|
177 |
containers in subclasses"
|
|
178 |
|
77
|
179 |
^ IdentitySet new:(self size)
|
1
|
180 |
! !
|