3175
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2014 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
|
14 |
Dictionary subclass:#PluggableDictionary
|
|
15 |
instanceVariableNames:'hashFunction compareFunction'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Collections-Unordered'
|
|
19 |
!
|
|
20 |
|
|
21 |
!PluggableDictionary class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 2014 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
|
39 |
a dictionary where the hash- and compare functions can be provided externally.
|
|
40 |
|
|
41 |
[author:]
|
|
42 |
Claus Gittinger
|
|
43 |
"
|
|
44 |
!
|
|
45 |
|
|
46 |
examples
|
|
47 |
"
|
|
48 |
|s|
|
|
49 |
|
|
50 |
s := PluggableDictionary
|
|
51 |
hashWith:[:k | k size]
|
|
52 |
compareWith:[:a :b | a notNil and:[b notNil and:[a asLowercase = b asLowercase]]].
|
|
53 |
s at:'hello' put:123.
|
|
54 |
s at:'world' put:222.
|
|
55 |
s at:'abc' put:333.
|
|
56 |
s at:'Hello'.
|
|
57 |
s at:'heLLo'.
|
|
58 |
s at:'ABC'.
|
|
59 |
s at:'WORLD'.
|
|
60 |
s size.
|
|
61 |
s includesKey:'heLlo'.
|
|
62 |
s includesKey:'wOrLd'.
|
|
63 |
s includesKey:'wOrLds'.
|
|
64 |
"
|
|
65 |
! !
|
|
66 |
|
|
67 |
!PluggableDictionary class methodsFor:'instance creation'!
|
|
68 |
|
|
69 |
hashWith:hashFunctionArg compareWith:compareFunctionArg
|
|
70 |
^ self new hashWith:hashFunctionArg compareWith:compareFunctionArg
|
|
71 |
! !
|
|
72 |
|
|
73 |
!PluggableDictionary methodsFor:'accessing'!
|
|
74 |
|
|
75 |
hashWith:hashFunctionArg compareWith:compareFunctionArg
|
|
76 |
hashFunction := hashFunctionArg.
|
|
77 |
compareFunction := compareFunctionArg.
|
|
78 |
! !
|
|
79 |
|
|
80 |
!PluggableDictionary methodsFor:'private'!
|
|
81 |
|
|
82 |
compareSame:element1 with:element2
|
|
83 |
"compare two elements for being the same.
|
|
84 |
Here, return the value from compareFunction"
|
|
85 |
|
|
86 |
^ compareFunction value:element1 value:element2
|
|
87 |
!
|
|
88 |
|
|
89 |
emptyCollectionForKeys
|
|
90 |
"return an empty collection used for keys.
|
|
91 |
Here, an IdentitySet is returned.
|
|
92 |
Made a separate method to allow redefinition for different kind of
|
|
93 |
containers in subclasses."
|
|
94 |
|
|
95 |
^ PluggableSet new:(self size) hashWith:hashFunction compareWith:compareFunction
|
|
96 |
!
|
|
97 |
|
|
98 |
find:key ifAbsent:aBlock
|
|
99 |
"Look for the key in the receiver. If it is found, return
|
|
100 |
the index of the association containing the key, otherwise
|
|
101 |
return the value of evaluating aBlock.
|
|
102 |
Redefined - since we inherit this code from Set-Dictionary
|
|
103 |
(one of the seldom cases, where I could make use of multiple inheritance
|
|
104 |
and inherit from IdentitySet ... sigh)"
|
|
105 |
|
|
106 |
|index "{ Class:SmallInteger }"
|
|
107 |
length "{ Class:SmallInteger }"
|
|
108 |
startIndex probe|
|
|
109 |
|
|
110 |
length := keyArray basicSize.
|
|
111 |
|
|
112 |
"/
|
|
113 |
"/ length < 10 ifTrue:[
|
|
114 |
"/ "assuming, that for small dictionaries the overhead of hashing
|
|
115 |
"/ is large ... maybe that proves wrong (if overhead of comparing
|
|
116 |
"/ is high)"
|
|
117 |
"/ ^ keyArray identityIndexOf:key ifAbsent:aBlock.
|
|
118 |
"/ ].
|
|
119 |
"/
|
|
120 |
|
|
121 |
startIndex := index := self initialIndexForKey:key.
|
|
122 |
|
|
123 |
[true] whileTrue:[
|
|
124 |
probe := keyArray basicAt:index.
|
|
125 |
(compareFunction value:probe value:key) ifTrue:[^ index]. "<<<< == is different from inherited"
|
|
126 |
(self slotIsEmpty:probe) ifTrue:[^ aBlock value].
|
|
127 |
|
|
128 |
index == length ifTrue:[
|
|
129 |
index := 1
|
|
130 |
] ifFalse:[
|
|
131 |
index := index + 1
|
|
132 |
].
|
|
133 |
index == startIndex ifTrue:[^ aBlock value]
|
|
134 |
]
|
|
135 |
!
|
|
136 |
|
|
137 |
findKeyOrNil:key
|
|
138 |
"Look for the key in the receiver.
|
|
139 |
If it is found, return return the index of the first unused slot.
|
|
140 |
Grow the receiver, if key was not found, and no unused slots were present"
|
|
141 |
|
|
142 |
|index "{ Class:SmallInteger }"
|
|
143 |
length "{ Class:SmallInteger }"
|
|
144 |
startIndex probe
|
|
145 |
delIndex "{ Class:SmallInteger }" |
|
|
146 |
|
|
147 |
delIndex := 0.
|
|
148 |
|
|
149 |
length := keyArray basicSize.
|
|
150 |
startIndex := index := self initialIndexForKey:key.
|
|
151 |
|
|
152 |
[true] whileTrue:[
|
|
153 |
probe := keyArray basicAt:index.
|
|
154 |
(compareFunction value:probe value:key) ifTrue:[^ index].
|
|
155 |
(self slotIsEmpty:probe) ifTrue:[
|
|
156 |
delIndex == 0 ifTrue:[^ index].
|
|
157 |
keyArray basicAt:delIndex put:nil.
|
|
158 |
^ delIndex
|
|
159 |
].
|
|
160 |
|
|
161 |
probe == DeletedEntry ifTrue:[
|
|
162 |
delIndex == 0 ifTrue:[
|
|
163 |
delIndex := index
|
|
164 |
]
|
|
165 |
].
|
|
166 |
|
|
167 |
index == length ifTrue:[
|
|
168 |
index := 1
|
|
169 |
] ifFalse:[
|
|
170 |
index := index + 1
|
|
171 |
].
|
|
172 |
index == startIndex ifTrue:[
|
|
173 |
delIndex ~~ 0 ifTrue:[
|
|
174 |
keyArray basicAt:delIndex put:nil.
|
|
175 |
^ delIndex
|
|
176 |
].
|
|
177 |
^ self grow findKeyOrNil:key
|
|
178 |
].
|
|
179 |
]
|
|
180 |
!
|
|
181 |
|
|
182 |
hashFor:aKey
|
|
183 |
"return an initial index given a key."
|
|
184 |
|
|
185 |
^ hashFunction value:aKey
|
|
186 |
! !
|
|
187 |
|
|
188 |
!PluggableDictionary methodsFor:'testing'!
|
|
189 |
|
|
190 |
occurrencesOf:anObject
|
|
191 |
"count & return how often anObject is stored in the dictionary.
|
|
192 |
This counts values - not keys.
|
|
193 |
Redefined to use #== (identity compare), NOT equality compare."
|
|
194 |
|
|
195 |
|cnt|
|
|
196 |
|
|
197 |
anObject isNil ifTrue:[^ super occurrencesOf:anObject].
|
|
198 |
|
|
199 |
cnt := 0.
|
|
200 |
valueArray do:[:element |
|
|
201 |
(compareFunction value:element value:anObject) ifTrue:[cnt := cnt + 1]
|
|
202 |
].
|
|
203 |
^ cnt
|
|
204 |
! !
|
|
205 |
|
|
206 |
!PluggableDictionary class methodsFor:'documentation'!
|
|
207 |
|
|
208 |
version
|
|
209 |
^ '$Header: /cvs/stx/stx/libbasic2/PluggableDictionary.st,v 1.1 2014-02-18 21:13:24 cg Exp $'
|
|
210 |
!
|
|
211 |
|
|
212 |
version_CVS
|
|
213 |
^ '$Header: /cvs/stx/stx/libbasic2/PluggableDictionary.st,v 1.1 2014-02-18 21:13:24 cg Exp $'
|
|
214 |
! !
|
|
215 |
|