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