15133
|
1 |
"
|
|
2 |
COPYRIGHT.
|
|
3 |
This is a Manchester Goodie protected by copyright.
|
|
4 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
5 |
part of it which is separately transmitted or stored:
|
|
6 |
* You must ensure that every copy includes this notice, and that
|
|
7 |
source and author(s) of the material are acknowledged.
|
|
8 |
* These conditions must be imposed on anyone who receives a copy.
|
|
9 |
* The material shall not be used for commercial gain without the prior
|
|
10 |
written consent of the author(s).
|
|
11 |
Further information on the copyright conditions may be obtained by
|
|
12 |
sending electronic mail:
|
|
13 |
To: goodies-lib@cs.man.ac.uk
|
|
14 |
Subject: copyright
|
|
15 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
16 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
17 |
|
|
18 |
(C) Copyright 1992 University of Manchester
|
|
19 |
For more information about the Manchester Goodies Library (from which
|
|
20 |
this file was distributed) send e-mail:
|
|
21 |
To: goodies-lib@cs.man.ac.uk
|
|
22 |
Subject: help
|
|
23 |
"
|
|
24 |
"{ Package: 'stx:libbasic2' }"
|
|
25 |
|
|
26 |
Dictionary subclass:#OrderedDictionary
|
|
27 |
instanceVariableNames:'order'
|
|
28 |
classVariableNames:''
|
|
29 |
poolDictionaries:''
|
|
30 |
category:'Collections-Sequenceable'
|
|
31 |
!
|
|
32 |
|
|
33 |
!OrderedDictionary class methodsFor:'documentation'!
|
|
34 |
|
|
35 |
copyright
|
|
36 |
"
|
|
37 |
COPYRIGHT.
|
|
38 |
This is a Manchester Goodie protected by copyright.
|
|
39 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
40 |
part of it which is separately transmitted or stored:
|
|
41 |
* You must ensure that every copy includes this notice, and that
|
|
42 |
source and author(s) of the material are acknowledged.
|
|
43 |
* These conditions must be imposed on anyone who receives a copy.
|
|
44 |
* The material shall not be used for commercial gain without the prior
|
|
45 |
written consent of the author(s).
|
|
46 |
Further information on the copyright conditions may be obtained by
|
|
47 |
sending electronic mail:
|
|
48 |
To: goodies-lib@cs.man.ac.uk
|
|
49 |
Subject: copyright
|
|
50 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
51 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
52 |
|
|
53 |
(C) Copyright 1992 University of Manchester
|
|
54 |
For more information about the Manchester Goodies Library (from which
|
|
55 |
this file was distributed) send e-mail:
|
|
56 |
To: goodies-lib@cs.man.ac.uk
|
|
57 |
Subject: help
|
|
58 |
"
|
|
59 |
!
|
|
60 |
|
|
61 |
documentation
|
|
62 |
"
|
|
63 |
I am a subclass of Dictionary whose elements (associations) are ordered in a
|
|
64 |
similar fashion to OrderedCollection.
|
|
65 |
That is, while being filled via #at:put: messages (or similar Dictionary protocol),
|
|
66 |
the order in which associations are added is remembered and accessable via the #atIndex:
|
|
67 |
or #order messages.
|
|
68 |
|
|
69 |
I have one instance variable:
|
|
70 |
|
|
71 |
order <OrderedCollection> Ordered collection of keys reflecting the order of
|
|
72 |
associations in the dictionary.
|
|
73 |
|
|
74 |
[author:]
|
|
75 |
Ifor Wyn Williams <ifor@uk.ac.man.cs>
|
|
76 |
|
|
77 |
[see also:]
|
|
78 |
OrderedCollection Dictionary
|
|
79 |
OrderedSet
|
|
80 |
"
|
|
81 |
!
|
|
82 |
|
|
83 |
examples
|
|
84 |
"
|
|
85 |
|o|
|
|
86 |
|
|
87 |
o := OrderedDictionary new.
|
|
88 |
|
|
89 |
o at:'one' put:1.
|
|
90 |
o at:'two' put:2.
|
|
91 |
o at:'three' put:3.
|
|
92 |
o at:'four' put:4.
|
|
93 |
o at:'five' put:5.
|
|
94 |
o at:'six' put:6.
|
|
95 |
o at:'seven' put:7.
|
|
96 |
o at:'eight' put:8.
|
|
97 |
o at:'nine' put:9.
|
|
98 |
o at:'zero' put:0.
|
|
99 |
|
|
100 |
o at:'eight'.
|
|
101 |
o atIndex:1.
|
|
102 |
o atIndex:5.
|
|
103 |
o atIndex:10.
|
|
104 |
|
|
105 |
o from:3 to:6 do:[:each | Transcript showCR:each ].
|
|
106 |
|
|
107 |
o collect:[:eachAssoc | eachAssoc key -> eachAssoc value squared].
|
|
108 |
o associations.
|
|
109 |
o order.
|
|
110 |
o reverse.
|
|
111 |
|
|
112 |
o atIndex:1.
|
|
113 |
o atIndex:5.
|
|
114 |
o atIndex:10.
|
|
115 |
"
|
|
116 |
!
|
|
117 |
|
|
118 |
info
|
|
119 |
"
|
|
120 |
NAME OrderedDictionary
|
|
121 |
AUTHOR Ifor Wyn Williams <ifor@uk.ac.man.cs>
|
|
122 |
CONTRIBUTOR Ifor Wyn Williams <ifor@uk.ac.man.cs>
|
|
123 |
FUNCTION An ordered dictionary
|
|
124 |
ST-VERSIONS 2.3-5, 4.0
|
|
125 |
PREREQUISITES
|
|
126 |
CONFLICTS
|
|
127 |
DISTRIBUTION global
|
|
128 |
VERSION 1.2
|
|
129 |
DATE 28.3.90
|
|
130 |
SUMMARY A dictionary that behaves like a SequencableCollection
|
|
131 |
(except that associations cannot be removed).
|
|
132 |
"
|
|
133 |
! !
|
|
134 |
|
|
135 |
!OrderedDictionary class methodsFor:'instance creation'!
|
|
136 |
|
|
137 |
new
|
|
138 |
^ super new initializeOrder
|
|
139 |
!
|
|
140 |
|
|
141 |
new: anInteger
|
|
142 |
^(super new: anInteger) initializeOrder
|
|
143 |
! !
|
|
144 |
|
|
145 |
!OrderedDictionary methodsFor:'accessing'!
|
|
146 |
|
|
147 |
after: anAssociation
|
|
148 |
"Return the association after anAssociation in the order.
|
|
149 |
If anAssociation is the last association in the order, return nil.
|
|
150 |
If anAssociation is not found, invoke an error notifier"
|
|
151 |
|
|
152 |
1 to: order size - 1 do: [:index | (self associationAt: (order at: index))
|
|
153 |
= anAssociation ifTrue: [^self associationAt: (order at: index + 1)]].
|
|
154 |
(self associationAt: (order last))
|
|
155 |
= anAssociation
|
|
156 |
ifTrue: [^nil]
|
|
157 |
ifFalse: [^self error: 'not found']
|
|
158 |
!
|
|
159 |
|
|
160 |
associations
|
|
161 |
"Return an OrderedCollection containing the receiver's associations."
|
|
162 |
|
|
163 |
^ order collect: [:key | self associationAt: key ].
|
|
164 |
!
|
|
165 |
|
|
166 |
at:aKey ifAbsentPut:valueBlock
|
|
167 |
|val|
|
|
168 |
|
|
169 |
^ self at:aKey ifAbsent:[ self at:aKey put:valueBlock value ]
|
|
170 |
!
|
|
171 |
|
|
172 |
at: key put: anObject
|
|
173 |
"Set the value at key to be anObject.
|
|
174 |
If key is not found, create a new entry for key and set is value to anObject.
|
|
175 |
If key is already present, the order remains unchanged.
|
|
176 |
Return anObject."
|
|
177 |
|
|
178 |
"/ claus: super can check this much faster ...
|
|
179 |
"/ (super includesKey:key)
|
|
180 |
"/ ... but that leads to trouble in add:* methods. (sigh)
|
|
181 |
|
|
182 |
(order includes: key) ifFalse: [order add: key].
|
|
183 |
^ super at: key put: anObject
|
|
184 |
!
|
|
185 |
|
|
186 |
atAll:indexCollection put: anObject
|
|
187 |
"Put anObject into the value field of every association specified by indexCollection,
|
|
188 |
which is typically an interval."
|
|
189 |
|
|
190 |
indexCollection do:[:index | self at:(order at: index) put:anObject]
|
|
191 |
!
|
|
192 |
|
|
193 |
atAllPut: anObject
|
|
194 |
"Put anObject into the value field of every association in the dictionary"
|
|
195 |
|
|
196 |
order do: [:key | self at: key put: anObject]
|
|
197 |
!
|
|
198 |
|
|
199 |
atIndex:index
|
|
200 |
"return an element at a given index"
|
|
201 |
|
|
202 |
^ self at:(order at:index)
|
|
203 |
|
|
204 |
"Created: 28.9.1995 / 13:49:39 / stefan"
|
|
205 |
!
|
|
206 |
|
|
207 |
atIndex:index put:anAssociation
|
|
208 |
"put an association to a given index. remove the old associatioan at this index"
|
|
209 |
|key|
|
|
210 |
|
|
211 |
key := anAssociation key.
|
|
212 |
(super includesKey:key) ifTrue:[
|
|
213 |
^ self error:'duplicate key'.
|
|
214 |
].
|
|
215 |
super removeKey:(order at:index) ifAbsent:[].
|
|
216 |
order at:index put:key.
|
|
217 |
^ super add:anAssociation.
|
|
218 |
|
|
219 |
"Created: 28.9.1995 / 16:30:15 / stefan"
|
|
220 |
!
|
|
221 |
|
|
222 |
before: anAssociation
|
|
223 |
"Return the association before anAssociation in the order.
|
|
224 |
If anAssociation is the first association in the order, return nil.
|
|
225 |
If anAssociation is not found, invoke an error notifier"
|
|
226 |
|
|
227 |
2 to:order size do:[:index |
|
|
228 |
(self associationAt:(order at:index)) = anAssociation
|
|
229 |
ifTrue:[ ^ self associationAt:(order at:index - 1)]
|
|
230 |
].
|
|
231 |
(self associationAt:order first) = anAssociation ifTrue: [^ nil].
|
|
232 |
^ self error: 'not found'
|
|
233 |
!
|
|
234 |
|
|
235 |
first
|
|
236 |
"Return the first association of the receiver.
|
|
237 |
Provide an error notification if the receiver contains no elements."
|
|
238 |
|
|
239 |
^ self associationAt: (order first)
|
|
240 |
|
|
241 |
"
|
|
242 |
OrderedDictionary new first
|
|
243 |
"
|
|
244 |
!
|
|
245 |
|
|
246 |
keyAt:index
|
|
247 |
"get the key at the given index"
|
|
248 |
|
|
249 |
^ order at:index.
|
|
250 |
|
|
251 |
"
|
|
252 |
|s|
|
|
253 |
|
|
254 |
s := OrderedDictionary new.
|
|
255 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'.
|
|
256 |
s keyAt:2
|
|
257 |
"
|
|
258 |
|
|
259 |
"Created: 29.9.1995 / 11:32:07 / stefan"
|
|
260 |
!
|
|
261 |
|
|
262 |
keys
|
|
263 |
"Return a OrderedCollection containing the receiver's keys."
|
|
264 |
|
|
265 |
^ order copy.
|
|
266 |
|
|
267 |
"
|
|
268 |
|s|
|
|
269 |
|
|
270 |
s := OrderedDictionary new.
|
|
271 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'.
|
|
272 |
s keys
|
|
273 |
"
|
|
274 |
!
|
|
275 |
|
|
276 |
last
|
|
277 |
"Return the last association of the receiver.
|
|
278 |
Provide an error notification if the receiver contains no elements."
|
|
279 |
|
|
280 |
^ self associationAt: (order last)
|
|
281 |
|
|
282 |
"
|
|
283 |
OrderedDictionary new last
|
|
284 |
"
|
|
285 |
!
|
|
286 |
|
|
287 |
order
|
|
288 |
"returns the keys in the order of their appearance"
|
|
289 |
|
|
290 |
^ order
|
|
291 |
|
|
292 |
"
|
|
293 |
|s|
|
|
294 |
|
|
295 |
s := OrderedDictionary new.
|
|
296 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'.
|
|
297 |
s order
|
|
298 |
"
|
|
299 |
!
|
|
300 |
|
|
301 |
valueAt:index
|
|
302 |
"get the value at the given index"
|
|
303 |
|
|
304 |
^ super at:(order at:index).
|
|
305 |
|
|
306 |
"
|
|
307 |
|s|
|
|
308 |
|
|
309 |
s := OrderedDictionary new.
|
|
310 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'.
|
|
311 |
s valueAt:2
|
|
312 |
"
|
|
313 |
!
|
|
314 |
|
|
315 |
values
|
|
316 |
"Return a OrderedCollection containing the receiver's values."
|
|
317 |
|
|
318 |
^ order collect: [:key | (self at: key) ].
|
|
319 |
! !
|
|
320 |
|
|
321 |
!OrderedDictionary methodsFor:'adding'!
|
|
322 |
|
|
323 |
add: anAssociation
|
|
324 |
"add anAssociation to the dictionary.
|
|
325 |
If anAssociation is already present in the dictionary,
|
|
326 |
the order will not be changed. (See also: #addLast:)"
|
|
327 |
|
|
328 |
| key |
|
|
329 |
|
|
330 |
key := anAssociation key.
|
|
331 |
(super includesKey: key)
|
|
332 |
ifFalse: [order add: key].
|
|
333 |
^ super add: anAssociation
|
|
334 |
!
|
|
335 |
|
|
336 |
add: anAssociation after: oldAssociation
|
|
337 |
"Add the argument, anAssociation, as an element of the dictionary. Put it
|
|
338 |
in the position just succeeding oldAssociation. Return anAssociation."
|
|
339 |
|
|
340 |
| index |
|
|
341 |
|
|
342 |
index := self indexOfAssociation: oldAssociation
|
|
343 |
ifAbsent: [self error: 'association not found'].
|
|
344 |
self removeFromOrder: anAssociation key.
|
|
345 |
order add: anAssociation key after: (order at: index).
|
|
346 |
super add: anAssociation.
|
|
347 |
^ anAssociation
|
|
348 |
!
|
|
349 |
|
|
350 |
add: anAssociation before: oldAssociation
|
|
351 |
"Add the argument, anAssociation, as an element of the dictionary. Put it
|
|
352 |
in the position just preceding oldAssociation. Return anAssociation."
|
|
353 |
|
|
354 |
| index |
|
|
355 |
|
|
356 |
index := self indexOfAssociation: oldAssociation
|
|
357 |
ifAbsent: [self error: 'association not found'].
|
|
358 |
self removeFromOrder: anAssociation key.
|
|
359 |
order add: anAssociation key before: (order at: index).
|
|
360 |
super add: anAssociation.
|
|
361 |
^ anAssociation
|
|
362 |
!
|
|
363 |
|
|
364 |
add: anAssociation beforeIndex: spot
|
|
365 |
"Add the argument, anAssociation, as an element of the receiver. Put it
|
|
366 |
in the position just preceding the indexed position spot. Return newObject."
|
|
367 |
|
|
368 |
self removeFromOrder: anAssociation key.
|
|
369 |
order add: anAssociation key beforeIndex: spot.
|
|
370 |
^ super add: anAssociation
|
|
371 |
|
|
372 |
"Modified: 28.9.1995 / 14:06:53 / stefan"
|
|
373 |
!
|
|
374 |
|
|
375 |
addAll:aCollectionOfAssociations
|
|
376 |
"Add each element of anOrderedCollectionOfAssociations at my end.
|
|
377 |
We expect the argument to enumerate associations with #reverseDo:;
|
|
378 |
if it does not (i.e. it is another OD or a dictionary), use #addAllAssociationsFirst:.
|
|
379 |
Returns the argument, aCollectionOfAssociations (sigh)."
|
|
380 |
|
|
381 |
self addAllLast:aCollectionOfAssociations.
|
|
382 |
^ aCollectionOfAssociations
|
|
383 |
|
|
384 |
"Modified: 28.2.1997 / 15:51:23 / cg"
|
|
385 |
!
|
|
386 |
|
|
387 |
addAllAssociations:aDictionaryOrOrderedDictionary
|
|
388 |
"Add each association of aDictionaryOrOrderedDictionary to my end.
|
|
389 |
We expect the argument to respond to #associationsDo:."
|
|
390 |
|
|
391 |
self addAllAssociationsLast:aDictionaryOrOrderedDictionary.
|
|
392 |
^ aDictionaryOrOrderedDictionary
|
|
393 |
|
|
394 |
"Created: 28.2.1997 / 15:52:02 / cg"
|
|
395 |
!
|
|
396 |
|
|
397 |
addAllAssociationsFirst:aDictionaryOrOrderedDictionary
|
|
398 |
"Add each association of aDictionaryOrOrderedDictionary at the beginning of the
|
|
399 |
receiver. We expect the argument to respond to #associationsReverseDo:."
|
|
400 |
|
|
401 |
aDictionaryOrOrderedDictionary associationsReverseDo:[:each | self addFirst:each].
|
|
402 |
^ aDictionaryOrOrderedDictionary
|
|
403 |
|
|
404 |
"Created: 28.2.1997 / 15:50:14 / cg"
|
|
405 |
!
|
|
406 |
|
|
407 |
addAllAssociationsLast:aDictionaryOrOrderedDictionary
|
|
408 |
"Add each association of aDictionaryOrOrderedDictionary at the end of the
|
|
409 |
receiver. We expect the argument to respond to #associationsDo:."
|
|
410 |
|
|
411 |
aDictionaryOrOrderedDictionary associationsDo:[:each | self addLast:each].
|
|
412 |
^ aDictionaryOrOrderedDictionary
|
|
413 |
|
|
414 |
"Created: 28.2.1997 / 15:48:37 / cg"
|
|
415 |
!
|
|
416 |
|
|
417 |
addFirst: anAssociation
|
|
418 |
"Add anAssociation to the beginning of the receiver."
|
|
419 |
|
|
420 |
self removeFromOrder: anAssociation key.
|
|
421 |
order addFirst: anAssociation key.
|
|
422 |
^ super add: anAssociation.
|
|
423 |
!
|
|
424 |
|
|
425 |
addLast: anAssociation
|
|
426 |
"Add anAssociation to the end of the receiver.
|
|
427 |
If anAssociation is already present in the dictionary,
|
|
428 |
it will be moved to the end. (See also: #add:)"
|
|
429 |
|
|
430 |
self removeFromOrder: anAssociation key.
|
|
431 |
order add: anAssociation key.
|
|
432 |
^ super add: anAssociation.
|
|
433 |
! !
|
|
434 |
|
|
435 |
!OrderedDictionary methodsFor:'copying'!
|
|
436 |
|
|
437 |
copyEmpty
|
|
438 |
"Return a copy of the receiver that contains no elements."
|
|
439 |
|
|
440 |
^ (self class) new: 10
|
|
441 |
!
|
|
442 |
|
|
443 |
copyFrom: startIndex to: endIndex
|
|
444 |
"Return a copy of the receiver that contains elements from
|
|
445 |
position startIndex to endIndex."
|
|
446 |
|
|
447 |
| newDict |
|
|
448 |
endIndex < startIndex ifTrue: [^self copyEmpty].
|
|
449 |
(startIndex < 1 or: [endIndex > order size])
|
|
450 |
ifTrue: [^ self errorNotFound].
|
|
451 |
newDict := self copyEmpty: endIndex - startIndex + 1.
|
|
452 |
startIndex to: endIndex do: [:index | newDict add: (self associationAt: (order at: index))].
|
|
453 |
^ newDict
|
|
454 |
!
|
|
455 |
|
|
456 |
copyWith: anAssociation
|
|
457 |
"Return a copy of the dictionary that is 1 bigger than the receiver and
|
|
458 |
includes the argument, anAssociation, at the end."
|
|
459 |
|
|
460 |
| newDict |
|
|
461 |
newDict := self copy.
|
|
462 |
newDict add: anAssociation.
|
|
463 |
^ newDict
|
|
464 |
!
|
|
465 |
|
|
466 |
copyWithout: anAssociation
|
|
467 |
"Return a copy of the dictionary that is 1 smaller than the receiver and
|
|
468 |
does not include the argument, anAssociation
|
|
469 |
No error is reported, if elementToSkip is not in the collection."
|
|
470 |
|
|
471 |
| newDict |
|
|
472 |
newDict := self class new:order size - 1.
|
|
473 |
self associationsDo: [:assoc | anAssociation = assoc ifFalse: [newDict add: assoc]]
|
|
474 |
!
|
|
475 |
|
|
476 |
postCopy
|
|
477 |
"have to copy the order too"
|
|
478 |
|
|
479 |
super postCopy.
|
|
480 |
order := order copy
|
|
481 |
! !
|
|
482 |
|
|
483 |
!OrderedDictionary methodsFor:'enumerating'!
|
|
484 |
|
|
485 |
associationsCollect: aBlock
|
|
486 |
"Evaluate aBlock with each of the associations of the dictionary as argument,
|
|
487 |
and return a new (ordered) collection with the results"
|
|
488 |
|
|
489 |
^ order collect:[:key | (aBlock value: (self associationAt:key))].
|
|
490 |
!
|
|
491 |
|
|
492 |
associationsDo: aBlock
|
|
493 |
"Evaluate aBlock for each of the dictionary's associations."
|
|
494 |
|
|
495 |
order do: [:key | aBlock value: (self associationAt: key)]
|
|
496 |
!
|
|
497 |
|
|
498 |
associationsDo: aBlock from: firstIndex to: secondIndex
|
|
499 |
"Evaluate aBlock with each of the dictionary's associations from index
|
|
500 |
firstIndex to index secondIndex as the argument."
|
|
501 |
|
|
502 |
firstIndex to: secondIndex do: [:index |
|
|
503 |
aBlock value: (self associationAt: (order at: index))
|
|
504 |
]
|
|
505 |
!
|
|
506 |
|
|
507 |
associationsReverseDo: aBlock
|
|
508 |
"Evaluate aBlock for each of the dictionary's associations in reverse order."
|
|
509 |
|
|
510 |
order reverseDo:[:key | aBlock value:(self associationAt:key)]
|
|
511 |
|
|
512 |
"Created: 28.2.1997 / 15:52:31 / cg"
|
|
513 |
!
|
|
514 |
|
|
515 |
associationsSelect:aBlock
|
|
516 |
"Evaluate aBlock with each of the dictionary's associations as the argument.
|
|
517 |
Collect into a new OrderedDictionary only those associations for which
|
|
518 |
aBlock evaluates to true. Return the new OrderedDictionary."
|
|
519 |
|
|
520 |
|newDict|
|
|
521 |
|
|
522 |
newDict := self class new.
|
|
523 |
order do:[:key |
|
|
524 |
|assoc|
|
|
525 |
|
|
526 |
assoc := self associationAt:key.
|
|
527 |
(aBlock value:assoc) ifTrue: [
|
|
528 |
newDict add:assoc
|
|
529 |
]
|
|
530 |
].
|
|
531 |
^ newDict
|
|
532 |
!
|
|
533 |
|
|
534 |
collect: aBlock
|
|
535 |
"Evaluate aBlock with each of the values of the dictionary as argument,
|
|
536 |
and return a new (ordered) collection with the results"
|
|
537 |
|
|
538 |
^ order collect:[:key | (aBlock value: (self at:key))].
|
|
539 |
!
|
|
540 |
|
|
541 |
do: aBlock
|
|
542 |
"Evaluate aBlock for each of the dictionary's values."
|
|
543 |
|
|
544 |
order do: [:key | aBlock value: (self at: key)]
|
|
545 |
!
|
|
546 |
|
|
547 |
do: aBlock from: firstIndex to: lastIndex
|
|
548 |
"Evaluate aBlock with each of the dictionary's associations from index
|
|
549 |
firstIndex to index secondIndex as the argument."
|
|
550 |
|
|
551 |
self from:firstIndex to:lastIndex do:aBlock.
|
|
552 |
!
|
|
553 |
|
|
554 |
findFirst:aBlock ifNone:exceptionalValue
|
|
555 |
"Return the index of the first association in the dictionary for which aBlock
|
|
556 |
evaluates as true. If the block does not evaluate to true, return exceptionalValue"
|
|
557 |
|
|
558 |
1 to:order size do:[:index |
|
|
559 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index]
|
|
560 |
].
|
|
561 |
^ exceptionalValue value
|
|
562 |
!
|
|
563 |
|
|
564 |
findLast:aBlock
|
|
565 |
"Return the index of the last association in the dictionary for which aBlock
|
|
566 |
evaluates as true. If the block does not evaluate to true, return 0"
|
|
567 |
|
|
568 |
order size to:1 by:-1 do: [:index |
|
|
569 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index]
|
|
570 |
].
|
|
571 |
^ 0
|
|
572 |
!
|
|
573 |
|
|
574 |
findLast:aBlock startingAt:startIndex
|
|
575 |
"Return the index of the last association in the dictionary for which aBlock
|
|
576 |
evaluates as true. Start the search at startIndex.
|
|
577 |
If the block does not evaluate to true, return 0"
|
|
578 |
|
|
579 |
startIndex to:1 by:-1 do: [:index |
|
|
580 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index]
|
|
581 |
].
|
|
582 |
^ 0
|
|
583 |
!
|
|
584 |
|
|
585 |
findLast:aBlock startingAt:startIndex endingAt:endIndex
|
|
586 |
"Return the index of the last association in the dictionary for which aBlock
|
|
587 |
evaluates as true. Start the search at startIndex.
|
|
588 |
End the search at endIndex or when an element is found.
|
|
589 |
If the block does not evaluate to true, return 0"
|
|
590 |
|
|
591 |
startIndex to:endIndex by:-1 do: [:index |
|
|
592 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index]
|
|
593 |
].
|
|
594 |
^ 0
|
|
595 |
!
|
|
596 |
|
|
597 |
from:firstIndex to:lastIndex do: aBlock
|
|
598 |
"Evaluate aBlock with each of the dictionary's associations from index
|
|
599 |
firstIndex to index secondIndex as the argument."
|
|
600 |
|
|
601 |
order from:firstIndex to:lastIndex do:[:key |
|
|
602 |
aBlock value: (self at:key)
|
|
603 |
].
|
|
604 |
!
|
|
605 |
|
|
606 |
keysAndValuesDo:aBlock
|
|
607 |
"perform the block for all keys in the collection.
|
|
608 |
|
|
609 |
See also:
|
|
610 |
#associationsDo: (which passes key-value associations)
|
|
611 |
#keysAndValuesDo: (which passes keys & values separately)
|
|
612 |
#do: (which passes values only)
|
|
613 |
|
|
614 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
615 |
Iterate over a copy to do this."
|
|
616 |
|
|
617 |
order do: [:key | aBlock value:key value:(self at: key)].
|
|
618 |
|
|
619 |
"Modified: / 26.6.1999 / 10:55:30 / ps"
|
|
620 |
"Created: / 15.10.1999 / 16:49:31 / cg"
|
|
621 |
"Modified: / 15.10.1999 / 16:53:50 / cg"
|
|
622 |
!
|
|
623 |
|
|
624 |
keysDo:aBlock
|
|
625 |
"evaluate the argument, aBlock for every key in the collection.
|
|
626 |
|
|
627 |
See also:
|
|
628 |
#associationsDo: (which passes key-value associations)
|
|
629 |
#keysAndValuesDo: (which passes keys & values separately)
|
|
630 |
#do: (which passes values only)
|
|
631 |
|
|
632 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
633 |
Iterate over a copy to do this."
|
|
634 |
|
|
635 |
order do:[:key | aBlock value:key].
|
|
636 |
|
|
637 |
"Created: / 26-06-1999 / 10:53:00 / ps"
|
|
638 |
"Modified: / 24-08-2010 / 10:14:06 / cg"
|
|
639 |
!
|
|
640 |
|
|
641 |
reverseDo: aBlock
|
|
642 |
"Evaluate aBlock with each of the dictionary's associations as the argument,
|
|
643 |
starting with the last element and taking each in sequence up to the first."
|
|
644 |
|
|
645 |
order size to:1 by:-1 do: [:index |
|
|
646 |
aBlock value:(self associationAt:(order at:index))
|
|
647 |
]
|
|
648 |
!
|
|
649 |
|
|
650 |
reversed
|
|
651 |
"Return with a new OrderedDictionary with its associations in reverse order."
|
|
652 |
|
|
653 |
| newDict|
|
|
654 |
|
|
655 |
newDict := self class new.
|
|
656 |
order size to:1 by:-1 do:[:index |
|
|
657 |
|key|
|
|
658 |
|
|
659 |
key := order at:index.
|
|
660 |
newDict at:key put:(self at:key)
|
|
661 |
].
|
|
662 |
^ newDict
|
|
663 |
!
|
|
664 |
|
|
665 |
select:aBlock
|
|
666 |
"Evaluate aBlock with each of the dictionary's values as the argument.
|
|
667 |
Collect into a new OrderedDictionary only those associations for which
|
|
668 |
aBlock evaluated to true. Return the new OrderedDictionary."
|
|
669 |
|
|
670 |
|newColl|
|
|
671 |
|
|
672 |
newColl := self species new.
|
|
673 |
order do:[:key |
|
|
674 |
|assoc|
|
|
675 |
|
|
676 |
assoc := self associationAt:key.
|
|
677 |
(aBlock value:(assoc value)) ifTrue: [
|
|
678 |
newColl add:assoc
|
|
679 |
]
|
|
680 |
].
|
|
681 |
^ newColl
|
|
682 |
! !
|
|
683 |
|
|
684 |
!OrderedDictionary methodsFor:'initialization'!
|
|
685 |
|
|
686 |
initializeOrder
|
|
687 |
order := OrderedCollection new
|
|
688 |
! !
|
|
689 |
|
|
690 |
!OrderedDictionary methodsFor:'private'!
|
|
691 |
|
|
692 |
removeFromOrder: aKey
|
|
693 |
order remove: aKey ifAbsent: []
|
|
694 |
! !
|
|
695 |
|
|
696 |
!OrderedDictionary methodsFor:'queries'!
|
|
697 |
|
|
698 |
occurrencesOfKey: aKey
|
|
699 |
"Return how many of the dictionary's keys are equal to aKey."
|
|
700 |
|
|
701 |
^ order count:[:eachKey | aKey = eachKey]
|
|
702 |
|
|
703 |
"/ cg: the original code was not very smalltalkish:
|
|
704 |
"/
|
|
705 |
"/ | count |
|
|
706 |
"/
|
|
707 |
"/ count := 0.
|
|
708 |
"/ 1 to: self size do: [:index | aKey = (order at: index) ifTrue: [count := count + 1]].
|
|
709 |
"/ ^count
|
|
710 |
!
|
|
711 |
|
|
712 |
occurrencesOfValue: aValue
|
|
713 |
"Return how many of the dictionary's values are equal to aValue."
|
|
714 |
|
|
715 |
^ order count:[:eachKey | aValue = (self at:eachKey)]
|
|
716 |
|
|
717 |
"/ cg: the original code was not very smalltalkish:
|
|
718 |
"/
|
|
719 |
"/ | count |
|
|
720 |
"/
|
|
721 |
"/ count := 0.
|
|
722 |
"/ 1 to: self size do: [:index |
|
|
723 |
"/ aValue = (self at: (order at: index)) ifTrue: [count := count + 1]].
|
|
724 |
"/ ^count
|
|
725 |
! !
|
|
726 |
|
|
727 |
!OrderedDictionary methodsFor:'removing'!
|
|
728 |
|
|
729 |
removeFirst
|
|
730 |
|key|
|
|
731 |
|
|
732 |
order size == 0 ifTrue:[
|
|
733 |
"error if collection is empty"
|
|
734 |
|
|
735 |
^ self emptyCollectionError.
|
|
736 |
].
|
|
737 |
key := order removeFirst.
|
|
738 |
^ super removeKey:key.
|
|
739 |
!
|
|
740 |
|
|
741 |
removeFromIndex:fromIndex toIndex:toIndex
|
|
742 |
"Returns the receiver."
|
|
743 |
|
|
744 |
| keys |
|
|
745 |
|
|
746 |
keys := order copyFrom:fromIndex to:toIndex.
|
|
747 |
order removeFromIndex:fromIndex toIndex:toIndex.
|
|
748 |
keys do:[ :key |
|
|
749 |
super removeKey:key.
|
|
750 |
].
|
|
751 |
|
|
752 |
"Created: 28.9.1995 / 12:04:33 / stefan"
|
|
753 |
!
|
|
754 |
|
|
755 |
removeKey:aKey
|
|
756 |
order remove:aKey.
|
|
757 |
^ super removeKey:aKey.
|
|
758 |
!
|
|
759 |
|
|
760 |
removeKey:aKey ifAbsent:aBlock
|
|
761 |
order remove:aKey ifAbsent:[].
|
|
762 |
^ super removeKey:aKey ifAbsent:aBlock.
|
|
763 |
|
|
764 |
"Created: / 31-01-2011 / 22:04:01 / cg"
|
|
765 |
!
|
|
766 |
|
|
767 |
removeLast
|
|
768 |
|key|
|
|
769 |
|
|
770 |
order size == 0 ifTrue:[
|
|
771 |
"error if collection is empty"
|
|
772 |
|
|
773 |
^ self emptyCollectionError.
|
|
774 |
].
|
|
775 |
key := order removeLast.
|
|
776 |
^ super removeKey:key.
|
|
777 |
! !
|
|
778 |
|
|
779 |
!OrderedDictionary methodsFor:'searching'!
|
|
780 |
|
|
781 |
identityIndexOfAssociation: anAssociation
|
|
782 |
"Return the identity index of anAssociation within the receiver. If the receiver
|
|
783 |
does not contain anAssociation, return 0."
|
|
784 |
|
|
785 |
^ self identityIndexOfAssociation: anAssociation ifAbsent: [0]
|
|
786 |
!
|
|
787 |
|
|
788 |
identityIndexOfAssociation: anAssociation ifAbsent: exceptionBlock
|
|
789 |
"Return the identity index of anAssociation within the receiver.
|
|
790 |
If the receiver does not contain anAssociation,
|
|
791 |
return the result of evaluating the exceptionBlock."
|
|
792 |
|
|
793 |
"/ as ST/X's dictionaries do not store the associations
|
|
794 |
"/ (instead, they store the keys and values in separate collections)
|
|
795 |
"/ this will not work. You have to think about it and rewrite your code.
|
|
796 |
self error:'this does not work in Smalltalk/X'.
|
|
797 |
|
|
798 |
order keysAndValuesDo:[:i :key |
|
|
799 |
(self associationAt:key) == anAssociation ifTrue: [^i]
|
|
800 |
].
|
|
801 |
^exceptionBlock value
|
|
802 |
!
|
|
803 |
|
|
804 |
identityIndexOfKey: aKey
|
|
805 |
"Return the identity index of aKey within the receiver. If the receiver
|
|
806 |
does not contain aKey, return 0."
|
|
807 |
|
|
808 |
^self identityIndexOfKey: aKey ifAbsent: [0]
|
|
809 |
!
|
|
810 |
|
|
811 |
identityIndexOfKey: aKey ifAbsent: exceptionBlock
|
|
812 |
"Return the identity index of aKey within the receiver. If the receiver does
|
|
813 |
not contain aKey, return the result of evaluating the exceptionBlock."
|
|
814 |
|
|
815 |
^ order identityIndexOf:aKey ifAbsent:exceptionBlock
|
|
816 |
!
|
|
817 |
|
|
818 |
identityIndexOfValue: aValue
|
|
819 |
"Return the identity index of aValue within the receiver. If the receiver
|
|
820 |
does not contain aValue, return 0."
|
|
821 |
|
|
822 |
^self identityIndexOfValue: aValue ifAbsent: [0]
|
|
823 |
!
|
|
824 |
|
|
825 |
identityIndexOfValue: aValue ifAbsent: exceptionBlock
|
|
826 |
"Return the identity index of aValue within the receiver. If the receiver
|
|
827 |
does not contain aValue, return the result of evaluating the exceptionBlock."
|
|
828 |
|
|
829 |
order keysAndValuesDo:[:i :key | (self at:key) == aValue ifTrue: [^i]].
|
|
830 |
^exceptionBlock value
|
|
831 |
!
|
|
832 |
|
|
833 |
indexOfAssociation: anAssociation
|
|
834 |
"Return the index of anAssociation within the receiver. If the receiver does
|
|
835 |
not contain anAssociation, return 0."
|
|
836 |
|
|
837 |
^self indexOfAssociation: anAssociation ifAbsent: [0]
|
|
838 |
!
|
|
839 |
|
|
840 |
indexOfAssociation: anAssociation ifAbsent: exceptionBlock
|
|
841 |
"Return the identity index of anAssociation within the receiver. If the receiver
|
|
842 |
does not contain anAssociation, return the result of evaluating the exceptionBlock."
|
|
843 |
|
|
844 |
order keysAndValuesDo:[:i :key |
|
|
845 |
(self associationAt:key) = anAssociation ifTrue: [^i]
|
|
846 |
].
|
|
847 |
^exceptionBlock value
|
|
848 |
!
|
|
849 |
|
|
850 |
indexOfKey: aKey
|
|
851 |
"Return the index of aKey within the receiver. If the receiver does
|
|
852 |
not contain aKey, return 0."
|
|
853 |
|
|
854 |
^self indexOfKey: aKey ifAbsent: [0]
|
|
855 |
!
|
|
856 |
|
|
857 |
indexOfKey: aKey ifAbsent: exceptionBlock
|
|
858 |
"Return the identity index of aKey within the receiver. If the receiver does
|
|
859 |
not contain aKey, return the result of evaluating the exceptionBlock."
|
|
860 |
|
|
861 |
^ order indexOf:aKey ifAbsent:exceptionBlock
|
|
862 |
!
|
|
863 |
|
|
864 |
indexOfValue: aValue
|
|
865 |
"Return the index of aValue within the receiver.
|
|
866 |
If the receiver does not contain aValue, return 0."
|
|
867 |
|
|
868 |
^self indexOfValue: aValue ifAbsent: [0]
|
|
869 |
!
|
|
870 |
|
|
871 |
indexOfValue: aValue ifAbsent: exceptionBlock
|
|
872 |
"Return the identity index of aValue within the receiver.
|
|
873 |
If the receiver does not contain aValue, return the result of evaluating the exceptionBlock."
|
|
874 |
|
|
875 |
order keysAndValuesDo:[:i :key | (self at:key) = aValue ifTrue: [^i]].
|
|
876 |
^ exceptionBlock value
|
|
877 |
!
|
|
878 |
|
|
879 |
nextIndexOfAssociation: aAssociation from: startIndex to: stopIndex
|
|
880 |
"Return the next index of aAssociation within the receiver between startIndex
|
|
881 |
and stopIndex. If the receiver does not contain aAssociation, return nil"
|
|
882 |
|
|
883 |
startIndex to: stopIndex do: [:i |
|
|
884 |
(self associationAt: (order at: i)) = aAssociation ifTrue: [^i]].
|
|
885 |
^nil
|
|
886 |
!
|
|
887 |
|
|
888 |
nextIndexOfKey: aKey from: startIndex to: stopIndex
|
|
889 |
"Return the next index of aKey within the receiver between startIndex and
|
|
890 |
stopIndex. If the receiver does not contain aKey, return nil"
|
|
891 |
|
|
892 |
startIndex to: stopIndex do: [:i |
|
|
893 |
(order at: i) = aKey ifTrue: [^i]].
|
|
894 |
^nil
|
|
895 |
!
|
|
896 |
|
|
897 |
nextIndexOfValue: aValue from: startIndex to: stopIndex
|
|
898 |
"Return the next index of aValue within the receiver between startIndex and
|
|
899 |
stopIndex. If the receiver does not contain aValue, return nil"
|
|
900 |
|
|
901 |
startIndex to: stopIndex do: [:i |
|
|
902 |
(self at: (order at: i)) = aValue ifTrue: [^i]].
|
|
903 |
^nil
|
|
904 |
!
|
|
905 |
|
|
906 |
prevIndexOfAssociation: aAssociation from: startIndex to: stopIndex
|
|
907 |
"Return the previous index of aAssociation within the receiver between
|
|
908 |
startIndex and stopIndex working backwards through the receiver.
|
|
909 |
If the receiver does not contain aAssociation, return nil"
|
|
910 |
|
|
911 |
startIndex to: stopIndex by: -1
|
|
912 |
do: [:i | (self associationAt: (order at: i)) = aAssociation ifTrue: [^i]].
|
|
913 |
^nil
|
|
914 |
!
|
|
915 |
|
|
916 |
prevIndexOfKey: aKey from: startIndex to: stopIndex
|
|
917 |
"Return the previous index of aKey within the receiver between startIndex and
|
|
918 |
stopIndex working backwards through the receiver.
|
|
919 |
If the receiver does not contain aKey, return nil"
|
|
920 |
|
|
921 |
startIndex to: stopIndex by: -1
|
|
922 |
do: [:i | (order at: i) = aKey ifTrue: [^i]].
|
|
923 |
^nil
|
|
924 |
!
|
|
925 |
|
|
926 |
prevIndexOfValue: aValue from: startIndex to: stopIndex
|
|
927 |
"Return the previous index of aValue within the receiver between startIndex
|
|
928 |
and stopIndex working backwards through the receiver.
|
|
929 |
If the receiver does not contain aValue, return nil"
|
|
930 |
|
|
931 |
startIndex to: stopIndex by: -1
|
|
932 |
do: [:i |
|
|
933 |
(self at: (order at: i)) = aValue ifTrue: [^i]].
|
|
934 |
^nil
|
|
935 |
! !
|
|
936 |
|
|
937 |
!OrderedDictionary methodsFor:'sorting & reordering'!
|
|
938 |
|
|
939 |
reverse
|
|
940 |
"Destructively reverse my order.
|
|
941 |
WARNING: this is a destructive operation, which modifies the receiver.
|
|
942 |
Please use reversed (with a d) for a functional version."
|
|
943 |
|
|
944 |
order reverse
|
|
945 |
!
|
|
946 |
|
|
947 |
sort
|
|
948 |
"Destructively sort my order.
|
|
949 |
WARNING: this is a destructive operation, which modifies the receiver"
|
|
950 |
|
|
951 |
order sort
|
|
952 |
!
|
|
953 |
|
|
954 |
sort:aSortBlock
|
|
955 |
"Destructively sort my order.
|
|
956 |
WARNING: this is a destructive operation, which modifies the receiver"
|
|
957 |
|
|
958 |
order sort:aSortBlock
|
|
959 |
! !
|
|
960 |
|
|
961 |
!OrderedDictionary class methodsFor:'documentation'!
|
|
962 |
|
|
963 |
version
|
|
964 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedDictionary.st,v 1.39 2013-04-25 07:15:35 stefan Exp $'
|
|
965 |
!
|
|
966 |
|
|
967 |
version_CVS
|
|
968 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedDictionary.st,v 1.39 2013-04-25 07:15:35 stefan Exp $'
|
|
969 |
! !
|
|
970 |
|