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