24499
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
23352
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
5 |
All Rights Reserved
|
23351
|
6 |
|
23352
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
23342
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
KeyedCollection subclass:#SmallDictionary
|
|
19 |
instanceVariableNames:'keysAndValues tally'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Unordered'
|
|
23 |
!
|
|
24 |
|
|
25 |
!SmallDictionary class methodsFor:'documentation'!
|
|
26 |
|
23352
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
|
|
39 |
"
|
|
40 |
!
|
|
41 |
|
23342
|
42 |
documentation
|
|
43 |
"
|
|
44 |
A lightweight Dictionary implementation (without hashing)
|
|
45 |
for small dictionaries.
|
|
46 |
|
|
47 |
Use this, when you have to store a small number (e.g. < 20) key/value pairs.
|
|
48 |
As a side effect of the implementation, the dictionary is also ordered.
|
|
49 |
|
|
50 |
Inspired by and compatible with RBSmallDictionary from RefactoryBrowser
|
|
51 |
(although the implementaion is different).
|
|
52 |
|
|
53 |
[instance variables:]
|
|
54 |
keysAndValues Array keys are stored at odd indices, values at even indices
|
|
55 |
tally SmallInterger the number of valid key/value pairs
|
|
56 |
|
|
57 |
[class variables:]
|
|
58 |
|
|
59 |
[see also:]
|
|
60 |
Dictionary
|
|
61 |
MethodDictionary
|
|
62 |
|
|
63 |
[author:]
|
|
64 |
Stefan Vogel
|
|
65 |
"
|
|
66 |
! !
|
|
67 |
|
|
68 |
!SmallDictionary class methodsFor:'instance creation'!
|
|
69 |
|
|
70 |
new
|
|
71 |
^ self basicNew initialize:2
|
|
72 |
|
|
73 |
"Modified: / 14-09-2018 / 15:58:29 / Stefan Vogel"
|
|
74 |
!
|
|
75 |
|
|
76 |
new:capacity
|
|
77 |
^ self basicNew initialize:capacity
|
|
78 |
|
|
79 |
"Modified: / 14-09-2018 / 15:58:49 / Stefan Vogel"
|
|
80 |
! !
|
|
81 |
|
|
82 |
!SmallDictionary methodsFor:'accessing'!
|
|
83 |
|
|
84 |
at:key ifAbsent:aBlock
|
24499
|
85 |
"return the element indexed by aKey -
|
|
86 |
return result of exceptionBlock if no element is stored under aKey"
|
|
87 |
|
23360
|
88 |
|keyIndex "{Class: SmallInteger}"|
|
23342
|
89 |
|
23360
|
90 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
|
91 |
^ keyIndex == 0 ifTrue:[aBlock value] ifFalse:[keysAndValues basicAt:keyIndex+1]
|
23342
|
92 |
|
23360
|
93 |
"Modified: / 18-09-2018 / 14:08:21 / Stefan Vogel"
|
23342
|
94 |
!
|
|
95 |
|
|
96 |
at:key ifAbsentPut:aBlock
|
24499
|
97 |
"return the element indexed by aKey if present,
|
|
98 |
if not present, store the result of evaluating valueBlock
|
|
99 |
under aKey and return it.
|
|
100 |
WARNING: do not add elements while iterating over the receiver.
|
|
101 |
Iterate over a copy to do this."
|
|
102 |
|
23342
|
103 |
|keyIndex|
|
|
104 |
|
23360
|
105 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
24499
|
106 |
keyIndex == 0 ifTrue:[
|
|
107 |
^ self privateAt:key put:aBlock value
|
23342
|
108 |
] ifFalse:[
|
24499
|
109 |
^ keysAndValues basicAt:keyIndex+1.
|
23342
|
110 |
]
|
|
111 |
|
23360
|
112 |
"Modified: / 18-09-2018 / 14:08:26 / Stefan Vogel"
|
23342
|
113 |
!
|
|
114 |
|
|
115 |
keys
|
|
116 |
"return a collection containing the keys of the receiver"
|
|
117 |
|
|
118 |
|keys sz "{Class: SmallInteger}"|
|
|
119 |
|
|
120 |
keys := Array new:tally.
|
|
121 |
sz := tally.
|
|
122 |
1 to:sz do:[:i | keys at:i put:(keysAndValues at:i*2-1)].
|
|
123 |
^ keys
|
|
124 |
|
|
125 |
"Created: / 14-09-2018 / 17:35:25 / Stefan Vogel"
|
|
126 |
!
|
|
127 |
|
|
128 |
order
|
|
129 |
"returns the keys in the order of their appearance"
|
|
130 |
|
|
131 |
^ self keys
|
|
132 |
|
|
133 |
"
|
|
134 |
|s|
|
|
135 |
|
|
136 |
s := SmallDictionary new.
|
|
137 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'.
|
|
138 |
s order
|
|
139 |
"
|
|
140 |
|
|
141 |
"Created: / 14-09-2018 / 17:30:44 / Stefan Vogel"
|
|
142 |
!
|
|
143 |
|
|
144 |
size
|
24499
|
145 |
"return the number of elements in the receiver."
|
|
146 |
|
23342
|
147 |
^ tally
|
|
148 |
|
|
149 |
"Modified (format): / 14-09-2018 / 15:44:11 / Stefan Vogel"
|
|
150 |
! !
|
|
151 |
|
|
152 |
!SmallDictionary methodsFor:'adding'!
|
|
153 |
|
|
154 |
add:anAssociation
|
24500
|
155 |
"add the argument, anAssociation to the receiver.
|
|
156 |
Returns the argument, anAssociation.
|
|
157 |
|
|
158 |
WARNING: do not add elements while iterating over the receiver.
|
|
159 |
Iterate over a copy to do this."
|
|
160 |
|
23342
|
161 |
self at:anAssociation key put:anAssociation value.
|
|
162 |
^ anAssociation
|
|
163 |
|
|
164 |
"Modified (format): / 14-09-2018 / 15:25:38 / Stefan Vogel"
|
|
165 |
!
|
|
166 |
|
|
167 |
at:key put:value
|
24500
|
168 |
"add the argument anObject under key, aKey to the receiver.
|
|
169 |
Return anObject (sigh).
|
|
170 |
|
|
171 |
WARNING: do not add elements while iterating over the receiver.
|
|
172 |
Iterate over a copy to do this."
|
|
173 |
|
23342
|
174 |
|keyIndex|
|
|
175 |
|
24500
|
176 |
"/ specially tuned for single-element small dicts (most are)
|
|
177 |
tally == 0 ifTrue:[
|
|
178 |
keysAndValues size >= 2 ifTrue:[
|
|
179 |
keysAndValues basicAt:1 put:key.
|
|
180 |
keysAndValues basicAt:2 put:value.
|
|
181 |
tally := 1.
|
|
182 |
^ self.
|
|
183 |
].
|
|
184 |
].
|
|
185 |
|
23360
|
186 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
|
187 |
keyIndex == 0 ifTrue:[
|
23342
|
188 |
self privateAt:key put:value
|
|
189 |
] ifFalse:[
|
24499
|
190 |
keysAndValues basicAt:keyIndex+1 put:value.
|
23360
|
191 |
].
|
|
192 |
^ value
|
23342
|
193 |
|
23360
|
194 |
"Modified: / 18-09-2018 / 14:12:13 / Stefan Vogel"
|
23342
|
195 |
! !
|
|
196 |
|
24179
|
197 |
!SmallDictionary methodsFor:'copying-private'!
|
23342
|
198 |
|
|
199 |
postCopy
|
|
200 |
keysAndValues := keysAndValues copy.
|
|
201 |
|
|
202 |
"Modified: / 14-09-2018 / 15:38:50 / Stefan Vogel"
|
|
203 |
! !
|
|
204 |
|
|
205 |
!SmallDictionary methodsFor:'enumerating'!
|
|
206 |
|
24499
|
207 |
do:aBlock
|
|
208 |
"enumerate the values"
|
|
209 |
|
23342
|
210 |
|sz "{Class: SmallInteger}"|
|
|
211 |
|
24499
|
212 |
sz := tally * 2.
|
23342
|
213 |
2 to:sz by:2 do:[:i | aBlock value:(keysAndValues at:i)]
|
|
214 |
|
|
215 |
"Modified: / 14-09-2018 / 16:30:18 / Stefan Vogel"
|
|
216 |
!
|
|
217 |
|
|
218 |
keysAndValuesDo:aBlock
|
|
219 |
|sz "{Class: SmallInteger}"|
|
|
220 |
|
|
221 |
sz := tally * 2.
|
24499
|
222 |
1 to:sz-1 by:2 do:[:i |
|
|
223 |
aBlock
|
|
224 |
value:(keysAndValues at:i)
|
|
225 |
value:(keysAndValues at:i+1)]
|
23342
|
226 |
|
|
227 |
"Modified: / 14-09-2018 / 16:31:18 / Stefan Vogel"
|
|
228 |
!
|
|
229 |
|
|
230 |
keysDo:aBlock
|
|
231 |
|sz "{Class: SmallInteger}"|
|
|
232 |
|
|
233 |
sz := tally * 2.
|
|
234 |
1 to:sz-1 by:2 do:[:i | aBlock value:(keysAndValues at:i)]
|
|
235 |
|
|
236 |
"Modified: / 14-09-2018 / 16:30:51 / Stefan Vogel"
|
|
237 |
! !
|
|
238 |
|
|
239 |
!SmallDictionary methodsFor:'initialize-release'!
|
|
240 |
|
|
241 |
initialize:capacity
|
|
242 |
keysAndValues := Array new:capacity*2.
|
|
243 |
tally := 0
|
|
244 |
|
|
245 |
"Created: / 14-09-2018 / 15:57:42 / Stefan Vogel"
|
|
246 |
! !
|
|
247 |
|
24501
|
248 |
|
23342
|
249 |
!SmallDictionary methodsFor:'private'!
|
|
250 |
|
23365
|
251 |
grow:capacity
|
23342
|
252 |
|newKeysAndValues|
|
|
253 |
|
|
254 |
newKeysAndValues := Array new:capacity*2.
|
|
255 |
newKeysAndValues replaceFrom:1 to:tally * 2 with:keysAndValues.
|
|
256 |
keysAndValues := newKeysAndValues.
|
|
257 |
|
23365
|
258 |
"Created: / 18-09-2018 / 15:37:24 / Stefan Vogel"
|
|
259 |
!
|
|
260 |
|
|
261 |
growKeysAndValues
|
|
262 |
"duplicate the capacity"
|
|
263 |
|
|
264 |
self grow:tally * 2
|
|
265 |
|
|
266 |
"Modified: / 18-09-2018 / 15:37:34 / Stefan Vogel"
|
23342
|
267 |
!
|
|
268 |
|
|
269 |
privateAt:key put:value
|
|
270 |
|sz "{Class: SmallInteger}"|
|
|
271 |
|
|
272 |
sz := tally * 2.
|
|
273 |
sz == keysAndValues size ifTrue: [self growKeysAndValues].
|
23360
|
274 |
keysAndValues
|
|
275 |
basicAt:sz+1 put:key;
|
|
276 |
basicAt:sz+2 put:value.
|
23342
|
277 |
tally := tally + 1.
|
|
278 |
^ value.
|
|
279 |
|
23360
|
280 |
"Modified: / 18-09-2018 / 13:33:30 / Stefan Vogel"
|
23342
|
281 |
! !
|
|
282 |
|
23365
|
283 |
!SmallDictionary methodsFor:'queries'!
|
|
284 |
|
23696
|
285 |
includesKey:aKey
|
|
286 |
^ (keysAndValues indexOf:aKey startingAt:1 step:2) ~~ 0
|
|
287 |
|
|
288 |
"Modified: / 18-09-2018 / 14:09:03 / Stefan Vogel"
|
|
289 |
!
|
|
290 |
|
|
291 |
isEmpty
|
|
292 |
^ tally == 0
|
|
293 |
|
|
294 |
"Created: / 14-09-2018 / 15:44:49 / Stefan Vogel"
|
|
295 |
!
|
|
296 |
|
23365
|
297 |
speciesForCollecting
|
|
298 |
^ Bag
|
|
299 |
|
|
300 |
"Created: / 18-09-2018 / 15:34:27 / Stefan Vogel"
|
|
301 |
! !
|
|
302 |
|
23342
|
303 |
!SmallDictionary methodsFor:'removing'!
|
|
304 |
|
|
305 |
empty
|
|
306 |
"RefactoryBrowser compatibility"
|
|
307 |
|
|
308 |
self removeAll
|
|
309 |
|
|
310 |
"Modified: / 14-09-2018 / 15:59:57 / Stefan Vogel"
|
|
311 |
!
|
|
312 |
|
24499
|
313 |
remove:anAssociation ifAbsent:anExceptionBlock
|
|
314 |
self removeKey:(anAssociation key) ifAbsent:anExceptionBlock.
|
|
315 |
^ anAssociation
|
23342
|
316 |
|
|
317 |
"Modified (format): / 14-09-2018 / 15:40:44 / Stefan Vogel"
|
|
318 |
!
|
|
319 |
|
|
320 |
removeAll
|
|
321 |
tally := 0.
|
24499
|
322 |
keysAndValues atAllPut:nil.
|
23342
|
323 |
|
|
324 |
"Modified: / 14-09-2018 / 16:02:28 / Stefan Vogel"
|
|
325 |
!
|
|
326 |
|
|
327 |
removeKey:key ifAbsent:aBlock
|
|
328 |
|keyIndex "{Class:SmallInteger}"
|
|
329 |
sz "{Class:SmallInteger}"
|
|
330 |
value|
|
|
331 |
|
23360
|
332 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
23342
|
333 |
keyIndex == 0 ifTrue:[
|
|
334 |
^ aBlock value
|
|
335 |
].
|
|
336 |
value := keysAndValues at:keyIndex+1.
|
|
337 |
sz := tally*2.
|
|
338 |
keyIndex to:sz-2 do:[:i |
|
24499
|
339 |
keysAndValues at:i put:(keysAndValues at:i + 2).
|
23342
|
340 |
].
|
|
341 |
keysAndValues at:sz-1 put:nil.
|
|
342 |
keysAndValues at:sz put:nil.
|
|
343 |
tally := tally - 1.
|
|
344 |
^ value
|
|
345 |
|
23360
|
346 |
"Modified: / 18-09-2018 / 14:09:15 / Stefan Vogel"
|
23342
|
347 |
! !
|
|
348 |
|
|
349 |
!SmallDictionary methodsFor:'testing'!
|
|
350 |
|
23351
|
351 |
isDictionary
|
24499
|
352 |
"return true, if the receiver is some kind of dictionary;
|
|
353 |
true returned here - the method is redefined from Object."
|
|
354 |
|
23351
|
355 |
^ true
|
|
356 |
|
|
357 |
"Created: / 14-09-2018 / 18:31:24 / Stefan Vogel"
|
|
358 |
!
|
|
359 |
|
23365
|
360 |
isFixedSize
|
|
361 |
"return true if the receiver cannot grow - this will vanish once
|
|
362 |
Arrays and Strings learn how to grow ..."
|
|
363 |
|
|
364 |
^ false
|
|
365 |
|
|
366 |
"Created: / 18-09-2018 / 15:35:25 / Stefan Vogel"
|
23342
|
367 |
! !
|
|
368 |
|
24503
|
369 |
!SmallDictionary methodsFor:'visiting'!
|
|
370 |
|
|
371 |
acceptVisitor:aVisitor with:aParameter
|
|
372 |
"dispatch for visitor pattern; send #visitDictionary:with: to aVisitor"
|
|
373 |
|
|
374 |
^ aVisitor visitDictionary:self with:aParameter
|
|
375 |
! !
|
|
376 |
|
23342
|
377 |
!SmallDictionary class methodsFor:'documentation'!
|
|
378 |
|
|
379 |
version_CVS
|
|
380 |
^ '$Header$'
|
|
381 |
! !
|
|
382 |
|