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