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