23888
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
3615
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1998 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
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 |
"
|
9482
|
14 |
"{ Package: 'stx:libbasic' }"
|
8893
|
15 |
|
19469
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
3615
|
18 |
Collection subclass:#KeyedCollection
|
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Abstract'
|
|
23 |
!
|
|
24 |
|
|
25 |
!KeyedCollection class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1998 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 |
|
|
42 |
documentation
|
|
43 |
"
|
|
44 |
Abstract superclass for collections which have a key->value mapping.
|
|
45 |
This abstract class provides functionality common to those collections,
|
17120
|
46 |
without knowing how the concrete class implements things.
|
|
47 |
Thus, all methods found here depend on some basic mechanisms
|
|
48 |
to be defined in the concrete class.
|
3615
|
49 |
These basic methods are usually defined as #subclassResponsibility here.
|
|
50 |
Some methods are also redefined for better performance.
|
|
51 |
|
|
52 |
Subclasses should at least implement:
|
|
53 |
at:ifAbsent: - accessing elements
|
|
54 |
removeKey:ifAbsent - removing
|
|
55 |
keysAndValuesDo: - enumerating
|
|
56 |
|
|
57 |
[author:]
|
|
58 |
Claus Gittinger
|
|
59 |
"
|
|
60 |
! !
|
|
61 |
|
21649
|
62 |
!KeyedCollection class methodsFor:'instance creation'!
|
|
63 |
|
23354
|
64 |
decodeFromLiteralArray:anArray
|
|
65 |
"create & return a new instance from information encoded in anArray."
|
|
66 |
|
|
67 |
|dictionary
|
|
68 |
sz "{ Class: SmallInteger }"|
|
|
69 |
|
|
70 |
sz := anArray size.
|
|
71 |
dictionary := self new:sz//2.
|
|
72 |
2 to:sz by:2 do:[:idx | |key val|
|
|
73 |
key := (anArray at:idx) decodeAsLiteralArray.
|
|
74 |
val := (anArray at:idx+1) decodeAsLiteralArray.
|
|
75 |
dictionary at:key put:val
|
|
76 |
].
|
|
77 |
^ dictionary
|
|
78 |
|
|
79 |
"
|
|
80 |
(SmallDictionary new
|
|
81 |
at:1 put:'one';
|
|
82 |
at:2 put:'two';
|
|
83 |
yourself
|
|
84 |
) literalArrayEncoding decodeAsLiteralArray
|
|
85 |
"
|
|
86 |
|
|
87 |
"Created: / 17-09-2018 / 14:51:59 / Stefan Vogel"
|
|
88 |
!
|
|
89 |
|
23341
|
90 |
withAll:aCollection
|
|
91 |
"create a KeyedCollection from another Collection with keys"
|
21655
|
92 |
|
|
93 |
|newDict|
|
|
94 |
|
23341
|
95 |
newDict := self new:aCollection size.
|
|
96 |
aCollection keysAndValuesDo:[:key :value |
|
21655
|
97 |
newDict at:key put:value.
|
|
98 |
].
|
|
99 |
^ newDict
|
|
100 |
|
|
101 |
"
|
|
102 |
|d|
|
|
103 |
|
|
104 |
d := Dictionary withKeys:#(a b c d e) andValues:#(1 2 3 4 5).
|
23341
|
105 |
SmallDictionary withAll:d.
|
|
106 |
|
|
107 |
SmallDictionary withAll:#('one' 'two' 'three' 'four').
|
21655
|
108 |
"
|
|
109 |
|
|
110 |
"Created: / 17-03-2017 / 11:29:09 / stefan"
|
23341
|
111 |
"Modified (comment): / 14-09-2018 / 17:54:12 / Stefan Vogel"
|
21655
|
112 |
!
|
|
113 |
|
21649
|
114 |
withAssociations:aCollectionOfAssociations
|
|
115 |
"return a new instance where associations are taken from the argument"
|
|
116 |
|
21655
|
117 |
|newDict|
|
21649
|
118 |
|
21655
|
119 |
newDict := self new:aCollectionOfAssociations size.
|
21649
|
120 |
aCollectionOfAssociations do:[:assoc |
|
|
121 |
newDict at:assoc key put:assoc value
|
|
122 |
].
|
|
123 |
^ newDict
|
|
124 |
|
|
125 |
"
|
23341
|
126 |
SmallDictionary withAssociations:{ #'one'->1 .
|
21649
|
127 |
#'two'->2 .
|
|
128 |
#'three'->3 .
|
|
129 |
#'four'->4 }
|
|
130 |
"
|
|
131 |
|
|
132 |
"Created: / 16-03-2017 / 12:15:38 / stefan"
|
21655
|
133 |
"Modified: / 17-03-2017 / 11:30:55 / stefan"
|
23341
|
134 |
"Modified (comment): / 14-09-2018 / 17:45:56 / Stefan Vogel"
|
21649
|
135 |
!
|
|
136 |
|
23341
|
137 |
withKeyValuePairs:aCollection
|
|
138 |
"return a new instance where keys and values are taken from
|
|
139 |
elements of aCollection, which are pairs (2-element SeuqenceableCollection) of key and value"
|
|
140 |
|
|
141 |
|newDict|
|
|
142 |
|
|
143 |
newDict := self new:(aCollection size).
|
|
144 |
aCollection do:[:pair |
|
|
145 |
newDict at:(pair first) put:(pair second)
|
|
146 |
].
|
|
147 |
^ newDict
|
|
148 |
|
|
149 |
"
|
|
150 |
SmallDictionary withKeyValuePairs:#( ('one' 1) ('two' 2) ('three' 3) ('four' 4))
|
|
151 |
"
|
|
152 |
|
|
153 |
"Created: / 14-09-2018 / 17:44:35 / Stefan Vogel"
|
|
154 |
!
|
|
155 |
|
|
156 |
withKeys:keyCollection andValues:valueCollection
|
21649
|
157 |
"return a new instance where keys and values are taken from
|
|
158 |
the argumentArrays."
|
|
159 |
|
|
160 |
|newColl sz "{ Class: SmallInteger }"|
|
|
161 |
|
23341
|
162 |
sz := keyCollection size.
|
21649
|
163 |
newColl := self new:sz.
|
|
164 |
1 to:sz do:[:index |
|
23341
|
165 |
newColl at:(keyCollection at:index) put:(valueCollection at:index).
|
21649
|
166 |
].
|
|
167 |
^ newColl
|
|
168 |
|
|
169 |
"Created: / 16-03-2017 / 11:06:51 / stefan"
|
|
170 |
"Modified (format): / 16-03-2017 / 12:12:09 / stefan"
|
23341
|
171 |
"Modified (format): / 14-09-2018 / 17:50:05 / Stefan Vogel"
|
21649
|
172 |
!
|
|
173 |
|
23341
|
174 |
withKeysAndValues:aSequenceableCollection
|
21649
|
175 |
"return a new instance where keys and values are taken from alternating
|
23341
|
176 |
elements of aSequenceableCollection"
|
21649
|
177 |
|
|
178 |
|newDict sz "{ Class: SmallInteger }"|
|
|
179 |
|
23341
|
180 |
sz := aSequenceableCollection size.
|
23398
|
181 |
sz odd ifTrue:[
|
|
182 |
SubscriptOutOfBoundsError
|
|
183 |
raiseRequestWith:sz+1
|
|
184 |
errorString:('odd number of elements - subscript (' , (sz+1) printString , ') would be out of bounds')
|
|
185 |
].
|
21655
|
186 |
newDict := self new:sz.
|
21649
|
187 |
1 to:sz by:2 do:[:i |
|
23341
|
188 |
newDict at:(aSequenceableCollection at:i) put:(aSequenceableCollection at:i+1)
|
21649
|
189 |
].
|
|
190 |
^ newDict
|
|
191 |
|
|
192 |
"
|
23341
|
193 |
SmallDictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4)
|
23398
|
194 |
SmallDictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3 'incomplete')
|
21649
|
195 |
"
|
|
196 |
|
|
197 |
"Created: / 16-03-2017 / 12:13:27 / stefan"
|
21655
|
198 |
"Modified: / 17-03-2017 / 11:32:42 / stefan"
|
23398
|
199 |
"Modified (comment): / 04-10-2018 / 10:09:46 / Stefan Vogel"
|
21649
|
200 |
! !
|
|
201 |
|
8893
|
202 |
!KeyedCollection class methodsFor:'queries'!
|
|
203 |
|
|
204 |
isAbstract
|
11222
|
205 |
"Return if this class is an abstract class.
|
|
206 |
True is returned for KeyedCollection here; false for subclasses.
|
19469
|
207 |
Abstract subclasses must redefine this again."
|
11222
|
208 |
|
8893
|
209 |
^ self == KeyedCollection
|
|
210 |
! !
|
|
211 |
|
3615
|
212 |
!KeyedCollection methodsFor:'accessing'!
|
|
213 |
|
21653
|
214 |
associationAt:aKey
|
21649
|
215 |
"return an association consisting of aKey and the element indexed
|
|
216 |
by aKey -
|
|
217 |
report an error, if no element is stored under aKey."
|
|
218 |
|
21653
|
219 |
^ self associationAt:aKey ifAbsent:[self errorKeyNotFound:aKey]
|
|
220 |
|
|
221 |
"Modified: / 16-03-2017 / 17:19:52 / stefan"
|
|
222 |
!
|
|
223 |
|
|
224 |
associationAt:aKey ifAbsent:exceptionBlock
|
|
225 |
"return an association consisting of aKey and the element indexed by aKey -
|
|
226 |
return result of exceptionBlock if no element is stored under aKey.
|
|
227 |
Warning: this is a comatibility interface only, with a different semantic as
|
|
228 |
the original ST80 implementation. The returned assoc is created on the fly,
|
|
229 |
and not the one stored in the receiver (there are not assocs there)"
|
|
230 |
|
|
231 |
|value|
|
|
232 |
|
|
233 |
value := self at:aKey ifAbsent:[^ exceptionBlock value].
|
|
234 |
^ Association key:aKey value:value.
|
|
235 |
|
|
236 |
"Created: / 16-03-2017 / 17:17:05 / stefan"
|
|
237 |
!
|
|
238 |
|
|
239 |
associations
|
|
240 |
"return an ordered collection containing the receiver's associations."
|
|
241 |
|
|
242 |
|coll|
|
|
243 |
|
|
244 |
coll := OrderedCollection new.
|
|
245 |
self associationsDo:[:assoc | coll add:assoc].
|
|
246 |
^ coll
|
|
247 |
|
|
248 |
"Created: / 16-03-2017 / 17:30:47 / stefan"
|
21649
|
249 |
!
|
|
250 |
|
3615
|
251 |
at:key
|
|
252 |
"return the value stored under akey.
|
|
253 |
Raise an error if not found"
|
|
254 |
|
|
255 |
^ self at:key ifAbsent:[self errorKeyNotFound:key].
|
|
256 |
|
|
257 |
"Modified: / 19.6.1998 / 00:48:27 / cg"
|
|
258 |
!
|
|
259 |
|
|
260 |
at:key ifAbsent:exceptionBlock
|
|
261 |
"return the value stored under akey.
|
|
262 |
Return the value from evaluating exceptionBlock if not found"
|
|
263 |
|
|
264 |
^ self subclassResponsibility
|
|
265 |
|
|
266 |
"Created: / 19.6.1998 / 00:48:23 / cg"
|
|
267 |
!
|
|
268 |
|
21653
|
269 |
at:aKey ifAbsent:default update:aBlock
|
|
270 |
"update the element stored under aKey with the result from
|
|
271 |
evaluating aBlock with the previous stored value as argument, or with default,
|
|
272 |
if there was no such key initially.
|
|
273 |
Return the new value stored."
|
|
274 |
|
|
275 |
^ self at:aKey put:(aBlock value:(self at:aKey ifAbsent:default))
|
|
276 |
|
|
277 |
"Created: / 16-03-2017 / 17:28:15 / stefan"
|
|
278 |
!
|
|
279 |
|
|
280 |
at:aKey ifAbsentPut:valueBlock
|
|
281 |
"return the element indexed by aKey if present,
|
|
282 |
if not present, store the result of evaluating valueBlock
|
|
283 |
under aKey and return it.
|
|
284 |
WARNING: do not add elements while iterating over the receiver.
|
|
285 |
Iterate over a copy to do this."
|
|
286 |
|
|
287 |
^ self at:aKey ifAbsent:[self at:aKey put:valueBlock value].
|
|
288 |
|
|
289 |
"Created: / 16-03-2017 / 17:23:07 / stefan"
|
|
290 |
!
|
|
291 |
|
|
292 |
at:aKey ifPresent:aBlock
|
|
293 |
"try to retrieve the value stored at aKey.
|
|
294 |
If there is nothing stored under this key, do nothing.
|
|
295 |
Otherwise, evaluate aBlock, passing the retrieved value as argument."
|
|
296 |
|
|
297 |
|v|
|
|
298 |
|
|
299 |
v := self at:aKey ifAbsent:[^ nil].
|
|
300 |
^ aBlock value:v.
|
|
301 |
|
|
302 |
"Created: / 16-03-2017 / 17:11:27 / stefan"
|
|
303 |
!
|
|
304 |
|
|
305 |
at:aKey put:anObject
|
|
306 |
"add the argument anObject under key, aKey to the receiver.
|
|
307 |
Return anObject (sigh).
|
|
308 |
WARNING: do not add elements while iterating over the receiver.
|
|
309 |
Iterate over a copy to do this."
|
|
310 |
|
|
311 |
^ self subclassResponsibility
|
|
312 |
|
|
313 |
"Created: / 16-03-2017 / 17:33:12 / stefan"
|
|
314 |
!
|
|
315 |
|
|
316 |
at:aKey put:anObject ifPresent:aBlock
|
|
317 |
"if the receiver contains an element stored under aKey,
|
|
318 |
retrieve it and evaluate aBlock passing the element as argument,
|
23888
|
319 |
return the block's value.
|
21653
|
320 |
If not, store aValue under the key.
|
|
321 |
Use this with an error-reporting block, to ensure that no keys are reused"
|
|
322 |
|
|
323 |
|value isAbsent|
|
|
324 |
|
21655
|
325 |
value := self at:aKey ifAbsentPut:[isAbsent := true. anObject].
|
21653
|
326 |
isAbsent notNil ifTrue:[
|
|
327 |
^ value.
|
|
328 |
].
|
|
329 |
|
|
330 |
^ aBlock value:value.
|
|
331 |
|
|
332 |
"Created: / 16-03-2017 / 17:38:00 / stefan"
|
21655
|
333 |
"Modified: / 17-03-2017 / 11:37:26 / stefan"
|
23888
|
334 |
"Modified (comment): / 13-03-2019 / 10:26:05 / Claus Gittinger"
|
21653
|
335 |
!
|
|
336 |
|
|
337 |
at:aKey update:aBlock
|
|
338 |
"update the element stored under aKey with the result from
|
|
339 |
evaluating aBlock with the previous stored value as argument.
|
|
340 |
Report an error if there was no such key initially.
|
|
341 |
Return the new value stored."
|
|
342 |
|
|
343 |
^ self at:aKey put:(aBlock value:(self at:aKey))
|
|
344 |
|
|
345 |
"Created: / 16-03-2017 / 17:29:22 / stefan"
|
|
346 |
!
|
|
347 |
|
21368
|
348 |
keyAtEqualValue:value
|
3615
|
349 |
"return the key under which value is stored.
|
21368
|
350 |
Raise an error if not found.
|
|
351 |
This is a slow access, since the receiver is searched sequentially.
|
|
352 |
NOTICE:
|
21369
|
353 |
The value is searched using equality compare"
|
3615
|
354 |
|
21368
|
355 |
^ self keyAtEqualValue:value ifAbsent:[self errorValueNotFound:value].
|
3615
|
356 |
|
21368
|
357 |
"Created: / 07-02-2017 / 11:11:41 / cg"
|
3615
|
358 |
!
|
|
359 |
|
21368
|
360 |
keyAtEqualValue:value ifAbsent:exceptionBlock
|
|
361 |
"return the key under which value is stored.
|
|
362 |
If not found, return the value from evaluating exceptionBlock.
|
|
363 |
This is a slow access, since the receiver is searched sequentially.
|
|
364 |
NOTICE:
|
|
365 |
The value is searched using equality compare"
|
|
366 |
|
|
367 |
self keysAndValuesDo:[:elKey :elValue |
|
|
368 |
value = elValue ifTrue:[^ elKey]
|
|
369 |
].
|
|
370 |
^ exceptionBlock value
|
|
371 |
|
|
372 |
"Created: / 07-02-2017 / 11:12:03 / cg"
|
|
373 |
!
|
|
374 |
|
|
375 |
keyAtIdenticalValue:value
|
3615
|
376 |
"return the key under which value is stored.
|
21368
|
377 |
Raise an error if not found.
|
|
378 |
This is a slow access, since the receiver is searched sequentially.
|
|
379 |
NOTICE:
|
|
380 |
The value is searched using identity compare"
|
|
381 |
|
|
382 |
^ self keyAtIdenticalValue:value ifAbsent:[self errorValueNotFound:value].
|
|
383 |
|
|
384 |
"Created: / 07-02-2017 / 11:12:23 / cg"
|
|
385 |
!
|
|
386 |
|
|
387 |
keyAtIdenticalValue:value ifAbsent:exceptionBlock
|
|
388 |
"return the key under which value is stored.
|
|
389 |
If not found, return the value from evaluating exceptionBlock.
|
|
390 |
This is a slow access, since the receiver is searched sequentially.
|
|
391 |
NOTICE:
|
|
392 |
The value is searched using identity compare"
|
3615
|
393 |
|
|
394 |
self keysAndValuesDo:[:elKey :elValue |
|
|
395 |
value == elValue ifTrue:[^ elKey]
|
|
396 |
].
|
|
397 |
^ exceptionBlock value
|
|
398 |
|
21368
|
399 |
"Created: / 07-02-2017 / 11:12:46 / cg"
|
|
400 |
!
|
|
401 |
|
|
402 |
keyAtValue:value
|
|
403 |
"return the key under which value is stored.
|
|
404 |
Raise an error if not found.
|
|
405 |
This is a slow access, since the receiver is searched sequentially.
|
|
406 |
NOTICE:
|
21653
|
407 |
The value is searched using equality compare"
|
21368
|
408 |
|
21653
|
409 |
|
|
410 |
^ self keyAtEqualValue:value.
|
21368
|
411 |
|
|
412 |
"Created: / 19-06-1998 / 00:49:16 / cg"
|
|
413 |
"Modified (comment): / 07-02-2017 / 11:13:29 / cg"
|
21653
|
414 |
"Modified (comment): / 16-03-2017 / 18:00:28 / stefan"
|
21368
|
415 |
!
|
|
416 |
|
|
417 |
keyAtValue:value ifAbsent:exceptionBlock
|
|
418 |
"return the key under which value is stored.
|
|
419 |
If not found, return the value from evaluating exceptionBlock.
|
|
420 |
This is a slow access, since the receiver is searched sequentially.
|
|
421 |
NOTICE:
|
21653
|
422 |
The value is searched using equality compare"
|
21368
|
423 |
|
21653
|
424 |
|
|
425 |
^ self keyAtEqualValue:value ifAbsent:exceptionBlock.
|
21368
|
426 |
|
|
427 |
"Created: / 19-06-1998 / 00:50:34 / cg"
|
|
428 |
"Modified: / 07-02-2017 / 11:13:20 / cg"
|
21653
|
429 |
"Modified (comment): / 16-03-2017 / 18:00:37 / stefan"
|
3615
|
430 |
!
|
|
431 |
|
|
432 |
keys
|
|
433 |
"return a collection containing the keys of the receiver"
|
|
434 |
|
|
435 |
|keyCollection|
|
|
436 |
|
|
437 |
keyCollection := OrderedCollection new.
|
|
438 |
self keysDo:[:aKey |
|
|
439 |
keyCollection add:aKey
|
|
440 |
].
|
|
441 |
^ keyCollection
|
|
442 |
|
|
443 |
"Modified: / 19.6.1998 / 00:48:27 / cg"
|
|
444 |
"Created: / 19.6.1998 / 00:51:49 / cg"
|
|
445 |
! !
|
|
446 |
|
23338
|
447 |
!KeyedCollection methodsFor:'adding'!
|
|
448 |
|
|
449 |
addAll:aCollection
|
|
450 |
"ANSI 5.7.2.1:
|
|
451 |
Message: addAll: dictionary
|
|
452 |
Synopsis
|
|
453 |
Store the elements of dictionary in the receiver at the
|
|
454 |
corresponding keys from dictionary.
|
|
455 |
Definition: <abstractDictionary>
|
|
456 |
This message is equivalent to repeatedly sending the #at:put:
|
|
457 |
message to the receiver with each of the keys and elements in
|
|
458 |
dictionary in turn. If a key in dictionary is key equivalent
|
|
459 |
to a key in the receiver, the associated element in dictionary
|
|
460 |
replaces the element in the receiver.
|
|
461 |
|
|
462 |
Returns the argument, aCollection (sigh).
|
|
463 |
|
|
464 |
WARNING: do not add elements while iterating over the receiver.
|
|
465 |
Iterate over a copy to do this."
|
|
466 |
|
|
467 |
|
|
468 |
self ~~ aCollection ifTrue:[
|
|
469 |
aCollection isSequenceable ifTrue:[
|
|
470 |
aCollection do:[:eachPair |
|
|
471 |
self at:eachPair key put:eachPair value.
|
|
472 |
].
|
|
473 |
] ifFalse:[
|
|
474 |
aCollection keysAndValuesDo:[:eachKey :eachValue |
|
|
475 |
self at:eachKey put:eachValue.
|
|
476 |
]
|
|
477 |
].
|
|
478 |
].
|
|
479 |
^ aCollection
|
|
480 |
|
|
481 |
"
|
|
482 |
|d1 d2|
|
|
483 |
|
|
484 |
d1 := Dictionary new.
|
|
485 |
d1 at:1 put:'one'.
|
|
486 |
d1 at:2 put:'two'.
|
|
487 |
d2 := Dictionary new.
|
|
488 |
d2 at:3 put:'three'.
|
|
489 |
d2 at:4 put:'four'.
|
|
490 |
d1 addAll:d2.
|
|
491 |
d1.
|
|
492 |
"
|
|
493 |
|
|
494 |
"Created: / 14-09-2018 / 16:41:49 / Stefan Vogel"
|
|
495 |
!
|
|
496 |
|
|
497 |
declareAllFrom:aDictionaryOrNil
|
|
498 |
"merge all key-value pairs from aDictionary into the receiver.
|
|
499 |
Values present in the arg will always end up in the receiver;
|
|
500 |
i.e. a value coming from the argument is already in the receiver,
|
|
501 |
the value from aDictionaryOrNil is stored into the receiver.
|
|
502 |
|
|
503 |
sigh:
|
|
504 |
For compatibility with #declare:from: the behavior should be changed as following:
|
|
505 |
If the receiver already contains a key, the existing value is retained.
|
|
506 |
To keep the compatibility with other smalltalks, the semantics of this remains
|
|
507 |
as is, and #declareAllNewFrom: was added for convenience.
|
|
508 |
See #declareAllNewFrom: which does exactly what this name implies."
|
|
509 |
|
|
510 |
(aDictionaryOrNil notNil and:[self ~~ aDictionaryOrNil]) ifTrue:[
|
|
511 |
aDictionaryOrNil keysAndValuesDo:[:key :value |
|
|
512 |
self at:key put:value.
|
|
513 |
].
|
|
514 |
]
|
|
515 |
|
|
516 |
"Created: / 14-09-2018 / 16:46:38 / Stefan Vogel"
|
|
517 |
!
|
|
518 |
|
|
519 |
declareAllNewFrom:aDictionaryOrNil
|
|
520 |
"merge all new key-value pairs from aDictionary into the receiver
|
|
521 |
i.e. If the receiver already contains a key, the existing value is retained.
|
|
522 |
See also #declareAllFrom:"
|
|
523 |
|
|
524 |
(aDictionaryOrNil notNil and:[self ~~ aDictionaryOrNil]) ifTrue:[
|
|
525 |
aDictionaryOrNil keysAndValuesDo:[:key :value |
|
|
526 |
(self includesKey:key) ifFalse:[
|
|
527 |
self at:key put:value.
|
|
528 |
].
|
|
529 |
]
|
|
530 |
]
|
|
531 |
|
|
532 |
"Created: / 14-09-2018 / 16:56:36 / Stefan Vogel"
|
|
533 |
! !
|
|
534 |
|
23354
|
535 |
!KeyedCollection methodsFor:'converting'!
|
|
536 |
|
|
537 |
literalArrayEncoding
|
|
538 |
|literalArray idx|
|
|
539 |
|
|
540 |
literalArray := Array new:(self size * 2)+1.
|
|
541 |
literalArray at:1 put:self class name.
|
|
542 |
idx := 2.
|
|
543 |
self keysAndValuesDo:[:eachKey :eachValue |
|
|
544 |
literalArray
|
|
545 |
at:idx put:eachKey literalArrayEncoding;
|
|
546 |
at:idx+1 put:eachValue literalArrayEncoding.
|
|
547 |
idx := idx + 2.
|
|
548 |
].
|
|
549 |
^ literalArray
|
|
550 |
|
|
551 |
"
|
|
552 |
|dict|
|
|
553 |
|
|
554 |
dict := SmallDictionary new.
|
|
555 |
dict at:1 put:'bla'.
|
|
556 |
dict at:'fasel' put:#[1 2 3 4].
|
|
557 |
dict literalArrayEncoding
|
|
558 |
"
|
|
559 |
|
|
560 |
"Created: / 17-09-2018 / 14:49:52 / Stefan Vogel"
|
|
561 |
! !
|
|
562 |
|
3615
|
563 |
!KeyedCollection methodsFor:'enumerating'!
|
|
564 |
|
21655
|
565 |
associationsDo:aBlock
|
|
566 |
"perform the block for all associations in the collection.
|
|
567 |
|
|
568 |
See also:
|
|
569 |
#do: (which passes values to its block)
|
|
570 |
#keysDo: (which passes only keys to its block)
|
|
571 |
#keysAndValuesDo: (which passes keys&values)
|
|
572 |
|
|
573 |
This is much like #keysAndValuesDo:, but aBlock gets the
|
|
574 |
key and value as a single association argument.
|
|
575 |
#keysAndValuesDo: and is a bit faster therefore (no intermediate objects).
|
|
576 |
|
|
577 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
578 |
Iterate over a copy to do this."
|
|
579 |
|
|
580 |
self keysAndValuesDo:[:eachKey :eachValue|
|
|
581 |
aBlock value:(Association key:eachKey value:eachValue).
|
|
582 |
].
|
|
583 |
|
|
584 |
"Created: / 17-03-2017 / 11:05:29 / stefan"
|
|
585 |
!
|
|
586 |
|
23338
|
587 |
associationsSelect:aBlock
|
|
588 |
"return a new collection with all elements from the receiver, for which
|
|
589 |
the argument aBlock evaluates to true.
|
|
590 |
The block gets keys and values as an association argument.
|
|
591 |
|
|
592 |
See also: #keysAndValuesSelect: (which is slightly faster),
|
|
593 |
#select: (which only passes the value)
|
|
594 |
|
|
595 |
This is much like #keysAndValuesSelect:, but aBlock gets the
|
|
596 |
key and value as a single association argument.
|
|
597 |
#keysAndValuesSelect: and is a bit faster therefore (no intermediate objects).
|
|
598 |
|
|
599 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
600 |
Iterate over a copy to do this."
|
|
601 |
|
|
602 |
|newCollection|
|
|
603 |
|
|
604 |
newCollection := self species new.
|
|
605 |
self keysAndValuesDo:[:key :value |
|
|
606 |
(aBlock value:(Association key:key value:value)) ifTrue:[
|
|
607 |
newCollection at:key put:value
|
|
608 |
]
|
|
609 |
].
|
|
610 |
^ newCollection
|
|
611 |
|
|
612 |
"
|
|
613 |
|ages|
|
|
614 |
|
|
615 |
ages := Dictionary new.
|
|
616 |
ages at:'cg' put:37.
|
|
617 |
ages at:'ca' put:33.
|
|
618 |
ages at:'sv' put:36.
|
|
619 |
ages at:'tk' put:28.
|
|
620 |
|
|
621 |
ages associationsSelect:[:assoc |
|
|
622 |
(assoc key startsWith:'c') or:[assoc value < 30]].
|
|
623 |
"
|
|
624 |
|
|
625 |
"Created: / 14-09-2018 / 16:44:11 / Stefan Vogel"
|
|
626 |
!
|
|
627 |
|
3615
|
628 |
do:aBlock
|
|
629 |
"evaluate aBlock for each value"
|
|
630 |
|
|
631 |
self keysAndValuesDo:[:elKey :elValue | aBlock value:elValue]
|
|
632 |
|
|
633 |
"Created: / 19.6.1998 / 00:56:24 / cg"
|
|
634 |
!
|
|
635 |
|
|
636 |
keysAndValuesDo:aBlock
|
|
637 |
"evaluate aBlock for each key and value"
|
|
638 |
|
|
639 |
^ self subclassResponsibility
|
|
640 |
|
|
641 |
"Created: / 19.6.1998 / 00:56:52 / cg"
|
23341
|
642 |
!
|
|
643 |
|
23363
|
644 |
keysAndValuesSelect:aBlock
|
|
645 |
"return a new collection with all elements from the receiver, for which
|
|
646 |
the argument aBlock evaluates to true.
|
|
647 |
The block gets keys and values as separate arguments.
|
|
648 |
|
|
649 |
See also:
|
|
650 |
#associationsSelect: (which passes key-value pairs),
|
|
651 |
#keysSelect: (which passes key values),
|
|
652 |
#select: (which only passes the value)
|
|
653 |
|
|
654 |
This is much like #associationsSelect:, but aBlock gets the
|
|
655 |
key and value as two separate arguments.
|
|
656 |
#associationsSelect: is a bit slower.
|
|
657 |
|
|
658 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
659 |
Iterate over a copy to do this."
|
|
660 |
|
|
661 |
|newCollection|
|
|
662 |
|
|
663 |
newCollection := self species new.
|
|
664 |
self keysAndValuesDo:[:key :value |
|
|
665 |
(aBlock value:key value:value) ifTrue:[
|
|
666 |
newCollection at:key put:value
|
|
667 |
]
|
|
668 |
].
|
|
669 |
^ newCollection
|
|
670 |
|
|
671 |
"
|
|
672 |
|ages|
|
|
673 |
|
|
674 |
ages := SmallDictionary new.
|
|
675 |
ages at:'cg' put:37.
|
|
676 |
ages at:'ca' put:33.
|
|
677 |
ages at:'sv' put:36.
|
|
678 |
ages at:'tk' put:28.
|
|
679 |
|
|
680 |
ages keysAndValuesSelect:[:name :age |
|
|
681 |
(name startsWith:'c') or:[age < 30]].
|
|
682 |
"
|
|
683 |
|
|
684 |
"Created: / 18-09-2018 / 15:22:05 / Stefan Vogel"
|
|
685 |
!
|
|
686 |
|
|
687 |
keysSelect:aBlock
|
|
688 |
"return a new collection with all elements from the receiver, for which
|
|
689 |
the argument aBlock evaluates to true.
|
|
690 |
The block gets the individual keys as its single argument.
|
|
691 |
|
|
692 |
See also:
|
|
693 |
#associationsSelect: (which passes key->value associations),
|
|
694 |
#keysAndValuesSelect: (which passes key & value args)
|
|
695 |
#select: (which passes values as arg),
|
|
696 |
|
|
697 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
698 |
Iterate over a copy to do this."
|
|
699 |
|
|
700 |
|newCollection|
|
|
701 |
|
|
702 |
newCollection := self species new.
|
|
703 |
self keysAndValuesDo:[:key :value |
|
|
704 |
(aBlock value:key) ifTrue:[
|
|
705 |
newCollection at:key put:value
|
|
706 |
]
|
|
707 |
].
|
|
708 |
^ newCollection
|
|
709 |
|
|
710 |
"
|
|
711 |
|d|
|
|
712 |
|
|
713 |
d := SmallDictionary new.
|
|
714 |
d at:#foo put:#bar.
|
|
715 |
d at:#bar put:#baz.
|
|
716 |
d at:#baz put:#foo.
|
|
717 |
|
|
718 |
d keysSelect:[:el | el startsWith:'b'].
|
|
719 |
"
|
|
720 |
|
|
721 |
"Created: / 18-09-2018 / 15:23:26 / Stefan Vogel"
|
|
722 |
!
|
|
723 |
|
23341
|
724 |
printElementsDo:aBlock
|
|
725 |
"redefined, so #printOn: prints associations"
|
|
726 |
|
|
727 |
^ self associationsDo:aBlock
|
|
728 |
|
|
729 |
"Created: / 14-09-2018 / 17:47:48 / Stefan Vogel"
|
23363
|
730 |
!
|
|
731 |
|
|
732 |
select:aBlock
|
|
733 |
"return a new collection with all elements from the receiver, for which
|
|
734 |
the argument aBlock evaluates to true.
|
|
735 |
The block gets the individual values as its single argument.
|
|
736 |
|
|
737 |
See also:
|
|
738 |
#associationsSelect: (which passes key->value associations),
|
|
739 |
#keysAndValuesSelect: (which passes key & value args)
|
|
740 |
#keysSelect: (which passes key values),
|
|
741 |
|
|
742 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
743 |
Iterate over a copy to do this."
|
|
744 |
|
|
745 |
|newCollection|
|
|
746 |
|
|
747 |
newCollection := self species new.
|
|
748 |
self keysAndValuesDo:[:key :value |
|
|
749 |
(aBlock value:value) ifTrue:[
|
|
750 |
newCollection at:key put:value
|
|
751 |
]
|
|
752 |
].
|
|
753 |
^ newCollection
|
|
754 |
|
|
755 |
"
|
|
756 |
|d|
|
|
757 |
|
|
758 |
d := SmallDictionary new.
|
|
759 |
d at:#foo put:#bar.
|
|
760 |
d at:#bar put:#baz.
|
|
761 |
d at:#baz put:#foo.
|
|
762 |
|
|
763 |
d select:[:el | el startsWith:'b'].
|
|
764 |
"
|
|
765 |
|
|
766 |
"Created: / 18-09-2018 / 15:25:15 / Stefan Vogel"
|
3615
|
767 |
! !
|
|
768 |
|
21655
|
769 |
|
23706
|
770 |
!KeyedCollection methodsFor:'queries'!
|
|
771 |
|
|
772 |
includesAssociation:anAssociation
|
|
773 |
"return true, if there is an association in the receiver with the
|
|
774 |
same key and value as the argument, anAssociation.
|
|
775 |
NOTICE: in contrast to #includes:, this compares both key and value."
|
|
776 |
|
|
777 |
|val|
|
|
778 |
|
|
779 |
val := self at:(anAssociation key) ifAbsent:[^ false].
|
|
780 |
^ val = anAssociation value
|
|
781 |
|
|
782 |
"Created: / 14-09-2018 / 16:48:10 / Stefan Vogel"
|
|
783 |
!
|
|
784 |
|
|
785 |
includesIdenticalKey:aKey
|
|
786 |
"return true, if the argument, aKey is a key in the receiver"
|
|
787 |
|
|
788 |
self keysDo:[:elKey | aKey == elKey ifTrue:[^ true]].
|
|
789 |
^ false
|
|
790 |
|
|
791 |
"Created: / 19.6.1998 / 00:55:05 / cg"
|
|
792 |
!
|
|
793 |
|
|
794 |
includesKey:aKey
|
|
795 |
"return true, if the argument, aKey is a key in the receiver"
|
|
796 |
|
|
797 |
self keysDo:[:elKey | aKey = elKey ifTrue:[^ true]].
|
|
798 |
^ false
|
|
799 |
|
|
800 |
"Created: / 19.6.1998 / 00:55:05 / cg"
|
|
801 |
! !
|
|
802 |
|
3615
|
803 |
!KeyedCollection methodsFor:'removing'!
|
|
804 |
|
21732
|
805 |
removeAllKeys:aCollection
|
|
806 |
"remove all keys of the argument, aCollection from the receiver.
|
|
807 |
Raises an error, if some element-to-remove is not in the receiver."
|
|
808 |
|
|
809 |
aCollection do:[:element | self removeKey:element].
|
|
810 |
|
|
811 |
"Modified (comment): / 28-04-2017 / 13:36:15 / stefan"
|
|
812 |
!
|
|
813 |
|
23338
|
814 |
removeAssociation:assoc
|
|
815 |
"remove the association from the collection.
|
|
816 |
If it was not in the collection report an error.
|
|
817 |
Only the key is used in the passed argument, and a new
|
|
818 |
association, for the key and the previously stored value is returned.
|
|
819 |
|
|
820 |
WARNING: do not remove elements while iterating over the receiver.
|
|
821 |
See #saveRemoveKey: to do this."
|
|
822 |
|
|
823 |
|key|
|
|
824 |
|
|
825 |
key := assoc key.
|
|
826 |
^ Association key:key value:(self removeKey:key)
|
|
827 |
|
|
828 |
"Created: / 14-09-2018 / 16:20:47 / Stefan Vogel"
|
|
829 |
!
|
|
830 |
|
3615
|
831 |
removeKey:aKey
|
|
832 |
"remove key (and the value stored under that key) from the
|
|
833 |
receiver; raise an error if no such element is contained"
|
|
834 |
|
|
835 |
^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey]
|
|
836 |
|
|
837 |
"Created: / 19.6.1998 / 00:53:25 / cg"
|
|
838 |
"Modified: / 19.6.1998 / 00:54:02 / cg"
|
|
839 |
!
|
|
840 |
|
|
841 |
removeKey:aKey ifAbsent:exceptionBlock
|
|
842 |
"remove key (and the value stored under that key) from the
|
19844
|
843 |
receiver; return the value which was stored previously there.
|
|
844 |
If no such element is contained, return the value
|
3615
|
845 |
from evaluating exceptionBlock"
|
|
846 |
|
|
847 |
^ self subclassResponsibility
|
|
848 |
|
|
849 |
"Created: / 19.6.1998 / 00:53:58 / cg"
|
|
850 |
! !
|
|
851 |
|
21653
|
852 |
!KeyedCollection methodsFor:'searching'!
|
|
853 |
|
|
854 |
findFirst:aBlock ifNone:exceptionValue
|
|
855 |
"find the index of the first element, for which evaluation of the argument, aBlock returns true;
|
|
856 |
return its index or the value from exceptionValue if none detected.
|
|
857 |
This is much like #detect:ifNone:, however, here an INDEX is returned,
|
|
858 |
while #detect:ifNone: returns the element.
|
|
859 |
|
|
860 |
Here we return the first key for which aBlock matches the value.
|
|
861 |
Note that there is no order in a Dictionary, so any element is first."
|
|
862 |
|
|
863 |
self keysAndValuesDo:[:eachKey :eachValue| (aBlock value:eachValue) ifTrue:[^ eachKey]].
|
|
864 |
^ exceptionValue value.
|
|
865 |
|
|
866 |
"
|
|
867 |
(KeyValueList withKeys:#('a' 'b' 'c') andValues:#('bla' 'hello' 'hallo'))
|
|
868 |
findFirst:[:v| v first = $h].
|
|
869 |
"
|
|
870 |
|
|
871 |
"Created: / 16-03-2017 / 17:46:02 / stefan"
|
|
872 |
!
|
|
873 |
|
|
874 |
findFirstKey:aBlock
|
|
875 |
"find and return the first key, for which evaluation of the argument, aBlock
|
|
876 |
returns true; return nil if none is detected."
|
|
877 |
|
|
878 |
self keysDo:[:key |
|
|
879 |
(aBlock value:key) ifTrue:[^ key].
|
|
880 |
].
|
|
881 |
^ nil
|
|
882 |
|
|
883 |
"Created: 8.10.1996 / 22:01:31 / cg"
|
|
884 |
"Modified: 8.10.1996 / 22:02:03 / cg"
|
|
885 |
! !
|
|
886 |
|
3615
|
887 |
!KeyedCollection class methodsFor:'documentation'!
|
|
888 |
|
|
889 |
version
|
19469
|
890 |
^ '$Header$'
|
12667
|
891 |
!
|
|
892 |
|
|
893 |
version_CVS
|
19469
|
894 |
^ '$Header$'
|
3615
|
895 |
! !
|
17120
|
896 |
|