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 |
"
|
24860
|
66 |
!
|
|
67 |
|
|
68 |
examples
|
|
69 |
"
|
|
70 |
Creation timing:
|
|
71 |
|
|
72 |
{SmallDictionary. Dictionary. OrderedDictionary} do:[:eachClass|
|
|
73 |
|keys values dict time|
|
|
74 |
|
|
75 |
keys := #(aaaaa bbbb cccc dddd ffff gggg hhhh jjjj kkkk llll).
|
|
76 |
values := #(1 2 3 4 5 6 7 8 9 10).
|
|
77 |
|
|
78 |
|
|
79 |
time := TimeDuration toRun:[
|
|
80 |
1000000 timesRepeat:[
|
|
81 |
dict := eachClass withKeys:keys andValues:values.
|
|
82 |
]
|
|
83 |
].
|
|
84 |
Transcript showCR:(eachClass name, ' time: ', time printString).
|
|
85 |
]
|
|
86 |
|
|
87 |
|
|
88 |
Access timing:
|
|
89 |
|
|
90 |
{SmallDictionary. Dictionary. OrderedDictionary} do:[:eachClass|
|
|
91 |
|keys values dict time|
|
|
92 |
|
|
93 |
keys := #(aaaaa bbbb cccc dddd ffff gggg hhhh jjjj kkkk llll).
|
|
94 |
values := #(1 2 3 4 5 6 7 8 9 10).
|
|
95 |
|
|
96 |
dict := eachClass withKeys:keys andValues:values.
|
|
97 |
|
|
98 |
time := TimeDuration toRun:[
|
|
99 |
1000000 timesRepeat:[
|
|
100 |
1 to:keys size do:[:idx|
|
|
101 |
dict at:(keys at:idx)
|
|
102 |
]
|
|
103 |
]
|
|
104 |
].
|
|
105 |
Transcript showCR:(eachClass name, ' time: ', time printString).
|
|
106 |
]
|
|
107 |
"
|
23342
|
108 |
! !
|
|
109 |
|
|
110 |
!SmallDictionary class methodsFor:'instance creation'!
|
|
111 |
|
|
112 |
new
|
|
113 |
^ self basicNew initialize:2
|
|
114 |
|
|
115 |
"Modified: / 14-09-2018 / 15:58:29 / Stefan Vogel"
|
|
116 |
!
|
|
117 |
|
|
118 |
new:capacity
|
|
119 |
^ self basicNew initialize:capacity
|
|
120 |
|
|
121 |
"Modified: / 14-09-2018 / 15:58:49 / Stefan Vogel"
|
|
122 |
! !
|
|
123 |
|
|
124 |
!SmallDictionary methodsFor:'accessing'!
|
|
125 |
|
|
126 |
at:key ifAbsent:aBlock
|
24499
|
127 |
"return the element indexed by aKey -
|
|
128 |
return result of exceptionBlock if no element is stored under aKey"
|
|
129 |
|
23360
|
130 |
|keyIndex "{Class: SmallInteger}"|
|
23342
|
131 |
|
23360
|
132 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
|
133 |
^ keyIndex == 0 ifTrue:[aBlock value] ifFalse:[keysAndValues basicAt:keyIndex+1]
|
23342
|
134 |
|
23360
|
135 |
"Modified: / 18-09-2018 / 14:08:21 / Stefan Vogel"
|
23342
|
136 |
!
|
|
137 |
|
|
138 |
at:key ifAbsentPut:aBlock
|
24499
|
139 |
"return the element indexed by aKey if present,
|
|
140 |
if not present, store the result of evaluating valueBlock
|
|
141 |
under aKey and return it.
|
|
142 |
WARNING: do not add elements while iterating over the receiver.
|
|
143 |
Iterate over a copy to do this."
|
|
144 |
|
23342
|
145 |
|keyIndex|
|
|
146 |
|
23360
|
147 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
24499
|
148 |
keyIndex == 0 ifTrue:[
|
24860
|
149 |
^ self privateAppendKey:key value:aBlock value
|
23342
|
150 |
] ifFalse:[
|
24499
|
151 |
^ keysAndValues basicAt:keyIndex+1.
|
23342
|
152 |
]
|
|
153 |
|
24860
|
154 |
"Modified: / 23-10-2019 / 11:30:07 / Stefan Vogel"
|
23342
|
155 |
!
|
|
156 |
|
|
157 |
keys
|
|
158 |
"return a collection containing the keys of the receiver"
|
|
159 |
|
|
160 |
|keys sz "{Class: SmallInteger}"|
|
|
161 |
|
|
162 |
sz := tally.
|
24860
|
163 |
keys := Array new:sz.
|
23342
|
164 |
1 to:sz do:[:i | keys at:i put:(keysAndValues at:i*2-1)].
|
|
165 |
^ keys
|
|
166 |
|
24860
|
167 |
"
|
|
168 |
|s|
|
|
169 |
|
|
170 |
s := SmallDictionary new.
|
|
171 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'.
|
|
172 |
s keys
|
|
173 |
"
|
|
174 |
|
23342
|
175 |
"Created: / 14-09-2018 / 17:35:25 / Stefan Vogel"
|
24860
|
176 |
"Modified (comment): / 23-10-2019 / 10:41:35 / Stefan Vogel"
|
23342
|
177 |
!
|
|
178 |
|
|
179 |
order
|
|
180 |
"returns the keys in the order of their appearance"
|
|
181 |
|
|
182 |
^ self keys
|
|
183 |
|
|
184 |
"Created: / 14-09-2018 / 17:30:44 / Stefan Vogel"
|
24860
|
185 |
"Modified (comment): / 23-10-2019 / 10:41:12 / Stefan Vogel"
|
23342
|
186 |
!
|
|
187 |
|
|
188 |
size
|
24499
|
189 |
"return the number of elements in the receiver."
|
|
190 |
|
23342
|
191 |
^ tally
|
|
192 |
|
|
193 |
"Modified (format): / 14-09-2018 / 15:44:11 / Stefan Vogel"
|
24860
|
194 |
!
|
|
195 |
|
|
196 |
values
|
|
197 |
"returns the values in the order of their appearance"
|
|
198 |
|
|
199 |
|values sz "{Class: SmallInteger}"|
|
|
200 |
|
|
201 |
sz := tally.
|
|
202 |
values := Array new:sz.
|
|
203 |
1 to:sz do:[:i | values at:i put:(keysAndValues at:i*2)].
|
|
204 |
^ values
|
|
205 |
|
|
206 |
"
|
|
207 |
|s|
|
|
208 |
|
|
209 |
s := SmallDictionary new.
|
|
210 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'.
|
|
211 |
s values
|
|
212 |
"
|
|
213 |
|
|
214 |
"Created: / 23-10-2019 / 10:39:55 / Stefan Vogel"
|
|
215 |
! !
|
|
216 |
|
|
217 |
!SmallDictionary methodsFor:'accessing - ordered'!
|
|
218 |
|
|
219 |
atIndex:index
|
|
220 |
"return an element at a given index"
|
|
221 |
|
|
222 |
^ keysAndValues basicAt:index*2.
|
|
223 |
|
|
224 |
"Created: / 23-10-2019 / 10:33:15 / Stefan Vogel"
|
|
225 |
!
|
|
226 |
|
|
227 |
atIndex:index ifAbsent:aBlock
|
|
228 |
"return an element at a given index"
|
|
229 |
|
|
230 |
index > tally ifTrue:[
|
|
231 |
aBlock value.
|
|
232 |
].
|
|
233 |
|
|
234 |
^ keysAndValues basicAt:index*2.
|
|
235 |
|
|
236 |
"Created: / 23-10-2019 / 10:34:29 / Stefan Vogel"
|
|
237 |
!
|
|
238 |
|
|
239 |
firstKey
|
|
240 |
"return the first key"
|
|
241 |
|
|
242 |
^ keysAndValues basicAt:1.
|
|
243 |
|
|
244 |
"Created: / 23-10-2019 / 10:36:22 / Stefan Vogel"
|
|
245 |
!
|
|
246 |
|
|
247 |
keyAt:index
|
|
248 |
"return a key at a given index"
|
|
249 |
|
|
250 |
^ keysAndValues basicAt:index*2 - 1.
|
|
251 |
|
|
252 |
"Created: / 23-10-2019 / 10:35:25 / Stefan Vogel"
|
|
253 |
!
|
|
254 |
|
|
255 |
last
|
|
256 |
"return the last value"
|
|
257 |
|
|
258 |
^ keysAndValues basicAt:tally*2.
|
|
259 |
|
|
260 |
"Created: / 23-10-2019 / 10:43:41 / Stefan Vogel"
|
|
261 |
!
|
|
262 |
|
|
263 |
lastKey
|
|
264 |
"return the last key"
|
|
265 |
|
|
266 |
^ keysAndValues basicAt:tally*2-1.
|
|
267 |
|
|
268 |
"Created: / 23-10-2019 / 10:37:06 / Stefan Vogel"
|
|
269 |
!
|
|
270 |
|
|
271 |
valueAt:index
|
|
272 |
"return an element at a given index"
|
|
273 |
|
|
274 |
^ keysAndValues basicAt:index*2.
|
|
275 |
|
|
276 |
"Created: / 23-10-2019 / 10:37:56 / Stefan Vogel"
|
23342
|
277 |
! !
|
|
278 |
|
|
279 |
!SmallDictionary methodsFor:'adding'!
|
|
280 |
|
|
281 |
add:anAssociation
|
24500
|
282 |
"add the argument, anAssociation to the receiver.
|
|
283 |
Returns the argument, anAssociation.
|
|
284 |
|
|
285 |
WARNING: do not add elements while iterating over the receiver.
|
|
286 |
Iterate over a copy to do this."
|
|
287 |
|
23342
|
288 |
self at:anAssociation key put:anAssociation value.
|
|
289 |
^ anAssociation
|
|
290 |
|
|
291 |
"Modified (format): / 14-09-2018 / 15:25:38 / Stefan Vogel"
|
|
292 |
!
|
|
293 |
|
|
294 |
at:key put:value
|
24500
|
295 |
"add the argument anObject under key, aKey to the receiver.
|
|
296 |
Return anObject (sigh).
|
|
297 |
|
|
298 |
WARNING: do not add elements while iterating over the receiver.
|
|
299 |
Iterate over a copy to do this."
|
|
300 |
|
24860
|
301 |
|keyIndex "{ Class:SmallInteger }"|
|
23342
|
302 |
|
24500
|
303 |
"/ specially tuned for single-element small dicts (most are)
|
|
304 |
tally == 0 ifTrue:[
|
|
305 |
keysAndValues size >= 2 ifTrue:[
|
|
306 |
keysAndValues basicAt:1 put:key.
|
|
307 |
keysAndValues basicAt:2 put:value.
|
|
308 |
tally := 1.
|
|
309 |
^ self.
|
|
310 |
].
|
|
311 |
].
|
|
312 |
|
23360
|
313 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
|
314 |
keyIndex == 0 ifTrue:[
|
24860
|
315 |
"append at the end"
|
|
316 |
self privateAppendKey:key value:value
|
23342
|
317 |
] ifFalse:[
|
24499
|
318 |
keysAndValues basicAt:keyIndex+1 put:value.
|
23360
|
319 |
].
|
|
320 |
^ value
|
23342
|
321 |
|
24860
|
322 |
"Modified: / 23-10-2019 / 11:30:28 / Stefan Vogel"
|
23342
|
323 |
! !
|
|
324 |
|
24179
|
325 |
!SmallDictionary methodsFor:'copying-private'!
|
23342
|
326 |
|
|
327 |
postCopy
|
|
328 |
keysAndValues := keysAndValues copy.
|
|
329 |
|
|
330 |
"Modified: / 14-09-2018 / 15:38:50 / Stefan Vogel"
|
|
331 |
! !
|
|
332 |
|
24860
|
333 |
!SmallDictionary privateMethodsFor:'copying-private'!
|
|
334 |
|
|
335 |
privateAppendKey:key value:value
|
|
336 |
"append a key/value pair at the end.
|
|
337 |
Grow the collection if necessary."
|
|
338 |
|
|
339 |
|sz "{Class: SmallInteger}"|
|
|
340 |
|
|
341 |
sz := tally * 2.
|
|
342 |
sz == keysAndValues size ifTrue:[
|
|
343 |
self growKeysAndValues
|
|
344 |
].
|
|
345 |
keysAndValues
|
|
346 |
basicAt:sz + 1 put:key;
|
|
347 |
basicAt:sz + 2 put:value.
|
|
348 |
tally := tally + 1.
|
|
349 |
^ value.
|
|
350 |
|
|
351 |
"Created: / 23-10-2019 / 11:28:50 / Stefan Vogel"
|
|
352 |
! !
|
|
353 |
|
23342
|
354 |
!SmallDictionary methodsFor:'enumerating'!
|
|
355 |
|
24499
|
356 |
do:aBlock
|
|
357 |
"enumerate the values"
|
|
358 |
|
23342
|
359 |
|sz "{Class: SmallInteger}"|
|
|
360 |
|
24499
|
361 |
sz := tally * 2.
|
23342
|
362 |
2 to:sz by:2 do:[:i | aBlock value:(keysAndValues at:i)]
|
|
363 |
|
|
364 |
"Modified: / 14-09-2018 / 16:30:18 / Stefan Vogel"
|
|
365 |
!
|
|
366 |
|
|
367 |
keysAndValuesDo:aBlock
|
|
368 |
|sz "{Class: SmallInteger}"|
|
|
369 |
|
|
370 |
sz := tally * 2.
|
24499
|
371 |
1 to:sz-1 by:2 do:[:i |
|
|
372 |
aBlock
|
|
373 |
value:(keysAndValues at:i)
|
|
374 |
value:(keysAndValues at:i+1)]
|
23342
|
375 |
|
|
376 |
"Modified: / 14-09-2018 / 16:31:18 / Stefan Vogel"
|
|
377 |
!
|
|
378 |
|
|
379 |
keysDo:aBlock
|
|
380 |
|sz "{Class: SmallInteger}"|
|
|
381 |
|
|
382 |
sz := tally * 2.
|
|
383 |
1 to:sz-1 by:2 do:[:i | aBlock value:(keysAndValues at:i)]
|
|
384 |
|
|
385 |
"Modified: / 14-09-2018 / 16:30:51 / Stefan Vogel"
|
|
386 |
! !
|
|
387 |
|
|
388 |
!SmallDictionary methodsFor:'initialize-release'!
|
|
389 |
|
|
390 |
initialize:capacity
|
|
391 |
keysAndValues := Array new:capacity*2.
|
|
392 |
tally := 0
|
|
393 |
|
|
394 |
"Created: / 14-09-2018 / 15:57:42 / Stefan Vogel"
|
|
395 |
! !
|
|
396 |
|
24501
|
397 |
|
23342
|
398 |
!SmallDictionary methodsFor:'private'!
|
|
399 |
|
23365
|
400 |
grow:capacity
|
23342
|
401 |
|newKeysAndValues|
|
|
402 |
|
|
403 |
newKeysAndValues := Array new:capacity*2.
|
|
404 |
newKeysAndValues replaceFrom:1 to:tally * 2 with:keysAndValues.
|
|
405 |
keysAndValues := newKeysAndValues.
|
|
406 |
|
23365
|
407 |
"Created: / 18-09-2018 / 15:37:24 / Stefan Vogel"
|
|
408 |
!
|
|
409 |
|
|
410 |
growKeysAndValues
|
|
411 |
"duplicate the capacity"
|
|
412 |
|
|
413 |
self grow:tally * 2
|
|
414 |
|
|
415 |
"Modified: / 18-09-2018 / 15:37:34 / Stefan Vogel"
|
23342
|
416 |
! !
|
|
417 |
|
23365
|
418 |
!SmallDictionary methodsFor:'queries'!
|
|
419 |
|
23696
|
420 |
includesKey:aKey
|
|
421 |
^ (keysAndValues indexOf:aKey startingAt:1 step:2) ~~ 0
|
|
422 |
|
|
423 |
"Modified: / 18-09-2018 / 14:09:03 / Stefan Vogel"
|
|
424 |
!
|
|
425 |
|
|
426 |
isEmpty
|
|
427 |
^ tally == 0
|
|
428 |
|
|
429 |
"Created: / 14-09-2018 / 15:44:49 / Stefan Vogel"
|
|
430 |
!
|
|
431 |
|
23365
|
432 |
speciesForCollecting
|
24856
|
433 |
^ SmallBag
|
|
434 |
|
|
435 |
"
|
|
436 |
(SmallDictionary
|
|
437 |
withKeys:#( 1 2 3 4 5 6 7 )
|
|
438 |
andValues:#(10 20 30 10 20 30 10)
|
|
439 |
) collect:[:each| each < 25]
|
|
440 |
"
|
23365
|
441 |
|
|
442 |
"Created: / 18-09-2018 / 15:34:27 / Stefan Vogel"
|
24856
|
443 |
"Modified (comment): / 22-10-2019 / 20:15:32 / Stefan Vogel"
|
23365
|
444 |
! !
|
|
445 |
|
23342
|
446 |
!SmallDictionary methodsFor:'removing'!
|
|
447 |
|
|
448 |
empty
|
|
449 |
"RefactoryBrowser compatibility"
|
|
450 |
|
|
451 |
self removeAll
|
|
452 |
|
|
453 |
"Modified: / 14-09-2018 / 15:59:57 / Stefan Vogel"
|
|
454 |
!
|
|
455 |
|
24499
|
456 |
remove:anAssociation ifAbsent:anExceptionBlock
|
|
457 |
self removeKey:(anAssociation key) ifAbsent:anExceptionBlock.
|
|
458 |
^ anAssociation
|
23342
|
459 |
|
|
460 |
"Modified (format): / 14-09-2018 / 15:40:44 / Stefan Vogel"
|
|
461 |
!
|
|
462 |
|
|
463 |
removeAll
|
|
464 |
tally := 0.
|
24499
|
465 |
keysAndValues atAllPut:nil.
|
23342
|
466 |
|
|
467 |
"Modified: / 14-09-2018 / 16:02:28 / Stefan Vogel"
|
|
468 |
!
|
|
469 |
|
|
470 |
removeKey:key ifAbsent:aBlock
|
|
471 |
|keyIndex "{Class:SmallInteger}"
|
|
472 |
sz "{Class:SmallInteger}"
|
|
473 |
value|
|
|
474 |
|
23360
|
475 |
keyIndex := keysAndValues indexOf:key startingAt:1 step:2.
|
23342
|
476 |
keyIndex == 0 ifTrue:[
|
|
477 |
^ aBlock value
|
|
478 |
].
|
|
479 |
value := keysAndValues at:keyIndex+1.
|
|
480 |
sz := tally*2.
|
|
481 |
keyIndex to:sz-2 do:[:i |
|
24499
|
482 |
keysAndValues at:i put:(keysAndValues at:i + 2).
|
23342
|
483 |
].
|
|
484 |
keysAndValues at:sz-1 put:nil.
|
|
485 |
keysAndValues at:sz put:nil.
|
|
486 |
tally := tally - 1.
|
|
487 |
^ value
|
|
488 |
|
23360
|
489 |
"Modified: / 18-09-2018 / 14:09:15 / Stefan Vogel"
|
23342
|
490 |
! !
|
|
491 |
|
|
492 |
!SmallDictionary methodsFor:'testing'!
|
|
493 |
|
23351
|
494 |
isDictionary
|
24499
|
495 |
"return true, if the receiver is some kind of dictionary;
|
|
496 |
true returned here - the method is redefined from Object."
|
|
497 |
|
23351
|
498 |
^ true
|
|
499 |
|
|
500 |
"Created: / 14-09-2018 / 18:31:24 / Stefan Vogel"
|
|
501 |
!
|
|
502 |
|
23365
|
503 |
isFixedSize
|
|
504 |
"return true if the receiver cannot grow - this will vanish once
|
|
505 |
Arrays and Strings learn how to grow ..."
|
|
506 |
|
|
507 |
^ false
|
|
508 |
|
|
509 |
"Created: / 18-09-2018 / 15:35:25 / Stefan Vogel"
|
23342
|
510 |
! !
|
|
511 |
|
24503
|
512 |
!SmallDictionary methodsFor:'visiting'!
|
|
513 |
|
|
514 |
acceptVisitor:aVisitor with:aParameter
|
|
515 |
"dispatch for visitor pattern; send #visitDictionary:with: to aVisitor"
|
|
516 |
|
|
517 |
^ aVisitor visitDictionary:self with:aParameter
|
|
518 |
! !
|
|
519 |
|
23342
|
520 |
!SmallDictionary class methodsFor:'documentation'!
|
|
521 |
|
|
522 |
version_CVS
|
|
523 |
^ '$Header$'
|
|
524 |
! !
|
|
525 |
|