author | Claus Gittinger <cg@exept.de> |
Tue, 16 Jul 2013 19:47:48 +0200 | |
changeset 15528 | a3c55af6af90 |
parent 15451 | 8be26a61d680 |
child 15534 | 808593c7efb3 |
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 |
||
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 |
|
15451 | 607 |
"perform the block for all keys and values in the collection. |
608 |
Enumerate them in the order by which they were added. |
|
15133 | 609 |
|
610 |
See also: |
|
611 |
#associationsDo: (which passes key-value associations) |
|
612 |
#keysAndValuesDo: (which passes keys & values separately) |
|
613 |
#do: (which passes values only) |
|
614 |
||
615 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
616 |
Iterate over a copy to do this." |
|
617 |
||
618 |
order do: [:key | aBlock value:key value:(self at: key)]. |
|
619 |
||
620 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
621 |
"Created: / 15.10.1999 / 16:49:31 / cg" |
|
622 |
"Modified: / 15.10.1999 / 16:53:50 / cg" |
|
623 |
! |
|
624 |
||
15451 | 625 |
keysAndValuesReverseDo:aBlock |
626 |
"perform the block for all keys and values in the collection. |
|
627 |
Enumerate them in the reverse order from which they were added. |
|
628 |
||
629 |
See also: |
|
630 |
#associationsDo: (which passes key-value associations) |
|
631 |
#keysAndValuesDo: (which passes keys & values separately) |
|
632 |
#do: (which passes values only) |
|
633 |
||
634 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
635 |
Iterate over a copy to do this." |
|
636 |
||
637 |
order reverseDo: [:key | aBlock value:key value:(self at: key)]. |
|
638 |
||
639 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
640 |
"Created: / 15.10.1999 / 16:49:31 / cg" |
|
641 |
"Modified: / 15.10.1999 / 16:53:50 / cg" |
|
642 |
! |
|
643 |
||
15133 | 644 |
keysDo:aBlock |
645 |
"evaluate the argument, aBlock for every key in the collection. |
|
646 |
||
647 |
See also: |
|
648 |
#associationsDo: (which passes key-value associations) |
|
649 |
#keysAndValuesDo: (which passes keys & values separately) |
|
650 |
#do: (which passes values only) |
|
651 |
||
652 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
653 |
Iterate over a copy to do this." |
|
654 |
||
655 |
order do:[:key | aBlock value:key]. |
|
656 |
||
657 |
"Created: / 26-06-1999 / 10:53:00 / ps" |
|
658 |
"Modified: / 24-08-2010 / 10:14:06 / cg" |
|
659 |
! |
|
660 |
||
661 |
reverseDo: aBlock |
|
662 |
"Evaluate aBlock with each of the dictionary's associations as the argument, |
|
663 |
starting with the last element and taking each in sequence up to the first." |
|
664 |
||
665 |
order size to:1 by:-1 do: [:index | |
|
666 |
aBlock value:(self associationAt:(order at:index)) |
|
667 |
] |
|
668 |
! |
|
669 |
||
670 |
reversed |
|
671 |
"Return with a new OrderedDictionary with its associations in reverse order." |
|
672 |
||
673 |
| newDict| |
|
674 |
||
675 |
newDict := self class new. |
|
676 |
order size to:1 by:-1 do:[:index | |
|
677 |
|key| |
|
678 |
||
679 |
key := order at:index. |
|
680 |
newDict at:key put:(self at:key) |
|
681 |
]. |
|
682 |
^ newDict |
|
683 |
! |
|
684 |
||
685 |
select:aBlock |
|
686 |
"Evaluate aBlock with each of the dictionary's values as the argument. |
|
687 |
Collect into a new OrderedDictionary only those associations for which |
|
688 |
aBlock evaluated to true. Return the new OrderedDictionary." |
|
689 |
||
690 |
|newColl| |
|
691 |
||
692 |
newColl := self species new. |
|
693 |
order do:[:key | |
|
694 |
|assoc| |
|
695 |
||
696 |
assoc := self associationAt:key. |
|
697 |
(aBlock value:(assoc value)) ifTrue: [ |
|
698 |
newColl add:assoc |
|
699 |
] |
|
700 |
]. |
|
701 |
^ newColl |
|
702 |
! ! |
|
703 |
||
704 |
!OrderedDictionary methodsFor:'initialization'! |
|
705 |
||
706 |
initializeOrder |
|
707 |
order := OrderedCollection new |
|
708 |
! ! |
|
709 |
||
710 |
!OrderedDictionary methodsFor:'private'! |
|
711 |
||
712 |
removeFromOrder: aKey |
|
713 |
order remove: aKey ifAbsent: [] |
|
714 |
! ! |
|
715 |
||
716 |
!OrderedDictionary methodsFor:'queries'! |
|
717 |
||
718 |
occurrencesOfKey: aKey |
|
719 |
"Return how many of the dictionary's keys are equal to aKey." |
|
720 |
||
721 |
^ order count:[:eachKey | aKey = eachKey] |
|
722 |
||
723 |
"/ cg: the original code was not very smalltalkish: |
|
724 |
"/ |
|
725 |
"/ | count | |
|
726 |
"/ |
|
727 |
"/ count := 0. |
|
728 |
"/ 1 to: self size do: [:index | aKey = (order at: index) ifTrue: [count := count + 1]]. |
|
729 |
"/ ^count |
|
730 |
! |
|
731 |
||
732 |
occurrencesOfValue: aValue |
|
733 |
"Return how many of the dictionary's values are equal to aValue." |
|
734 |
||
735 |
^ order count:[:eachKey | aValue = (self at:eachKey)] |
|
736 |
||
737 |
"/ cg: the original code was not very smalltalkish: |
|
738 |
"/ |
|
739 |
"/ | count | |
|
740 |
"/ |
|
741 |
"/ count := 0. |
|
742 |
"/ 1 to: self size do: [:index | |
|
743 |
"/ aValue = (self at: (order at: index)) ifTrue: [count := count + 1]]. |
|
744 |
"/ ^count |
|
745 |
! ! |
|
746 |
||
747 |
!OrderedDictionary methodsFor:'removing'! |
|
748 |
||
749 |
removeFirst |
|
750 |
|key| |
|
751 |
||
752 |
order size == 0 ifTrue:[ |
|
753 |
"error if collection is empty" |
|
754 |
||
755 |
^ self emptyCollectionError. |
|
756 |
]. |
|
757 |
key := order removeFirst. |
|
758 |
^ super removeKey:key. |
|
759 |
! |
|
760 |
||
761 |
removeFromIndex:fromIndex toIndex:toIndex |
|
762 |
"Returns the receiver." |
|
763 |
||
764 |
| keys | |
|
765 |
||
766 |
keys := order copyFrom:fromIndex to:toIndex. |
|
767 |
order removeFromIndex:fromIndex toIndex:toIndex. |
|
768 |
keys do:[ :key | |
|
769 |
super removeKey:key. |
|
770 |
]. |
|
771 |
||
772 |
"Created: 28.9.1995 / 12:04:33 / stefan" |
|
773 |
! |
|
774 |
||
775 |
removeKey:aKey |
|
776 |
order remove:aKey. |
|
777 |
^ super removeKey:aKey. |
|
778 |
! |
|
779 |
||
780 |
removeKey:aKey ifAbsent:aBlock |
|
781 |
order remove:aKey ifAbsent:[]. |
|
782 |
^ super removeKey:aKey ifAbsent:aBlock. |
|
783 |
||
784 |
"Created: / 31-01-2011 / 22:04:01 / cg" |
|
785 |
! |
|
786 |
||
787 |
removeLast |
|
788 |
|key| |
|
789 |
||
790 |
order size == 0 ifTrue:[ |
|
791 |
"error if collection is empty" |
|
792 |
||
793 |
^ self emptyCollectionError. |
|
794 |
]. |
|
795 |
key := order removeLast. |
|
796 |
^ super removeKey:key. |
|
797 |
! ! |
|
798 |
||
799 |
!OrderedDictionary methodsFor:'searching'! |
|
800 |
||
801 |
identityIndexOfAssociation: anAssociation |
|
802 |
"Return the identity index of anAssociation within the receiver. If the receiver |
|
803 |
does not contain anAssociation, return 0." |
|
804 |
||
805 |
^ self identityIndexOfAssociation: anAssociation ifAbsent: [0] |
|
806 |
! |
|
807 |
||
808 |
identityIndexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
809 |
"Return the identity index of anAssociation within the receiver. |
|
810 |
If the receiver does not contain anAssociation, |
|
811 |
return the result of evaluating the exceptionBlock." |
|
812 |
||
813 |
"/ as ST/X's dictionaries do not store the associations |
|
814 |
"/ (instead, they store the keys and values in separate collections) |
|
815 |
"/ this will not work. You have to think about it and rewrite your code. |
|
816 |
self error:'this does not work in Smalltalk/X'. |
|
817 |
||
818 |
order keysAndValuesDo:[:i :key | |
|
819 |
(self associationAt:key) == anAssociation ifTrue: [^i] |
|
820 |
]. |
|
821 |
^exceptionBlock value |
|
822 |
! |
|
823 |
||
824 |
identityIndexOfKey: aKey |
|
825 |
"Return the identity index of aKey within the receiver. If the receiver |
|
826 |
does not contain aKey, return 0." |
|
827 |
||
828 |
^self identityIndexOfKey: aKey ifAbsent: [0] |
|
829 |
! |
|
830 |
||
831 |
identityIndexOfKey: aKey ifAbsent: exceptionBlock |
|
832 |
"Return the identity index of aKey within the receiver. If the receiver does |
|
833 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
834 |
||
835 |
^ order identityIndexOf:aKey ifAbsent:exceptionBlock |
|
836 |
! |
|
837 |
||
838 |
identityIndexOfValue: aValue |
|
839 |
"Return the identity index of aValue within the receiver. If the receiver |
|
840 |
does not contain aValue, return 0." |
|
841 |
||
842 |
^self identityIndexOfValue: aValue ifAbsent: [0] |
|
843 |
! |
|
844 |
||
845 |
identityIndexOfValue: aValue ifAbsent: exceptionBlock |
|
846 |
"Return the identity index of aValue within the receiver. If the receiver |
|
847 |
does not contain aValue, return the result of evaluating the exceptionBlock." |
|
848 |
||
849 |
order keysAndValuesDo:[:i :key | (self at:key) == aValue ifTrue: [^i]]. |
|
850 |
^exceptionBlock value |
|
851 |
! |
|
852 |
||
853 |
indexOfAssociation: anAssociation |
|
854 |
"Return the index of anAssociation within the receiver. If the receiver does |
|
855 |
not contain anAssociation, return 0." |
|
856 |
||
857 |
^self indexOfAssociation: anAssociation ifAbsent: [0] |
|
858 |
! |
|
859 |
||
860 |
indexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
861 |
"Return the identity index of anAssociation within the receiver. If the receiver |
|
862 |
does not contain anAssociation, return the result of evaluating the exceptionBlock." |
|
863 |
||
864 |
order keysAndValuesDo:[:i :key | |
|
865 |
(self associationAt:key) = anAssociation ifTrue: [^i] |
|
866 |
]. |
|
867 |
^exceptionBlock value |
|
868 |
! |
|
869 |
||
870 |
indexOfKey: aKey |
|
871 |
"Return the index of aKey within the receiver. If the receiver does |
|
872 |
not contain aKey, return 0." |
|
873 |
||
874 |
^self indexOfKey: aKey ifAbsent: [0] |
|
875 |
! |
|
876 |
||
877 |
indexOfKey: aKey ifAbsent: exceptionBlock |
|
878 |
"Return the identity index of aKey within the receiver. If the receiver does |
|
879 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
880 |
||
881 |
^ order indexOf:aKey ifAbsent:exceptionBlock |
|
882 |
! |
|
883 |
||
884 |
indexOfValue: aValue |
|
885 |
"Return the index of aValue within the receiver. |
|
886 |
If the receiver does not contain aValue, return 0." |
|
887 |
||
888 |
^self indexOfValue: aValue ifAbsent: [0] |
|
889 |
! |
|
890 |
||
891 |
indexOfValue: aValue ifAbsent: exceptionBlock |
|
892 |
"Return the identity index of aValue within the receiver. |
|
893 |
If the receiver does not contain aValue, return the result of evaluating the exceptionBlock." |
|
894 |
||
895 |
order keysAndValuesDo:[:i :key | (self at:key) = aValue ifTrue: [^i]]. |
|
896 |
^ exceptionBlock value |
|
897 |
! |
|
898 |
||
899 |
nextIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
900 |
"Return the next index of aAssociation within the receiver between startIndex |
|
901 |
and stopIndex. If the receiver does not contain aAssociation, return nil" |
|
902 |
||
903 |
startIndex to: stopIndex do: [:i | |
|
904 |
(self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
|
905 |
^nil |
|
906 |
! |
|
907 |
||
908 |
nextIndexOfKey: aKey from: startIndex to: stopIndex |
|
909 |
"Return the next index of aKey within the receiver between startIndex and |
|
910 |
stopIndex. If the receiver does not contain aKey, return nil" |
|
911 |
||
912 |
startIndex to: stopIndex do: [:i | |
|
913 |
(order at: i) = aKey ifTrue: [^i]]. |
|
914 |
^nil |
|
915 |
! |
|
916 |
||
917 |
nextIndexOfValue: aValue from: startIndex to: stopIndex |
|
918 |
"Return the next index of aValue within the receiver between startIndex and |
|
919 |
stopIndex. If the receiver does not contain aValue, return nil" |
|
920 |
||
921 |
startIndex to: stopIndex do: [:i | |
|
922 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
923 |
^nil |
|
924 |
! |
|
925 |
||
926 |
prevIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
927 |
"Return the previous index of aAssociation within the receiver between |
|
928 |
startIndex and stopIndex working backwards through the receiver. |
|
929 |
If the receiver does not contain aAssociation, return nil" |
|
930 |
||
931 |
startIndex to: stopIndex by: -1 |
|
932 |
do: [:i | (self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
|
933 |
^nil |
|
934 |
! |
|
935 |
||
936 |
prevIndexOfKey: aKey from: startIndex to: stopIndex |
|
937 |
"Return the previous index of aKey within the receiver between startIndex and |
|
938 |
stopIndex working backwards through the receiver. |
|
939 |
If the receiver does not contain aKey, return nil" |
|
940 |
||
941 |
startIndex to: stopIndex by: -1 |
|
942 |
do: [:i | (order at: i) = aKey ifTrue: [^i]]. |
|
943 |
^nil |
|
944 |
! |
|
945 |
||
946 |
prevIndexOfValue: aValue from: startIndex to: stopIndex |
|
947 |
"Return the previous index of aValue within the receiver between startIndex |
|
948 |
and stopIndex working backwards through the receiver. |
|
949 |
If the receiver does not contain aValue, return nil" |
|
950 |
||
951 |
startIndex to: stopIndex by: -1 |
|
952 |
do: [:i | |
|
953 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
954 |
^nil |
|
955 |
! ! |
|
956 |
||
957 |
!OrderedDictionary methodsFor:'sorting & reordering'! |
|
958 |
||
959 |
reverse |
|
960 |
"Destructively reverse my order. |
|
961 |
WARNING: this is a destructive operation, which modifies the receiver. |
|
962 |
Please use reversed (with a d) for a functional version." |
|
963 |
||
964 |
order reverse |
|
965 |
! |
|
966 |
||
967 |
sort |
|
968 |
"Destructively sort my order. |
|
969 |
WARNING: this is a destructive operation, which modifies the receiver" |
|
970 |
||
971 |
order sort |
|
972 |
! |
|
973 |
||
974 |
sort:aSortBlock |
|
975 |
"Destructively sort my order. |
|
976 |
WARNING: this is a destructive operation, which modifies the receiver" |
|
977 |
||
978 |
order sort:aSortBlock |
|
979 |
! ! |
|
980 |
||
981 |
!OrderedDictionary class methodsFor:'documentation'! |
|
982 |
||
983 |
version |
|
15451 | 984 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedDictionary.st,v 1.41 2013-07-01 14:48:19 cg Exp $' |
15133 | 985 |
! |
986 |
||
987 |
version_CVS |
|
15451 | 988 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedDictionary.st,v 1.41 2013-07-01 14:48:19 cg Exp $' |
15133 | 989 |
! ! |
990 |