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