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