author | Claus Gittinger <cg@exept.de> |
Wed, 26 Jun 2019 22:06:12 +0200 | |
changeset 24369 | e796791ea838 |
parent 24363 | 3188fbab129f |
child 24419 | d3a56dbbf696 |
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 |
||
24363 | 609 |
!OrderedDictionary methodsFor:'converting'! |
610 |
||
611 |
asInlineObject |
|
612 |
"return the receiver as an inline-object (which understands getters and setters for my slots)" |
|
613 |
||
614 |
^ InlineObject slotNamesAndValuesFromDictionary:self |
|
615 |
||
616 |
"Created: / 25-06-2019 / 16:53:29 / Claus Gittinger" |
|
617 |
! ! |
|
618 |
||
15133 | 619 |
!OrderedDictionary methodsFor:'copying'! |
620 |
||
621 |
copyEmpty |
|
622 |
"Return a copy of the receiver that contains no elements." |
|
623 |
||
15801 | 624 |
^ self species new: 10 |
15133 | 625 |
! |
626 |
||
23728 | 627 |
copyFrom:startIndex |
628 |
"Return a copy of the receiver that contains associations from |
|
629 |
position startIndex to the end." |
|
630 |
||
631 |
^ self copyFrom:startIndex to:self size |
|
632 |
||
633 |
"Created: / 13-02-2019 / 17:59:58 / Claus Gittinger" |
|
634 |
! |
|
635 |
||
15801 | 636 |
copyFrom:startIndex to:endIndex |
23728 | 637 |
"Return a copy of the receiver that contains associations from |
15133 | 638 |
position startIndex to endIndex." |
15801 | 639 |
|
640 |
|newDict| |
|
15133 | 641 |
|
15801 | 642 |
endIndex < startIndex ifTrue:[ |
643 |
^ self copyEmpty |
|
644 |
]. |
|
645 |
(startIndex < 1 or:[ endIndex > order size ]) ifTrue:[ |
|
646 |
^ self errorNotFound |
|
647 |
]. |
|
648 |
newDict := self copyEmpty:endIndex - startIndex + 1. |
|
649 |
startIndex to:endIndex do:[:index | |
|
650 |
newDict add:(self associationAt:(order at:index)) |
|
651 |
]. |
|
15133 | 652 |
^ newDict |
23728 | 653 |
|
654 |
"Modified (comment): / 13-02-2019 / 18:01:03 / Claus Gittinger" |
|
655 |
! |
|
656 |
||
657 |
copyTo:endIndex |
|
658 |
"Return a copy of the receiver that contains associations from |
|
659 |
the start to endIndex." |
|
660 |
||
661 |
^ self copyFrom:1 to:endIndex |
|
662 |
||
663 |
"Created: / 13-02-2019 / 18:00:24 / Claus Gittinger" |
|
664 |
! |
|
665 |
||
666 |
copyValuesFrom:startIndex |
|
667 |
"Return a copy of the receiver that contains values from |
|
668 |
position startIndex to the end." |
|
669 |
||
670 |
^ self copyValuesFrom:startIndex to:self size |
|
671 |
||
672 |
"Created: / 13-02-2019 / 18:01:24 / Claus Gittinger" |
|
15133 | 673 |
! |
674 |
||
23728 | 675 |
copyValuesFrom:startIndex to:endIndex |
676 |
"Return a copy of the receiver that contains values from |
|
677 |
position startIndex to endIndex." |
|
678 |
||
679 |
|outColl| |
|
680 |
||
681 |
endIndex < startIndex ifTrue:[ |
|
23735 | 682 |
^ OrderedCollection new |
23728 | 683 |
]. |
684 |
(startIndex < 1 or:[ endIndex > order size ]) ifTrue:[ |
|
685 |
^ self errorNotFound |
|
686 |
]. |
|
687 |
outColl := OrderedCollection new:(endIndex - startIndex + 1). |
|
688 |
startIndex to:endIndex do:[:index | |
|
689 |
outColl add:(self at:(order at:index)) |
|
690 |
]. |
|
691 |
^ outColl |
|
692 |
||
693 |
"Created: / 13-02-2019 / 18:03:19 / Claus Gittinger" |
|
23735 | 694 |
"Modified: / 13-02-2019 / 21:44:04 / Claus Gittinger" |
23728 | 695 |
! |
696 |
||
697 |
copyValuesTo:endIndex |
|
698 |
"Return a copy of the receiver that contains values from |
|
699 |
the start to endIndex." |
|
700 |
||
701 |
^ self copyValuesFrom:1 to:endIndex |
|
702 |
||
703 |
"Created: / 13-02-2019 / 18:02:13 / Claus Gittinger" |
|
24183 | 704 |
! ! |
705 |
||
706 |
!OrderedDictionary methodsFor:'copying-private'! |
|
23728 | 707 |
|
15133 | 708 |
postCopy |
709 |
"have to copy the order too" |
|
710 |
||
711 |
super postCopy. |
|
712 |
order := order copy |
|
713 |
! ! |
|
714 |
||
715 |
!OrderedDictionary methodsFor:'enumerating'! |
|
716 |
||
717 |
associationsCollect: aBlock |
|
718 |
"Evaluate aBlock with each of the associations of the dictionary as argument, |
|
719 |
and return a new (ordered) collection with the results" |
|
720 |
||
721 |
^ order collect:[:key | (aBlock value: (self associationAt:key))]. |
|
722 |
! |
|
723 |
||
724 |
associationsDo: aBlock |
|
16765 | 725 |
"Evaluate aBlock for each of the dictionary's associations. |
726 |
Enumerate them in the order by which they were added. |
|
727 |
||
728 |
See also: |
|
729 |
#keysAndValuesDo: (which passes keys & values separately) |
|
730 |
#keysDo: (which passes keys only) |
|
731 |
#do: (which passes values only)" |
|
15133 | 732 |
|
733 |
order do: [:key | aBlock value: (self associationAt: key)] |
|
734 |
! |
|
735 |
||
736 |
associationsDo: aBlock from: firstIndex to: secondIndex |
|
737 |
"Evaluate aBlock with each of the dictionary's associations from index |
|
738 |
firstIndex to index secondIndex as the argument." |
|
739 |
||
740 |
firstIndex to: secondIndex do: [:index | |
|
741 |
aBlock value: (self associationAt: (order at: index)) |
|
742 |
] |
|
743 |
! |
|
744 |
||
745 |
associationsReverseDo: aBlock |
|
746 |
"Evaluate aBlock for each of the dictionary's associations in reverse order." |
|
747 |
||
748 |
order reverseDo:[:key | aBlock value:(self associationAt:key)] |
|
749 |
||
750 |
"Created: 28.2.1997 / 15:52:31 / cg" |
|
751 |
! |
|
752 |
||
753 |
associationsSelect:aBlock |
|
754 |
"Evaluate aBlock with each of the dictionary's associations as the argument. |
|
755 |
Collect into a new OrderedDictionary only those associations for which |
|
756 |
aBlock evaluates to true. Return the new OrderedDictionary." |
|
757 |
||
758 |
|newDict| |
|
759 |
||
15801 | 760 |
newDict := self species new. |
15133 | 761 |
order do:[:key | |
762 |
|assoc| |
|
763 |
||
764 |
assoc := self associationAt:key. |
|
765 |
(aBlock value:assoc) ifTrue: [ |
|
15801 | 766 |
newDict at:key put:assoc value. |
15133 | 767 |
] |
768 |
]. |
|
769 |
^ newDict |
|
770 |
! |
|
771 |
||
772 |
collect: aBlock |
|
773 |
"Evaluate aBlock with each of the values of the dictionary as argument, |
|
774 |
and return a new (ordered) collection with the results" |
|
775 |
||
776 |
^ order collect:[:key | (aBlock value: (self at:key))]. |
|
777 |
! |
|
778 |
||
16216 | 779 |
detect: aBlock ifNone:anExceptionValue |
15759 | 780 |
"evaluate the argument, aBlock for each element in the receiver until |
781 |
the block returns true; in this case return the element which caused |
|
782 |
the true evaluation. |
|
16216 | 783 |
If none of the evaluations returns true, return the value of anExceptionValue" |
15759 | 784 |
|
785 |
order do:[:key | |
|
786 |
|el| |
|
15801 | 787 |
el := self at:key. |
788 |
(aBlock value:el) ifTrue:[^ el] |
|
15759 | 789 |
]. |
16216 | 790 |
^ anExceptionValue value |
15759 | 791 |
! |
792 |
||
15133 | 793 |
do: aBlock |
16765 | 794 |
"Evaluate aBlock for each of the dictionary's values. |
795 |
Enumerate them in the order by which they were added." |
|
15133 | 796 |
|
797 |
order do: [:key | aBlock value: (self at: key)] |
|
798 |
! |
|
799 |
||
15801 | 800 |
do: aBlock from: firstIndex to: lastIndex |
801 |
<resource: #obsolete> |
|
802 |
||
803 |
self obsoleteMethodWarning:'use #from:to:do:'. |
|
15133 | 804 |
|
805 |
self from:firstIndex to:lastIndex do:aBlock. |
|
806 |
! |
|
807 |
||
808 |
findFirst:aBlock ifNone:exceptionalValue |
|
809 |
"Return the index of the first association in the dictionary for which aBlock |
|
810 |
evaluates as true. If the block does not evaluate to true, return exceptionalValue" |
|
811 |
||
15801 | 812 |
|stop "{ Class: SmallInteger }" | |
813 |
||
814 |
stop := order size. |
|
815 |
1 to:stop do:[:index | |
|
15133 | 816 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
817 |
]. |
|
818 |
^ exceptionalValue value |
|
819 |
! |
|
820 |
||
821 |
findLast:aBlock |
|
822 |
"Return the index of the last association in the dictionary for which aBlock |
|
823 |
evaluates as true. If the block does not evaluate to true, return 0" |
|
824 |
||
15801 | 825 |
|start "{ Class: SmallInteger }"| |
826 |
||
827 |
start := order size. |
|
828 |
start to:1 by:-1 do: [:index | |
|
15133 | 829 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
830 |
]. |
|
831 |
^ 0 |
|
832 |
! |
|
833 |
||
834 |
findLast:aBlock startingAt:startIndex |
|
835 |
"Return the index of the last association in the dictionary for which aBlock |
|
15662 | 836 |
evaluates as true. Start the backward search at startIndex. |
15133 | 837 |
If the block does not evaluate to true, return 0" |
838 |
||
15801 | 839 |
|start "{ Class: SmallInteger }"| |
840 |
||
841 |
start := startIndex. |
|
842 |
start to:1 by:-1 do: [:index | |
|
15133 | 843 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
844 |
]. |
|
845 |
^ 0 |
|
846 |
! |
|
847 |
||
848 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
|
849 |
"Return the index of the last association in the dictionary for which aBlock |
|
850 |
evaluates as true. Start the search at startIndex. |
|
851 |
End the search at endIndex or when an element is found. |
|
852 |
If the block does not evaluate to true, return 0" |
|
853 |
||
15801 | 854 |
|start "{ Class: SmallInteger }" |
855 |
end "{ Class: SmallInteger }"| |
|
856 |
||
857 |
start := startIndex. |
|
858 |
end := endIndex. |
|
859 |
start to:end by:-1 do: [:index | |
|
15133 | 860 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
861 |
]. |
|
862 |
^ 0 |
|
863 |
! |
|
864 |
||
865 |
from:firstIndex to:lastIndex do: aBlock |
|
866 |
"Evaluate aBlock with each of the dictionary's associations from index |
|
867 |
firstIndex to index secondIndex as the argument." |
|
868 |
||
15801 | 869 |
|start "{ Class:SmallInteger }" |
870 |
stop "{ Class:SmallInteger }" | |
|
871 |
||
872 |
start := firstIndex. "/ these assignments force type checking... |
|
873 |
stop := lastIndex. "/ and guarantee inline loop code below. |
|
874 |
order from:start to:stop do:[:key | |
|
15133 | 875 |
aBlock value: (self at:key) |
876 |
]. |
|
877 |
! |
|
878 |
||
15759 | 879 |
keysAndValuesDetect: aBlock ifNone:exceptionBlock |
19736 | 880 |
"evaluate the argument, aBlock for each key and value in the receiver, |
881 |
until the block returns true; |
|
22950 | 882 |
in this case return the value which caused the true evaluation. |
15759 | 883 |
If none of the evaluations returns true, return the result of the |
884 |
evaluation of the exceptionBlock" |
|
885 |
||
886 |
order do:[:key | |
|
887 |
|el| |
|
15801 | 888 |
el := self at:key. |
889 |
(aBlock value:key value:el) ifTrue:[^ el] |
|
15759 | 890 |
]. |
891 |
^ exceptionBlock value |
|
892 |
! |
|
893 |
||
15133 | 894 |
keysAndValuesDo:aBlock |
15451 | 895 |
"perform the block for all keys and values in the collection. |
896 |
Enumerate them in the order by which they were added. |
|
15133 | 897 |
|
898 |
See also: |
|
899 |
#associationsDo: (which passes key-value associations) |
|
16765 | 900 |
#keysDo: (which passes keys only) |
15133 | 901 |
#do: (which passes values only) |
902 |
||
903 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
904 |
Iterate over a copy to do this." |
|
905 |
||
15801 | 906 |
order do:[:key | aBlock value:key value:(self at: key)]. |
15133 | 907 |
|
908 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
909 |
"Created: / 15.10.1999 / 16:49:31 / cg" |
|
910 |
"Modified: / 15.10.1999 / 16:53:50 / cg" |
|
911 |
! |
|
912 |
||
15451 | 913 |
keysAndValuesReverseDo:aBlock |
914 |
"perform the block for all keys and values in the collection. |
|
915 |
Enumerate them in the reverse order from which they were added. |
|
916 |
||
917 |
See also: |
|
918 |
#associationsDo: (which passes key-value associations) |
|
919 |
#keysAndValuesDo: (which passes keys & values separately) |
|
920 |
#do: (which passes values only) |
|
921 |
||
922 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
923 |
Iterate over a copy to do this." |
|
924 |
||
925 |
order reverseDo: [:key | aBlock value:key value:(self at: key)]. |
|
926 |
||
927 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
928 |
"Created: / 15.10.1999 / 16:49:31 / cg" |
|
929 |
"Modified: / 15.10.1999 / 16:53:50 / cg" |
|
930 |
! |
|
931 |
||
15133 | 932 |
keysDo:aBlock |
933 |
"evaluate the argument, aBlock for every key in the collection. |
|
16765 | 934 |
Enumerate them in the order by which they were added. |
15133 | 935 |
|
936 |
See also: |
|
937 |
#associationsDo: (which passes key-value associations) |
|
938 |
#keysAndValuesDo: (which passes keys & values separately) |
|
939 |
#do: (which passes values only) |
|
940 |
||
941 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
942 |
Iterate over a copy to do this." |
|
943 |
||
944 |
order do:[:key | aBlock value:key]. |
|
945 |
||
946 |
"Created: / 26-06-1999 / 10:53:00 / ps" |
|
947 |
"Modified: / 24-08-2010 / 10:14:06 / cg" |
|
948 |
! |
|
949 |
||
950 |
reverseDo: aBlock |
|
951 |
"Evaluate aBlock with each of the dictionary's associations as the argument, |
|
16765 | 952 |
starting with the last added element and taking each in sequence up to the first." |
15133 | 953 |
|
15801 | 954 |
|sz "{ Class:SmallInteger }"| |
955 |
||
956 |
sz := order size. |
|
957 |
sz to:1 by:-1 do: [:index | |
|
15133 | 958 |
aBlock value:(self associationAt:(order at:index)) |
959 |
] |
|
960 |
! |
|
961 |
||
962 |
reversed |
|
963 |
"Return with a new OrderedDictionary with its associations in reverse order." |
|
964 |
||
15801 | 965 |
^ self copy reverse. |
15133 | 966 |
! |
967 |
||
968 |
select:aBlock |
|
969 |
"Evaluate aBlock with each of the dictionary's values as the argument. |
|
970 |
Collect into a new OrderedDictionary only those associations for which |
|
971 |
aBlock evaluated to true. Return the new OrderedDictionary." |
|
972 |
||
973 |
|newColl| |
|
974 |
||
975 |
newColl := self species new. |
|
976 |
order do:[:key | |
|
15801 | 977 |
|val| |
15133 | 978 |
|
15801 | 979 |
val := self at:key. |
980 |
(aBlock value:val) ifTrue:[ |
|
981 |
newColl at:key put:val. |
|
15133 | 982 |
] |
983 |
]. |
|
984 |
^ newColl |
|
985 |
! ! |
|
986 |
||
987 |
!OrderedDictionary methodsFor:'initialization'! |
|
988 |
||
21218 | 989 |
initializeOrder:count |
990 |
order := OrderedCollection new:count |
|
15133 | 991 |
! ! |
992 |
||
993 |
!OrderedDictionary methodsFor:'private'! |
|
994 |
||
15801 | 995 |
removeFromOrder: aKey |
996 |
<resource: #obsolete> |
|
997 |
||
998 |
order remove: aKey ifAbsent: [] |
|
15133 | 999 |
! ! |
1000 |
||
1001 |
!OrderedDictionary methodsFor:'queries'! |
|
1002 |
||
1003 |
occurrencesOfKey: aKey |
|
1004 |
"Return how many of the dictionary's keys are equal to aKey." |
|
1005 |
||
1006 |
^ order count:[:eachKey | aKey = eachKey] |
|
1007 |
||
1008 |
"/ cg: the original code was not very smalltalkish: |
|
1009 |
"/ |
|
1010 |
"/ | count | |
|
1011 |
"/ |
|
1012 |
"/ count := 0. |
|
1013 |
"/ 1 to: self size do: [:index | aKey = (order at: index) ifTrue: [count := count + 1]]. |
|
1014 |
"/ ^count |
|
1015 |
! |
|
1016 |
||
1017 |
occurrencesOfValue: aValue |
|
1018 |
"Return how many of the dictionary's values are equal to aValue." |
|
1019 |
||
1020 |
^ order count:[:eachKey | aValue = (self at:eachKey)] |
|
1021 |
||
1022 |
"/ cg: the original code was not very smalltalkish: |
|
1023 |
"/ |
|
1024 |
"/ | count | |
|
1025 |
"/ |
|
1026 |
"/ count := 0. |
|
1027 |
"/ 1 to: self size do: [:index | |
|
1028 |
"/ aValue = (self at: (order at: index)) ifTrue: [count := count + 1]]. |
|
1029 |
"/ ^count |
|
1030 |
! ! |
|
1031 |
||
1032 |
!OrderedDictionary methodsFor:'removing'! |
|
1033 |
||
19814 | 1034 |
clearContents |
1035 |
"remove all elements from the receiver, but do not shrink. Returns the receiver." |
|
1036 |
||
1037 |
super clearContents. |
|
1038 |
order clearContents. |
|
1039 |
! |
|
1040 |
||
15133 | 1041 |
removeFirst |
24348 | 1042 |
"remove the first element from the receiver. |
1043 |
Return the removed element. |
|
1044 |
Raise an error if the receiver is empty" |
|
1045 |
||
15133 | 1046 |
|key| |
1047 |
||
1048 |
order size == 0 ifTrue:[ |
|
15801 | 1049 |
"error if collection is empty" |
1050 |
^ self emptyCollectionError. |
|
15133 | 1051 |
]. |
1052 |
key := order removeFirst. |
|
1053 |
^ super removeKey:key. |
|
24348 | 1054 |
|
1055 |
"Modified (comment): / 24-06-2019 / 12:45:56 / Claus Gittinger" |
|
15133 | 1056 |
! |
1057 |
||
1058 |
removeFromIndex:fromIndex toIndex:toIndex |
|
1059 |
"Returns the receiver." |
|
1060 |
||
15801 | 1061 |
|start "{ Class:SmallInteger }" |
1062 |
stop "{ Class:SmallInteger }" | |
|
15133 | 1063 |
|
15801 | 1064 |
start := fromIndex. |
1065 |
stop := toIndex. |
|
1066 |
||
1067 |
order from:start to:stop do:[:key | |
|
15133 | 1068 |
super removeKey:key. |
1069 |
]. |
|
15801 | 1070 |
order removeFromIndex:fromIndex toIndex:toIndex. |
15133 | 1071 |
|
1072 |
"Created: 28.9.1995 / 12:04:33 / stefan" |
|
1073 |
! |
|
1074 |
||
15801 | 1075 |
removeIndex:anInteger |
24349 | 1076 |
"remove an element from the receiver. |
1077 |
Return the removed element. |
|
1078 |
Raise an error if the index is invalid" |
|
24348 | 1079 |
|
15801 | 1080 |
self removeFromIndex:anInteger toIndex:anInteger. |
24348 | 1081 |
|
24349 | 1082 |
"Modified (comment): / 24-06-2019 / 12:49:44 / Claus Gittinger" |
15801 | 1083 |
! |
1084 |
||
15133 | 1085 |
removeKey:aKey |
24346 | 1086 |
"remove the association under aKey from the collection, |
1087 |
return the value previously stored there. |
|
1088 |
If it was not in the collection report an error. |
|
1089 |
||
1090 |
WARNING: do not remove elements while iterating over the receiver. |
|
1091 |
See #saveRemoveKey: to do this." |
|
1092 |
||
15133 | 1093 |
order remove:aKey. |
1094 |
^ super removeKey:aKey. |
|
24346 | 1095 |
|
1096 |
"Modified (comment): / 24-06-2019 / 12:44:45 / Claus Gittinger" |
|
15133 | 1097 |
! |
1098 |
||
1099 |
removeKey:aKey ifAbsent:aBlock |
|
19842 | 1100 |
"remove key (and the associated value) from the receiver, |
1101 |
return the value previously stored there. |
|
1102 |
If it was not in the collection return the result |
|
1103 |
from evaluating aBlock." |
|
1104 |
||
15801 | 1105 |
|oldValue| |
1106 |
||
1107 |
oldValue := super removeKey:aKey ifAbsent:aBlock. |
|
15133 | 1108 |
order remove:aKey ifAbsent:[]. |
15801 | 1109 |
^ oldValue. |
15133 | 1110 |
|
1111 |
"Created: / 31-01-2011 / 22:04:01 / cg" |
|
1112 |
! |
|
1113 |
||
1114 |
removeLast |
|
24348 | 1115 |
"remove the last element from the receiver. |
1116 |
Return the removed element. |
|
1117 |
Raise an error if the receiver is empty" |
|
1118 |
||
15133 | 1119 |
|key| |
1120 |
||
1121 |
order size == 0 ifTrue:[ |
|
24348 | 1122 |
"error if collection is empty" |
15133 | 1123 |
|
24348 | 1124 |
^ self emptyCollectionError. |
15133 | 1125 |
]. |
1126 |
key := order removeLast. |
|
1127 |
^ super removeKey:key. |
|
24348 | 1128 |
|
1129 |
"Modified (comment): / 24-06-2019 / 12:45:45 / Claus Gittinger" |
|
15133 | 1130 |
! ! |
1131 |
||
1132 |
!OrderedDictionary methodsFor:'searching'! |
|
1133 |
||
1134 |
identityIndexOfAssociation: anAssociation |
|
1135 |
"Return the identity index of anAssociation within the receiver. If the receiver |
|
1136 |
does not contain anAssociation, return 0." |
|
1137 |
||
1138 |
^ self identityIndexOfAssociation: anAssociation ifAbsent: [0] |
|
1139 |
! |
|
1140 |
||
1141 |
identityIndexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
1142 |
"Return the identity index of anAssociation within the receiver. |
|
1143 |
If the receiver does not contain anAssociation, |
|
1144 |
return the result of evaluating the exceptionBlock." |
|
1145 |
||
1146 |
"/ as ST/X's dictionaries do not store the associations |
|
1147 |
"/ (instead, they store the keys and values in separate collections) |
|
1148 |
"/ this will not work. You have to think about it and rewrite your code. |
|
1149 |
self error:'this does not work in Smalltalk/X'. |
|
1150 |
||
1151 |
order keysAndValuesDo:[:i :key | |
|
1152 |
(self associationAt:key) == anAssociation ifTrue: [^i] |
|
1153 |
]. |
|
1154 |
^exceptionBlock value |
|
1155 |
! |
|
1156 |
||
1157 |
identityIndexOfKey: aKey |
|
1158 |
"Return the identity index of aKey within the receiver. If the receiver |
|
1159 |
does not contain aKey, return 0." |
|
1160 |
||
1161 |
^self identityIndexOfKey: aKey ifAbsent: [0] |
|
1162 |
! |
|
1163 |
||
1164 |
identityIndexOfKey: aKey ifAbsent: exceptionBlock |
|
1165 |
"Return the identity index of aKey within the receiver. If the receiver does |
|
1166 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
1167 |
||
1168 |
^ order identityIndexOf:aKey ifAbsent:exceptionBlock |
|
1169 |
! |
|
1170 |
||
1171 |
identityIndexOfValue: aValue |
|
1172 |
"Return the identity index of aValue within the receiver. If the receiver |
|
1173 |
does not contain aValue, return 0." |
|
1174 |
||
1175 |
^self identityIndexOfValue: aValue ifAbsent: [0] |
|
1176 |
! |
|
1177 |
||
1178 |
identityIndexOfValue: aValue ifAbsent: exceptionBlock |
|
1179 |
"Return the identity index of aValue within the receiver. If the receiver |
|
1180 |
does not contain aValue, return the result of evaluating the exceptionBlock." |
|
1181 |
||
1182 |
order keysAndValuesDo:[:i :key | (self at:key) == aValue ifTrue: [^i]]. |
|
1183 |
^exceptionBlock value |
|
1184 |
! |
|
1185 |
||
1186 |
indexOfAssociation: anAssociation |
|
1187 |
"Return the index of anAssociation within the receiver. If the receiver does |
|
1188 |
not contain anAssociation, return 0." |
|
1189 |
||
1190 |
^self indexOfAssociation: anAssociation ifAbsent: [0] |
|
1191 |
! |
|
1192 |
||
1193 |
indexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
1194 |
"Return the identity index of anAssociation within the receiver. If the receiver |
|
1195 |
does not contain anAssociation, return the result of evaluating the exceptionBlock." |
|
1196 |
||
1197 |
order keysAndValuesDo:[:i :key | |
|
1198 |
(self associationAt:key) = anAssociation ifTrue: [^i] |
|
1199 |
]. |
|
1200 |
^exceptionBlock value |
|
1201 |
! |
|
1202 |
||
1203 |
indexOfKey: aKey |
|
1204 |
"Return the index of aKey within the receiver. If the receiver does |
|
1205 |
not contain aKey, return 0." |
|
1206 |
||
1207 |
^self indexOfKey: aKey ifAbsent: [0] |
|
1208 |
! |
|
1209 |
||
1210 |
indexOfKey: aKey ifAbsent: exceptionBlock |
|
1211 |
"Return the identity index of aKey within the receiver. If the receiver does |
|
1212 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
1213 |
||
1214 |
^ order indexOf:aKey ifAbsent:exceptionBlock |
|
1215 |
! |
|
1216 |
||
1217 |
indexOfValue: aValue |
|
1218 |
"Return the index of aValue within the receiver. |
|
1219 |
If the receiver does not contain aValue, return 0." |
|
1220 |
||
1221 |
^self indexOfValue: aValue ifAbsent: [0] |
|
1222 |
! |
|
1223 |
||
1224 |
indexOfValue: aValue ifAbsent: exceptionBlock |
|
1225 |
"Return the identity index of aValue within the receiver. |
|
1226 |
If the receiver does not contain aValue, return the result of evaluating the exceptionBlock." |
|
1227 |
||
1228 |
order keysAndValuesDo:[:i :key | (self at:key) = aValue ifTrue: [^i]]. |
|
1229 |
^ exceptionBlock value |
|
1230 |
! |
|
1231 |
||
1232 |
nextIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
1233 |
"Return the next index of aAssociation within the receiver between startIndex |
|
1234 |
and stopIndex. If the receiver does not contain aAssociation, return nil" |
|
1235 |
||
15801 | 1236 |
|start "{ Class:SmallInteger }" |
1237 |
stop "{ Class:SmallInteger }"| |
|
1238 |
||
1239 |
start := startIndex. |
|
1240 |
stop := stopIndex. |
|
1241 |
start to: stop do: [:i | |
|
15133 | 1242 |
(self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
1243 |
^nil |
|
1244 |
! |
|
1245 |
||
1246 |
nextIndexOfKey: aKey from: startIndex to: stopIndex |
|
1247 |
"Return the next index of aKey within the receiver between startIndex and |
|
1248 |
stopIndex. If the receiver does not contain aKey, return nil" |
|
1249 |
||
15801 | 1250 |
|start "{ Class:SmallInteger }" |
1251 |
stop "{ Class:SmallInteger }"| |
|
1252 |
||
1253 |
start := startIndex. |
|
1254 |
stop := stopIndex. |
|
1255 |
start to: stop do: [:i | |
|
15133 | 1256 |
(order at: i) = aKey ifTrue: [^i]]. |
1257 |
^nil |
|
1258 |
! |
|
1259 |
||
1260 |
nextIndexOfValue: aValue from: startIndex to: stopIndex |
|
1261 |
"Return the next index of aValue within the receiver between startIndex and |
|
1262 |
stopIndex. If the receiver does not contain aValue, return nil" |
|
1263 |
||
15801 | 1264 |
|start "{ Class:SmallInteger }" |
1265 |
stop "{ Class:SmallInteger }" | |
|
1266 |
||
1267 |
start := startIndex. |
|
1268 |
stop := stopIndex. |
|
1269 |
start to: stop do: [:i | |
|
15133 | 1270 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
1271 |
^nil |
|
1272 |
! |
|
1273 |
||
1274 |
prevIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
1275 |
"Return the previous index of aAssociation within the receiver between |
|
1276 |
startIndex and stopIndex working backwards through the receiver. |
|
1277 |
If the receiver does not contain aAssociation, return nil" |
|
1278 |
||
15801 | 1279 |
|start "{ Class:SmallInteger }" |
1280 |
stop "{ Class:SmallInteger }"| |
|
1281 |
||
1282 |
start := startIndex. |
|
1283 |
stop := stopIndex. |
|
1284 |
start to: stop by: -1 |
|
15133 | 1285 |
do: [:i | (self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
1286 |
^nil |
|
1287 |
! |
|
1288 |
||
1289 |
prevIndexOfKey: aKey from: startIndex to: stopIndex |
|
1290 |
"Return the previous index of aKey within the receiver between startIndex and |
|
1291 |
stopIndex working backwards through the receiver. |
|
1292 |
If the receiver does not contain aKey, return nil" |
|
1293 |
||
15801 | 1294 |
|start "{ Class:SmallInteger }" |
1295 |
stop "{ Class:SmallInteger }"| |
|
1296 |
||
1297 |
start := startIndex. |
|
1298 |
stop := stopIndex. |
|
1299 |
start to: stop by: -1 |
|
15133 | 1300 |
do: [:i | (order at: i) = aKey ifTrue: [^i]]. |
1301 |
^nil |
|
1302 |
! |
|
1303 |
||
15801 | 1304 |
prevIndexOfValue:aValue from:startIndex to:stopIndex |
15133 | 1305 |
"Return the previous index of aValue within the receiver between startIndex |
15801 | 1306 |
and stopIndex working backwards through the receiver. |
15133 | 1307 |
If the receiver does not contain aValue, return nil" |
15801 | 1308 |
|
1309 |
|start "{ Class:SmallInteger }" |
|
1310 |
stop "{ Class:SmallInteger }"| |
|
15133 | 1311 |
|
15801 | 1312 |
start := startIndex. |
1313 |
stop := stopIndex. |
|
1314 |
start to:stop by:-1 do:[:i | |
|
1315 |
(self at:(order at:i)) = aValue ifTrue:[ |
|
1316 |
^ i |
|
1317 |
] |
|
1318 |
]. |
|
1319 |
^ nil |
|
15133 | 1320 |
! ! |
1321 |
||
1322 |
!OrderedDictionary methodsFor:'sorting & reordering'! |
|
1323 |
||
1324 |
reverse |
|
1325 |
"Destructively reverse my order. |
|
1326 |
WARNING: this is a destructive operation, which modifies the receiver. |
|
1327 |
Please use reversed (with a d) for a functional version." |
|
1328 |
||
1329 |
order reverse |
|
1330 |
! |
|
1331 |
||
1332 |
sort |
|
1333 |
"Destructively sort my order. |
|
1334 |
WARNING: this is a destructive operation, which modifies the receiver" |
|
1335 |
||
1336 |
order sort |
|
1337 |
! |
|
1338 |
||
1339 |
sort:aSortBlock |
|
1340 |
"Destructively sort my order. |
|
1341 |
WARNING: this is a destructive operation, which modifies the receiver" |
|
1342 |
||
1343 |
order sort:aSortBlock |
|
1344 |
! ! |
|
1345 |
||
18465 | 1346 |
!OrderedDictionary methodsFor:'testing'! |
1347 |
||
1348 |
isOrdered |
|
1349 |
"return true, if the receiver's elements are ordered. |
|
1350 |
Re-redefined to true here, as I do have an order" |
|
1351 |
||
1352 |
^ true |
|
1353 |
! ! |
|
1354 |
||
15133 | 1355 |
!OrderedDictionary class methodsFor:'documentation'! |
1356 |
||
1357 |
version |
|
19099 | 1358 |
^ '$Header$' |
15133 | 1359 |
! |
1360 |
||
1361 |
version_CVS |
|
19099 | 1362 |
^ '$Header$' |
15133 | 1363 |
! ! |
1364 |