author | Claus Gittinger <cg@exept.de> |
Fri, 19 Apr 1996 12:47:25 +0200 | |
changeset 1221 | 46d72af387e9 |
parent 1188 | a9efdd724afc |
child 1245 | c8afea3d5af0 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Collection subclass:#SequenceableCollection |
|
911
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
14 |
instanceVariableNames:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
15 |
classVariableNames:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
16 |
poolDictionaries:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
17 |
category:'Collections-Abstract' |
1 | 18 |
! |
19 |
||
88 | 20 |
!SequenceableCollection class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
SequenceableCollections have ordered elements which can be accessed via |
|
39 |
a numeric index. |
|
40 |
SequenceableCollection is an abstract class - there are no instances of |
|
41 |
it in the system. See concrete subclasses (such as OrderedCollection). |
|
42 |
" |
|
43 |
! ! |
|
1 | 44 |
|
45 |
!SequenceableCollection class methodsFor:'instance creation'! |
|
46 |
||
47 |
new:size withAll:element |
|
360 | 48 |
"return a new collection of size, where all elements are |
49 |
initialized to element." |
|
1 | 50 |
|
51 |
|newCollection| |
|
52 |
||
53 |
newCollection := self new:size. |
|
54 |
newCollection atAllPut:element. |
|
55 |
^ newCollection |
|
360 | 56 |
! |
57 |
||
58 |
withSize:size |
|
59 |
"return a new collection of size. |
|
60 |
For variable size collections, this is different from #new:, |
|
61 |
in that #new: creates an empty collection with preallocated size, |
|
62 |
while #withSize: creates a non empty one." |
|
63 |
||
64 |
|newCollection| |
|
65 |
||
66 |
newCollection := self new:size. |
|
67 |
newCollection grow:size. |
|
68 |
^ newCollection |
|
69 |
||
70 |
" |
|
71 |
(OrderedCollection new:10) inspect. |
|
72 |
(OrderedCollection withSize:10) inspect. |
|
73 |
(Array new:10) inspect. |
|
74 |
(Array withSize:10) inspect. |
|
75 |
" |
|
1 | 76 |
! ! |
77 |
||
78 |
!SequenceableCollection methodsFor:'accessing'! |
|
79 |
||
32 | 80 |
at:index ifAbsent:exceptionBlock |
61 | 81 |
"return the element at index if valid. |
82 |
If the index is invalid, return the result of evaluating |
|
302 | 83 |
the exceptionblock. |
84 |
NOTICE: |
|
85 |
in ST-80, this message is only defined for Dictionaries, |
|
86 |
however, having a common protocol with indexed collections |
|
87 |
often simplifies things." |
|
32 | 88 |
|
89 |
((index < 1) or:[index > self size]) ifTrue:[ |
|
159 | 90 |
^ exceptionBlock value |
32 | 91 |
]. |
92 |
^ self at:index |
|
93 |
||
95 | 94 |
" |
95 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 96 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
97 |
||
95 | 98 |
(Dictionary with:(#foo -> #bar) |
159 | 99 |
with:(#frob -> #baz)) |
100 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 101 |
" |
362 | 102 |
! |
103 |
||
602 | 104 |
first |
105 |
"return the first element" |
|
106 |
||
107 |
^ self at:1 |
|
362 | 108 |
! |
109 |
||
110 |
keyAtEqualValue:value |
|
111 |
"return the index of a value. |
|
112 |
This is normally not used (use indexOf:), but makes the |
|
113 |
protocol more compatible with dictionaries." |
|
114 |
||
115 |
^ self indexOf:value |
|
116 |
! |
|
117 |
||
118 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
119 |
"return the index of a value. |
|
120 |
This is normally not used (use indexOf:), but makes the |
|
121 |
protocol more compatible with dictionaries." |
|
122 |
||
123 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 124 |
! |
125 |
||
126 |
keyAtValue:value |
|
127 |
"return the index of a value. |
|
128 |
This is normally not used (use indexOf:), but makes the |
|
129 |
protocol more compatible with dictionaries." |
|
130 |
||
131 |
^ self identityIndexOf:value |
|
132 |
! |
|
133 |
||
134 |
keyAtValue:value ifAbsent:exceptionBlock |
|
135 |
"return the index of a value. |
|
136 |
This is normally not used (use indexOf:), but makes the |
|
137 |
protocol more compatible with dictionaries." |
|
138 |
||
139 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
140 |
! |
|
141 |
||
142 |
last |
|
143 |
"return the last element" |
|
144 |
||
145 |
^ self at:(self size) |
|
1 | 146 |
! ! |
147 |
||
602 | 148 |
!SequenceableCollection methodsFor:'adding & removing'! |
149 |
||
150 |
add:anObject |
|
151 |
"append the argument, anObject to the collection. |
|
152 |
Return the argument, anObject. |
|
153 |
||
154 |
Notice, that this is modifies the receiver NOT a copy. |
|
155 |
Also note, that it may be a slow operation for some collections, |
|
156 |
due to the grow:-message, which is inefficient for fixed size |
|
157 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
158 |
||
159 |
|newSize| |
|
160 |
||
161 |
newSize := self size + 1. |
|
162 |
self grow:newSize. |
|
163 |
self at:newSize put:anObject. |
|
164 |
^ anObject |
|
165 |
||
166 |
" |
|
167 |
|a| |
|
168 |
a := #(1 2 3 4 5 6 7 8). |
|
169 |
a add:'hello'. |
|
170 |
a |
|
171 |
" |
|
172 |
" |
|
173 |
|c| |
|
174 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
175 |
c add:'hello'. |
|
176 |
c |
|
177 |
" |
|
178 |
! |
|
179 |
||
180 |
add:anElement beforeIndex:index |
|
181 |
"insert the first argument, anObject into the collection before slot index. |
|
182 |
Return the receiver (sigh - ST-80 compatibility). |
|
183 |
||
184 |
Notice, that this is modifies the receiver NOT a copy. |
|
185 |
Also note, that it may be a slow operation for some collections, |
|
186 |
due to the grow:-message, which is inefficient for fixed size |
|
187 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
188 |
||
189 |
|newSize| |
|
190 |
||
191 |
newSize := self size + 1. |
|
192 |
self grow:newSize. |
|
193 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
194 |
self at:index put:anElement |
|
195 |
||
196 |
" |
|
197 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
198 |
" |
|
202 | 199 |
! |
200 |
||
602 | 201 |
addFirst:anObject |
202 |
"prepend the argument, anObject to the collection. |
|
203 |
Return the argument, anObject. |
|
204 |
||
205 |
Notice, that this is modifies the receiver NOT a copy. |
|
206 |
Also note, that it may be a slow operation for some collections, |
|
207 |
due to the grow:-message, which is inefficient for fixed size |
|
208 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
209 |
||
210 |
|newSize| |
|
211 |
||
212 |
newSize := self size + 1. |
|
213 |
self grow:newSize. |
|
214 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
215 |
self at:1 put:anObject. |
|
216 |
^ anObject |
|
217 |
||
218 |
" |
|
219 |
|a| |
|
220 |
a:= #(1 2 3 4 5 6 7 8). |
|
221 |
a addFirst:'hello'. |
|
222 |
a |
|
223 |
" |
|
224 |
" |
|
225 |
|c| |
|
226 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
227 |
c addFirst:'hello'. |
|
228 |
c |
|
229 |
" |
|
69 | 230 |
! |
231 |
||
602 | 232 |
remove:anElement ifAbsent:aBlock |
233 |
"search for anElement and, if present remove and return it. |
|
234 |
If not present, return the value of evaluating aBlock. |
|
235 |
||
236 |
Notice, that this is modifies the receiver NOT a copy. |
|
237 |
Also note, that it may be a slow operation for some collections, |
|
238 |
due to the grow:-message, which is inefficient for fixed size |
|
239 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
240 |
||
241 |
|any |
|
242 |
dstIndex "{ Class: SmallInteger }" |
|
243 |
sz "{ Class: SmallInteger }"| |
|
244 |
||
245 |
dstIndex := 1. |
|
246 |
any := false. |
|
247 |
sz := self size. |
|
248 |
1 to:sz do:[:srcIndex | |
|
249 |
(anElement = (self at:srcIndex)) ifTrue:[ |
|
250 |
any := true |
|
251 |
] ifFalse:[ |
|
252 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
253 |
self at:dstIndex put:(self at:srcIndex) |
|
254 |
]. |
|
255 |
dstIndex := dstIndex + 1 |
|
256 |
] |
|
257 |
]. |
|
258 |
any ifTrue:[ |
|
259 |
self grow:dstIndex - 1. |
|
260 |
^ anElement |
|
261 |
]. |
|
262 |
^ aBlock value |
|
263 |
||
264 |
" |
|
265 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCr:'no'] |
|
266 |
#(1 2 3 4 5 6 7 8 9 0) remove:99 ifAbsent:[#oops] |
|
267 |
" |
|
268 |
! |
|
269 |
||
270 |
removeAtIndex:anIndex |
|
271 |
"remove the element stored at anIndex. Return the removed object. |
|
272 |
||
273 |
Notice, that this is modifies the receiver NOT a copy. |
|
274 |
Also note, that it may be a slow operation for some collections, |
|
275 |
due to the grow:-message, which is inefficient for fixed size |
|
276 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
277 |
||
278 |
|element| |
|
279 |
||
280 |
element := self at:anIndex. |
|
281 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
282 |
^ element |
|
283 |
||
284 |
" |
|
285 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
286 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
287 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
288 |
" |
|
13 | 289 |
! |
290 |
||
602 | 291 |
removeFirst |
292 |
"remove the first element of the receiver and return it. |
|
293 |
||
294 |
Notice, that this is modifies the receiver NOT a copy. |
|
295 |
Also note, that it may be a slow operation for some collections, |
|
296 |
due to the grow:-message, which is inefficient for fixed size |
|
297 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
298 |
||
299 |
^ self removeAtIndex:1 |
|
300 |
||
301 |
" |
|
302 |
|a| |
|
303 |
a := #(1 2 3 4 5 6). |
|
304 |
a removeFirst. |
|
305 |
a |
|
306 |
" |
|
13 | 307 |
! |
308 |
||
602 | 309 |
removeFromIndex:startIndex toIndex:endIndex |
310 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
311 |
Return the receiver. |
|
312 |
Returning the receiver is a historic leftover - it may at one |
|
313 |
time return a collection of the removed elements. |
|
314 |
||
315 |
Notice, that this is modifies the receiver - NOT a copy; |
|
316 |
therefore any other users of the receiver will also see this change. |
|
317 |
Also note, that it may be a slow operation for some collections, |
|
318 |
due to the grow:-message, which is inefficient for fixed size |
|
319 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
320 |
||
321 |
|size newSize| |
|
322 |
||
323 |
(endIndex >= (size := self size)) ifTrue:[ |
|
324 |
self grow:(startIndex - 1) |
|
325 |
] ifFalse:[ |
|
326 |
newSize := size - endIndex + startIndex - 1. |
|
327 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
328 |
self grow:newSize |
|
329 |
] |
|
330 |
||
331 |
"/ |newSize| |
|
332 |
"/ |
|
333 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
334 |
"/ newSize <= 0 ifTrue:[ |
|
335 |
"/ self grow:0 |
|
336 |
"/ ] ifFalse:[ |
|
337 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
338 |
"/ self grow:newSize |
|
339 |
"/ ] |
|
340 |
||
341 |
" |
|
342 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
|
343 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
344 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
345 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
346 |
" |
|
347 |
! |
|
348 |
||
349 |
removeIndex:index |
|
350 |
"remove the argument stored at index. Return the receiver. |
|
351 |
||
352 |
Notice, that this is modifies the receiver NOT a copy. |
|
353 |
Also note, that it may be a slow operation for some collections, |
|
354 |
due to the grow:-message, which is inefficient for fixed size |
|
355 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
356 |
||
357 |
self removeFromIndex:index toIndex:index |
|
358 |
||
359 |
" |
|
360 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
361 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
362 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
363 |
" |
|
77 | 364 |
! |
365 |
||
602 | 366 |
removeLast |
367 |
"remove the last element of the receiver and return it. |
|
368 |
||
369 |
Notice, that this is modifies the receiver NOT a copy. |
|
370 |
Also note, that it may be a slow operation for some collections, |
|
371 |
due to the grow:-message, which is inefficient for fixed size |
|
372 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
373 |
||
374 |
^ self removeAtIndex:(self size) |
|
375 |
||
376 |
" |
|
377 |
|a| |
|
378 |
a := #(1 2 3 4 5 6). |
|
379 |
a removeLast. |
|
380 |
a |
|
381 |
" |
|
382 |
! ! |
|
383 |
||
384 |
!SequenceableCollection methodsFor:'comparing'! |
|
385 |
||
386 |
= aCollection |
|
387 |
"return true if the receiver and aCollection represent collections |
|
388 |
with equal contents." |
|
389 |
||
390 |
|index "{ Class: SmallInteger }" |
|
391 |
stop "{ Class: SmallInteger }" | |
|
392 |
||
393 |
(aCollection == self) ifTrue:[^true]. |
|
394 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
395 |
||
396 |
stop := self size. |
|
397 |
stop == (aCollection size) ifFalse:[^false]. |
|
398 |
||
399 |
index := 1. |
|
400 |
[index <= stop] whileTrue:[ |
|
401 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
402 |
index := index + 1 |
|
403 |
]. |
|
404 |
^ true |
|
405 |
||
406 |
" |
|
407 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
|
408 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
409 |
#($1 $2 $3 $4 $5) = '12345' |
|
410 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
411 |
" |
|
412 |
! |
|
413 |
||
414 |
endsWith:aCollection |
|
415 |
"return true, if the receivers last elements match those |
|
416 |
of aCollection" |
|
417 |
||
418 |
|index1 "{ Class: SmallInteger }" |
|
419 |
index2 "{ Class: SmallInteger }" |
|
420 |
stop "{ Class: SmallInteger }" |
|
421 |
sz "{ Class: SmallInteger }"| |
|
422 |
||
423 |
(aCollection == self) ifTrue:[^ true]. |
|
424 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
425 |
||
426 |
stop := aCollection size. |
|
77 | 427 |
sz := self size. |
602 | 428 |
stop > sz ifTrue:[^false]. |
429 |
||
430 |
index1 := sz. |
|
431 |
index2 := stop. |
|
432 |
[index2 > 0] whileTrue:[ |
|
433 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
|
434 |
index1 := index1 - 1. |
|
435 |
index2 := index2 - 1 |
|
77 | 436 |
]. |
602 | 437 |
^ true |
438 |
||
439 |
" |
|
440 |
'abcde' endsWith:#($d $e) |
|
441 |
#[1 2 3 4] endsWith:#(3 4) |
|
442 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
|
443 |
" |
|
444 |
! |
|
445 |
||
446 |
hash |
|
447 |
"return a hash key for the receiver" |
|
448 |
||
449 |
"this hash is stupid - but for larger collections, the hashing |
|
450 |
time can become much bigger than the time lost in added probing. |
|
451 |
Time will show ..." |
|
452 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
453 |
^ ((self at:1 ifAbsent:0) hash * self size) bitAnd:16r3FFFFFFF |
602 | 454 |
|
455 |
" |
|
456 |
#(1 2 3 4 5) hash |
|
457 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
458 |
" |
|
459 |
! |
|
460 |
||
461 |
startsWith:aCollection |
|
462 |
"return true, if the receivers first elements match those |
|
463 |
of aCollection" |
|
464 |
||
465 |
|index "{ Class: SmallInteger }" |
|
466 |
stop "{ Class: SmallInteger }" | |
|
467 |
||
468 |
(aCollection == self) ifTrue:[^true]. |
|
469 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
470 |
||
471 |
stop := aCollection size. |
|
472 |
stop > self size ifTrue:[^false]. |
|
473 |
||
474 |
index := 1. |
|
475 |
[index <= stop] whileTrue:[ |
|
476 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
477 |
index := index + 1 |
|
478 |
]. |
|
479 |
^ true |
|
480 |
||
481 |
" |
|
482 |
'abcde' startsWith:#($a $b $c) |
|
483 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
484 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
485 |
" |
|
13 | 486 |
! ! |
487 |
||
260 | 488 |
!SequenceableCollection methodsFor:'converting'! |
489 |
||
1188 | 490 |
asStringWith:sepChar |
491 |
"return a string generated by concatenating my elements |
|
492 |
(which must be strings or nil) and embedding sepChar characters in between. |
|
493 |
Nil entries and empty strings are counted as empty lines." |
|
494 |
||
495 |
^ self asStringWith:sepChar from:1 to:(self size) |
|
496 |
! |
|
497 |
||
498 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
499 |
"return part of myself as a string with embedded sepCharacters. |
|
500 |
My elements must be strings or nil; nil entries and empty strings are |
|
501 |
taken as empty lines." |
|
502 |
||
503 |
^ self |
|
504 |
asStringWith:sepCharacter |
|
505 |
from:firstLine |
|
506 |
to:lastLine |
|
507 |
compressTabs:false |
|
508 |
final:nil |
|
509 |
" |
|
510 |
creating entries for searchpath: |
|
511 |
||
512 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
|
513 |
||
514 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
515 |
" |
|
516 |
||
517 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
518 |
! |
|
519 |
||
260 | 520 |
asStringWith:sepCharacter from:firstLine to:lastLine compressTabs:compressTabs final:endCharacter |
521 |
"return part of myself as a string with embedded sepCharacters. |
|
263 | 522 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 523 |
taken as empty lines. |
524 |
If the argument compressTabs is true, leading spaces are converted |
|
525 |
to tab-characters (8col tabs). The last line is followed by a final |
|
526 |
character (if non-nil). |
|
527 |
||
528 |
This method is tuned for big collections, in not creating many |
|
263 | 529 |
intermediate strings (has linear runtime). For very small collections |
530 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 531 |
|
293 | 532 |
|idx1 "{ Class:SmallInteger }" |
533 |
idx2 "{ Class:SmallInteger }" |
|
534 |
totalLength "{ Class:SmallInteger }" |
|
260 | 535 |
pos "{ Class:SmallInteger }" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
536 |
newString lineString spaces idx nTabs sepCnt |
1015 | 537 |
any16Bit stringClass| |
260 | 538 |
|
539 |
" |
|
540 |
first accumulate the size of the string, to avoid |
|
541 |
countless reallocations. If tabs are compressed, |
|
542 |
the size computed is not exact, but gives an upper bound ... |
|
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
543 |
On the fly, look if a 16bit string is needed. |
260 | 544 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
545 |
any16Bit := false. |
1015 | 546 |
stringClass := String. |
547 |
||
260 | 548 |
totalLength := 0. |
549 |
sepCnt := sepCharacter notNil ifTrue:[1] ifFalse:[0]. |
|
550 |
||
293 | 551 |
idx1 := firstLine. |
552 |
idx2 := lastLine. |
|
553 |
idx1 to:idx2 do:[:lineIndex | |
|
1015 | 554 |
lineString := self at:lineIndex. |
555 |
||
556 |
lineString isNil ifTrue:[ |
|
557 |
totalLength := totalLength + sepCnt |
|
558 |
] ifFalse: [ |
|
559 |
(lineString bitsPerCharacter == 16) ifTrue:[ |
|
560 |
any16Bit := true. |
|
1016 | 561 |
stringClass := lineString class |
1015 | 562 |
]. |
563 |
totalLength := totalLength + lineString size + sepCnt |
|
564 |
]. |
|
260 | 565 |
]. |
566 |
endCharacter isNil ifTrue:[ |
|
1015 | 567 |
totalLength := totalLength - 1 |
260 | 568 |
]. |
569 |
spaces := ' '. |
|
1016 | 570 |
newString := stringClass new:totalLength. |
260 | 571 |
|
572 |
" |
|
573 |
now, replace ... |
|
574 |
" |
|
575 |
pos := 1. |
|
293 | 576 |
idx1 to:idx2 do:[:lineIndex | |
1015 | 577 |
|thisLen| |
578 |
||
579 |
lineString := self at:lineIndex. |
|
580 |
thisLen := lineString size. |
|
581 |
thisLen ~~ 0 ifTrue:[ |
|
582 |
compressTabs ifTrue:[ |
|
583 |
" |
|
584 |
mhmh: could use withTabs from String-class here, |
|
585 |
but we should avoid creating too many temporary strings |
|
586 |
(especially, since this method is typically used when converting |
|
587 |
big texts such as when saving in the filebrowser ...). |
|
588 |
Therefore, we convert tabs inline here doing a direct replace |
|
589 |
in newString." |
|
590 |
||
591 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
592 |
nTabs := (idx-1) // 8. |
|
593 |
nTabs ~~ 0 ifTrue:[ |
|
594 |
"any tabs" |
|
595 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
596 |
pos := pos + nTabs. |
|
597 |
newString replaceFrom:pos with:lineString startingAt:(nTabs * 8 + 1). |
|
598 |
pos := pos + thisLen - (nTabs * 8). |
|
599 |
] ifFalse:[ |
|
600 |
newString replaceFrom:pos with:lineString. |
|
601 |
pos := pos + thisLen |
|
602 |
]. |
|
603 |
] ifFalse:[ |
|
604 |
newString replaceFrom:pos with:lineString. |
|
605 |
pos := pos + thisLen |
|
606 |
] |
|
607 |
]. |
|
608 |
(sepCharacter notNil |
|
609 |
and:[lineIndex ~~ lastLine]) ifTrue:[ |
|
610 |
newString at:pos put:sepCharacter. |
|
611 |
pos := pos + 1 |
|
612 |
] ifFalse:[ |
|
613 |
endCharacter notNil ifTrue:[ |
|
614 |
newString at:pos put:endCharacter. |
|
615 |
pos := pos + 1 |
|
616 |
] |
|
617 |
] |
|
260 | 618 |
]. |
619 |
||
620 |
" |
|
621 |
in case of tab compression, the result has to be |
|
622 |
cut to size ... sorry |
|
623 |
" |
|
624 |
pos ~~ totalLength ifTrue:[ |
|
1015 | 625 |
^ newString copyTo:(pos - 1) |
260 | 626 |
]. |
627 |
^ newString |
|
628 |
||
629 |
" |
|
630 |
creating entries for searchpath: |
|
631 |
||
632 |
#('foo' 'bar' 'baz' '/foo/bar') |
|
1015 | 633 |
asStringWith:$: from:1 to:4 compressTabs:false final:nil |
260 | 634 |
|
635 |
with trailing colon: |
|
636 |
||
637 |
#('foo' 'bar' 'baz' '/foo/bar') |
|
1015 | 638 |
asStringWith:$: from:1 to:4 compressTabs:false final:$: |
260 | 639 |
|
640 |
concatenating all elements: |
|
641 |
||
642 |
#('foo' 'bar' 'baz') |
|
1015 | 643 |
asStringWith:nil from:1 to:3 compressTabs:false final:nil |
260 | 644 |
|
645 |
creating a string from a collection of lines: |
|
646 |
||
647 |
#('foo' 'bar' 'baz') |
|
1015 | 648 |
asStringWith:(Character cr) from:1 to:3 compressTabs:false final:(Character cr) |
260 | 649 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
650 |
|
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
651 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
379 | 652 |
! |
653 |
||
602 | 654 |
asStringWithCRs |
655 |
"return a string generated by concatenating my elements |
|
656 |
(which must be strings or nil) and embedding cr characters in between. |
|
657 |
Nil entries and empty strings are counted as empty lines." |
|
658 |
||
659 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
660 |
! |
|
661 |
||
662 |
asStringWithCRsFrom:firstLine to:lastLine |
|
663 |
"return a string generated by concatenating some of my elements |
|
664 |
(which must be strings or nil) and embedding cr characters in between. |
|
665 |
Nil entries and empty strings are counted as empty lines." |
|
666 |
||
667 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
668 |
! |
|
669 |
||
670 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
671 |
"return part of myself as a string with embedded cr's. |
|
672 |
My elements must be strings or nil. |
|
673 |
If the argument compressTabs is true, leading spaces are converted |
|
674 |
to tab-characters (8col tabs). |
|
675 |
Nil entries and empty strings are taken as empty lines." |
|
676 |
||
677 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
678 |
! |
|
679 |
||
680 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
681 |
"return part of myself as a string with embedded cr's. |
|
682 |
My elements must be strings or nil; nil entries and empty strings are |
|
683 |
taken as empty lines. |
|
684 |
If the argument compressTabs is true, leading spaces are converted |
|
685 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
686 |
should be followed by a cr or not." |
|
687 |
||
688 |
^ self asStringWith:(Character cr) |
|
689 |
from:firstLine |
|
690 |
to:lastLine |
|
691 |
compressTabs:compressTabs |
|
692 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
693 |
! |
|
694 |
||
379 | 695 |
decodeAsLiteralArray |
696 |
|cls| |
|
697 |
||
698 |
cls := Smalltalk at:(self first) ifAbsent:nil. |
|
699 |
cls isNil ifTrue:[ |
|
700 |
^ self error:'unknown class in spec:' , self first. |
|
701 |
]. |
|
702 |
^ cls new fromLiteralArrayEncoding:self. |
|
703 |
||
704 |
" |
|
705 |
#(Point 10 20) decodeAsLiteralArray |
|
706 |
#(Rectangle 100 200 400 500) decodeAsLiteralArray |
|
707 |
" |
|
422 | 708 |
! |
709 |
||
710 |
literalArrayEncoding |
|
711 |
^ (self collect:[:el | el literalArrayEncoding]) asArray |
|
712 |
||
713 |
" |
|
714 |
(Array with:(Color red:50 green:50 blue:50) |
|
715 |
with:(1 @ 2) |
|
716 |
) literalArrayEncoding |
|
717 |
" |
|
260 | 718 |
! ! |
719 |
||
1 | 720 |
!SequenceableCollection methodsFor:'copying'! |
721 |
||
722 |
, aCollection |
|
723 |
"return a new collection formed from concatenating the receiver with |
|
44 | 724 |
the argument. The class of the new collection is determined by the |
725 |
receivers class, so mixing classes is possible, if the second collections |
|
726 |
elements can be stored into instances of the receivers class." |
|
1 | 727 |
|
728 |
|newCollection |
|
729 |
mySize "{ Class: SmallInteger }" |
|
730 |
newSize "{ Class: SmallInteger }" |
|
731 |
otherSize "{ Class: SmallInteger }" |
|
732 |
dstIndex "{ Class: SmallInteger }"| |
|
733 |
||
734 |
mySize := self size. |
|
735 |
otherSize := aCollection size. |
|
736 |
newSize := mySize + otherSize. |
|
202 | 737 |
|
252 | 738 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 739 |
|
740 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
741 |
dstIndex := mySize + 1. |
|
202 | 742 |
aCollection isSequenceable ifTrue:[ |
743 |
" |
|
744 |
yes, aCollection has indexed elements |
|
745 |
" |
|
159 | 746 |
newCollection replaceFrom:dstIndex to:newSize |
747 |
with:aCollection startingAt:1. |
|
748 |
^ newCollection |
|
1 | 749 |
] ifFalse:[ |
202 | 750 |
" |
751 |
no, enumerate aCollection |
|
752 |
" |
|
159 | 753 |
aCollection do:[:element | |
754 |
newCollection at:dstIndex put:element. |
|
755 |
dstIndex := dstIndex + 1 |
|
756 |
] |
|
1 | 757 |
]. |
758 |
^ newCollection |
|
44 | 759 |
|
95 | 760 |
" |
761 |
#($a $b $c) , #(1 2 3) |
|
762 |
#($a $b $c) , '123' |
|
763 |
'abc' , '123' |
|
764 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
765 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
766 |
'abc' asArray , #(1 2 3 4 5) |
|
767 |
" |
|
1 | 768 |
! |
769 |
||
602 | 770 |
copyFirst:count |
771 |
"return a new collection consisting of the receivers first count |
|
772 |
elements - this is just a rename of copyTo: - for compatibility." |
|
773 |
||
774 |
^ self copyFrom:1 to:count |
|
775 |
||
776 |
" |
|
777 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
778 |
'1234567890' copyFirst:4 |
|
779 |
" |
|
780 |
! |
|
781 |
||
782 |
copyFrom:start |
|
783 |
"return a new collection consisting of receivers elements |
|
784 |
from start to the end of the collection" |
|
785 |
||
786 |
^ self copyFrom:start to:(self size) |
|
787 |
||
788 |
" |
|
789 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
790 |
'1234567890' copyFrom:2 |
|
791 |
" |
|
792 |
! |
|
793 |
||
794 |
copyFrom:start to:stop |
|
795 |
"return a new collection consisting of receivers elements |
|
796 |
between start and stop" |
|
797 |
||
798 |
|newCollection newSize| |
|
799 |
||
800 |
newSize := stop - start + 1. |
|
801 |
newCollection := self copyEmptyAndGrow:newSize. |
|
802 |
newCollection replaceFrom:1 to:newSize with:self startingAt:start. |
|
803 |
^ newCollection |
|
804 |
||
805 |
" |
|
806 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
807 |
'1234567890' copyFrom:2 to:5 |
|
808 |
" |
|
809 |
! |
|
810 |
||
811 |
copyLast:count |
|
812 |
"return a new collection consisting of the receivers last count |
|
813 |
elements." |
|
814 |
||
815 |
|sz| |
|
816 |
||
817 |
sz := self size. |
|
818 |
^ self copyFrom:(sz - count + 1) to:sz |
|
819 |
||
820 |
" |
|
821 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
822 |
'1234567890' copyLast:4 |
|
823 |
" |
|
824 |
! |
|
825 |
||
826 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
|
827 |
"return a copy of the receiver, where the elements from startIndex to |
|
828 |
endIndex have been replaced by the elements of aCollection" |
|
829 |
||
830 |
|newColl sz replSize| |
|
831 |
||
832 |
replSize := aCollection size. |
|
833 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
834 |
newColl := self copyEmptyAndGrow:sz. |
|
835 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
836 |
newColl replaceFrom:startIndex with:aCollection. |
|
837 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
838 |
^ newColl |
|
839 |
||
840 |
" |
|
841 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 to:6 with:#(a b c d e f g h i j k) |
|
842 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
|
843 |
" |
|
844 |
! |
|
845 |
||
846 |
copyReplaceFrom:startIndex with:aCollection |
|
847 |
"return a copy of the receiver, where the elements from startIndex to |
|
848 |
the end have been replaced by the elements of aCollection" |
|
849 |
||
850 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
851 |
||
852 |
" |
|
853 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
|
854 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
855 |
" |
|
856 |
! |
|
857 |
||
858 |
copyThrough:element |
|
859 |
"return a new collection consisting of the receiver elements |
|
860 |
up-to (AND including) the first occurence of element." |
|
861 |
||
862 |
|idx| |
|
863 |
||
864 |
idx := self indexOf:element. |
|
865 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
866 |
^ self copyFrom:1 to:idx |
|
867 |
||
868 |
" |
|
869 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
870 |
'1234567890' copyThrough:$5 |
|
871 |
'1234567890' copyThrough:$a |
|
872 |
'1234567890' copyThrough:$1 |
|
873 |
" |
|
874 |
! |
|
875 |
||
876 |
copyTo:stop |
|
877 |
"return a new collection consisting of receivers elements |
|
878 |
from 1 up to (including) index stop" |
|
879 |
||
880 |
^ self copyFrom:1 to:stop |
|
881 |
||
882 |
" |
|
883 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
884 |
'1234567890' copyTo:4 |
|
885 |
" |
|
886 |
! |
|
887 |
||
888 |
copyUpTo:element |
|
889 |
"return a new collection consisting of the receiver elements |
|
890 |
up-to (but excluding) the first occurence of element; |
|
891 |
or to the end, if element is not included" |
|
892 |
||
893 |
|idx| |
|
894 |
||
895 |
idx := self indexOf:element. |
|
896 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
897 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
898 |
^ self copyFrom:1 to:(idx-1) |
|
899 |
||
900 |
" |
|
901 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
902 |
'1234567890' copyUpTo:$5 |
|
903 |
'1234567890' copyUpTo:$a |
|
904 |
'1234567890' copyUpTo:$1 |
|
905 |
'123456789' copyUpTo:$0 |
|
906 |
" |
|
907 |
! |
|
908 |
||
1 | 909 |
copyWith:newElement |
233 | 910 |
"return a new collection containing the receivers elements |
911 |
and the single new element, newElement. |
|
912 |
This is different from concatentation, which expects another collection |
|
913 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 914 |
|
915 |
|newCollection mySize newSize| |
|
916 |
||
917 |
mySize := self size. |
|
918 |
newSize := mySize + 1. |
|
252 | 919 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 920 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
921 |
newCollection at:newSize put:newElement. |
|
922 |
^newCollection |
|
44 | 923 |
|
95 | 924 |
" |
925 |
#(1 2 3 4 5) copyWith:$a |
|
926 |
'abcdefg' copyWith:$h |
|
927 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
928 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
929 |
" |
|
1 | 930 |
! |
931 |
||
61 | 932 |
copyWithout:elementToSkip |
933 |
"return a new collection consisting of a copy of the receiver, with |
|
934 |
ALL elements equal to elementToSkip are left out. |
|
935 |
No error is reported, if elementToSkip is not in the collection." |
|
936 |
||
293 | 937 |
|n "{ Class: SmallInteger }" |
938 |
sz "{ Class: SmallInteger }" |
|
939 |
srcIndex "{ Class: SmallInteger }" |
|
940 |
dstIndex "{ Class: SmallInteger }" |
|
941 |
skipIndex "{ Class: SmallInteger }" |
|
942 |
copy l| |
|
61 | 943 |
|
944 |
"the code below may look like overkill, |
|
945 |
however, for big collections its better to move data |
|
946 |
around in big chunks" |
|
947 |
||
948 |
n := self occurrencesOf:elementToSkip. |
|
949 |
n == 0 ifTrue:[^ self copy]. |
|
950 |
||
951 |
sz := self size. |
|
252 | 952 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 953 |
|
954 |
srcIndex := 1. |
|
955 |
dstIndex := 1. |
|
956 |
||
957 |
n timesRepeat:[ |
|
159 | 958 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
959 |
l := skipIndex - srcIndex. |
|
960 |
l ~~ 0 ifTrue:[ |
|
961 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
962 |
with:self startingAt:srcIndex. |
|
963 |
dstIndex := dstIndex + l |
|
964 |
]. |
|
965 |
srcIndex := skipIndex + 1 |
|
61 | 966 |
]. |
967 |
l := sz - srcIndex. |
|
968 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
159 | 969 |
with:self startingAt:srcIndex. |
61 | 970 |
^ copy |
971 |
||
95 | 972 |
" |
973 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
974 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
975 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
202 | 976 |
'abcdefghi' copyWithout:$h |
977 |
'abcdefg' copyWithout:$h |
|
95 | 978 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
979 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
980 |
#(90 80 70 60 50) copyWithout:70 |
|
981 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
982 |
" |
|
61 | 983 |
! |
984 |
||
602 | 985 |
copyWithoutFirst:elementToSkip |
986 |
"return a new collection consisting of a copy of the receivers elements |
|
987 |
without the first elementToSkip, if it was present. |
|
988 |
No error is reported, if elementToSkip is not in the collection." |
|
989 |
||
990 |
|copy skipIndex sz| |
|
991 |
||
992 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
993 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
994 |
||
995 |
sz := self size - 1. |
|
996 |
copy := self copyEmptyAndGrow:sz. |
|
997 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
998 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
999 |
^ copy |
|
1000 |
||
1001 |
" |
|
1002 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
1003 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
1004 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
1005 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
1006 |
" |
|
1007 |
! |
|
1008 |
||
61 | 1009 |
copyWithoutIndex:omitIndex |
1010 |
"return a new collection consisting of receivers elements |
|
1011 |
without the argument stored at omitIndex" |
|
1012 |
||
1013 |
|copy sz| |
|
1014 |
||
202 | 1015 |
sz := self size - 1. |
252 | 1016 |
copy := self copyEmptyAndGrow:sz. |
293 | 1017 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
1018 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 1019 |
^ copy |
1020 |
||
95 | 1021 |
" |
1022 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
1023 |
'abcdefghijkl' copyWithoutIndex:5 |
|
1024 |
" |
|
1 | 1025 |
! |
1026 |
||
202 | 1027 |
copyWithoutLast:count |
1028 |
"return a new collection consisting of the receivers elements |
|
1029 |
except the last count elements." |
|
1030 |
||
1031 |
|sz| |
|
1032 |
||
1033 |
sz := self size. |
|
1034 |
^ self copyTo:(sz - count) |
|
1035 |
||
1036 |
" |
|
1037 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
|
1038 |
'1234567890' copyWithoutLast:4 |
|
1039 |
" |
|
602 | 1040 |
! ! |
1041 |
||
1042 |
!SequenceableCollection methodsFor:'enumerating'! |
|
1043 |
||
1044 |
collect:aBlock |
|
1045 |
"evaluate the argument, aBlock for every element in the collection |
|
1046 |
and return a collection of the results" |
|
1047 |
||
1048 |
|newCollection |
|
1049 |
sz "{ Class:SmallInteger }"| |
|
1050 |
||
1051 |
sz := self size. |
|
1052 |
newCollection := self copyEmptyAndGrow:sz. |
|
1053 |
1 to:sz do:[:index | |
|
1054 |
newCollection at:index put:(aBlock value:(self at:index)). |
|
1055 |
]. |
|
1056 |
^ newCollection |
|
1057 |
||
1058 |
" |
|
1059 |
#(one two three four five six) collect:[:element | element asUppercase] |
|
1060 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
1061 |
(1 to:9) collect:[:element | element * element] |
|
1062 |
" |
|
202 | 1063 |
! |
1064 |
||
602 | 1065 |
do:aBlock |
1066 |
"evaluate the argument, aBlock for every element in the collection." |
|
1067 |
||
1068 |
|stop "{ Class:SmallInteger }"| |
|
1069 |
||
1070 |
stop := self size. |
|
1071 |
1 to:stop do:[:index | |
|
1072 |
aBlock value:(self at:index). |
|
1073 |
] |
|
1074 |
" |
|
1075 |
#(one two three four five six) do:[:element | Transcript showCr:element] |
|
1076 |
" |
|
1077 |
! |
|
1078 |
||
1079 |
from:startIndex do:aBlock |
|
1080 |
"evaluate the argument, aBlock for the elements starting with the |
|
1081 |
element at startIndex to the end." |
|
1082 |
||
1083 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 1084 |
|
95 | 1085 |
" |
602 | 1086 |
#(one two three four five six) from:3 do:[:element | Transcript showCr:element] |
1087 |
" |
|
1088 |
! |
|
1089 |
||
1090 |
from:start to:stop collect:aBlock |
|
1091 |
"evaluate the argument, aBlock for the elements indexed by start |
|
1092 |
to stop in the collection and return a collection of the results" |
|
1093 |
||
1094 |
|newCollection sz |
|
1095 |
idx "{ Class:SmallInteger }"| |
|
1096 |
||
1097 |
sz := stop - start + 1. |
|
1098 |
newCollection := self copyEmptyAndGrow:sz. |
|
1099 |
idx := 1. |
|
1100 |
start to:stop do:[:index | |
|
1101 |
newCollection at:idx put:(aBlock value:(self at:index)). |
|
1102 |
idx := idx + 1 |
|
1103 |
]. |
|
1104 |
^ newCollection |
|
1105 |
||
1106 |
" |
|
1107 |
#(one two three four five six) from:2 to:4 collect:[:element | element asUppercase] |
|
1108 |
" |
|
1109 |
! |
|
1110 |
||
1111 |
from:index1 to:index2 do:aBlock |
|
1112 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1113 |
index2 in the collection" |
|
1114 |
||
1115 |
|start "{ Class:SmallInteger }" |
|
1116 |
stop "{ Class:SmallInteger }" | |
|
1117 |
||
1118 |
start := index1. |
|
1119 |
stop := index2. |
|
1120 |
start to:stop do:[:index | |
|
1121 |
aBlock value:(self at:index). |
|
1122 |
] |
|
1123 |
||
1124 |
" |
|
1125 |
#(one two three four five six) from:3 to:5 do:[:element | Transcript showCr:element] |
|
1126 |
" |
|
1127 |
! |
|
1128 |
||
1129 |
from:index1 to:index2 reverseDo:aBlock |
|
1130 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1131 |
index2 in the collection. Step in reverse order" |
|
1132 |
||
1133 |
|start "{ Class:SmallInteger }" |
|
1134 |
stop "{ Class:SmallInteger }" | |
|
1135 |
||
1136 |
start := index1. |
|
1137 |
stop := index2. |
|
1138 |
stop to:start by:-1 do:[:index | |
|
1139 |
aBlock value:(self at:index). |
|
1140 |
] |
|
1141 |
||
1142 |
" |
|
1143 |
#(one two three four five six) from:3 to:5 reverseDo:[:element | Transcript showCr:element] |
|
95 | 1144 |
" |
61 | 1145 |
! |
1146 |
||
602 | 1147 |
keysAndValuesDo:aTwoArgBlock |
1148 |
"evaluate the argument, aBlock for every element in the collection, |
|
1149 |
passing both index and element as arguments." |
|
1150 |
||
1151 |
|stop "{ Class:SmallInteger }"| |
|
1152 |
||
1153 |
stop := self size. |
|
1154 |
1 to:stop do:[:index | |
|
1155 |
aTwoArgBlock value:index value:(self at:index). |
|
1156 |
] |
|
1157 |
" |
|
1158 |
#(one two three four five six) keysAndValuesDo:[:key :element | Transcript show:key; space; showCr:element] |
|
1159 |
" |
|
1160 |
! |
|
1161 |
||
1162 |
nonNilElementsDo:aBlock |
|
1163 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
1164 |
||
1165 |
|stop "{ Class:SmallInteger }" |
|
1166 |
element| |
|
1167 |
||
1168 |
stop := self size. |
|
1169 |
1 to:stop do:[:index | |
|
1170 |
(element := self at:index) notNil ifTrue:[ |
|
1171 |
aBlock value:element. |
|
1172 |
] |
|
1173 |
] |
|
1174 |
" |
|
1175 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCr:element] |
|
1176 |
" |
|
1177 |
! |
|
1178 |
||
1179 |
pairWiseCollect:aBlock |
|
1180 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1181 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1182 |
An error will be reported, if the number of elements in the receiver |
|
1183 |
is not a multiple of 2. |
|
1184 |
Collect the results and return a new collection containing those." |
|
1185 |
||
1186 |
|stop newCollection dstIdx "{ Class:SmallInteger }"| |
|
1187 |
||
1188 |
stop := self size. |
|
1189 |
newCollection := self copyEmptyAndGrow:stop // 2. |
|
1190 |
dstIdx := 1. |
|
1191 |
1 to:stop by:2 do:[:index | |
|
1192 |
newCollection at:dstIdx put:(aBlock value:(self at:index) value:(self at:index+1)). |
|
1193 |
dstIdx := dstIdx + 1 |
|
1194 |
]. |
|
1195 |
^ newCollection |
|
44 | 1196 |
|
95 | 1197 |
" |
602 | 1198 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
1199 |
pairWiseCollect:[:num :sym | sym->num] |
|
1200 |
||
1201 |
||
1202 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1203 |
pairWiseCollect:[:x :y | x@y] |
|
1204 |
" |
|
1205 |
! |
|
1206 |
||
1207 |
pairWiseDo:aBlock |
|
1208 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1209 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1210 |
An error will be reported, if the number of elements in the receiver |
|
1211 |
is not a multiple of 2." |
|
1212 |
||
1213 |
|stop "{ Class:SmallInteger }"| |
|
1214 |
||
1215 |
stop := self size. |
|
1216 |
1 to:stop by:2 do:[:index | |
|
1217 |
aBlock value:(self at:index) value:(self at:index+1). |
|
1218 |
] |
|
1219 |
" |
|
1220 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
1221 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCr:sym] |
|
1222 |
||
1223 |
||
1224 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1225 |
pairWiseDo:[:x :y | Transcript showCr:x@y] |
|
95 | 1226 |
" |
61 | 1227 |
! |
1228 |
||
602 | 1229 |
reverseDo:aBlock |
1230 |
"evaluate the argument, aBlock for every element in the collection |
|
1231 |
in reverse order" |
|
1232 |
||
1233 |
|sz "{ Class:SmallInteger }"| |
|
1234 |
||
1235 |
sz := self size. |
|
1236 |
sz to:1 by:-1 do:[:index | |
|
1237 |
aBlock value:(self at:index). |
|
1238 |
] |
|
1239 |
||
1240 |
" |
|
1241 |
#(one two three four five six) reverseDo:[:element | Transcript showCr:element] |
|
1242 |
" |
|
1243 |
! |
|
1244 |
||
1245 |
select:aBlock |
|
1246 |
"evaluate the argument, aBlock for every element in the collection |
|
1247 |
and return a collection of all elements for which the block return |
|
1248 |
true" |
|
1249 |
||
1250 |
|element newColl species needCopy |
|
1251 |
sz "{ Class:SmallInteger }"| |
|
1252 |
||
1253 |
sz := self size. |
|
1254 |
species := self species. |
|
1255 |
species growIsCheap ifFalse:[ |
|
1256 |
newColl := OrderedCollection new:sz. |
|
1257 |
needCopy := true |
|
1258 |
] ifTrue:[ |
|
1259 |
newColl := self copyEmpty:sz. |
|
1260 |
needCopy := false |
|
1261 |
]. |
|
1262 |
1 to:sz do:[:index | |
|
1263 |
element := self at:index. |
|
1264 |
(aBlock value:element) ifTrue:[ |
|
1265 |
newColl add:element |
|
1266 |
]. |
|
1267 |
]. |
|
1268 |
needCopy ifTrue:[ |
|
1269 |
newColl := (species withAll:newColl) postCopyFrom:self |
|
1270 |
]. |
|
61 | 1271 |
^ newColl |
1272 |
||
95 | 1273 |
" |
602 | 1274 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
1275 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
1276 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
1277 |
(1 to:9) select:[:element | element odd] |
|
1278 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 1279 |
" |
186 | 1280 |
! |
1281 |
||
602 | 1282 |
with:aSequenceableCollection do:aTwoArgBlock |
1283 |
"evaluate the argument, aBlock for successive elements from |
|
1284 |
each the receiver and the argument, aSequenceableCollection. |
|
1285 |
The second argument, aBlock must be a two-argument block. |
|
1286 |
The collection argument must implement access via a numeric key." |
|
1287 |
||
1288 |
|stop "{ Class: SmallInteger }" | |
|
1289 |
||
1290 |
stop := self size. |
|
1291 |
1 to:stop do:[:index | |
|
1292 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
|
1293 |
] |
|
186 | 1294 |
" |
602 | 1295 |
#(one two three four five six) with:(1 to:10) do:[:el1 :el2 | Transcript show:el1; space; showCr:el2] |
186 | 1296 |
" |
1 | 1297 |
! ! |
1298 |
||
1299 |
!SequenceableCollection methodsFor:'filling and replacing'! |
|
1300 |
||
602 | 1301 |
atAll:indexCollection put:anObject |
1302 |
"put anObject into all indexes from indexCollection in the receiver. |
|
1303 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1304 |
therefore the change may affect all others referencing the receiver." |
|
1305 |
||
1306 |
indexCollection do:[:index | self at:index put:anObject] |
|
1307 |
||
1308 |
" |
|
1309 |
(Array new:10) atAll:(1 to:5) put:0 |
|
1310 |
(Array new:10) atAll:#(1 5 6 9) put:0 |
|
1311 |
" |
|
1312 |
! |
|
1313 |
||
1314 |
atAllPut:anObject |
|
1315 |
"replace all elements of the collection by the argument, anObject. |
|
1316 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1317 |
therefore the change may affect all others referencing the receiver." |
|
1318 |
||
1319 |
self from:1 to:(self size) put:anObject |
|
1320 |
||
1321 |
" |
|
1322 |
(Array new:10) atAllPut:1 |
|
1323 |
(String new:10) atAllPut:$a |
|
1324 |
" |
|
1325 |
! |
|
1326 |
||
1 | 1327 |
from:index1 to:index2 put:anObject |
1328 |
"replace the elements from index1 to index2 of the collection |
|
44 | 1329 |
by the argument, anObject. |
1330 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1331 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1332 |
|
1333 |
|index "{ Class: SmallInteger }" |
|
1334 |
end "{ Class: SmallInteger }"| |
|
1335 |
||
1336 |
index := index1. |
|
1337 |
end := index2. |
|
1338 |
[index <= end] whileTrue:[ |
|
159 | 1339 |
self at:index put:anObject. |
1340 |
index := index + 1 |
|
1 | 1341 |
] |
44 | 1342 |
|
95 | 1343 |
" |
1344 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
|
1345 |
'abcdefghijkl' from:3 to:6 put:$X |
|
1346 |
" |
|
1 | 1347 |
! |
1348 |
||
1349 |
replaceAll:oldObject by:newObject |
|
44 | 1350 |
"replace all oldObjects by newObject in the receiver. |
1351 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1352 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1353 |
|
362 | 1354 |
^ self replaceAll:oldObject by:newObject from:1 to:self size |
1355 |
||
1356 |
" |
|
1357 |
'123123abc123' replaceAll:$1 by:$* |
|
1358 |
" |
|
1359 |
! |
|
293 | 1360 |
|
362 | 1361 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
1362 |
"replace all oldObjects found between startIndex and endIndex, |
|
1363 |
by newObject in the receiver. |
|
1364 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1365 |
therefore the change may affect all others referencing the receiver." |
|
1366 |
||
1367 |
|start "{ Class: SmallInteger }" |
|
1368 |
stop "{ Class: SmallInteger }"| |
|
1369 |
||
1370 |
start := startIndex. |
|
1371 |
stop := stopIndex. |
|
1372 |
startIndex to:stopIndex do:[:index | |
|
159 | 1373 |
(self at:index) = oldObject ifTrue:[ |
1374 |
self at:index put:newObject |
|
1375 |
] |
|
1 | 1376 |
] |
44 | 1377 |
|
95 | 1378 |
" |
362 | 1379 |
'123123abc123' replaceAll:$1 by:$* |
1380 |
'123123abc123' replaceAll:$1 by:$* from:1 to:6 |
|
95 | 1381 |
" |
1382 |
! |
|
1383 |
||
602 | 1384 |
replaceAll:oldObject with:newObject |
1385 |
"ST-80 compatibility: an alias for #replaceAll:by:" |
|
1386 |
||
1387 |
^ self replaceAll:oldObject by:newObject |
|
1388 |
! |
|
1389 |
||
1390 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
|
1391 |
"ST-80 compatibility: an alias for #replaceAll:by:from:to:" |
|
1392 |
||
1393 |
^ self replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
1394 |
! |
|
1395 |
||
95 | 1396 |
replaceAny:aCollection by:newObject |
1397 |
"replace all elements, which are in aCollection by newObject in the receiver. |
|
1398 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1399 |
therefore the change may affect all others referencing the receiver." |
|
1400 |
||
362 | 1401 |
^ self replaceAny:aCollection by:newObject from:1 to:self size |
1402 |
||
1403 |
" |
|
1404 |
'123123abc123' replaceAny:#($1 $2) by:$* |
|
1405 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) by:'*' |
|
1406 |
" |
|
1407 |
! |
|
293 | 1408 |
|
362 | 1409 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
1410 |
"replace all elements within a range, |
|
1411 |
which are in contained in aCollection by newObject. |
|
1412 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1413 |
therefore the change may affect all others referencing the receiver." |
|
1414 |
||
1415 |
|start "{ Class: SmallInteger }" |
|
1416 |
stop "{ Class: SmallInteger }"| |
|
1417 |
||
1418 |
start := startIndex. |
|
1419 |
stop := stopIndex. |
|
1420 |
startIndex to:stopIndex do:[:index | |
|
159 | 1421 |
(aCollection includes:(self at:index)) ifTrue:[ |
1422 |
self at:index put:newObject |
|
1423 |
] |
|
95 | 1424 |
] |
1425 |
||
1426 |
" |
|
362 | 1427 |
'123123abc123' replaceAny:#($1 $2) by:$* from:1 to:6 |
95 | 1428 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) by:'*' |
1429 |
" |
|
1 | 1430 |
! |
1431 |
||
1432 |
replaceFrom:start to:stop with:replacementCollection |
|
44 | 1433 |
"replace elements in the receiver between index start and stop, |
1434 |
with elements taken from replacementCollection starting at 1. |
|
1435 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1436 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1437 |
|
1438 |
^ self replaceFrom:start |
|
159 | 1439 |
to:stop |
1440 |
with:replacementCollection |
|
1441 |
startingAt:1 |
|
61 | 1442 |
|
95 | 1443 |
" |
1444 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
1445 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
1446 |
" |
|
1 | 1447 |
! |
1448 |
||
1449 |
replaceFrom:start to:stop with:replacementCollection startingAt:repStart |
|
44 | 1450 |
"replace elements in the receiver between index start and stop, |
1451 |
with elements taken from replacementCollection starting at repStart. |
|
1452 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1453 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1454 |
|
1455 |
|srcIndex "{ Class: SmallInteger }" |
|
1456 |
dstIndex "{ Class: SmallInteger }" |
|
1457 |
end "{ Class: SmallInteger }" | |
|
1458 |
||
293 | 1459 |
(repStart < start) ifTrue:[ |
1460 |
" must do reverse copy " |
|
1461 |
srcIndex := repStart + (stop - start). |
|
1462 |
dstIndex := stop. |
|
1463 |
end := start. |
|
1464 |
[dstIndex >= end] whileTrue:[ |
|
1465 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
1466 |
srcIndex := srcIndex - 1. |
|
1467 |
dstIndex := dstIndex - 1 |
|
1468 |
]. |
|
1469 |
^ self |
|
1 | 1470 |
]. |
1471 |
||
1472 |
srcIndex := repStart. |
|
1473 |
dstIndex := start. |
|
1474 |
end := stop. |
|
1475 |
[dstIndex <= end] whileTrue:[ |
|
159 | 1476 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
1477 |
srcIndex := srcIndex + 1. |
|
1478 |
dstIndex := dstIndex + 1 |
|
1 | 1479 |
] |
61 | 1480 |
|
95 | 1481 |
" |
282 | 1482 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
1483 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1484 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1485 |
||
1486 |
|c| |
|
1487 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1488 |
c replaceFrom:2 to:3 with:c startingAt:4 |
|
1489 |
||
1490 |
|c| |
|
1491 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1492 |
c replaceFrom:4 to:5 with:c startingAt:2 |
|
95 | 1493 |
" |
61 | 1494 |
! |
1495 |
||
602 | 1496 |
replaceFrom:start with:replacementCollection |
1497 |
"replace elements in the receiver starting at start, |
|
1498 |
with elements taken from replacementCollection starting at 1 |
|
1499 |
to the end of replacementCollection. |
|
1500 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1501 |
therefore the change may affect all others referencing the receiver." |
|
1502 |
||
1503 |
^ self replaceFrom:start |
|
1504 |
to:(start + replacementCollection size - 1) |
|
1505 |
with:replacementCollection |
|
1506 |
startingAt:1 |
|
1507 |
||
1508 |
" |
|
1509 |
'1234567890' replaceFrom:5 with:'abc' |
|
1510 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
1511 |
" |
|
1512 |
! |
|
1513 |
||
1514 |
replaceFrom:start with:replacementCollection startingAt:offset |
|
1515 |
"replace elements in the receiver starting at start, |
|
1516 |
with elements taken from replacementCollection starting at offset |
|
1517 |
to the end of replacementCollection. |
|
1518 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1519 |
therefore the change may affect all others referencing the receiver." |
|
1520 |
||
1521 |
^ self replaceFrom:start |
|
1522 |
to:(start + replacementCollection size - offset) |
|
1523 |
with:replacementCollection |
|
1524 |
startingAt:offset |
|
1525 |
||
1526 |
" |
|
1527 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
|
1528 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
|
1529 |
" |
|
1530 |
! |
|
1531 |
||
61 | 1532 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
1533 |
"replace elements in destColl with elements from the receiver. |
|
1534 |
Notice: This operation modifies the destination collection, NOT a copy; |
|
1535 |
therefore the change may affect all others referencing this object." |
|
1536 |
||
1537 |
destColl replaceFrom:destStartIndex to:destEndIndex with:self startingAt:sourceStart |
|
1538 |
||
1539 |
" |
|
1540 |
|s| |
|
1541 |
s := 'abcdefghijklmnop'. |
|
1542 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
1543 |
s'123defghijklmnop' |
|
1544 |
" |
|
1 | 1545 |
! ! |
1546 |
||
602 | 1547 |
!SequenceableCollection methodsFor:'queries'! |
1548 |
||
1549 |
firstIndex |
|
1550 |
"return the first elements index" |
|
1551 |
||
1552 |
^ 1 |
|
1553 |
! |
|
1554 |
||
1555 |
isSequenceable |
|
1556 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
1557 |
||
1558 |
^ true |
|
1 | 1559 |
! |
1560 |
||
602 | 1561 |
isSequenceableCollection |
1562 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
1563 |
This method is a historic leftover and will be removed." |
|
1564 |
||
1565 |
^ true |
|
1 | 1566 |
! |
1567 |
||
602 | 1568 |
keys |
1569 |
"return a collection with all keys in the Smalltalk dictionary" |
|
1570 |
||
1571 |
|sz| |
|
1572 |
||
1 | 1573 |
sz := self size. |
602 | 1574 |
sz == 0 ifTrue:[ |
1575 |
^ #() |
|
61 | 1576 |
]. |
602 | 1577 |
^ 1 to:sz |
1 | 1578 |
! |
1579 |
||
602 | 1580 |
lastIndex |
1581 |
"return the last elements index" |
|
1582 |
||
1583 |
^ self size |
|
61 | 1584 |
! |
1585 |
||
602 | 1586 |
size |
1587 |
"return the number of elements in the collection. |
|
1588 |
concrete implementations must define this" |
|
1589 |
||
1590 |
^ self subclassResponsibility |
|
1 | 1591 |
! ! |
1592 |
||
1593 |
!SequenceableCollection methodsFor:'searching'! |
|
1594 |
||
1595 |
detect:aBlock ifNone:exceptionBlock |
|
1596 |
"find the first element, for which evaluation of the argument, aBlock |
|
1597 |
return true; if none does so, return the evaluation of exceptionBlock |
|
1598 |
||
1599 |
reimplemented here for speed" |
|
1600 |
||
32 | 1601 |
|stop "{ Class: SmallInteger }" |
1 | 1602 |
element| |
1603 |
||
32 | 1604 |
stop := self size. |
1605 |
1 to:stop do:[:index | |
|
159 | 1606 |
element := self at:index. |
1607 |
(aBlock value:element) ifTrue:[ |
|
1608 |
^ element |
|
1609 |
]. |
|
1 | 1610 |
]. |
1611 |
^ exceptionBlock value |
|
159 | 1612 |
|
1613 |
" |
|
1614 |
#(11 12 13 14) detect:[:n | n odd] ifNone:['sorry'] |
|
1615 |
#(12 14 16 18) detect:[:n | n odd] ifNone:['sorry'] |
|
1616 |
" |
|
1 | 1617 |
! |
1618 |
||
602 | 1619 |
findFirst:aBlock |
1620 |
"find the first element, for which evaluation of the argument, aBlock |
|
1621 |
returns true; return its index or 0 if none detected." |
|
1622 |
||
1623 |
|stop "{ Class: SmallInteger }" | |
|
1624 |
||
32 | 1625 |
stop := self size. |
602 | 1626 |
1 to:stop do:[:index | |
1627 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
1 | 1628 |
]. |
1629 |
^ 0 |
|
159 | 1630 |
|
95 | 1631 |
" |
602 | 1632 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
1633 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
1634 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
1635 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
95 | 1636 |
" |
1 | 1637 |
! |
1638 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1639 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1640 |
"find the first element, for which evaluation of the argument, aBlock |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1641 |
returns true; return its index or 0 if none detected." |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1642 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1643 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1644 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1645 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1646 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1647 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1648 |
start to:stop do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1649 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1650 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1651 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1652 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1653 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1654 |
#(1 4 3 4 3 6) findFirst:[:x | (x > 3)] startingAt:4 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1655 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1656 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1657 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1658 |
|
602 | 1659 |
findLast:aBlock |
1660 |
"find the last element, for which evaluation of the argument, aBlock |
|
1661 |
returns true; return its index or 0 if none detected." |
|
1662 |
||
1663 |
|start "{ Class: SmallInteger }"| |
|
1664 |
||
1665 |
start := self size. |
|
1666 |
start to:1 by:-1 do:[:index | |
|
1667 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
252 | 1668 |
]. |
1669 |
^ 0 |
|
1670 |
||
1671 |
" |
|
602 | 1672 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
1673 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 1674 |
" |
1675 |
! |
|
1676 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1677 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1678 |
"find the last element, for which evaluation of the argument, aBlock |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1679 |
returns true; return its index or 0 if none detected." |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1680 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1681 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1682 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1683 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1684 |
start to:1 by:-1 do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1685 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1686 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1687 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1688 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1689 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1690 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1691 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1692 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1693 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1694 |
|
1 | 1695 |
identityIndexOf:anElement |
1696 |
"search the collection for anElement using identity compare (i.e. ==); |
|
1697 |
if found, return the index otherwise return 0." |
|
1698 |
||
32 | 1699 |
^ self identityIndexOf:anElement startingAt:1 |
44 | 1700 |
|
95 | 1701 |
" |
159 | 1702 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
1703 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 |
|
1704 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
360 | 1705 |
|
1706 |
be careful: |
|
1707 |
||
1708 |
#(10 20 30 40.0 50 60 70) indexOf:40.0 |
|
1709 |
#(10 20 30 40.0 50 60 70) identityIndexOf:40.0 |
|
1710 |
||
95 | 1711 |
" |
1 | 1712 |
! |
1713 |
||
1714 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
|
1715 |
"search the collection for anElement using identity compare (i.e. ==); |
|
1716 |
if found, return the index otherwise return the value of the |
|
1717 |
exceptionBlock." |
|
1718 |
||
1719 |
|index| |
|
1720 |
||
32 | 1721 |
index := self identityIndexOf:anElement startingAt:1. |
1 | 1722 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
1723 |
^ index |
|
159 | 1724 |
|
1725 |
" |
|
1726 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
1727 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
1728 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
1729 |
#(10 20 30 40 50 60 70) indexOf:40.0 ifAbsent:['none'] |
|
1730 |
" |
|
1 | 1731 |
! |
1732 |
||
1733 |
identityIndexOf:anElement startingAt:start |
|
159 | 1734 |
"search the collection for anElement, starting search at index start |
1 | 1735 |
using identity compare (i.e. ==); |
1736 |
if found, return the index otherwise return 0." |
|
1737 |
||
32 | 1738 |
|startIndex "{ Class: SmallInteger }" |
1739 |
stop "{ Class: SmallInteger }" | |
|
1 | 1740 |
|
32 | 1741 |
startIndex := start. |
1742 |
stop := self size. |
|
1743 |
startIndex to:stop do:[:index | |
|
159 | 1744 |
anElement == (self at:index) ifTrue:[^ index]. |
1 | 1745 |
]. |
1746 |
^ 0 |
|
44 | 1747 |
|
95 | 1748 |
" |
159 | 1749 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40 startingAt:5 |
1750 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
|
1751 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
95 | 1752 |
" |
1 | 1753 |
! |
1754 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1755 |
identityIndexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1756 |
"search the collection for anElement, starting the search at index start; |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1757 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1758 |
If found (within the range), return the index, otherwise return 0. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1759 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1760 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1761 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1762 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1763 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1764 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1765 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1766 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1767 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1768 |
anElement == (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1769 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1770 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1771 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1772 |
"Created: 12.4.1996 / 18:23:07 / cg" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1773 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1774 |
|
1 | 1775 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
159 | 1776 |
"search the collection for anElement, starting search at index start; |
1 | 1777 |
if found, return the index otherwise return the value of the |
1778 |
exceptionBlock. |
|
1779 |
This one searches for identical objects (i.e. ==)." |
|
1780 |
||
1781 |
|index| |
|
1782 |
||
1783 |
index := self identityIndexOf:anElement startingAt:start. |
|
1784 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
1785 |
^ index |
|
159 | 1786 |
|
1787 |
" |
|
1788 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
1789 |
#(10 20 30 40 10) identityIndexOf:35 startingAt:3 ifAbsent:['none'] |
|
1790 |
" |
|
1791 |
! |
|
1792 |
||
602 | 1793 |
includes:anElement |
1794 |
"return true if the collection contains anElement; false otherwise. |
|
1795 |
Comparison is done using equality compare (i.e. =). |
|
1796 |
See #includesIdentical: if identity is asked for." |
|
1797 |
||
1798 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
1799 |
^ true |
|
1800 |
||
1801 |
" |
|
1802 |
#(10 20 30 40 50 60 70) includes:99 |
|
1803 |
#(10 20 30 40 50 60 70) includes:40 |
|
1804 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
1805 |
" |
|
1806 |
! |
|
1807 |
||
1808 |
includesIdentical:anElement |
|
1809 |
"return true if the collection contains anElement; false otherwise. |
|
1810 |
Comparison is done using identity compare (i.e. ==). |
|
1811 |
See #includes: if equality is asked for." |
|
1812 |
||
1813 |
((self identityIndexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
1814 |
^ true |
|
1815 |
||
1816 |
" |
|
1817 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
1818 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
1819 |
#(10 20 30 40 50 60 70) includes:40 |
|
1820 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
1821 |
||
1822 |
be careful: |
|
1823 |
||
1824 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
|
1825 |
#(10 20 30 40.0 50 60 70) includesIdentical:40.0 |
|
1826 |
" |
|
1827 |
! |
|
1828 |
||
1829 |
indexOf:anElement |
|
1830 |
"search the collection for anElement; |
|
1831 |
if found, return the index otherwise return 0. |
|
1832 |
The comparison is done using = |
|
1833 |
(i.e. equality test - not identity test)." |
|
1834 |
||
1835 |
^ self indexOf:anElement startingAt:1 |
|
1836 |
||
1837 |
" |
|
1838 |
#(10 20 30 40 50 60 70) indexOf:40 |
|
1839 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
1840 |
" |
|
1841 |
! |
|
1842 |
||
1843 |
indexOf:anElement ifAbsent:exceptionBlock |
|
1844 |
"search the collection for anElement; |
|
1845 |
if found, return the index otherwise return the value of the |
|
1846 |
exceptionBlock. |
|
1847 |
The comparison is done using = |
|
1848 |
(i.e. equality test - not identity test)." |
|
1849 |
||
1850 |
|index| |
|
1851 |
||
1852 |
index := self indexOf:anElement startingAt:1. |
|
1853 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
1854 |
^ index |
|
1855 |
||
1856 |
" |
|
1857 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
1858 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
1859 |
#(10 20 30 40 10 20 30 40) indexOf:35 ifAbsent:['none'] |
|
1860 |
" |
|
1861 |
! |
|
1862 |
||
1863 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
|
1864 |
"search for the first occurence of elementToFind starting at start, |
|
1865 |
stopping the search at stop. If found, replace the element by replacement |
|
1866 |
and return the index. |
|
1867 |
If not found, return 0." |
|
1868 |
||
1869 |
|idx| |
|
1870 |
||
1871 |
idx := self indexOf:elementToFind startingAt:start. |
|
1872 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
|
1873 |
self at:idx put:replacement. |
|
1874 |
^ idx |
|
1875 |
]. |
|
1876 |
^ 0 |
|
1877 |
||
1878 |
" |
|
1879 |
|a| |
|
1880 |
||
1881 |
a := #(10 20 30 40 50 60 70). |
|
1882 |
(a indexOf:30 replaceWith:nil startingAt:1 stoppingAt:7) printNL. |
|
1883 |
a printNL. |
|
1884 |
" |
|
1885 |
! |
|
1886 |
||
1887 |
indexOf:anElement startingAt:start |
|
1888 |
"search the collection for anElement, starting the search at index start; |
|
1889 |
if found, return the index otherwise return 0. |
|
1890 |
The comparison is done using = |
|
1891 |
(i.e. equality test - not identity test)." |
|
1892 |
||
1893 |
|startIndex "{ Class: SmallInteger }" |
|
1894 |
stop "{ Class: SmallInteger }" | |
|
1895 |
||
1896 |
startIndex := start. |
|
1897 |
stop := self size. |
|
1898 |
startIndex to:stop do:[:index | |
|
1899 |
anElement = (self at:index) ifTrue:[^ index]. |
|
1900 |
]. |
|
1901 |
^ 0 |
|
1902 |
||
1903 |
" |
|
1904 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
1905 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
1906 |
" |
|
1907 |
! |
|
1908 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1909 |
indexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1910 |
"search the collection for anElement, starting the search at index start; |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1911 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1912 |
If found (within the range), return the index, otherwise return 0. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1913 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1914 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1915 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1916 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1917 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1918 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1919 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1920 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1921 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1922 |
anElement = (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1923 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1924 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1925 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1926 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1927 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1928 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1929 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1930 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1931 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1932 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1933 |
"Created: 12.4.1996 / 17:37:13 / cg" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1934 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1935 |
|
602 | 1936 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
1937 |
"search the collection for anElement starting the search at index start; |
|
1938 |
if found, return the index otherwise return the value of the |
|
1939 |
exceptionBlock. |
|
1940 |
The comparison is done using = |
|
1941 |
(i.e. equality test - not identity test)." |
|
1942 |
||
1943 |
|index| |
|
1944 |
||
1945 |
index := self indexOf:anElement startingAt:start. |
|
1946 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
1947 |
^ index |
|
1948 |
||
1949 |
" |
|
1950 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
1951 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
1952 |
#(10 20 30 40 10 20 30 40) indexOf:35 startingAt:5 ifAbsent:['none'] |
|
1953 |
" |
|
1954 |
! |
|
1955 |
||
159 | 1956 |
indexOfAny:aCollection |
1957 |
"search the collection for an element in aCollection. |
|
1958 |
if found, return the index otherwise return 0. |
|
1959 |
The comparison is done using = |
|
1960 |
(i.e. equality test - not identity test). |
|
1961 |
||
1962 |
Notice, that for big collections, the runtime of this search |
|
1963 |
grows proportional to size(receiver) * size(aCollection). |
|
1964 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
1965 |
||
1966 |
^ self indexOfAny:aCollection startingAt:1 |
|
1967 |
||
1968 |
" |
|
1969 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
1970 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
1971 |
" |
|
1972 |
! |
|
1973 |
||
1974 |
indexOfAny:aCollection startingAt:start |
|
1975 |
"search the collection for an element in aCollection, |
|
1976 |
starting the search at index start; |
|
1977 |
if found, return the index otherwise return 0. |
|
1978 |
The comparison is done using = |
|
1979 |
(i.e. equality test - not identity test). |
|
1980 |
||
1981 |
Notice, that for big collections, the runtime of this search |
|
1982 |
grows proportional to size(receiver) * size(aCollection). |
|
1983 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
1984 |
||
1985 |
||
1986 |
|startIndex "{ Class: SmallInteger }" |
|
1987 |
stop "{ Class: SmallInteger }" | |
|
1988 |
||
1989 |
startIndex := start. |
|
1990 |
stop := self size. |
|
1991 |
startIndex to:stop do:[:index | |
|
1992 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
|
1993 |
]. |
|
1994 |
^ 0 |
|
1995 |
" |
|
1996 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
|
1997 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
1998 |
" |
|
1 | 1999 |
! |
2000 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2001 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2002 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2003 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2004 |
if found, return the index. If not, return the value returned by exceptionBlock. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2005 |
The comparison is done using = |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2006 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2007 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2008 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2009 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2010 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2011 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2012 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2013 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2014 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2015 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2016 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2017 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2018 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2019 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2020 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2021 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2022 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(99 88 77) startingAt:5 ifAbsent:['oops'] |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2023 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2024 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2025 |
|
368 | 2026 |
indexOfSubCollection:aCollection |
2027 |
"find a subcollection. |
|
2028 |
If found, return the index; if not found, return 0." |
|
2029 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2030 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 2031 |
|
2032 |
" |
|
2033 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
|
2034 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
2035 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
2036 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
2037 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
2038 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
2039 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
2040 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
2041 |
" |
|
2042 |
! |
|
2043 |
||
602 | 2044 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
2045 |
"find a subcollection. If found, return the index; |
|
2046 |
if not found, return the result of evaluating exceptionBlock." |
|
2047 |
||
2048 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
2049 |
! |
|
2050 |
||
368 | 2051 |
indexOfSubCollection:aCollection startingAt:startIndex |
2052 |
"find a subcollection starting at index. |
|
2053 |
If found, return the index; if not found, return 0." |
|
2054 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2055 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 2056 |
! |
2057 |
||
2058 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
2059 |
"find a subcollection, starting at index. If found, return the index; |
|
2060 |
if not found, return the result of evaluating exceptionBlock. |
|
2061 |
This is a q&d hack - not very efficient" |
|
2062 |
||
2063 |
|same first |
|
2064 |
mySize "{Class: SmallInteger }" |
|
2065 |
checkIndex "{Class: SmallInteger }" |
|
2066 |
cmpIndex "{Class: SmallInteger }" |
|
2067 |
sz "{Class: SmallInteger }"| |
|
2068 |
||
2069 |
mySize := self size. |
|
2070 |
sz := aCollection size. |
|
2071 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
2072 |
first := aCollection at:1. |
|
2073 |
||
2074 |
checkIndex := startIndex - 1. |
|
2075 |
[true] whileTrue:[ |
|
2076 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
|
2077 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
2078 |
||
2079 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
2080 |
^ 0 |
|
2081 |
]. |
|
2082 |
same := true. |
|
2083 |
cmpIndex := 1. |
|
2084 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
2085 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
2086 |
same := false |
|
2087 |
] ifTrue:[ |
|
2088 |
cmpIndex := cmpIndex + 1 |
|
2089 |
] |
|
2090 |
]. |
|
2091 |
same ifTrue:[^ checkIndex]. |
|
2092 |
] |
|
2093 |
||
2094 |
" |
|
2095 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
2096 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
2097 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
2098 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
2099 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
2100 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
2101 |
" |
|
602 | 2102 |
! |
2103 |
||
2104 |
lastIndexOf:anElement |
|
2105 |
"search the collection backwards for anElement; |
|
2106 |
if found, return the index otherwise return 0. |
|
2107 |
The comparison is done using = |
|
2108 |
(i.e. equality test - not identity test)." |
|
2109 |
||
2110 |
^ self lastIndexOf:anElement startingAt:self size |
|
2111 |
||
2112 |
" |
|
2113 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
2114 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
2115 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
2116 |
#(10 20 30 40 50 60 70) lastIndexOf:10 |
|
2117 |
" |
|
2118 |
! |
|
2119 |
||
2120 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
|
2121 |
"search the collection backwards for anElement; |
|
2122 |
if found, return the index otherwise return the value of the |
|
2123 |
exceptionBlock. |
|
2124 |
The comparison is done using = |
|
2125 |
(i.e. equality test - not identity test)." |
|
2126 |
||
2127 |
|index| |
|
2128 |
||
2129 |
index := self lastIndexOf:anElement startingAt:self size. |
|
2130 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2131 |
^ index |
|
2132 |
||
2133 |
" |
|
2134 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
|
2135 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
2136 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
2137 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 ifAbsent:['none'] |
|
2138 |
" |
|
2139 |
! |
|
2140 |
||
2141 |
lastIndexOf:anElement startingAt:start |
|
2142 |
"search the collection backwards for anElement, starting the search at index start; |
|
2143 |
if found, return the index otherwise return 0. |
|
2144 |
The comparison is done using = |
|
2145 |
(i.e. equality test - not identity test)." |
|
2146 |
||
2147 |
|startIndex "{ Class: SmallInteger }"| |
|
2148 |
||
2149 |
startIndex := start. |
|
2150 |
startIndex to:1 by:-1 do:[:index | |
|
2151 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2152 |
]. |
|
2153 |
^ 0 |
|
2154 |
||
2155 |
" |
|
2156 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
|
2157 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
2158 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
2159 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
2160 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:4 |
|
2161 |
" |
|
2162 |
! |
|
2163 |
||
2164 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
|
2165 |
"search the collection backwards for anElement starting the search at |
|
2166 |
index start; if found, return the index |
|
2167 |
otherwise return the value of the exceptionBlock. |
|
2168 |
The comparison is done using = |
|
2169 |
(i.e. equality test - not identity test)." |
|
2170 |
||
2171 |
|index| |
|
2172 |
||
2173 |
index := self lastIndexOf:anElement startingAt:start. |
|
2174 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2175 |
^ index |
|
2176 |
||
2177 |
" |
|
2178 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
2179 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
2180 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
2181 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 ifAbsent:['none'] |
|
2182 |
" |
|
2183 |
! |
|
2184 |
||
2185 |
nextIndexOf:anElement from:start to:stop |
|
2186 |
"search the collection for anElement, starting the search at index start, |
|
2187 |
stopping at:stop; |
|
2188 |
if found, return the index otherwise return 0. |
|
2189 |
The comparison is done using = |
|
2190 |
(i.e. equality test - not identity test)." |
|
2191 |
||
2192 |
|startIndex "{ Class: SmallInteger }" |
|
2193 |
stopIndex "{ Class: SmallInteger }" | |
|
2194 |
||
2195 |
startIndex := start. |
|
2196 |
stopIndex := stop. |
|
2197 |
startIndex to:stop do:[:index | |
|
2198 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2199 |
]. |
|
2200 |
^ 0 |
|
2201 |
||
2202 |
" |
|
2203 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
2204 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
2205 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
2206 |
" |
|
2207 |
! |
|
2208 |
||
2209 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
2210 |
"search the collection for anElement, starting the search at index start |
|
2211 |
and stopping at stop; |
|
2212 |
if found, return the index otherwise return the value of the |
|
2213 |
exceptionBlock. |
|
2214 |
The comparison is done using = |
|
2215 |
(i.e. equality test - not identity test)." |
|
2216 |
||
2217 |
|index| |
|
2218 |
||
2219 |
index := self nextIndexOf:anElement from:start to:stop. |
|
2220 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2221 |
^ index |
|
2222 |
||
2223 |
" |
|
2224 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
2225 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
2226 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
2227 |
" |
|
1 | 2228 |
! ! |
2229 |
||
2230 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
2231 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2232 |
quickSortFrom:inBegin to:inEnd |
1 | 2233 |
"actual quicksort worker for sort-message" |
2234 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2235 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2236 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2237 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2238 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2239 |
middleElement temp1 temp2 | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2240 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2241 |
begin := inBegin. "/ this also does a type-check |
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2242 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2243 |
|
1 | 2244 |
b := begin. |
2245 |
e := end. |
|
2246 |
middleElement := self at:((b + e) // 2). |
|
2247 |
||
2248 |
[b < e] whileTrue:[ |
|
159 | 2249 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
2250 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
1 | 2251 |
|
159 | 2252 |
(b <= e) ifTrue:[ |
2253 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2254 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2255 |
self at:b put:temp2. self at:e put:temp1 |
159 | 2256 |
]. |
2257 |
b := b + 1. |
|
2258 |
e := e - 1 |
|
2259 |
] |
|
1 | 2260 |
]. |
2261 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e]. |
|
2262 |
(b < end) ifTrue:[self quickSortFrom:b to:end] |
|
2263 |
! |
|
2264 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2265 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
1 | 2266 |
"actual quicksort worker for sort:-message" |
2267 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2268 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2269 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2270 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2271 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2272 |
middleElement temp1 temp2 | |
1 | 2273 |
|
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2274 |
begin := inBegin. "/ this also does a type-check |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2275 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2276 |
|
1 | 2277 |
b := begin. |
2278 |
e := end. |
|
2279 |
middleElement := self at:((b + e) // 2). |
|
2280 |
||
2281 |
[b < e] whileTrue:[ |
|
159 | 2282 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
2283 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
1 | 2284 |
|
159 | 2285 |
(b <= e) ifTrue:[ |
2286 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2287 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2288 |
self at:b put:temp2. self at:e put:temp1 |
159 | 2289 |
]. |
2290 |
b := b + 1. |
|
2291 |
e := e - 1 |
|
2292 |
] |
|
1 | 2293 |
]. |
2294 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock]. |
|
2295 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock] |
|
2296 |
! |
|
2297 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2298 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
1 | 2299 |
"actual quicksort worker for sort:with:-message" |
2300 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2301 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2302 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2303 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2304 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2305 |
middleElement temp1 temp2 | |
1 | 2306 |
|
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2307 |
begin := inBegin. "/ this also does a type-check |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2308 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2309 |
|
1 | 2310 |
b := begin. |
2311 |
e := end. |
|
2312 |
middleElement := self at:((b + e) // 2). |
|
2313 |
||
2314 |
[b < e] whileTrue:[ |
|
159 | 2315 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
2316 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
1 | 2317 |
|
159 | 2318 |
(b <= e) ifTrue:[ |
2319 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2320 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2321 |
self at:b put:temp2. self at:e put:temp1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2322 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2323 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
159 | 2324 |
]. |
2325 |
b := b + 1. |
|
2326 |
e := e - 1 |
|
2327 |
] |
|
1 | 2328 |
]. |
2329 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
|
2330 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
|
2331 |
! |
|
2332 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2333 |
quickSortFrom:inBegin to:inEnd with:aCollection |
602 | 2334 |
"actual quicksort worker for sortWith-message" |
2335 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2336 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2337 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2338 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2339 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2340 |
middleElement temp1 temp2 | |
602 | 2341 |
|
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2342 |
begin := inBegin. "/ this also does a type-check |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2343 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2344 |
|
602 | 2345 |
b := begin. |
2346 |
e := end. |
|
2347 |
middleElement := self at:((b + e) // 2). |
|
2348 |
||
2349 |
[b < e] whileTrue:[ |
|
2350 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
2351 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
2352 |
||
2353 |
(b <= e) ifTrue:[ |
|
2354 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2355 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2356 |
self at:b put:temp2. self at:e put:temp1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2357 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2358 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
602 | 2359 |
]. |
2360 |
b := b + 1. |
|
2361 |
e := e - 1 |
|
2362 |
] |
|
2363 |
]. |
|
2364 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e with:aCollection]. |
|
2365 |
(b < end) ifTrue:[self quickSortFrom:b to:end with:aCollection] |
|
2366 |
! |
|
2367 |
||
2368 |
reverse |
|
2369 |
"reverse the order of the elements inplace" |
|
2370 |
||
2371 |
|lowIndex "{ Class: SmallInteger }" |
|
2372 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2373 |
t1 t2| |
602 | 2374 |
|
2375 |
hiIndex := self size. |
|
2376 |
lowIndex := 1. |
|
2377 |
[lowIndex < hiIndex] whileTrue:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2378 |
t1 := self at:lowIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2379 |
t2 := self at:hiIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2380 |
self at:lowIndex put:t2. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2381 |
self at:hiIndex put:t1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2382 |
|
602 | 2383 |
lowIndex := lowIndex + 1. |
2384 |
hiIndex := hiIndex - 1 |
|
2385 |
] |
|
69 | 2386 |
" |
602 | 2387 |
#(4 5 6 7 7) reverse |
2388 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 2389 |
" |
1 | 2390 |
! |
2391 |
||
2392 |
sort |
|
2393 |
"sort the collection inplace. The elements are compared using |
|
159 | 2394 |
> and < i.e. they should offer a magnitude-like protocol. |
2395 |
The implementation uses the quicksort algorithm, which may not be |
|
2396 |
the best possible for all situations." |
|
13 | 2397 |
|
32 | 2398 |
|stop| |
1 | 2399 |
|
32 | 2400 |
stop := self size. |
2401 |
(stop > 1) ifTrue:[ |
|
159 | 2402 |
self quickSortFrom:1 to:stop |
1 | 2403 |
] |
2404 |
||
95 | 2405 |
" |
2406 |
#(1 16 7 98 3 19 4 0) sort |
|
2407 |
||
2408 |
|data| |
|
2409 |
data := Random new next:100000. |
|
2410 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2411 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2412 |
data reverse. |
|
2413 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2414 |
" |
|
1 | 2415 |
! |
2416 |
||
2417 |
sort:sortBlock |
|
2418 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2419 |
for comparison. This allows any sort criteria to be implemented." |
|
2420 |
||
32 | 2421 |
|stop| |
1 | 2422 |
|
32 | 2423 |
stop := self size. |
2424 |
(stop > 1) ifTrue:[ |
|
159 | 2425 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
1 | 2426 |
] |
2427 |
||
95 | 2428 |
" |
2429 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
2430 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
2431 |
" |
|
1 | 2432 |
! |
2433 |
||
2434 |
sort:sortBlock with:aCollection |
|
2435 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 2436 |
for comparison. Also reorder the elements in aCollection. |
2437 |
Use, when you have a key collection to sort some other collection with." |
|
1 | 2438 |
|
32 | 2439 |
|stop| |
1 | 2440 |
|
32 | 2441 |
stop := self size. |
2442 |
(stop > 1) ifTrue:[ |
|
159 | 2443 |
self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
1 | 2444 |
] |
2445 |
||
69 | 2446 |
" |
2447 |
|c1 c2| |
|
1 | 2448 |
c1 := #(1 16 7 9). |
2449 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
2450 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 2451 |
c1 printNL. |
2452 |
c2 printNL |
|
69 | 2453 |
" |
1 | 2454 |
! |
2455 |
||
602 | 2456 |
sortWith:aCollection |
2457 |
"sort the receiver collection inplace, also sort aCollection with it. |
|
2458 |
Use, when you have a key collection to sort another collection with." |
|
2459 |
||
2460 |
|stop| |
|
356 | 2461 |
|
2462 |
stop := self size. |
|
602 | 2463 |
(stop > 1) ifTrue:[ |
2464 |
self quickSortFrom:1 to:stop with:aCollection |
|
1 | 2465 |
] |
77 | 2466 |
|
2467 |
" |
|
602 | 2468 |
|c1 c2| |
2469 |
c1 := #(1 16 7 9). |
|
2470 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
2471 |
c1 sortWith:c2. |
|
2472 |
c1 printNL. |
|
2473 |
c2 printNL |
|
424 | 2474 |
" |
2475 |
! |
|
2476 |
||
602 | 2477 |
topologicalSort:sortBlock |
2478 |
"sort the collection inplace using a sloooow sort algorithm. |
|
2479 |
This algorithm has O-square runtime behavior and should be used only |
|
2480 |
in special situations. |
|
2481 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
2482 |
a>c (for example, to sort classes by inheritance) |
|
2483 |
||
2484 |
In other situations, use #sort; which implements the quicksort algorithm. |
|
77 | 2485 |
" |
602 | 2486 |
|
2487 |
|smallestIndex "{ Class: SmallInteger }" |
|
2488 |
end "{ Class: SmallInteger }" |
|
2489 |
smallest thisOne| |
|
2490 |
||
2491 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
2492 |
||
2493 |
end := self size. |
|
862 | 2494 |
1 to:end-1 do:[:index | |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2495 |
|index2| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2496 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2497 |
smallest := self at:index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2498 |
smallestIndex := index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2499 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2500 |
index2 := index + 1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2501 |
[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2502 |
thisOne := self at:index2. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2503 |
(index2 ~~ smallestIndex and:[(sortBlock value:thisOne value:smallest)]) ifTrue:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2504 |
smallestIndex := index2. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2505 |
smallest := thisOne. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2506 |
index2 := index + 1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2507 |
] ifFalse:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2508 |
index2 := index2 + 1.. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2509 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2510 |
] doWhile:[index2 <= end]. |
862 | 2511 |
|
2512 |
"/ (index + 1) to:end do:[:index2 | |
|
2513 |
"/ thisOne := self at:index2. |
|
2514 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
2515 |
"/ smallestIndex := index2. |
|
2516 |
"/ smallest := thisOne |
|
2517 |
"/ ]. |
|
2518 |
"/ ]. |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2519 |
(smallestIndex ~~ index) ifTrue:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2520 |
thisOne := self at:index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2521 |
self at:index put:smallest. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2522 |
self at:smallestIndex put:thisOne |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2523 |
]. |
1 | 2524 |
] |
95 | 2525 |
|
77 | 2526 |
" |
602 | 2527 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
2528 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
2529 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
2530 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 2531 |
" |
1 | 2532 |
! ! |
602 | 2533 |
|
628 | 2534 |
!SequenceableCollection class methodsFor:'documentation'! |
2535 |
||
2536 |
version |
|
1188 | 2537 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.54 1996-04-16 11:50:01 ca Exp $' |
628 | 2538 |
! ! |