author | Claus Gittinger <cg@exept.de> |
Thu, 08 Nov 2007 17:34:59 +0100 | |
changeset 1913 | 4c2622e92a91 |
parent 1589 | f0607bf986e0 |
child 1917 | 61c602336f3d |
permissions | -rw-r--r-- |
757 | 1 |
" |
2 |
COPYRIGHT. |
|
3 |
This is a Manchester Goodie protected by copyright. |
|
4 |
These conditions are imposed on the whole Goodie, and on any significant |
|
5 |
part of it which is separately transmitted or stored: |
|
6 |
* You must ensure that every copy includes this notice, and that |
|
7 |
source and author(s) of the material are acknowledged. |
|
8 |
* These conditions must be imposed on anyone who receives a copy. |
|
9 |
* The material shall not be used for commercial gain without the prior |
|
10 |
written consent of the author(s). |
|
11 |
Further information on the copyright conditions may be obtained by |
|
12 |
sending electronic mail: |
|
13 |
To: goodies-lib@cs.man.ac.uk |
|
14 |
Subject: copyright |
|
15 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
|
16 |
Computer Science, The University, Manchester M13 9PL, UK |
|
62 | 17 |
|
757 | 18 |
(C) Copyright 1992 University of Manchester |
19 |
For more information about the Manchester Goodies Library (from which |
|
20 |
this file was distributed) send e-mail: |
|
21 |
To: goodies-lib@cs.man.ac.uk |
|
22 |
Subject: help |
|
23 |
" |
|
24 |
||
1130 | 25 |
"{ Package: 'stx:libbasic2' }" |
959 | 26 |
|
757 | 27 |
Dictionary subclass:#OrderedDictionary |
28 |
instanceVariableNames:'order' |
|
29 |
classVariableNames:'' |
|
30 |
poolDictionaries:'' |
|
31 |
category:'Collections-Sequenceable' |
|
62 | 32 |
! |
33 |
||
757 | 34 |
!OrderedDictionary class methodsFor:'documentation'! |
498 | 35 |
|
36 |
copyright |
|
37 |
" |
|
38 |
COPYRIGHT. |
|
39 |
This is a Manchester Goodie protected by copyright. |
|
40 |
These conditions are imposed on the whole Goodie, and on any significant |
|
41 |
part of it which is separately transmitted or stored: |
|
42 |
* You must ensure that every copy includes this notice, and that |
|
43 |
source and author(s) of the material are acknowledged. |
|
44 |
* These conditions must be imposed on anyone who receives a copy. |
|
45 |
* The material shall not be used for commercial gain without the prior |
|
46 |
written consent of the author(s). |
|
47 |
Further information on the copyright conditions may be obtained by |
|
48 |
sending electronic mail: |
|
49 |
To: goodies-lib@cs.man.ac.uk |
|
50 |
Subject: copyright |
|
51 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
|
52 |
Computer Science, The University, Manchester M13 9PL, UK |
|
53 |
||
54 |
(C) Copyright 1992 University of Manchester |
|
55 |
For more information about the Manchester Goodies Library (from which |
|
56 |
this file was distributed) send e-mail: |
|
57 |
To: goodies-lib@cs.man.ac.uk |
|
58 |
Subject: help |
|
59 |
" |
|
60 |
! |
|
62 | 61 |
|
498 | 62 |
documentation |
63 |
" |
|
64 |
I am a subclass of Dictionary whose elements (associations) are ordered in a |
|
65 |
similar fashion to OrderedCollection. |
|
1242 | 66 |
That is, while being filled via #at:put: messages (or similar Dictionary protocol), |
67 |
the order in which associations are added is remembered and accessable via the #atIndex: |
|
68 |
or #order messages. |
|
498 | 69 |
|
70 |
I have one instance variable: |
|
71 |
||
72 |
order <OrderedCollection> Ordered collection of keys reflecting the order of |
|
1242 | 73 |
associations in the dictionary. |
498 | 74 |
|
75 |
[author:] |
|
1242 | 76 |
Ifor Wyn Williams <ifor@uk.ac.man.cs> |
498 | 77 |
|
78 |
[see also:] |
|
1242 | 79 |
OrderedCollection Dictionary |
1581 | 80 |
OrderedSet |
1242 | 81 |
" |
82 |
! |
|
83 |
||
84 |
examples |
|
85 |
" |
|
86 |
|o| |
|
87 |
||
88 |
o := OrderedDictionary new. |
|
89 |
||
90 |
o at:'one' put:1. |
|
91 |
o at:'two' put:2. |
|
92 |
o at:'three' put:3. |
|
93 |
o at:'four' put:4. |
|
94 |
o at:'five' put:5. |
|
95 |
o at:'six' put:6. |
|
96 |
o at:'seven' put:7. |
|
97 |
o at:'eight' put:8. |
|
98 |
o at:'nine' put:9. |
|
99 |
o at:'zero' put:0. |
|
100 |
||
101 |
o at:'eight'. |
|
102 |
o atIndex:1. |
|
103 |
o atIndex:5. |
|
104 |
o atIndex:10. |
|
105 |
||
106 |
o from:3 to:6 do:[:each | Transcript showCR:each ]. |
|
107 |
||
108 |
o collect:[:eachAssoc | eachAssoc key -> eachAssoc value squared]. |
|
109 |
o associations. |
|
110 |
o order. |
|
111 |
o reverse. |
|
112 |
||
113 |
o atIndex:1. |
|
114 |
o atIndex:5. |
|
115 |
o atIndex:10. |
|
498 | 116 |
" |
62 | 117 |
! |
118 |
||
757 | 119 |
info |
120 |
" |
|
121 |
NAME OrderedDictionary |
|
122 |
AUTHOR Ifor Wyn Williams <ifor@uk.ac.man.cs> |
|
123 |
CONTRIBUTOR Ifor Wyn Williams <ifor@uk.ac.man.cs> |
|
124 |
FUNCTION An ordered dictionary |
|
125 |
ST-VERSIONS 2.3-5, 4.0 |
|
126 |
PREREQUISITES |
|
127 |
CONFLICTS |
|
128 |
DISTRIBUTION global |
|
129 |
VERSION 1.2 |
|
130 |
DATE 28.3.90 |
|
131 |
SUMMARY A dictionary that behaves like a SequencableCollection |
|
132 |
(except that associations cannot be removed). |
|
133 |
" |
|
62 | 134 |
! ! |
135 |
||
757 | 136 |
!OrderedDictionary class methodsFor:'instance creation'! |
137 |
||
138 |
new |
|
1571 | 139 |
^ super new initializeOrder |
959 | 140 |
! |
62 | 141 |
|
757 | 142 |
new: anInteger |
1571 | 143 |
^(super new: anInteger) initializeOrder |
959 | 144 |
! ! |
62 | 145 |
|
757 | 146 |
!OrderedDictionary methodsFor:'accessing'! |
62 | 147 |
|
757 | 148 |
after: anAssociation |
1242 | 149 |
"Return the association after anAssociation in the order. |
150 |
If anAssociation is the last association in the order, return nil. |
|
151 |
If anAssociation is not found, invoke an error notifier" |
|
62 | 152 |
|
1242 | 153 |
1 to: order size - 1 do: [:index | (self associationAt: (order at: index)) |
154 |
= anAssociation ifTrue: [^self associationAt: (order at: index + 1)]]. |
|
155 |
(self associationAt: (order last)) |
|
156 |
= anAssociation |
|
157 |
ifTrue: [^nil] |
|
158 |
ifFalse: [^self error: 'not found'] |
|
959 | 159 |
! |
62 | 160 |
|
757 | 161 |
associations |
1242 | 162 |
"Return an OrderedCollection containing the receiver's associations." |
497
1337168140ae
added #addAllAssociations: , #addAllAssociationsFirst: and #addAllAssociationsLast:
Claus Gittinger <cg@exept.de>
parents:
457
diff
changeset
|
163 |
|
1242 | 164 |
^ order collect: [:key | self associationAt: key ]. |
959 | 165 |
! |
166 |
||
167 |
at:aKey ifAbsentPut:valueBlock |
|
168 |
|val| |
|
169 |
||
170 |
^ self at:aKey ifAbsent:[ self at:aKey put:valueBlock value ] |
|
171 |
! |
|
497
1337168140ae
added #addAllAssociations: , #addAllAssociationsFirst: and #addAllAssociationsLast:
Claus Gittinger <cg@exept.de>
parents:
457
diff
changeset
|
172 |
|
757 | 173 |
at: key put: anObject |
1242 | 174 |
"Set the value at key to be anObject. |
175 |
If key is not found, create a new entry for key and set is value to anObject. |
|
176 |
If key is already present, the order remains unchanged. |
|
177 |
Return anObject." |
|
62 | 178 |
|
1242 | 179 |
"/ claus: super can check this much faster ... |
180 |
"/ (super includesKey:key) |
|
181 |
"/ ... but that leads to trouble in add:* methods. (sigh) |
|
757 | 182 |
|
1242 | 183 |
(order includes: key) |
184 |
ifFalse: [order add: key]. |
|
185 |
^ super at: key put: anObject |
|
959 | 186 |
! |
62 | 187 |
|
1242 | 188 |
atAll:indexCollection put: anObject |
189 |
"Put anObject into the value field of every association specified by indexCollection, |
|
190 |
which is typically an interval." |
|
757 | 191 |
|
1242 | 192 |
indexCollection do:[:index | self at:(order at: index) put:anObject] |
959 | 193 |
! |
62 | 194 |
|
757 | 195 |
atAllPut: anObject |
1242 | 196 |
"Put anObject into the value field of every association in the dictionary" |
757 | 197 |
|
1242 | 198 |
order do: [:key | self at: key put: anObject] |
959 | 199 |
! |
62 | 200 |
|
757 | 201 |
atIndex:index |
202 |
"return an element at a given index" |
|
1242 | 203 |
|
757 | 204 |
^ self at:(order at:index) |
205 |
||
206 |
"Created: 28.9.1995 / 13:49:39 / stefan" |
|
62 | 207 |
! |
208 |
||
757 | 209 |
atIndex:index put:anAssociation |
210 |
"put an association to a given index. remove the old associatioan at this index" |
|
211 |
|key| |
|
62 | 212 |
|
757 | 213 |
key := anAssociation key. |
214 |
(super includesKey:key) ifTrue:[ |
|
959 | 215 |
^ self error:'duplicate key'. |
757 | 216 |
]. |
217 |
super removeKey:(order at:index) ifAbsent:[]. |
|
218 |
order at:index put:key. |
|
959 | 219 |
^ super add:anAssociation. |
62 | 220 |
|
757 | 221 |
"Created: 28.9.1995 / 16:30:15 / stefan" |
62 | 222 |
! |
223 |
||
757 | 224 |
before: anAssociation |
1242 | 225 |
"Return the association before anAssociation in the order. |
226 |
If anAssociation is the first association in the order, return nil. |
|
227 |
If anAssociation is not found, invoke an error notifier" |
|
62 | 228 |
|
1242 | 229 |
2 to:order size do:[:index | |
230 |
(self associationAt:(order at:index)) = anAssociation |
|
231 |
ifTrue:[ ^ self associationAt:(order at:index - 1)] |
|
232 |
]. |
|
233 |
(self associationAt:order first) = anAssociation ifTrue: [^ nil]. |
|
234 |
^ self error: 'not found' |
|
959 | 235 |
! |
62 | 236 |
|
757 | 237 |
first |
1242 | 238 |
"Return the first association of the receiver. |
239 |
Provide an error notification if the receiver contains no elements." |
|
757 | 240 |
|
1242 | 241 |
order emptyCheck. |
242 |
^ self associationAt: (order first) |
|
959 | 243 |
! |
757 | 244 |
|
245 |
keyAt:index |
|
246 |
"get the key at the given index" |
|
1242 | 247 |
|
757 | 248 |
^ order at:index. |
249 |
||
250 |
"Created: 29.9.1995 / 11:32:07 / stefan" |
|
62 | 251 |
! |
252 |
||
757 | 253 |
keys |
1242 | 254 |
"Return a OrderedCollection containing the receiver's keys." |
757 | 255 |
|
1242 | 256 |
^ order copy. |
959 | 257 |
! |
62 | 258 |
|
757 | 259 |
last |
1242 | 260 |
"Return the last association of the receiver. |
261 |
Provide an error notification if the receiver contains no elements." |
|
62 | 262 |
|
1242 | 263 |
order emptyCheck. |
264 |
^ self associationAt: (order last) |
|
959 | 265 |
! |
62 | 266 |
|
757 | 267 |
order |
1242 | 268 |
"returns the values in the order of their appearance" |
269 |
||
270 |
^ order |
|
959 | 271 |
! |
62 | 272 |
|
757 | 273 |
values |
1242 | 274 |
"Return a OrderedCollection containing the receiver's values." |
62 | 275 |
|
1242 | 276 |
^ order collect: [:key | (self at: key) ]. |
959 | 277 |
! ! |
62 | 278 |
|
757 | 279 |
!OrderedDictionary methodsFor:'adding'! |
62 | 280 |
|
757 | 281 |
add: anAssociation |
282 |
"add anAssociation to the dictionary" |
|
62 | 283 |
|
757 | 284 |
| key | |
62 | 285 |
|
757 | 286 |
key := anAssociation key. |
287 |
(super includesKey: key) |
|
959 | 288 |
ifFalse: [order add: key]. |
289 |
^ super add: anAssociation |
|
757 | 290 |
! |
62 | 291 |
|
757 | 292 |
add: anAssociation after: oldAssociation |
1242 | 293 |
"Add the argument, anAssociation, as an element of the dictionary. Put it |
294 |
in the position just succeeding oldAssociation. Return anAssociation." |
|
62 | 295 |
|
1242 | 296 |
| index | |
62 | 297 |
|
1242 | 298 |
index := self indexOfAssociation: oldAssociation |
299 |
ifAbsent: [self error: 'association not found']. |
|
300 |
self removeFromOrder: anAssociation key. |
|
301 |
order add: anAssociation key after: (order at: index). |
|
302 |
super add: anAssociation. |
|
303 |
^ anAssociation |
|
757 | 304 |
! |
62 | 305 |
|
757 | 306 |
add: anAssociation before: oldAssociation |
1242 | 307 |
"Add the argument, anAssociation, as an element of the dictionary. Put it |
308 |
in the position just preceding oldAssociation. Return anAssociation." |
|
62 | 309 |
|
1242 | 310 |
| index | |
62 | 311 |
|
1242 | 312 |
index := self indexOfAssociation: oldAssociation |
313 |
ifAbsent: [self error: 'association not found']. |
|
314 |
self removeFromOrder: anAssociation key. |
|
315 |
order add: anAssociation key before: (order at: index). |
|
316 |
super add: anAssociation. |
|
317 |
^ anAssociation |
|
757 | 318 |
! |
504
df82d5a5de53
undone last change - all add: methods have trouble with that....
Claus Gittinger <cg@exept.de>
parents:
503
diff
changeset
|
319 |
|
757 | 320 |
add: anAssociation beforeIndex: spot |
1242 | 321 |
"Add the argument, anAssociation, as an element of the receiver. Put it |
322 |
in the position just preceding the indexed position spot. Return newObject." |
|
62 | 323 |
|
1242 | 324 |
self removeFromOrder: anAssociation key. |
325 |
order add: anAssociation key beforeIndex: spot. |
|
326 |
^ super add: anAssociation |
|
62 | 327 |
|
757 | 328 |
"Modified: 28.9.1995 / 14:06:53 / stefan" |
329 |
! |
|
62 | 330 |
|
757 | 331 |
addAll:aCollectionOfAssociations |
332 |
"Add each element of anOrderedCollectionOfAssociations at my end. |
|
333 |
We expect the argument to enumerate associations with #reverseDo:; |
|
1348 | 334 |
if it does not (i.e. it is another OD or a dictionary), use #addAllAssociationsFirst:. |
335 |
Returns the argument, aCollectionOfAssociations (sigh)." |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
336 |
|
959 | 337 |
self addAllLast:aCollectionOfAssociations. |
338 |
^ aCollectionOfAssociations |
|
757 | 339 |
|
340 |
"Modified: 28.2.1997 / 15:51:23 / cg" |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
341 |
! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
342 |
|
757 | 343 |
addAllAssociations:aDictionaryOrOrderedDictionary |
344 |
"Add each association of aDictionaryOrOrderedDictionary to my end. |
|
345 |
We expect the argument to respond to #associationsDo:." |
|
346 |
||
959 | 347 |
self addAllAssociationsLast:aDictionaryOrOrderedDictionary. |
348 |
^ aDictionaryOrOrderedDictionary |
|
757 | 349 |
|
350 |
"Created: 28.2.1997 / 15:52:02 / cg" |
|
351 |
! |
|
352 |
||
353 |
addAllAssociationsFirst:aDictionaryOrOrderedDictionary |
|
354 |
"Add each association of aDictionaryOrOrderedDictionary at the beginning of the |
|
355 |
receiver. We expect the argument to respond to #associationsReverseDo:." |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
356 |
|
959 | 357 |
aDictionaryOrOrderedDictionary associationsReverseDo:[:each | self addFirst:each]. |
358 |
^ aDictionaryOrOrderedDictionary |
|
757 | 359 |
|
360 |
"Created: 28.2.1997 / 15:50:14 / cg" |
|
361 |
! |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
362 |
|
757 | 363 |
addAllAssociationsLast:aDictionaryOrOrderedDictionary |
364 |
"Add each association of aDictionaryOrOrderedDictionary at the end of the |
|
365 |
receiver. We expect the argument to respond to #associationsDo:." |
|
366 |
||
959 | 367 |
aDictionaryOrOrderedDictionary associationsDo:[:each | self addLast:each]. |
368 |
^ aDictionaryOrOrderedDictionary |
|
757 | 369 |
|
370 |
"Created: 28.2.1997 / 15:48:37 / cg" |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
371 |
! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
372 |
|
959 | 373 |
addFirst: anAssociation |
1242 | 374 |
"Add anAssociation to the beginning of the receiver." |
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
375 |
|
1242 | 376 |
self removeFromOrder: anAssociation key. |
377 |
order addFirst: anAssociation key. |
|
378 |
^ super add: anAssociation. |
|
757 | 379 |
! |
62 | 380 |
|
757 | 381 |
addLast: anAssociation |
1242 | 382 |
"Add anAssociation to the end of the receiver." |
62 | 383 |
|
1242 | 384 |
self removeFromOrder: anAssociation key. |
385 |
order addLast: anAssociation key. |
|
386 |
^ super add: anAssociation. |
|
838 | 387 |
! ! |
62 | 388 |
|
757 | 389 |
!OrderedDictionary methodsFor:'copying'! |
62 | 390 |
|
391 |
copyEmpty |
|
1242 | 392 |
"Return a copy of the receiver that contains no elements." |
62 | 393 |
|
1242 | 394 |
^ (self class) new: 10 |
959 | 395 |
! |
62 | 396 |
|
397 |
copyEmpty: aSize |
|
1242 | 398 |
"Return a copy of the receiver that contains no elements." |
62 | 399 |
|
1242 | 400 |
^ (self class) new: aSize |
959 | 401 |
! |
62 | 402 |
|
403 |
copyFrom: startIndex to: endIndex |
|
1242 | 404 |
"Return a copy of the receiver that contains elements from |
405 |
position startIndex to endIndex." |
|
62 | 406 |
|
1242 | 407 |
| newDict | |
408 |
endIndex < startIndex ifTrue: [^self copyEmpty]. |
|
409 |
(startIndex < 1 or: [endIndex > order size]) |
|
410 |
ifTrue: [^self error: 'No such element']. |
|
411 |
newDict := self copyEmpty: endIndex - startIndex + 1. |
|
412 |
startIndex to: endIndex do: [:index | newDict add: (self associationAt: (order at: index))]. |
|
413 |
^ newDict |
|
959 | 414 |
! |
62 | 415 |
|
416 |
copyWith: anAssociation |
|
1242 | 417 |
"Return a copy of the dictionary that is 1 bigger than the receiver and |
418 |
includes the argument, anAssociation, at the end." |
|
62 | 419 |
|
1242 | 420 |
| newDict | |
421 |
newDict := self copy. |
|
422 |
newDict add: anAssociation. |
|
423 |
^ newDict |
|
959 | 424 |
! |
62 | 425 |
|
426 |
copyWithout: anAssociation |
|
1242 | 427 |
"Return a copy of the dictionary that is 1 smaller than the receiver and |
428 |
does not include the argument, anAssociation" |
|
62 | 429 |
|
1242 | 430 |
| newDict | |
431 |
newDict := OrderedDictionary new: order size - 1. |
|
432 |
self associationsDo: [:assoc | anAssociation = assoc ifFalse: [newDict add: assoc]] |
|
959 | 433 |
! ! |
62 | 434 |
|
757 | 435 |
!OrderedDictionary methodsFor:'enumerating'! |
436 |
||
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
437 |
associationsCollect: aBlock |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
438 |
"Evaluate aBlock with each of the associations of the dictionary as argument, |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
439 |
and return a new (ordered) collection with the results" |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
440 |
|
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
441 |
^ order collect:[:key | (aBlock value: (self associationAt:key))]. |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
442 |
! |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
443 |
|
757 | 444 |
associationsDo: aBlock |
1242 | 445 |
"Evaluate aBlock for each of the dictionary's associations." |
757 | 446 |
|
1242 | 447 |
order do: [:key | aBlock value: (self associationAt: key)] |
757 | 448 |
! |
449 |
||
450 |
associationsDo: aBlock from: firstIndex to: secondIndex |
|
1242 | 451 |
"Evaluate aBlock with each of the dictionary's associations from index |
452 |
firstIndex to index secondIndex as the argument." |
|
757 | 453 |
|
1242 | 454 |
firstIndex to: secondIndex do: [:index | |
455 |
aBlock value: (self associationAt: (order at: index)) |
|
456 |
] |
|
757 | 457 |
! |
458 |
||
459 |
associationsReverseDo: aBlock |
|
460 |
"Evaluate aBlock for each of the dictionary's associations in reverse order." |
|
461 |
||
462 |
order reverseDo:[:key | aBlock value:(self associationAt:key)] |
|
463 |
||
464 |
"Created: 28.2.1997 / 15:52:31 / cg" |
|
465 |
! |
|
466 |
||
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
467 |
associationsSelect:aBlock |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
468 |
"Evaluate aBlock with each of the dictionary's associations as the argument. |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
469 |
Collect into a new OrderedDictionary only those associations for which |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
470 |
aBlock evaluates to true. Return the new OrderedDictionary." |
757 | 471 |
|
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
472 |
|newDict| |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
473 |
|
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
474 |
newDict := OrderedDictionary new. |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
475 |
order do:[:key | |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
476 |
|assoc| |
757 | 477 |
|
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
478 |
assoc := self associationAt:key. |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
479 |
(aBlock value:assoc) ifTrue: [ |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
480 |
newDict add:assoc |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
481 |
] |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
482 |
]. |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
483 |
^ newDict |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
484 |
! |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
485 |
|
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
486 |
collect: aBlock |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
487 |
"Evaluate aBlock with each of the values of the dictionary as argument, |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
488 |
and return a new (ordered) collection with the results" |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
489 |
|
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
490 |
^ order collect:[:key | (aBlock value: (self at:key))]. |
757 | 491 |
! |
492 |
||
493 |
do: aBlock |
|
1242 | 494 |
"Evaluate aBlock for each of the dictionary's values." |
757 | 495 |
|
1242 | 496 |
order do: [:key | aBlock value: (self at: key)] |
757 | 497 |
! |
498 |
||
1242 | 499 |
do: aBlock from: firstIndex to: lastIndex |
500 |
"Evaluate aBlock with each of the dictionary's associations from index |
|
501 |
firstIndex to index secondIndex as the argument." |
|
757 | 502 |
|
1242 | 503 |
self from:firstIndex to:lastIndex do:aBlock. |
757 | 504 |
! |
505 |
||
506 |
findFirst: aBlock |
|
1242 | 507 |
"Return the index of the first association in the dictionary for which aBlock |
508 |
evaluates as true. If the block does not evaluate to true, return 0" |
|
757 | 509 |
|
1242 | 510 |
1 to:order size do:[:index | |
511 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
512 |
]. |
|
513 |
^ 0 |
|
757 | 514 |
! |
515 |
||
1242 | 516 |
findLast:aBlock |
517 |
"Return the index of the last association in the dictionary for which aBlock |
|
1424 | 518 |
evaluates as true. If the block does not evaluate to true, return 0" |
757 | 519 |
|
1242 | 520 |
order size to:1 by:-1 do: [:index | |
521 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
522 |
]. |
|
523 |
^ 0 |
|
524 |
! |
|
525 |
||
1424 | 526 |
findLast:aBlock startingAt:startIndex |
527 |
"Return the index of the last association in the dictionary for which aBlock |
|
528 |
evaluates as true. Start the search at startIndex. |
|
529 |
If the block does not evaluate to true, return 0" |
|
530 |
||
531 |
startIndex to:1 by:-1 do: [:index | |
|
532 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
533 |
]. |
|
534 |
^ 0 |
|
535 |
! |
|
536 |
||
537 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
|
538 |
"Return the index of the last association in the dictionary for which aBlock |
|
539 |
evaluates as true. Start the search at startIndex. |
|
540 |
End the search at endIndex or when an element is found. |
|
541 |
If the block does not evaluate to true, return 0" |
|
542 |
||
543 |
startIndex to:endIndex by:-1 do: [:index | |
|
544 |
(aBlock value:(self associationAt:(order at:index))) ifTrue: [^ index] |
|
545 |
]. |
|
546 |
^ 0 |
|
547 |
! |
|
548 |
||
1242 | 549 |
from:firstIndex to:lastIndex do: aBlock |
550 |
"Evaluate aBlock with each of the dictionary's associations from index |
|
551 |
firstIndex to index secondIndex as the argument." |
|
552 |
||
553 |
order from:firstIndex to:lastIndex do:[:key | |
|
554 |
aBlock value: (self at:key) |
|
555 |
]. |
|
757 | 556 |
! |
557 |
||
835 | 558 |
keysAndValuesDo:aBlock |
559 |
"perform the block for all keys in the collection. |
|
560 |
||
561 |
See also: |
|
1242 | 562 |
#associationsDo: (which passes key-value associations) |
563 |
#keysAndValuesDo: (which passes keys & values separately) |
|
564 |
#do: (which passes values only) |
|
835 | 565 |
|
566 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
1242 | 567 |
Iterate over a copy to do this." |
568 |
||
569 |
order do: [:key | aBlock value:key value:(self at: key)]. |
|
835 | 570 |
|
571 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
572 |
"Created: / 15.10.1999 / 16:49:31 / cg" |
|
573 |
"Modified: / 15.10.1999 / 16:53:50 / cg" |
|
574 |
! |
|
575 |
||
757 | 576 |
keysDo:aBlock |
577 |
"perform the block for all keys in the collection. |
|
578 |
||
579 |
See also: |
|
1242 | 580 |
#associationsDo: (which passes key-value associations) |
581 |
#keysAndValuesDo: (which passes keys & values separately) |
|
582 |
#do: (which passes values only) |
|
757 | 583 |
|
584 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
1242 | 585 |
Iterate over a copy to do this." |
757 | 586 |
|
1242 | 587 |
order do:[:key | aBlock value:key]. |
757 | 588 |
|
589 |
"Created: / 26.6.1999 / 10:53:00 / ps" |
|
590 |
"Modified: / 26.6.1999 / 10:55:30 / ps" |
|
591 |
! |
|
592 |
||
593 |
reverse |
|
1242 | 594 |
"Destructively reverse my order. |
595 |
WARNING: this is a destructive operation, which modifies the receiver." |
|
757 | 596 |
|
1242 | 597 |
order reverse |
757 | 598 |
! |
599 |
||
600 |
reverseDo: aBlock |
|
1242 | 601 |
"Evaluate aBlock with each of the dictionary's associations as the argument, |
602 |
starting with the last element and taking each in sequence up to the first." |
|
757 | 603 |
|
1242 | 604 |
order size to:1 by:-1 do: [:index | |
605 |
aBlock value:(self associationAt:(order at:index)) |
|
606 |
] |
|
757 | 607 |
! |
608 |
||
1242 | 609 |
reversed |
610 |
"Return with a new OrderedDictionary with its associations in reverse order." |
|
611 |
||
612 |
| newDict| |
|
757 | 613 |
|
1242 | 614 |
newDict := OrderedDictionary new. |
615 |
order size to:1 by:-1 do:[:index | |
|
616 |
|key| |
|
617 |
||
618 |
key := order at:index. |
|
619 |
newDict at:key put:(self at:key) |
|
620 |
]. |
|
621 |
^ newDict |
|
622 |
! |
|
757 | 623 |
|
1242 | 624 |
select:aBlock |
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
625 |
"Evaluate aBlock with each of the dictionary's values as the argument. |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
626 |
Collect into a new OrderedDictionary only those associations for which |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
627 |
aBlock evaluated to true. Return the new OrderedDictionary." |
1242 | 628 |
|
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
629 |
|newColl| |
757 | 630 |
|
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
631 |
newColl := self species new. |
1242 | 632 |
order do:[:key | |
633 |
|assoc| |
|
634 |
||
635 |
assoc := self associationAt:key. |
|
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
636 |
(aBlock value:(assoc value)) ifTrue: [ |
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
637 |
newColl add:assoc |
1242 | 638 |
] |
639 |
]. |
|
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
640 |
^ newColl |
757 | 641 |
! ! |
642 |
||
1571 | 643 |
!OrderedDictionary methodsFor:'initialization'! |
757 | 644 |
|
1571 | 645 |
initializeOrder |
646 |
order := OrderedCollection new |
|
647 |
! ! |
|
648 |
||
649 |
!OrderedDictionary methodsFor:'private'! |
|
757 | 650 |
|
651 |
removeFromOrder: aKey |
|
959 | 652 |
order remove: aKey ifAbsent: [] |
653 |
! ! |
|
757 | 654 |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
655 |
!OrderedDictionary methodsFor:'removing'! |
757 | 656 |
|
775
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
657 |
removeFirst |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
658 |
|key| |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
659 |
|
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
660 |
order size == 0 ifTrue:[ |
838 | 661 |
"error if collection is empty" |
775
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
662 |
|
838 | 663 |
^ self emptyCollectionError. |
775
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
664 |
]. |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
665 |
key := order removeFirst. |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
666 |
^ super removeKey:key. |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
667 |
! |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
668 |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
669 |
removeFromIndex:fromIndex toIndex:toIndex |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
670 |
| keys | |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
671 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
672 |
keys := order copyFrom:fromIndex to:toIndex. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
673 |
order removeFromIndex:fromIndex toIndex:toIndex. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
674 |
keys do:[ :key | |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
675 |
super removeKey:key. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
676 |
]. |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
677 |
|
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
678 |
"Created: 28.9.1995 / 12:04:33 / stefan" |
775
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
679 |
! |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
680 |
|
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
681 |
removeKey:aKey |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
682 |
order remove:aKey. |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
683 |
^ super removeKey:aKey. |
1444 | 684 |
! |
685 |
||
686 |
removeLast |
|
687 |
|key| |
|
688 |
||
689 |
order size == 0 ifTrue:[ |
|
690 |
"error if collection is empty" |
|
691 |
||
692 |
^ self emptyCollectionError. |
|
693 |
]. |
|
694 |
key := order removeLast. |
|
695 |
^ super removeKey:key. |
|
120
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
696 |
! ! |
fa3590433788
New methods for SequencableCollection access.
Stefan Vogel <sv@exept.de>
parents:
67
diff
changeset
|
697 |
|
1130 | 698 |
!OrderedDictionary methodsFor:'searching'! |
699 |
||
700 |
identityIndexOfAssociation: anAssociation |
|
1242 | 701 |
"Return the identity index of anAssociation within the receiver. If the receiver |
702 |
does not contain anAssociation, return 0." |
|
1130 | 703 |
|
1242 | 704 |
^ self identityIndexOfAssociation: anAssociation ifAbsent: [0] |
1130 | 705 |
! |
706 |
||
707 |
identityIndexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
1242 | 708 |
"Return the identity index of anAssociation within the receiver. |
709 |
If the receiver does not contain anAssociation, |
|
710 |
return the result of evaluating the exceptionBlock." |
|
1130 | 711 |
|
1242 | 712 |
"/ as ST/X's dictionaries do not store the associations |
713 |
"/ (instead, they store the keys and values in separate collections) |
|
714 |
"/ this will not work. You have to think about it and rewrite your code. |
|
715 |
self error:'this does not work in Smalltalk/X'. |
|
716 |
||
717 |
order keysAndValuesDo:[:i :key | |
|
718 |
(self associationAt:key) == anAssociation ifTrue: [^i] |
|
719 |
]. |
|
720 |
^exceptionBlock value |
|
1130 | 721 |
! |
722 |
||
723 |
identityIndexOfKey: aKey |
|
1242 | 724 |
"Return the identity index of aKey within the receiver. If the receiver |
725 |
does not contain aKey, return 0." |
|
1130 | 726 |
|
1242 | 727 |
^self identityIndexOfKey: aKey ifAbsent: [0] |
1130 | 728 |
! |
729 |
||
730 |
identityIndexOfKey: aKey ifAbsent: exceptionBlock |
|
1242 | 731 |
"Return the identity index of aKey within the receiver. If the receiver does |
732 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
1130 | 733 |
|
1242 | 734 |
^ order identityIndexOf:aKey ifAbsent:exceptionBlock |
1130 | 735 |
! |
736 |
||
737 |
identityIndexOfValue: aValue |
|
1242 | 738 |
"Return the identity index of aValue within the receiver. If the receiver |
739 |
does not contain aValue, return 0." |
|
1130 | 740 |
|
1242 | 741 |
^self identityIndexOfValue: aValue ifAbsent: [0] |
1130 | 742 |
! |
743 |
||
744 |
identityIndexOfValue: aValue ifAbsent: exceptionBlock |
|
1242 | 745 |
"Return the identity index of aValue within the receiver. If the receiver |
746 |
does not contain aValue, return the result of evaluating the exceptionBlock." |
|
1130 | 747 |
|
1242 | 748 |
order keysAndValuesDo:[:i :key | (self at:key) == aValue ifTrue: [^i]]. |
749 |
^exceptionBlock value |
|
1130 | 750 |
! |
751 |
||
752 |
indexOfAssociation: anAssociation |
|
1242 | 753 |
"Return the index of anAssociation within the receiver. If the receiver does |
754 |
not contain anAssociation, return 0." |
|
1130 | 755 |
|
1242 | 756 |
^self indexOfAssociation: anAssociation ifAbsent: [0] |
1130 | 757 |
! |
758 |
||
759 |
indexOfAssociation: anAssociation ifAbsent: exceptionBlock |
|
1242 | 760 |
"Return the identity index of anAssociation within the receiver. If the receiver |
761 |
does not contain anAssociation, return the result of evaluating the exceptionBlock." |
|
1130 | 762 |
|
1242 | 763 |
order keysAndValuesDo:[:i :key | |
764 |
(self associationAt:key) = anAssociation ifTrue: [^i] |
|
765 |
]. |
|
766 |
^exceptionBlock value |
|
1130 | 767 |
! |
768 |
||
769 |
indexOfKey: aKey |
|
1242 | 770 |
"Return the index of aKey within the receiver. If the receiver does |
771 |
not contain aKey, return 0." |
|
1130 | 772 |
|
1242 | 773 |
^self indexOfKey: aKey ifAbsent: [0] |
1130 | 774 |
! |
775 |
||
776 |
indexOfKey: aKey ifAbsent: exceptionBlock |
|
1242 | 777 |
"Return the identity index of aKey within the receiver. If the receiver does |
778 |
not contain aKey, return the result of evaluating the exceptionBlock." |
|
1130 | 779 |
|
1242 | 780 |
^ order indexOf:aKey ifAbsent:exceptionBlock |
1130 | 781 |
! |
782 |
||
783 |
indexOfValue: aValue |
|
1242 | 784 |
"Return the index of aValue within the receiver. |
785 |
If the receiver does not contain aValue, return 0." |
|
1130 | 786 |
|
1242 | 787 |
^self indexOfValue: aValue ifAbsent: [0] |
1130 | 788 |
! |
789 |
||
790 |
indexOfValue: aValue ifAbsent: exceptionBlock |
|
1242 | 791 |
"Return the identity index of aValue within the receiver. |
792 |
If the receiver does not contain aValue, return the result of evaluating the exceptionBlock." |
|
1130 | 793 |
|
1242 | 794 |
order keysAndValuesDo:[:i :key | (self at:key) = aValue ifTrue: [^i]]. |
795 |
^ exceptionBlock value |
|
1130 | 796 |
! |
797 |
||
798 |
nextIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
1242 | 799 |
"Return the next index of aAssociation within the receiver between startIndex |
800 |
and stopIndex. If the receiver does not contain aAssociation, return nil" |
|
1130 | 801 |
|
1242 | 802 |
startIndex to: stopIndex do: [:i | |
803 |
(self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
|
804 |
^nil |
|
1130 | 805 |
! |
806 |
||
807 |
nextIndexOfKey: aKey from: startIndex to: stopIndex |
|
1242 | 808 |
"Return the next index of aKey within the receiver between startIndex and |
809 |
stopIndex. If the receiver does not contain aKey, return nil" |
|
1130 | 810 |
|
1242 | 811 |
startIndex to: stopIndex do: [:i | |
812 |
(order at: i) = aKey ifTrue: [^i]]. |
|
813 |
^nil |
|
1130 | 814 |
! |
815 |
||
816 |
nextIndexOfValue: aValue from: startIndex to: stopIndex |
|
1242 | 817 |
"Return the next index of aValue within the receiver between startIndex and |
818 |
stopIndex. If the receiver does not contain aValue, return nil" |
|
1130 | 819 |
|
1242 | 820 |
startIndex to: stopIndex do: [:i | |
821 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
822 |
^nil |
|
1130 | 823 |
! |
824 |
||
825 |
prevIndexOfAssociation: aAssociation from: startIndex to: stopIndex |
|
1242 | 826 |
"Return the previous index of aAssociation within the receiver between |
827 |
startIndex and stopIndex working backwards through the receiver. |
|
828 |
If the receiver does not contain aAssociation, return nil" |
|
1130 | 829 |
|
1242 | 830 |
startIndex to: stopIndex by: -1 |
831 |
do: [:i | (self associationAt: (order at: i)) = aAssociation ifTrue: [^i]]. |
|
832 |
^nil |
|
1130 | 833 |
! |
834 |
||
835 |
prevIndexOfKey: aKey from: startIndex to: stopIndex |
|
1242 | 836 |
"Return the previous index of aKey within the receiver between startIndex and |
837 |
stopIndex working backwards through the receiver. |
|
838 |
If the receiver does not contain aKey, return nil" |
|
1130 | 839 |
|
1242 | 840 |
startIndex to: stopIndex by: -1 |
841 |
do: [:i | (order at: i) = aKey ifTrue: [^i]]. |
|
842 |
^nil |
|
1130 | 843 |
! |
844 |
||
845 |
prevIndexOfValue: aValue from: startIndex to: stopIndex |
|
1242 | 846 |
"Return the previous index of aValue within the receiver between startIndex |
847 |
and stopIndex working backwards through the receiver. |
|
848 |
If the receiver does not contain aValue, return nil" |
|
1130 | 849 |
|
1242 | 850 |
startIndex to: stopIndex by: -1 |
851 |
do: [:i | |
|
852 |
(self at: (order at: i)) = aValue ifTrue: [^i]]. |
|
853 |
^nil |
|
1130 | 854 |
! ! |
855 |
||
757 | 856 |
!OrderedDictionary methodsFor:'testing'! |
62 | 857 |
|
757 | 858 |
occurrencesOfKey: aKey |
1242 | 859 |
"Return how many of the dictionary's keys are equal to aKey." |
860 |
||
861 |
^ order count:[:eachKey | aKey = eachKey] |
|
62 | 862 |
|
1242 | 863 |
"/ cg: the original code was not very smalltalkish: |
864 |
"/ |
|
865 |
"/ | count | |
|
866 |
"/ |
|
867 |
"/ count := 0. |
|
868 |
"/ 1 to: self size do: [:index | aKey = (order at: index) ifTrue: [count := count + 1]]. |
|
869 |
"/ ^count |
|
959 | 870 |
! |
62 | 871 |
|
757 | 872 |
occurrencesOfValue: aValue |
1242 | 873 |
"Return how many of the dictionary's values are equal to aValue." |
874 |
||
875 |
^ order count:[:eachKey | aValue = (self at:eachKey)] |
|
62 | 876 |
|
1242 | 877 |
"/ cg: the original code was not very smalltalkish: |
878 |
"/ |
|
879 |
"/ | count | |
|
880 |
"/ |
|
881 |
"/ count := 0. |
|
882 |
"/ 1 to: self size do: [:index | |
|
883 |
"/ aValue = (self at: (order at: index)) ifTrue: [count := count + 1]]. |
|
884 |
"/ ^count |
|
959 | 885 |
! ! |
757 | 886 |
|
775
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
887 |
!OrderedDictionary class methodsFor:'documentation'! |
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
888 |
|
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
889 |
version |
1589
f0607bf986e0
fixed collect/associationsCollect:
Claus Gittinger <cg@exept.de>
parents:
1581
diff
changeset
|
890 |
^ '$Header: /cvs/stx/stx/libbasic2/Attic/OrderedDictionary.st,v 1.22 2006-01-25 12:22:23 cg Exp $' |
775
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
891 |
|
aafd903af15e
added #removeKey: and #removeFirst.
Claus Gittinger <cg@exept.de>
parents:
757
diff
changeset
|
892 |
! ! |