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