author | Claus Gittinger <cg@exept.de> |
Fri, 08 Nov 1996 22:16:44 +0100 | |
changeset 457 | c862c91716b6 |
parent 120 | fa3590433788 |
child 497 | 1337168140ae |
permissions | -rw-r--r-- |
457 | 1 |
"{ Package: 'goodies/Collections-OrderedDictionary' }" |
2 |
||
62 | 3 |
" NAME OrderedDictionary |
67 | 4 |
AUTHOR Ifor Wyn Williams <ifor@uk.ac.man.cs> |
5 |
CONTRIBUTOR Ifor Wyn Williams <ifor@uk.ac.man.cs> |
|
6 |
FUNCTION An ordered dictionary |
|
7 |
ST-VERSIONS 2.3-5, 4.0 |
|
8 |
PREREQUISITES |
|
9 |
CONFLICTS |
|
10 |
DISTRIBUTION global |
|
11 |
VERSION 1.2 |
|
12 |
DATE 28.3.90 |
|
62 | 13 |
SUMMARY A dictionary that behaves like a SequencableCollection |
14 |
(except that associations cannot be removed). |
|
15 |
"! |
|
16 |
||
17 |
'From Smalltalk-80, Version 2.4 of 28 January 1989 on 28 March 1990 at 11:32:50 am'! |
|
18 |
||
19 |
Dictionary subclass: #OrderedDictionary |
|
67 | 20 |
instanceVariableNames: 'order ' |
21 |
classVariableNames: '' |
|
22 |
poolDictionaries: '' |
|
23 |
category: 'Collections-Sequenceable' |
|
62 | 24 |
! |
25 |
||
26 |
OrderedDictionary comment: |
|
27 |
'I am a subclass of Dictionary whose elements (associations) are ordered in a |
|
28 |
similar fashion to OrderedCollection. |
|
29 |
||
30 |
I have one instance variable: |
|
31 |
||
32 |
order <OrderedCollection> Ordered collection of keys reflecting the order of |
|
67 | 33 |
associations in the dictionary. ' |
62 | 34 |
! |
35 |
||
36 |
!OrderedDictionary methodsFor: 'adding'! |
|
37 |
||
38 |
add: anAssociation |
|
67 | 39 |
"add anAssociation to the dictionary" |
62 | 40 |
|
67 | 41 |
| key | |
62 | 42 |
|
67 | 43 |
key := anAssociation key. |
44 |
(super includesKey: key) |
|
45 |
ifFalse: [order add: key]. |
|
46 |
super add: anAssociation |
|
62 | 47 |
! |
48 |
||
49 |
add: anAssociation after: oldAssociation |
|
67 | 50 |
"Add the argument, anAssociation, as an element of the dictionary. Put it |
51 |
in the position just succeeding oldAssociation. Return anAssociation." |
|
62 | 52 |
|
67 | 53 |
| index | |
62 | 54 |
|
67 | 55 |
index := self indexOfAssociation: oldAssociation |
56 |
ifAbsent: [self error: 'association not found']. |
|
57 |
self removeFromOrder: anAssociation key. |
|
58 |
order add: anAssociation key after: (order at: index). |
|
59 |
super add: anAssociation. |
|
60 |
^ anAssociation |
|
62 | 61 |
! |
62 |
||
63 |
add: anAssociation before: oldAssociation |
|
67 | 64 |
"Add the argument, anAssociation, as an element of the dictionary. Put it |
65 |
in the position just preceding oldAssociation. Return anAssociation." |
|
62 | 66 |
|
67 | 67 |
| index | |
62 | 68 |
|
67 | 69 |
index := self indexOfAssociation: oldAssociation |
70 |
ifAbsent: [self error: 'association not found']. |
|
71 |
self removeFromOrder: anAssociation key. |
|
72 |
order add: anAssociation key before: (order at: index). |
|
73 |
super add: anAssociation. |
|
74 |
^ anAssociation |
|
62 | 75 |
! |
76 |
||
77 |
add: anAssociation beforeIndex: spot |
|
67 | 78 |
"Add the argument, anAssociation, as an element of the receiver. Put it |
79 |
in the position just preceding the indexed position spot. Return newObject." |
|
62 | 80 |
|
67 | 81 |
self removeFromOrder: anAssociation key. |
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
82 |
order add: anAssociation key beforeIndex: spot. |
67 | 83 |
super add: anAssociation |
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
84 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
85 |
"Modified: 28.9.1995 / 14:06:53 / stefan" |
62 | 86 |
! |
87 |
||
88 |
addAll: aCollectionOfAssociations |
|
67 | 89 |
"Add each element of aCollectionOfAssociations at my end." |
62 | 90 |
|
67 | 91 |
aCollectionOfAssociations associationsDo: [:elems | self add: elems] |
62 | 92 |
! |
93 |
||
94 |
addAllFirst: anOrderedCollectionOfAssociations |
|
67 | 95 |
"Add each element of anOrderedCollectionOfAssociations at the beginning |
96 |
of the receiver." |
|
62 | 97 |
|
67 | 98 |
anOrderedCollectionOfAssociations reverseDo: [:each | self addFirst: each] |
62 | 99 |
! |
100 |
||
101 |
addAllLast: anOrderedCollectionOfAssociations |
|
67 | 102 |
"Add each element of anOrderedCollectionOfAssociations at the end of the |
103 |
receiver." |
|
62 | 104 |
|
67 | 105 |
anOrderedCollectionOfAssociations do: [:each | self addLast: each] |
62 | 106 |
! |
107 |
||
108 |
addFirst: anAssociation |
|
67 | 109 |
"Add anAssociation to the beginning of the receiver." |
62 | 110 |
|
67 | 111 |
self removeFromOrder: anAssociation key. |
112 |
order addFirst: anAssociation key. |
|
113 |
super add: anAssociation. |
|
62 | 114 |
! |
115 |
||
116 |
addLast: anAssociation |
|
67 | 117 |
"Add anAssociation to the end of the receiver." |
62 | 118 |
|
67 | 119 |
self removeFromOrder: anAssociation key. |
120 |
order addLast: anAssociation key. |
|
121 |
super add: anAssociation. |
|
62 | 122 |
"/! |
123 |
"/ |
|
124 |
"/grow |
|
125 |
"/ "Increase the number of elements in the dictionary" |
|
126 |
"/ |
|
127 |
"/ ^ super grow |
|
128 |
"/ |
|
129 |
"/ | newSelf | |
|
130 |
"/ |
|
131 |
"/ newSelf := (self class) new: (self basicSize + self growSize). |
|
132 |
"/ order do: [:key | newSelf add: (self associationAt: key)]. |
|
133 |
"/ self become: newSelf |
|
134 |
"/ |
|
135 |
! ! |
|
136 |
||
137 |
!OrderedDictionary methodsFor: 'enumerating'! |
|
138 |
||
139 |
associationsDo: aBlock |
|
67 | 140 |
"Evaluate aBlock for each of the dictionary's associations." |
62 | 141 |
|
67 | 142 |
order do: [:key | aBlock value: (self associationAt: key)] |
62 | 143 |
! |
144 |
||
145 |
associationsDo: aBlock from: firstIndex to: secondIndex |
|
67 | 146 |
"Evaluate aBlock with each of the dictionary's associations from index |
147 |
firstIndex to index secondIndex as the argument." |
|
62 | 148 |
|
67 | 149 |
firstIndex to: secondIndex do: [:index | |
150 |
aBlock value: (self associationAt: (order at: index)) |
|
151 |
] |
|
62 | 152 |
! |
153 |
||
154 |
collect: aBlock |
|
67 | 155 |
"Evaluate aBlock with each of the associations of the dictionary as the |
156 |
argument. The block should return an association which will be added to the |
|
157 |
new OrderedDictionary" |
|
62 | 158 |
|
67 | 159 |
| newDict | |
62 | 160 |
|
67 | 161 |
newDict := OrderedDictionary new. |
162 |
1 to: order size do: [:index | |
|
163 |
newDict add: (aBlock value: (self associationAt: (order at: index))) |
|
164 |
]. |
|
165 |
^ newDict |
|
62 | 166 |
! |
167 |
||
168 |
do: aBlock |
|
67 | 169 |
"Evaluate aBlock for each of the dictionary's values." |
62 | 170 |
|
67 | 171 |
order do: [:key | aBlock value: (self at: key)] |
62 | 172 |
! |
173 |
||
174 |
do: aBlock from: firstIndex to: secondIndex |
|
67 | 175 |
"Evaluate aBlock with each of the dictionary's associations from index |
176 |
firstIndex to index secondIndex as the argument." |
|
62 | 177 |
|
67 | 178 |
firstIndex to: secondIndex do: [:index | |
179 |
aBlock value: (self at: (order at: index)) |
|
180 |
] |
|
62 | 181 |
! |
182 |
||
183 |
findFirst: aBlock |
|
67 | 184 |
"Return the index of the first association in the dictionary for which aBlock |
185 |
evaluates as true. If the block does not evaluate to true, return 0" |
|
62 | 186 |
|
67 | 187 |
1 to: order size do: [:index | |
188 |
(aBlock value: (self associationAt: (order at: index))) ifTrue: [^index] |
|
189 |
]. |
|
190 |
^ 0 |
|
62 | 191 |
! |
192 |
||
193 |
findLast: aBlock |
|
67 | 194 |
"Return the index of the last association in the dictionary for which aBlock |
62 | 195 |
|
67 | 196 |
evaluates as true. If the block does not evaluate to true, return 0" |
62 | 197 |
|
67 | 198 |
order size to: 1 by: -1 do: [:index | |
199 |
(aBlock value: (self associationAt: (order at: index))) ifTrue: [^index] |
|
200 |
]. |
|
201 |
^ 0 |
|
62 | 202 |
! |
203 |
||
204 |
reverse |
|
67 | 205 |
"Return with a new OrderedDictionary with its associations in reverse order." |
62 | 206 |
|
207 |
||
67 | 208 |
| newDict| |
62 | 209 |
|
67 | 210 |
newDict := OrderedDictionary new. |
211 |
order size to: 1 by: -1 do:[:index | |
|
212 |
|key| |
|
62 | 213 |
|
67 | 214 |
key := order at: index. |
215 |
newDict at: key put: (self at: key) |
|
216 |
]. |
|
217 |
^newDict |
|
62 | 218 |
! |
219 |
||
220 |
reverseDo: aBlock |
|
67 | 221 |
"Evaluate aBlock with each of the dictionary's associations as the argument, |
222 |
starting with the last element and taking each in sequence up to the first." |
|
62 | 223 |
|
67 | 224 |
order size to: 1 by: -1 do: [:index | |
225 |
aBlock value: (self associationAt: (order at: index)) |
|
226 |
] |
|
62 | 227 |
! |
228 |
||
229 |
select: aBlock |
|
67 | 230 |
"Evaluate aBlock with each of the dictionary's associations as the argument. |
231 |
Collect into a new OrderedDictionary only those associations for which |
|
232 |
aBlock evaluates to true. Return the new OrderedDictionary." |
|
62 | 233 |
|
67 | 234 |
| newDict| |
62 | 235 |
|
67 | 236 |
newDict := OrderedDictionary new. |
237 |
1 to: order size do:[:index | |
|
238 |
|key assoc| |
|
62 | 239 |
|
67 | 240 |
key := order at: index. |
241 |
assoc := self associationAt: key. |
|
242 |
(aBlock value: assoc) ifTrue: [ |
|
243 |
newDict add: assoc |
|
244 |
] |
|
245 |
]. |
|
246 |
^newDict |
|
62 | 247 |
! ! |
248 |
||
249 |
!OrderedDictionary methodsFor: 'accessing index'! |
|
250 |
||
251 |
identityIndexOfAssociation: anAssociation |
|
67 | 252 |
"Return the identity index of anAssociation within the receiver. If the receiver |
253 |
does not contain anAssociation, return 0." |
|
62 | 254 |
|
67 | 255 |
^self identityIndexOfAssociation: anAssociation ifAbsent: [0] |
62 | 256 |
! |
257 |
||
258 |
identityIndexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
67 | 259 |
"Return the identity index of anAssociation within the receiver. |
260 |
If the receiver does not contain anAssociation, |
|
261 |
return the result of evaluating the exceptionBlock." |
|
62 | 262 |
|
67 | 263 |
1 to: order size do: [:i | |
264 |
(self associationAt: (order at: i)) == anAssociation ifTrue: [^i] |
|
265 |
]. |
|
266 |
^exceptionBlock value |
|
62 | 267 |
! |
268 |
||
269 |
identityIndexOfKey: aKey |
|
67 | 270 |
"Return the identity index of aKey within the receiver. If the receiver |
271 |
does not contain aKey, return 0." |
|
62 | 272 |
|
67 | 273 |
^self identityIndexOfKey: aKey ifAbsent: [0] |
62 | 274 |
! |
275 |
||
276 |
identityIndexOfKey: aKey ifAbsent: exceptionBlock |
|
67 | 277 |
"Return the identity index of aKey within the receiver. If the receiver does |
278 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
62 | 279 |
|
67 | 280 |
1 to: order size do: [:i | (order at: i) == aKey ifTrue: [^i]]. |
281 |
^exceptionBlock value |
|
62 | 282 |
! |
283 |
||
284 |
identityIndexOfValue: aValue |
|
67 | 285 |
"Return the identity index of aValue within the receiver. If the receiver |
286 |
does not contain aValue, return 0." |
|
62 | 287 |
|
67 | 288 |
^self identityIndexOfValue: aValue ifAbsent: [0] |
62 | 289 |
! |
290 |
||
291 |
identityIndexOfValue: aValue ifAbsent: exceptionBlock |
|
67 | 292 |
"Return the identity index of aValue within the receiver. If the receiver |
293 |
does not contain aValue, return the result of evaluating the exceptionBlock." |
|
62 | 294 |
|
67 | 295 |
1 to: order size do: [:i | |
296 |
(self at: (order at: i)) == aValue ifTrue: [^i]]. |
|
297 |
^exceptionBlock value |
|
62 | 298 |
! |
299 |
||
300 |
indexOfAssociation: anAssociation |
|
67 | 301 |
"Return the index of anAssociation within the receiver. If the receiver does |
302 |
not contain anAssociation, return 0." |
|
62 | 303 |
|
67 | 304 |
^self indexOfAssociation: anAssociation ifAbsent: [0] |
62 | 305 |
! |
306 |
||
307 |
indexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
67 | 308 |
"Return the identity index of anAssociation within the receiver. If the receiver |
309 |
does not contain anAssociation, return the result of evaluating the exceptionBlock." |
|
62 | 310 |
|
67 | 311 |
1 to: order size do: [:i | |
312 |
(self associationAt: (order at: i)) = anAssociation ifTrue: [^i] |
|
313 |
]. |
|
314 |
^exceptionBlock value |
|
62 | 315 |
! |
316 |
||
317 |
indexOfKey: aKey |
|
67 | 318 |
"Return the index of aKey within the receiver. If the receiver does |
319 |
not contain aKey, return 0." |
|
62 | 320 |
|
67 | 321 |
^self indexOfKey: aKey ifAbsent: [0] |
62 | 322 |
! |
323 |
||
324 |
indexOfKey: aKey ifAbsent: exceptionBlock |
|
67 | 325 |
"Return the identity index of aKey within the receiver. If the receiver does |
326 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
62 | 327 |
|
67 | 328 |
1 to: order size do: [:i | (order at: i) = aKey ifTrue: [^i]]. |
329 |
^exceptionBlock value |
|
62 | 330 |
! |
331 |
||
332 |
indexOfValue: aValue |
|
67 | 333 |
"Return the index of aValue within the receiver. If the receiver does |
334 |
not contain aValue, return 0." |
|
62 | 335 |
|
67 | 336 |
^self indexOfValue: aValue ifAbsent: [0] |
62 | 337 |
! |
338 |
||
339 |
indexOfValue: aValue ifAbsent: exceptionBlock |
|
67 | 340 |
"Return the identity index of aValue within the receiver. If the receiver |
341 |
does not contain aValue, return the result of evaluating the exceptionBlock." |
|
62 | 342 |
|
67 | 343 |
1 to: order size do: [:i | |
344 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
345 |
^exceptionBlock value |
|
62 | 346 |
! |
347 |
||
348 |
nextIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
67 | 349 |
"Return the next index of aAssociation within the receiver between startIndex |
350 |
and stopIndex. If the receiver does not contain aAssociation, return nil" |
|
62 | 351 |
|
67 | 352 |
startIndex to: stopIndex do: [:i | |
353 |
(self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
|
354 |
^nil |
|
62 | 355 |
! |
356 |
||
357 |
nextIndexOfKey: aKey from: startIndex to: stopIndex |
|
67 | 358 |
"Return the next index of aKey within the receiver between startIndex and |
359 |
stopIndex. If the receiver does not contain aKey, return nil" |
|
62 | 360 |
|
67 | 361 |
startIndex to: stopIndex do: [:i | |
362 |
(order at: i) = aKey ifTrue: [^i]]. |
|
363 |
^nil |
|
62 | 364 |
! |
365 |
||
366 |
nextIndexOfValue: aValue from: startIndex to: stopIndex |
|
67 | 367 |
"Return the next index of aValue within the receiver between startIndex and |
368 |
stopIndex. If the receiver does not contain aValue, return nil" |
|
62 | 369 |
|
67 | 370 |
startIndex to: stopIndex do: [:i | |
371 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
372 |
^nil |
|
62 | 373 |
! |
374 |
||
375 |
prevIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
67 | 376 |
"Return the previous index of aAssociation within the receiver between |
377 |
startIndex |
|
378 |
and stopIndex working backwards through the receiver. If the receiver does |
|
62 | 379 |
|
67 | 380 |
not contain aAssociation, return nil" |
62 | 381 |
|
67 | 382 |
startIndex |
383 |
to: stopIndex |
|
384 |
by: -1 |
|
385 |
do: [:i | (self associationAt: (order at: i)) |
|
386 |
= aAssociation ifTrue: [^i]]. |
|
387 |
^nil! |
|
62 | 388 |
|
389 |
prevIndexOfKey: aKey from: startIndex to: stopIndex |
|
67 | 390 |
"Return the previous index of aKey within the receiver between startIndex and |
62 | 391 |
|
67 | 392 |
stopIndex working backwards through the receiver. If the receiver does not |
62 | 393 |
|
67 | 394 |
contain aKey, return nil" |
62 | 395 |
|
67 | 396 |
startIndex |
397 |
to: stopIndex |
|
398 |
by: -1 |
|
399 |
do: [:i | (order at: i) |
|
400 |
= aKey ifTrue: [^i]]. |
|
401 |
^nil! |
|
62 | 402 |
|
403 |
prevIndexOfValue: aValue from: startIndex to: stopIndex |
|
67 | 404 |
"Return the previous index of aValue within the receiver between startIndex |
62 | 405 |
|
67 | 406 |
and stopIndex working backwards through the receiver. If the receiver does |
62 | 407 |
|
67 | 408 |
not contain aValue, return nil" |
62 | 409 |
|
67 | 410 |
startIndex |
411 |
to: stopIndex |
|
412 |
by: -1 |
|
413 |
do: [:i | (self at: (order at: i)) |
|
414 |
= aValue ifTrue: [^i]]. |
|
415 |
^nil! ! |
|
62 | 416 |
|
417 |
!OrderedDictionary methodsFor: 'testing'! |
|
418 |
||
419 |
occurrencesOfKey: aKey |
|
67 | 420 |
"Return how many of the dictionary's keys are equal to aKey." |
62 | 421 |
|
67 | 422 |
| count | |
423 |
count := 0. |
|
424 |
1 to: self size do: [:index | aKey = (order at: index) ifTrue: [count := count |
|
62 | 425 |
+ 1]]. |
67 | 426 |
^count! |
62 | 427 |
|
428 |
occurrencesOfValue: aValue |
|
67 | 429 |
"Return how many of the dictionary's values are equal to aValue." |
62 | 430 |
|
67 | 431 |
| count | |
432 |
count := 0. |
|
433 |
1 to: self size do: [:index | aValue = (self at: (order at: index)) ifTrue: |
|
434 |
[count := count + 1]]. |
|
435 |
^count! ! |
|
62 | 436 |
|
437 |
!OrderedDictionary methodsFor: 'accessing'! |
|
438 |
||
439 |
after: anAssociation |
|
67 | 440 |
"Return the association after anAssociation in the order. If anAssociation is |
62 | 441 |
the |
67 | 442 |
last association in the order, return the undefined object. If anAssociation |
62 | 443 |
is |
67 | 444 |
not found, invoke an error notifier" |
62 | 445 |
|
67 | 446 |
1 to: order size - 1 do: [:index | (self associationAt: (order at: index)) |
447 |
= anAssociation ifTrue: [^self associationAt: (order at: index + 1)]]. |
|
448 |
(self associationAt: (order last)) |
|
449 |
= anAssociation |
|
450 |
ifTrue: [^nil] |
|
451 |
ifFalse: [^self error: 'not found']! |
|
62 | 452 |
|
453 |
associations |
|
67 | 454 |
"Return an OrderedCollection containing the receiver's associations." |
62 | 455 |
|
67 | 456 |
| anOrderedCollection | |
457 |
anOrderedCollection := OrderedCollection new: order size. |
|
458 |
order do: [:key | anOrderedCollection add: (self associationAt: key)]. |
|
459 |
^anOrderedCollection! |
|
62 | 460 |
|
461 |
at: key put: anObject |
|
67 | 462 |
"Set the value at key to be anObject. If key is not found, create a new |
463 |
entry for key and set is value to anObject. Return anObject." |
|
62 | 464 |
|
67 | 465 |
(order includes: key) |
466 |
ifFalse: [order add: key]. |
|
467 |
super at: key put: anObject! |
|
62 | 468 |
|
469 |
atAll: anInterval put: anObject |
|
67 | 470 |
"Put anObject into the value field of every association specified by the interval" |
62 | 471 |
|
472 |
||
67 | 473 |
anInterval do: [:index | self at: (order at: index) |
474 |
put: anObject]! |
|
62 | 475 |
|
476 |
atAllPut: anObject |
|
67 | 477 |
"Put anObject into the value field of every association in the dictionary" |
62 | 478 |
|
67 | 479 |
order do: [:key| self at: key put: anObject]! |
62 | 480 |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
481 |
atIndex:index |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
482 |
"return an element at a given index" |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
483 |
^ self at:(order at:index) |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
484 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
485 |
"Created: 28.9.1995 / 13:49:39 / stefan" |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
486 |
! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
487 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
488 |
atIndex:index put:anAssociation |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
489 |
"put an association to a given index. remove the old associatioan at this index" |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
490 |
|key| |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
491 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
492 |
key := anAssociation key. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
493 |
(super includesKey:key) ifTrue:[ |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
494 |
^ self error:'duplicate key'. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
495 |
]. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
496 |
super removeKey:(order at:index) ifAbsent:[]. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
497 |
order at:index put:key. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
498 |
super add:anAssociation. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
499 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
500 |
"Created: 28.9.1995 / 16:30:15 / stefan" |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
501 |
! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
502 |
|
62 | 503 |
before: anAssociation |
67 | 504 |
"Return the association before anAssociation in the order. If anAssociation |
62 | 505 |
is the |
67 | 506 |
first association in the order, return the undefined object. If anAssociation |
62 | 507 |
is |
67 | 508 |
not found, invoke an error notifier" |
62 | 509 |
|
67 | 510 |
2 to: order size do: [:index | (self associationAt: (order at: index)) |
511 |
= anAssociation ifTrue: [^self associationAt: (order at: index - 1)]]. |
|
512 |
(self associationAt: order first) |
|
513 |
= anAssociation |
|
514 |
ifTrue: [^nil] |
|
515 |
ifFalse: [^self error: 'not found']! |
|
62 | 516 |
|
517 |
first |
|
67 | 518 |
"Return the first association of the receiver. Provide an error |
519 |
notification if the receiver contains no elements." |
|
62 | 520 |
|
67 | 521 |
order emptyCheck. |
522 |
^self associationAt: (order first)! |
|
62 | 523 |
|
524 |
keys |
|
67 | 525 |
"Return a OrderedCollection containing the receiver's keys." |
62 | 526 |
|
67 | 527 |
^order copy.! |
62 | 528 |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
529 |
keyAt:index |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
530 |
"get the key at the given index" |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
531 |
^ order at:index. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
532 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
533 |
"Created: 29.9.1995 / 11:32:07 / stefan" |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
534 |
! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
535 |
|
62 | 536 |
last |
67 | 537 |
"Return the last association of the receiver. Provide an error |
538 |
notification if the receiver contains no elements." |
|
62 | 539 |
|
67 | 540 |
order emptyCheck. |
541 |
^self associationAt: (order last)! |
|
62 | 542 |
|
543 |
order |
|
67 | 544 |
^order! |
62 | 545 |
|
546 |
values |
|
67 | 547 |
"Return a OrderedCollection containing the receiver's values." |
62 | 548 |
|
67 | 549 |
| anOrderedCollection | |
550 |
anOrderedCollection := OrderedCollection new: order size. |
|
551 |
order do: [:key | anOrderedCollection add: (self at: key)]. |
|
552 |
^anOrderedCollection! ! |
|
62 | 553 |
|
554 |
!OrderedDictionary methodsFor: 'private'! |
|
555 |
||
556 |
initialize |
|
67 | 557 |
order := OrderedCollection new! |
62 | 558 |
|
559 |
removeFromOrder: aKey |
|
67 | 560 |
order remove: aKey ifAbsent: []! ! |
62 | 561 |
|
562 |
!OrderedDictionary methodsFor: 'copying'! |
|
563 |
||
564 |
copyEmpty |
|
67 | 565 |
"Return a copy of the receiver that contains no elements." |
62 | 566 |
|
67 | 567 |
^(self class) new: 10! |
62 | 568 |
|
569 |
copyEmpty: aSize |
|
67 | 570 |
"Return a copy of the receiver that contains no elements." |
62 | 571 |
|
67 | 572 |
^(self class) new: aSize! |
62 | 573 |
|
574 |
copyFrom: startIndex to: endIndex |
|
67 | 575 |
"Return a copy of the receiver that contains elements from |
576 |
position startIndex to endIndex." |
|
62 | 577 |
|
67 | 578 |
| newDict | |
579 |
endIndex < startIndex ifTrue: [^self copyEmpty]. |
|
580 |
(startIndex < 1 or: [endIndex > order size]) |
|
581 |
ifTrue: [^self error: 'No such element']. |
|
582 |
newDict := self copyEmpty: endIndex - startIndex + 1. |
|
583 |
startIndex to: endIndex do: [:index | newDict add: (self associationAt: (order |
|
62 | 584 |
at: index))]. |
67 | 585 |
^newDict! |
62 | 586 |
|
587 |
copyWith: anAssociation |
|
67 | 588 |
"Return a copy of the dictionary that is 1 bigger than the receiver and |
589 |
includes the argument, anAssociation, at the end." |
|
62 | 590 |
|
67 | 591 |
| newDict | |
592 |
newDict := self copy. |
|
593 |
newDict add: anAssociation. |
|
594 |
^newDict! |
|
62 | 595 |
|
596 |
copyWithout: anAssociation |
|
67 | 597 |
"Return a copy of the dictionary that is 1 smaller than the receiver and does |
62 | 598 |
|
67 | 599 |
not includes the argument, anAssociation" |
62 | 600 |
|
67 | 601 |
| newDict | |
602 |
newDict := OrderedDictionary new: order size - 1. |
|
603 |
self associationsDo: [:assoc | anAssociation = assoc ifFalse: [newDict add: |
|
62 | 604 |
assoc]]! ! |
605 |
||
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
606 |
!OrderedDictionary methodsFor:'removing'! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
607 |
removeFromIndex:fromIndex toIndex:toIndex |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
608 |
| keys | |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
609 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
610 |
keys := order copyFrom:fromIndex to:toIndex. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
611 |
order removeFromIndex:fromIndex toIndex:toIndex. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
612 |
keys do:[ :key | |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
613 |
super removeKey:key. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
614 |
]. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
615 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
616 |
"Created: 28.9.1995 / 12:04:33 / stefan" |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
617 |
! ! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
618 |
|
62 | 619 |
|
620 |
!OrderedDictionary class methodsFor: 'instance creation'! |
|
621 |
||
622 |
new |
|
67 | 623 |
^super new initialize! |
62 | 624 |
|
625 |
new: anInteger |
|
67 | 626 |
^(super new: anInteger) initialize! ! |
62 | 627 |
|
628 |
"COPYRIGHT. |
|
629 |
The above file is a Manchester Goodie protected by copyright. |
|
630 |
These conditions are imposed on the whole Goodie, and on any significant |
|
631 |
part of it which is separately transmitted or stored: |
|
67 | 632 |
* You must ensure that every copy includes this notice, and that |
633 |
source and author(s) of the material are acknowledged. |
|
634 |
* These conditions must be imposed on anyone who receives a copy. |
|
635 |
* The material shall not be used for commercial gain without the prior |
|
636 |
written consent of the author(s). |
|
62 | 637 |
Further information on the copyright conditions may be obtained by |
638 |
sending electronic mail: |
|
67 | 639 |
To: goodies-lib@cs.man.ac.uk |
640 |
Subject: copyright |
|
62 | 641 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
642 |
Computer Science, The University, Manchester M13 9PL, UK |
|
643 |
||
644 |
(C) Copyright 1992 University of Manchester |
|
645 |
For more information about the Manchester Goodies Library (from which |
|
646 |
this file was distributed) send e-mail: |
|
67 | 647 |
To: goodies-lib@cs.man.ac.uk |
648 |
Subject: help |
|
62 | 649 |
"! |