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