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