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