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