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