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