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