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