author | Stefan Vogel <sv@exept.de> |
Sat, 10 Aug 2013 13:22:40 +0200 | |
changeset 15614 | 70bf14a15a59 |
parent 15534 | 808593c7efb3 |
child 15662 | e87729351c27 |
permissions | -rw-r--r-- |
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 |
" |
|
15134
84e886ef6b5f
Packet change libbasic2 -> libbasic
Stefan Vogel <sv@exept.de>
parents:
15133
diff
changeset
|
24 |
"{ Package: 'stx:libbasic' }" |
15133 | 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 |
||
15534 | 207 |
atIndex:index ifAbsent:aBlock |
208 |
"return an element at a given index" |
|
209 |
||
210 |
^ self at:(order at:index ifAbsent:[^ aBlock value]) |
|
211 |
! |
|
212 |
||
15133 | 213 |
atIndex:index put:anAssociation |
214 |
"put an association to a given index. remove the old associatioan at this index" |
|
215 |
|key| |
|
216 |
||
217 |
key := anAssociation key. |
|
218 |
(super includesKey:key) ifTrue:[ |
|
219 |
^ self error:'duplicate key'. |
|
220 |
]. |
|
221 |
super removeKey:(order at:index) ifAbsent:[]. |
|
222 |
order at:index put:key. |
|
223 |
^ super add:anAssociation. |
|
224 |
||
225 |
"Created: 28.9.1995 / 16:30:15 / stefan" |
|
226 |
! |
|
227 |
||
228 |
before: anAssociation |
|
229 |
"Return the association before anAssociation in the order. |
|
230 |
If anAssociation is the first association in the order, return nil. |
|
231 |
If anAssociation is not found, invoke an error notifier" |
|
232 |
||
233 |
2 to:order size do:[:index | |
|
234 |
(self associationAt:(order at:index)) = anAssociation |
|
235 |
ifTrue:[ ^ self associationAt:(order at:index - 1)] |
|
236 |
]. |
|
237 |
(self associationAt:order first) = anAssociation ifTrue: [^ nil]. |
|
238 |
^ self error: 'not found' |
|
239 |
! |
|
240 |
||
241 |
first |
|
242 |
"Return the first association of the receiver. |
|
243 |
Provide an error notification if the receiver contains no elements." |
|
244 |
||
245 |
^ self associationAt: (order first) |
|
246 |
||
247 |
" |
|
248 |
OrderedDictionary new first |
|
249 |
" |
|
250 |
! |
|
251 |
||
252 |
keyAt:index |
|
253 |
"get the key at the given index" |
|
254 |
||
255 |
^ order at:index. |
|
256 |
||
257 |
" |
|
258 |
|s| |
|
259 |
||
260 |
s := OrderedDictionary new. |
|
261 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'. |
|
262 |
s keyAt:2 |
|
263 |
" |
|
264 |
||
265 |
"Created: 29.9.1995 / 11:32:07 / stefan" |
|
266 |
! |
|
267 |
||
268 |
keys |
|
269 |
"Return a OrderedCollection containing the receiver's keys." |
|
270 |
||
271 |
^ order copy. |
|
272 |
||
273 |
" |
|
274 |
|s| |
|
275 |
||
276 |
s := OrderedDictionary new. |
|
277 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'. |
|
278 |
s keys |
|
279 |
" |
|
280 |
! |
|
281 |
||
282 |
last |
|
283 |
"Return the last association of the receiver. |
|
284 |
Provide an error notification if the receiver contains no elements." |
|
285 |
||
286 |
^ self associationAt: (order last) |
|
287 |
||
288 |
" |
|
289 |
OrderedDictionary new last |
|
290 |
" |
|
291 |
! |
|
292 |
||
293 |
order |
|
294 |
"returns the keys in the order of their appearance" |
|
295 |
||
296 |
^ order |
|
297 |
||
298 |
" |
|
299 |
|s| |
|
300 |
||
301 |
s := OrderedDictionary new. |
|
302 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'. |
|
303 |
s order |
|
304 |
" |
|
305 |
! |
|
306 |
||
307 |
valueAt:index |
|
308 |
"get the value at the given index" |
|
309 |
||
310 |
^ super at:(order at:index). |
|
311 |
||
312 |
" |
|
313 |
|s| |
|
314 |
||
315 |
s := OrderedDictionary new. |
|
316 |
s at:#a put:'aaa'; at:#b put:'bbb'; at:#c put:'ccc'; at:#d put:'ddd'; at:#a put:'aaaa'. |
|
317 |
s valueAt:2 |
|
318 |
" |
|
319 |
! |
|
320 |
||
321 |
values |
|
322 |
"Return a OrderedCollection containing the receiver's values." |
|
323 |
||
324 |
^ order collect: [:key | (self at: key) ]. |
|
325 |
! ! |
|
326 |
||
327 |
!OrderedDictionary methodsFor:'adding'! |
|
328 |
||
329 |
add: anAssociation |
|
330 |
"add anAssociation to the dictionary. |
|
331 |
If anAssociation is already present in the dictionary, |
|
332 |
the order will not be changed. (See also: #addLast:)" |
|
333 |
||
334 |
| key | |
|
335 |
||
336 |
key := anAssociation key. |
|
337 |
(super includesKey: key) |
|
338 |
ifFalse: [order add: key]. |
|
339 |
^ super add: anAssociation |
|
340 |
! |
|
341 |
||
342 |
add: anAssociation after: oldAssociation |
|
343 |
"Add the argument, anAssociation, as an element of the dictionary. Put it |
|
344 |
in the position just succeeding oldAssociation. Return anAssociation." |
|
345 |
||
346 |
| index | |
|
347 |
||
348 |
index := self indexOfAssociation: oldAssociation |
|
349 |
ifAbsent: [self error: 'association not found']. |
|
350 |
self removeFromOrder: anAssociation key. |
|
351 |
order add: anAssociation key after: (order at: index). |
|
352 |
super add: anAssociation. |
|
353 |
^ anAssociation |
|
354 |
! |
|
355 |
||
356 |
add: anAssociation before: oldAssociation |
|
357 |
"Add the argument, anAssociation, as an element of the dictionary. Put it |
|
358 |
in the position just preceding oldAssociation. Return anAssociation." |
|
359 |
||
360 |
| index | |
|
361 |
||
362 |
index := self indexOfAssociation: oldAssociation |
|
363 |
ifAbsent: [self error: 'association not found']. |
|
364 |
self removeFromOrder: anAssociation key. |
|
365 |
order add: anAssociation key before: (order at: index). |
|
366 |
super add: anAssociation. |
|
367 |
^ anAssociation |
|
368 |
! |
|
369 |
||
370 |
add: anAssociation beforeIndex: spot |
|
371 |
"Add the argument, anAssociation, as an element of the receiver. Put it |
|
372 |
in the position just preceding the indexed position spot. Return newObject." |
|
373 |
||
374 |
self removeFromOrder: anAssociation key. |
|
375 |
order add: anAssociation key beforeIndex: spot. |
|
376 |
^ super add: anAssociation |
|
377 |
||
378 |
"Modified: 28.9.1995 / 14:06:53 / stefan" |
|
379 |
! |
|
380 |
||
381 |
addAll:aCollectionOfAssociations |
|
382 |
"Add each element of anOrderedCollectionOfAssociations at my end. |
|
383 |
We expect the argument to enumerate associations with #reverseDo:; |
|
384 |
if it does not (i.e. it is another OD or a dictionary), use #addAllAssociationsFirst:. |
|
385 |
Returns the argument, aCollectionOfAssociations (sigh)." |
|
386 |
||
387 |
self addAllLast:aCollectionOfAssociations. |
|
388 |
^ aCollectionOfAssociations |
|
389 |
||
390 |
"Modified: 28.2.1997 / 15:51:23 / cg" |
|
391 |
! |
|
392 |
||
393 |
addAllAssociations:aDictionaryOrOrderedDictionary |
|
394 |
"Add each association of aDictionaryOrOrderedDictionary to my end. |
|
395 |
We expect the argument to respond to #associationsDo:." |
|
396 |
||
397 |
self addAllAssociationsLast:aDictionaryOrOrderedDictionary. |
|
398 |
^ aDictionaryOrOrderedDictionary |
|
399 |
||
400 |
"Created: 28.2.1997 / 15:52:02 / cg" |
|
401 |
! |
|
402 |
||
403 |
addAllAssociationsFirst:aDictionaryOrOrderedDictionary |
|
404 |
"Add each association of aDictionaryOrOrderedDictionary at the beginning of the |
|
405 |
receiver. We expect the argument to respond to #associationsReverseDo:." |
|
406 |
||
407 |
aDictionaryOrOrderedDictionary associationsReverseDo:[:each | self addFirst:each]. |
|
408 |
^ aDictionaryOrOrderedDictionary |
|
409 |
||
410 |
"Created: 28.2.1997 / 15:50:14 / cg" |
|
411 |
! |
|
412 |
||
413 |
addAllAssociationsLast:aDictionaryOrOrderedDictionary |
|
414 |
"Add each association of aDictionaryOrOrderedDictionary at the end of the |
|
415 |
receiver. We expect the argument to respond to #associationsDo:." |
|
416 |
||
417 |
aDictionaryOrOrderedDictionary associationsDo:[:each | self addLast:each]. |
|
418 |
^ aDictionaryOrOrderedDictionary |
|
419 |
||
420 |
"Created: 28.2.1997 / 15:48:37 / cg" |
|
421 |
! |
|
422 |
||
423 |
addFirst: anAssociation |
|
424 |
"Add anAssociation to the beginning of the receiver." |
|
425 |
||
426 |
self removeFromOrder: anAssociation key. |
|
427 |
order addFirst: anAssociation key. |
|
428 |
^ super add: anAssociation. |
|
429 |
! |
|
430 |
||
431 |
addLast: anAssociation |
|
432 |
"Add anAssociation to the end of the receiver. |
|
433 |
If anAssociation is already present in the dictionary, |
|
434 |
it will be moved to the end. (See also: #add:)" |
|
435 |
||
436 |
self removeFromOrder: anAssociation key. |
|
437 |
order add: anAssociation key. |
|
438 |
^ super add: anAssociation. |
|
439 |
! ! |
|
440 |
||
441 |
!OrderedDictionary methodsFor:'copying'! |
|
442 |
||
443 |
copyEmpty |
|
444 |
"Return a copy of the receiver that contains no elements." |
|
445 |
||
446 |
^ (self class) new: 10 |
|
447 |
! |
|
448 |
||
449 |
copyFrom: startIndex to: endIndex |
|
450 |
"Return a copy of the receiver that contains elements from |
|
451 |
position startIndex to endIndex." |
|
452 |
||
453 |
| newDict | |
|
454 |
endIndex < startIndex ifTrue: [^self copyEmpty]. |
|
455 |
(startIndex < 1 or: [endIndex > order size]) |
|
456 |
ifTrue: [^ self errorNotFound]. |
|
457 |
newDict := self copyEmpty: endIndex - startIndex + 1. |
|
458 |
startIndex to: endIndex do: [:index | newDict add: (self associationAt: (order at: index))]. |
|
459 |
^ newDict |
|
460 |
! |
|
461 |
||
462 |
copyWith: anAssociation |
|
463 |
"Return a copy of the dictionary that is 1 bigger than the receiver and |
|
464 |
includes the argument, anAssociation, at the end." |
|
465 |
||
466 |
| newDict | |
|
467 |
newDict := self copy. |
|
468 |
newDict add: anAssociation. |
|
469 |
^ newDict |
|
470 |
! |
|
471 |
||
472 |
copyWithout: anAssociation |
|
473 |
"Return a copy of the dictionary that is 1 smaller than the receiver and |
|
474 |
does not include the argument, anAssociation |
|
475 |
No error is reported, if elementToSkip is not in the collection." |
|
476 |
||
477 |
| newDict | |
|
478 |
newDict := self class new:order size - 1. |
|
479 |
self associationsDo: [:assoc | anAssociation = assoc ifFalse: [newDict add: assoc]] |
|
480 |
! |
|
481 |
||
482 |
postCopy |
|
483 |
"have to copy the order too" |
|
484 |
||
485 |
super postCopy. |
|
486 |
order := order copy |
|
487 |
! ! |
|
488 |
||
489 |
!OrderedDictionary methodsFor:'enumerating'! |
|
490 |
||
491 |
associationsCollect: aBlock |
|
492 |
"Evaluate aBlock with each of the associations of the dictionary as argument, |
|
493 |
and return a new (ordered) collection with the results" |
|
494 |
||
495 |
^ order collect:[:key | (aBlock value: (self associationAt:key))]. |
|
496 |
! |
|
497 |
||
498 |
associationsDo: aBlock |
|
499 |
"Evaluate aBlock for each of the dictionary's associations." |
|
500 |
||
501 |
order do: [:key | aBlock value: (self associationAt: key)] |
|
502 |
! |
|
503 |
||
504 |
associationsDo: aBlock from: firstIndex to: secondIndex |
|
505 |
"Evaluate aBlock with each of the dictionary's associations from index |
|
506 |
firstIndex to index secondIndex as the argument." |
|
507 |
||
508 |
firstIndex to: secondIndex do: [:index | |
|
509 |
aBlock value: (self associationAt: (order at: index)) |
|
510 |
] |
|
511 |
! |
|
512 |
||
513 |
associationsReverseDo: aBlock |
|
514 |
"Evaluate aBlock for each of the dictionary's associations in reverse order." |
|
515 |
||
516 |
order reverseDo:[:key | aBlock value:(self associationAt:key)] |
|
517 |
||
518 |
"Created: 28.2.1997 / 15:52:31 / cg" |
|
519 |
! |
|
520 |
||
521 |
associationsSelect:aBlock |
|
522 |
"Evaluate aBlock with each of the dictionary's associations as the argument. |
|
523 |
Collect into a new OrderedDictionary only those associations for which |
|
524 |
aBlock evaluates to true. Return the new OrderedDictionary." |
|
525 |
||
526 |
|newDict| |
|
527 |
||
528 |
newDict := self class new. |
|
529 |
order do:[:key | |
|
530 |
|assoc| |
|
531 |
||
532 |
assoc := self associationAt:key. |
|
533 |
(aBlock value:assoc) ifTrue: [ |
|
534 |
newDict add:assoc |
|
535 |
] |
|
536 |
]. |
|
537 |
^ newDict |
|
538 |
! |
|
539 |
||
540 |
collect: aBlock |
|
541 |
"Evaluate aBlock with each of the values of the dictionary as argument, |
|
542 |
and return a new (ordered) collection with the results" |
|
543 |
||
544 |
^ order collect:[:key | (aBlock value: (self at:key))]. |
|
545 |
! |
|
546 |
||
547 |
do: aBlock |
|
548 |
"Evaluate aBlock for each of the dictionary's values." |
|
549 |
||
550 |
order do: [:key | aBlock value: (self at: key)] |
|
551 |
! |
|
552 |
||
553 |
do: aBlock from: firstIndex to: lastIndex |
|
554 |
"Evaluate aBlock with each of the dictionary's associations from index |
|
555 |
firstIndex to index secondIndex as the argument." |
|
556 |
||
557 |
self from:firstIndex to:lastIndex do:aBlock. |
|
558 |
! |
|
559 |
||
560 |
findFirst:aBlock ifNone:exceptionalValue |
|
561 |
"Return the index of the first association in the dictionary for which aBlock |
|
562 |
evaluates as true. If the block does not evaluate to true, return exceptionalValue" |
|
563 |
||
564 |
1 to:order size do:[:index | |
|
565 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
566 |
]. |
|
567 |
^ exceptionalValue value |
|
568 |
! |
|
569 |
||
570 |
findLast:aBlock |
|
571 |
"Return the index of the last association in the dictionary for which aBlock |
|
572 |
evaluates as true. If the block does not evaluate to true, return 0" |
|
573 |
||
574 |
order size to:1 by:-1 do: [:index | |
|
575 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
576 |
]. |
|
577 |
^ 0 |
|
578 |
! |
|
579 |
||
580 |
findLast:aBlock startingAt:startIndex |
|
581 |
"Return the index of the last association in the dictionary for which aBlock |
|
582 |
evaluates as true. Start the search at startIndex. |
|
583 |
If the block does not evaluate to true, return 0" |
|
584 |
||
585 |
startIndex to:1 by:-1 do: [:index | |
|
586 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
587 |
]. |
|
588 |
^ 0 |
|
589 |
! |
|
590 |
||
591 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
|
592 |
"Return the index of the last association in the dictionary for which aBlock |
|
593 |
evaluates as true. Start the search at startIndex. |
|
594 |
End the search at endIndex or when an element is found. |
|
595 |
If the block does not evaluate to true, return 0" |
|
596 |
||
597 |
startIndex to:endIndex by:-1 do: [:index | |
|
598 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
599 |
]. |
|
600 |
^ 0 |
|
601 |
! |
|
602 |
||
603 |
from:firstIndex to:lastIndex do: aBlock |
|
604 |
"Evaluate aBlock with each of the dictionary's associations from index |
|
605 |
firstIndex to index secondIndex as the argument." |
|
606 |
||
607 |
order from:firstIndex to:lastIndex do:[:key | |
|
608 |
aBlock value: (self at:key) |
|
609 |
]. |
|
610 |
! |
|
611 |
||
612 |
keysAndValuesDo:aBlock |
|
15451 | 613 |
"perform the block for all keys and values in the collection. |
614 |
Enumerate them in the order by which they were added. |
|
15133 | 615 |
|
616 |
See also: |
|
617 |
#associationsDo: (which passes key-value associations) |
|
618 |
#keysAndValuesDo: (which passes keys & values separately) |
|
619 |
#do: (which passes values only) |
|
620 |
||
621 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
622 |
Iterate over a copy to do this." |
|
623 |
||
624 |
order do: [:key | aBlock value:key value:(self at: key)]. |
|
625 |
||
626 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
627 |
"Created: / 15.10.1999 / 16:49:31 / cg" |
|
628 |
"Modified: / 15.10.1999 / 16:53:50 / cg" |
|
629 |
! |
|
630 |
||
15451 | 631 |
keysAndValuesReverseDo:aBlock |
632 |
"perform the block for all keys and values in the collection. |
|
633 |
Enumerate them in the reverse order from which they were added. |
|
634 |
||
635 |
See also: |
|
636 |
#associationsDo: (which passes key-value associations) |
|
637 |
#keysAndValuesDo: (which passes keys & values separately) |
|
638 |
#do: (which passes values only) |
|
639 |
||
640 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
641 |
Iterate over a copy to do this." |
|
642 |
||
643 |
order reverseDo: [:key | aBlock value:key value:(self at: key)]. |
|
644 |
||
645 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
646 |
"Created: / 15.10.1999 / 16:49:31 / cg" |
|
647 |
"Modified: / 15.10.1999 / 16:53:50 / cg" |
|
648 |
! |
|
649 |
||
15133 | 650 |
keysDo:aBlock |
651 |
"evaluate the argument, aBlock for every key in the collection. |
|
652 |
||
653 |
See also: |
|
654 |
#associationsDo: (which passes key-value associations) |
|
655 |
#keysAndValuesDo: (which passes keys & values separately) |
|
656 |
#do: (which passes values only) |
|
657 |
||
658 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
659 |
Iterate over a copy to do this." |
|
660 |
||
661 |
order do:[:key | aBlock value:key]. |
|
662 |
||
663 |
"Created: / 26-06-1999 / 10:53:00 / ps" |
|
664 |
"Modified: / 24-08-2010 / 10:14:06 / cg" |
|
665 |
! |
|
666 |
||
667 |
reverseDo: aBlock |
|
668 |
"Evaluate aBlock with each of the dictionary's associations as the argument, |
|
669 |
starting with the last element and taking each in sequence up to the first." |
|
670 |
||
671 |
order size to:1 by:-1 do: [:index | |
|
672 |
aBlock value:(self associationAt:(order at:index)) |
|
673 |
] |
|
674 |
! |
|
675 |
||
676 |
reversed |
|
677 |
"Return with a new OrderedDictionary with its associations in reverse order." |
|
678 |
||
679 |
| newDict| |
|
680 |
||
681 |
newDict := self class new. |
|
682 |
order size to:1 by:-1 do:[:index | |
|
683 |
|key| |
|
684 |
||
685 |
key := order at:index. |
|
686 |
newDict at:key put:(self at:key) |
|
687 |
]. |
|
688 |
^ newDict |
|
689 |
! |
|
690 |
||
691 |
select:aBlock |
|
692 |
"Evaluate aBlock with each of the dictionary's values as the argument. |
|
693 |
Collect into a new OrderedDictionary only those associations for which |
|
694 |
aBlock evaluated to true. Return the new OrderedDictionary." |
|
695 |
||
696 |
|newColl| |
|
697 |
||
698 |
newColl := self species new. |
|
699 |
order do:[:key | |
|
700 |
|assoc| |
|
701 |
||
702 |
assoc := self associationAt:key. |
|
703 |
(aBlock value:(assoc value)) ifTrue: [ |
|
704 |
newColl add:assoc |
|
705 |
] |
|
706 |
]. |
|
707 |
^ newColl |
|
708 |
! ! |
|
709 |
||
710 |
!OrderedDictionary methodsFor:'initialization'! |
|
711 |
||
712 |
initializeOrder |
|
713 |
order := OrderedCollection new |
|
714 |
! ! |
|
715 |
||
716 |
!OrderedDictionary methodsFor:'private'! |
|
717 |
||
718 |
removeFromOrder: aKey |
|
719 |
order remove: aKey ifAbsent: [] |
|
720 |
! ! |
|
721 |
||
722 |
!OrderedDictionary methodsFor:'queries'! |
|
723 |
||
724 |
occurrencesOfKey: aKey |
|
725 |
"Return how many of the dictionary's keys are equal to aKey." |
|
726 |
||
727 |
^ order count:[:eachKey | aKey = eachKey] |
|
728 |
||
729 |
"/ cg: the original code was not very smalltalkish: |
|
730 |
"/ |
|
731 |
"/ | count | |
|
732 |
"/ |
|
733 |
"/ count := 0. |
|
734 |
"/ 1 to: self size do: [:index | aKey = (order at: index) ifTrue: [count := count + 1]]. |
|
735 |
"/ ^count |
|
736 |
! |
|
737 |
||
738 |
occurrencesOfValue: aValue |
|
739 |
"Return how many of the dictionary's values are equal to aValue." |
|
740 |
||
741 |
^ order count:[:eachKey | aValue = (self at:eachKey)] |
|
742 |
||
743 |
"/ cg: the original code was not very smalltalkish: |
|
744 |
"/ |
|
745 |
"/ | count | |
|
746 |
"/ |
|
747 |
"/ count := 0. |
|
748 |
"/ 1 to: self size do: [:index | |
|
749 |
"/ aValue = (self at: (order at: index)) ifTrue: [count := count + 1]]. |
|
750 |
"/ ^count |
|
751 |
! ! |
|
752 |
||
753 |
!OrderedDictionary methodsFor:'removing'! |
|
754 |
||
755 |
removeFirst |
|
756 |
|key| |
|
757 |
||
758 |
order size == 0 ifTrue:[ |
|
759 |
"error if collection is empty" |
|
760 |
||
761 |
^ self emptyCollectionError. |
|
762 |
]. |
|
763 |
key := order removeFirst. |
|
764 |
^ super removeKey:key. |
|
765 |
! |
|
766 |
||
767 |
removeFromIndex:fromIndex toIndex:toIndex |
|
768 |
"Returns the receiver." |
|
769 |
||
770 |
| keys | |
|
771 |
||
772 |
keys := order copyFrom:fromIndex to:toIndex. |
|
773 |
order removeFromIndex:fromIndex toIndex:toIndex. |
|
774 |
keys do:[ :key | |
|
775 |
super removeKey:key. |
|
776 |
]. |
|
777 |
||
778 |
"Created: 28.9.1995 / 12:04:33 / stefan" |
|
779 |
! |
|
780 |
||
781 |
removeKey:aKey |
|
782 |
order remove:aKey. |
|
783 |
^ super removeKey:aKey. |
|
784 |
! |
|
785 |
||
786 |
removeKey:aKey ifAbsent:aBlock |
|
787 |
order remove:aKey ifAbsent:[]. |
|
788 |
^ super removeKey:aKey ifAbsent:aBlock. |
|
789 |
||
790 |
"Created: / 31-01-2011 / 22:04:01 / cg" |
|
791 |
! |
|
792 |
||
793 |
removeLast |
|
794 |
|key| |
|
795 |
||
796 |
order size == 0 ifTrue:[ |
|
797 |
"error if collection is empty" |
|
798 |
||
799 |
^ self emptyCollectionError. |
|
800 |
]. |
|
801 |
key := order removeLast. |
|
802 |
^ super removeKey:key. |
|
803 |
! ! |
|
804 |
||
805 |
!OrderedDictionary methodsFor:'searching'! |
|
806 |
||
807 |
identityIndexOfAssociation: anAssociation |
|
808 |
"Return the identity index of anAssociation within the receiver. If the receiver |
|
809 |
does not contain anAssociation, return 0." |
|
810 |
||
811 |
^ self identityIndexOfAssociation: anAssociation ifAbsent: [0] |
|
812 |
! |
|
813 |
||
814 |
identityIndexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
815 |
"Return the identity index of anAssociation within the receiver. |
|
816 |
If the receiver does not contain anAssociation, |
|
817 |
return the result of evaluating the exceptionBlock." |
|
818 |
||
819 |
"/ as ST/X's dictionaries do not store the associations |
|
820 |
"/ (instead, they store the keys and values in separate collections) |
|
821 |
"/ this will not work. You have to think about it and rewrite your code. |
|
822 |
self error:'this does not work in Smalltalk/X'. |
|
823 |
||
824 |
order keysAndValuesDo:[:i :key | |
|
825 |
(self associationAt:key) == anAssociation ifTrue: [^i] |
|
826 |
]. |
|
827 |
^exceptionBlock value |
|
828 |
! |
|
829 |
||
830 |
identityIndexOfKey: aKey |
|
831 |
"Return the identity index of aKey within the receiver. If the receiver |
|
832 |
does not contain aKey, return 0." |
|
833 |
||
834 |
^self identityIndexOfKey: aKey ifAbsent: [0] |
|
835 |
! |
|
836 |
||
837 |
identityIndexOfKey: aKey ifAbsent: exceptionBlock |
|
838 |
"Return the identity index of aKey within the receiver. If the receiver does |
|
839 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
840 |
||
841 |
^ order identityIndexOf:aKey ifAbsent:exceptionBlock |
|
842 |
! |
|
843 |
||
844 |
identityIndexOfValue: aValue |
|
845 |
"Return the identity index of aValue within the receiver. If the receiver |
|
846 |
does not contain aValue, return 0." |
|
847 |
||
848 |
^self identityIndexOfValue: aValue ifAbsent: [0] |
|
849 |
! |
|
850 |
||
851 |
identityIndexOfValue: aValue ifAbsent: exceptionBlock |
|
852 |
"Return the identity index of aValue within the receiver. If the receiver |
|
853 |
does not contain aValue, return the result of evaluating the exceptionBlock." |
|
854 |
||
855 |
order keysAndValuesDo:[:i :key | (self at:key) == aValue ifTrue: [^i]]. |
|
856 |
^exceptionBlock value |
|
857 |
! |
|
858 |
||
859 |
indexOfAssociation: anAssociation |
|
860 |
"Return the index of anAssociation within the receiver. If the receiver does |
|
861 |
not contain anAssociation, return 0." |
|
862 |
||
863 |
^self indexOfAssociation: anAssociation ifAbsent: [0] |
|
864 |
! |
|
865 |
||
866 |
indexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
867 |
"Return the identity index of anAssociation within the receiver. If the receiver |
|
868 |
does not contain anAssociation, return the result of evaluating the exceptionBlock." |
|
869 |
||
870 |
order keysAndValuesDo:[:i :key | |
|
871 |
(self associationAt:key) = anAssociation ifTrue: [^i] |
|
872 |
]. |
|
873 |
^exceptionBlock value |
|
874 |
! |
|
875 |
||
876 |
indexOfKey: aKey |
|
877 |
"Return the index of aKey within the receiver. If the receiver does |
|
878 |
not contain aKey, return 0." |
|
879 |
||
880 |
^self indexOfKey: aKey ifAbsent: [0] |
|
881 |
! |
|
882 |
||
883 |
indexOfKey: aKey ifAbsent: exceptionBlock |
|
884 |
"Return the identity index of aKey within the receiver. If the receiver does |
|
885 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
886 |
||
887 |
^ order indexOf:aKey ifAbsent:exceptionBlock |
|
888 |
! |
|
889 |
||
890 |
indexOfValue: aValue |
|
891 |
"Return the index of aValue within the receiver. |
|
892 |
If the receiver does not contain aValue, return 0." |
|
893 |
||
894 |
^self indexOfValue: aValue ifAbsent: [0] |
|
895 |
! |
|
896 |
||
897 |
indexOfValue: aValue ifAbsent: exceptionBlock |
|
898 |
"Return the identity index of aValue within the receiver. |
|
899 |
If the receiver does not contain aValue, return the result of evaluating the exceptionBlock." |
|
900 |
||
901 |
order keysAndValuesDo:[:i :key | (self at:key) = aValue ifTrue: [^i]]. |
|
902 |
^ exceptionBlock value |
|
903 |
! |
|
904 |
||
905 |
nextIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
906 |
"Return the next index of aAssociation within the receiver between startIndex |
|
907 |
and stopIndex. If the receiver does not contain aAssociation, return nil" |
|
908 |
||
909 |
startIndex to: stopIndex do: [:i | |
|
910 |
(self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
|
911 |
^nil |
|
912 |
! |
|
913 |
||
914 |
nextIndexOfKey: aKey from: startIndex to: stopIndex |
|
915 |
"Return the next index of aKey within the receiver between startIndex and |
|
916 |
stopIndex. If the receiver does not contain aKey, return nil" |
|
917 |
||
918 |
startIndex to: stopIndex do: [:i | |
|
919 |
(order at: i) = aKey ifTrue: [^i]]. |
|
920 |
^nil |
|
921 |
! |
|
922 |
||
923 |
nextIndexOfValue: aValue from: startIndex to: stopIndex |
|
924 |
"Return the next index of aValue within the receiver between startIndex and |
|
925 |
stopIndex. If the receiver does not contain aValue, return nil" |
|
926 |
||
927 |
startIndex to: stopIndex do: [:i | |
|
928 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
929 |
^nil |
|
930 |
! |
|
931 |
||
932 |
prevIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
933 |
"Return the previous index of aAssociation within the receiver between |
|
934 |
startIndex and stopIndex working backwards through the receiver. |
|
935 |
If the receiver does not contain aAssociation, return nil" |
|
936 |
||
937 |
startIndex to: stopIndex by: -1 |
|
938 |
do: [:i | (self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
|
939 |
^nil |
|
940 |
! |
|
941 |
||
942 |
prevIndexOfKey: aKey from: startIndex to: stopIndex |
|
943 |
"Return the previous index of aKey within the receiver between startIndex and |
|
944 |
stopIndex working backwards through the receiver. |
|
945 |
If the receiver does not contain aKey, return nil" |
|
946 |
||
947 |
startIndex to: stopIndex by: -1 |
|
948 |
do: [:i | (order at: i) = aKey ifTrue: [^i]]. |
|
949 |
^nil |
|
950 |
! |
|
951 |
||
952 |
prevIndexOfValue: aValue from: startIndex to: stopIndex |
|
953 |
"Return the previous index of aValue within the receiver between startIndex |
|
954 |
and stopIndex working backwards through the receiver. |
|
955 |
If the receiver does not contain aValue, return nil" |
|
956 |
||
957 |
startIndex to: stopIndex by: -1 |
|
958 |
do: [:i | |
|
959 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
960 |
^nil |
|
961 |
! ! |
|
962 |
||
963 |
!OrderedDictionary methodsFor:'sorting & reordering'! |
|
964 |
||
965 |
reverse |
|
966 |
"Destructively reverse my order. |
|
967 |
WARNING: this is a destructive operation, which modifies the receiver. |
|
968 |
Please use reversed (with a d) for a functional version." |
|
969 |
||
970 |
order reverse |
|
971 |
! |
|
972 |
||
973 |
sort |
|
974 |
"Destructively sort my order. |
|
975 |
WARNING: this is a destructive operation, which modifies the receiver" |
|
976 |
||
977 |
order sort |
|
978 |
! |
|
979 |
||
980 |
sort:aSortBlock |
|
981 |
"Destructively sort my order. |
|
982 |
WARNING: this is a destructive operation, which modifies the receiver" |
|
983 |
||
984 |
order sort:aSortBlock |
|
985 |
! ! |
|
986 |
||
987 |
!OrderedDictionary class methodsFor:'documentation'! |
|
988 |
||
989 |
version |
|
15534 | 990 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedDictionary.st,v 1.42 2013-07-22 08:25:33 stefan Exp $' |
15133 | 991 |
! |
992 |
||
993 |
version_CVS |
|
15534 | 994 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedDictionary.st,v 1.42 2013-07-22 08:25:33 stefan Exp $' |
15133 | 995 |
! ! |
996 |