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