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