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