1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
|
22 |
All Rights Reserved
|
|
23 |
'!
|
1
|
24 |
|
88
|
25 |
!SequenceableCollection class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
30 |
All Rights Reserved
|
|
31 |
|
88
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
1
|
40 |
|
88
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.12 1994-06-02 16:21:55 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
1
|
46 |
|
88
|
47 |
documentation
|
|
48 |
"
|
|
49 |
SequenceableCollections have ordered elements which can be accessed via
|
|
50 |
a numeric index.
|
|
51 |
SequenceableCollection is an abstract class - there are no instances of
|
|
52 |
it in the system. See concrete subclasses (such as OrderedCollection).
|
|
53 |
"
|
|
54 |
! !
|
1
|
55 |
|
|
56 |
!SequenceableCollection class methodsFor:'instance creation'!
|
|
57 |
|
|
58 |
new:size withAll:element
|
|
59 |
"return a new Collection of size, where all elements are
|
|
60 |
initialized to element"
|
|
61 |
|
|
62 |
|newCollection|
|
|
63 |
|
|
64 |
newCollection := self new:size.
|
|
65 |
newCollection atAllPut:element.
|
|
66 |
^ newCollection
|
|
67 |
! !
|
|
68 |
|
|
69 |
!SequenceableCollection methodsFor:'accessing'!
|
|
70 |
|
|
71 |
first
|
|
72 |
"return the first element"
|
|
73 |
|
|
74 |
^ self at:1
|
|
75 |
!
|
|
76 |
|
|
77 |
last
|
|
78 |
"return the last element"
|
|
79 |
|
|
80 |
^ self at:(self size)
|
32
|
81 |
!
|
|
82 |
|
|
83 |
at:index ifAbsent:exceptionBlock
|
61
|
84 |
"return the element at index if valid.
|
|
85 |
If the index is invalid, return the result of evaluating
|
|
86 |
the exceptionblock."
|
32
|
87 |
|
|
88 |
((index < 1) or:[index > self size]) ifTrue:[
|
|
89 |
^ exceptionBlock value
|
|
90 |
].
|
|
91 |
^ self at:index
|
|
92 |
|
|
93 |
"#(1 2 3) at:4 ifAbsent:['no such index']"
|
61
|
94 |
"(Dictionary with:(#foo -> #bar)
|
|
95 |
with:(#frob -> #baz)) at:#foobar ifAbsent:['no such index']"
|
1
|
96 |
! !
|
|
97 |
|
13
|
98 |
!SequenceableCollection methodsFor:'queries'!
|
|
99 |
|
69
|
100 |
isSequenceableCollection
|
|
101 |
"return true, if the receiver is some kind of sequenceableCollection"
|
|
102 |
|
|
103 |
^ true
|
|
104 |
!
|
|
105 |
|
13
|
106 |
firstIndex
|
|
107 |
"return the first elements index"
|
|
108 |
|
|
109 |
^ 1
|
|
110 |
!
|
|
111 |
|
|
112 |
lastIndex
|
|
113 |
"return the last elements index"
|
|
114 |
|
|
115 |
^ self size
|
|
116 |
!
|
|
117 |
|
|
118 |
size
|
|
119 |
"return the number of elements in the collection.
|
|
120 |
concrete implementations must define this"
|
|
121 |
|
|
122 |
^ self subclassResponsibility
|
77
|
123 |
!
|
|
124 |
|
|
125 |
keys
|
|
126 |
"return a collection with all keys in the Smalltalk dictionary"
|
|
127 |
|
|
128 |
|sz|
|
|
129 |
|
|
130 |
sz := self size.
|
|
131 |
sz == 0 ifTrue:[
|
|
132 |
^ #()
|
|
133 |
].
|
|
134 |
^ 1 to:sz
|
13
|
135 |
! !
|
|
136 |
|
1
|
137 |
!SequenceableCollection methodsFor:'comparing'!
|
|
138 |
|
|
139 |
= aCollection
|
|
140 |
"return true if the receiver and aCollection represent collections
|
|
141 |
with equal contents."
|
|
142 |
|
|
143 |
|index "{ Class: SmallInteger }"
|
|
144 |
stop "{ Class: SmallInteger }" |
|
|
145 |
|
|
146 |
(aCollection == self) ifTrue:[^true].
|
69
|
147 |
(aCollection isSequenceableCollection) ifFalse:[^false].
|
1
|
148 |
|
|
149 |
stop := self size.
|
|
150 |
stop == (aCollection size) ifFalse:[^false].
|
61
|
151 |
|
1
|
152 |
index := 1.
|
|
153 |
[index <= stop] whileTrue:[
|
|
154 |
(self at:index) = (aCollection at:index) ifFalse:[^false].
|
|
155 |
index := index + 1
|
|
156 |
].
|
|
157 |
^ true
|
44
|
158 |
|
69
|
159 |
"
|
|
160 |
#(1 2 3 4 5) = #(1 2 3 4 5)
|
|
161 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5)
|
|
162 |
#($1 $2 $3 $4 $5) = '12345'
|
|
163 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection
|
|
164 |
"
|
|
165 |
!
|
|
166 |
|
|
167 |
hash
|
|
168 |
"return a hash key for the receiver"
|
|
169 |
|
|
170 |
"this hash is stupid - but for larger collections, the hashing
|
|
171 |
time can become much bigger than the time lost in added probing.
|
|
172 |
Time will show ..."
|
|
173 |
|
|
174 |
^ (self at:1 ifAbsent:[0]) hash * self size
|
|
175 |
|
|
176 |
"
|
|
177 |
#(1 2 3 4 5) hash
|
|
178 |
#(1 2 3 4 5.0) asOrderedCollection hash
|
|
179 |
"
|
1
|
180 |
!
|
|
181 |
|
|
182 |
startsWith:aCollection
|
|
183 |
"return true, if the receivers first elements match those
|
|
184 |
of aCollection"
|
|
185 |
|
|
186 |
|index "{ Class: SmallInteger }"
|
|
187 |
stop "{ Class: SmallInteger }" |
|
|
188 |
|
|
189 |
(aCollection == self) ifTrue:[^true].
|
69
|
190 |
(aCollection isSequenceableCollection) ifFalse:[^false].
|
1
|
191 |
|
|
192 |
stop := aCollection size.
|
|
193 |
stop > self size ifTrue:[^false].
|
|
194 |
|
|
195 |
index := 1.
|
|
196 |
[index <= stop] whileTrue:[
|
|
197 |
(self at:index) = (aCollection at:index) ifFalse:[^false].
|
|
198 |
index := index + 1
|
|
199 |
].
|
|
200 |
^ true
|
|
201 |
|
69
|
202 |
"
|
|
203 |
'abcde' startsWith:#($a $b $c)
|
|
204 |
#[1 2 3 4] startsWith:#(1 2 3)
|
|
205 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3)
|
|
206 |
"
|
1
|
207 |
!
|
|
208 |
|
|
209 |
endsWith:aCollection
|
|
210 |
"return true, if the receivers last elements match those
|
|
211 |
of aCollection"
|
|
212 |
|
|
213 |
|index1 "{ Class: SmallInteger }"
|
|
214 |
index2 "{ Class: SmallInteger }"
|
61
|
215 |
stop "{ Class: SmallInteger }"
|
|
216 |
sz "{ Class: SmallInteger }"|
|
1
|
217 |
|
61
|
218 |
(aCollection == self) ifTrue:[^ true].
|
69
|
219 |
(aCollection isSequenceableCollection) ifFalse:[^ false].
|
1
|
220 |
|
|
221 |
stop := aCollection size.
|
61
|
222 |
sz := self size.
|
|
223 |
stop > sz ifTrue:[^false].
|
1
|
224 |
|
61
|
225 |
index1 := sz.
|
13
|
226 |
index2 := stop.
|
1
|
227 |
[index2 > 0] whileTrue:[
|
61
|
228 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false].
|
1
|
229 |
index1 := index1 - 1.
|
|
230 |
index2 := index2 - 1
|
|
231 |
].
|
|
232 |
^ true
|
|
233 |
|
69
|
234 |
"
|
|
235 |
'abcde' endsWith:#($d $e)
|
|
236 |
#[1 2 3 4] endsWith:#(3 4)
|
|
237 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4)
|
|
238 |
"
|
1
|
239 |
! !
|
|
240 |
|
|
241 |
!SequenceableCollection methodsFor:'copying'!
|
|
242 |
|
|
243 |
, aCollection
|
|
244 |
"return a new collection formed from concatenating the receiver with
|
44
|
245 |
the argument. The class of the new collection is determined by the
|
|
246 |
receivers class, so mixing classes is possible, if the second collections
|
|
247 |
elements can be stored into instances of the receivers class."
|
1
|
248 |
|
|
249 |
|newCollection
|
|
250 |
mySize "{ Class: SmallInteger }"
|
|
251 |
newSize "{ Class: SmallInteger }"
|
|
252 |
otherSize "{ Class: SmallInteger }"
|
|
253 |
dstIndex "{ Class: SmallInteger }"|
|
|
254 |
|
|
255 |
mySize := self size.
|
|
256 |
otherSize := aCollection size.
|
|
257 |
newSize := mySize + otherSize.
|
|
258 |
newCollection := self species new:newSize.
|
|
259 |
|
|
260 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1.
|
|
261 |
dstIndex := mySize + 1.
|
69
|
262 |
(aCollection isSequenceableCollection) ifTrue:[
|
1
|
263 |
"yes, aCollection has indexed elements"
|
|
264 |
newCollection replaceFrom:dstIndex to:newSize
|
|
265 |
with:aCollection startingAt:1.
|
|
266 |
^ newCollection
|
|
267 |
] ifFalse:[
|
|
268 |
"no, enumerate aCollection"
|
|
269 |
aCollection do:[:element |
|
|
270 |
newCollection at:dstIndex put:element.
|
|
271 |
dstIndex := dstIndex + 1
|
|
272 |
]
|
|
273 |
].
|
|
274 |
^ newCollection
|
44
|
275 |
|
|
276 |
"#($a $b $c) , #(1 2 3)"
|
|
277 |
"#($a $b $c) , '123'"
|
|
278 |
"'abc' , '123'"
|
|
279 |
"'abc' , #($q $w $e $r $t $y) asSortedCollection"
|
|
280 |
"'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers"
|
61
|
281 |
"'abc' asArray , #(1 2 3 4 5)"
|
1
|
282 |
!
|
|
283 |
|
|
284 |
copyWith:newElement
|
61
|
285 |
"return a new collection consisting of a copy of the receivers elements
|
44
|
286 |
plus the argument."
|
1
|
287 |
|
|
288 |
|newCollection mySize newSize|
|
|
289 |
|
|
290 |
mySize := self size.
|
|
291 |
newSize := mySize + 1.
|
|
292 |
newCollection := self species new:newSize.
|
|
293 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1.
|
|
294 |
newCollection at:newSize put:newElement.
|
|
295 |
^newCollection
|
44
|
296 |
|
|
297 |
"#(1 2 3 4 5) copyWith:$a"
|
|
298 |
"'abcdefg' copyWith:$h"
|
|
299 |
"'abcdefg' copyWith:'123'" "-- will fail: string cannot be stored into string"
|
|
300 |
"'abcdefg' copyWith:1" "-- will fail: integer cannot be stored into string"
|
1
|
301 |
!
|
|
302 |
|
61
|
303 |
copyWithoutFirst:elementToSkip
|
|
304 |
"return a new collection consisting of a copy of the receivers elements
|
|
305 |
without the first elementToSkip, if it was present.
|
|
306 |
No error is reported, if elementToSkip is not in the collection."
|
1
|
307 |
|
44
|
308 |
|copy skipIndex sz|
|
1
|
309 |
|
44
|
310 |
skipIndex := self indexOf:elementToSkip startingAt:1.
|
1
|
311 |
(skipIndex == 0) ifTrue:[^ self copy].
|
44
|
312 |
|
|
313 |
sz := self size.
|
|
314 |
copy := self class new:(sz - 1).
|
|
315 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1.
|
|
316 |
copy replaceFrom:skipIndex to:(sz - 1) with:self startingAt:(skipIndex + 1).
|
|
317 |
^ copy
|
|
318 |
|
61
|
319 |
"#($a $b $c $d $e $f $g) copyWithoutFirst:$d"
|
|
320 |
"#($a $b $c $d $e $f $g) copyWithoutFirst:$x"
|
|
321 |
"#(90 80 70 60 50) copyWithoutFirst:70"
|
|
322 |
"#(90 80 70 80 60 45 80 50) copyWithoutFirst:80"
|
|
323 |
!
|
|
324 |
|
|
325 |
copyWithout:elementToSkip
|
|
326 |
"return a new collection consisting of a copy of the receiver, with
|
|
327 |
ALL elements equal to elementToSkip are left out.
|
|
328 |
No error is reported, if elementToSkip is not in the collection."
|
|
329 |
|
|
330 |
|n copy srcIndex dstIndex skipIndex sz l|
|
|
331 |
|
|
332 |
"the code below may look like overkill,
|
|
333 |
however, for big collections its better to move data
|
|
334 |
around in big chunks"
|
|
335 |
|
|
336 |
n := self occurrencesOf:elementToSkip.
|
|
337 |
n == 0 ifTrue:[^ self copy].
|
|
338 |
|
|
339 |
sz := self size.
|
|
340 |
copy := self species new:(sz - n).
|
|
341 |
|
|
342 |
srcIndex := 1.
|
|
343 |
dstIndex := 1.
|
|
344 |
|
|
345 |
n timesRepeat:[
|
|
346 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex.
|
|
347 |
l := skipIndex - srcIndex.
|
|
348 |
l ~~ 0 ifTrue:[
|
|
349 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1)
|
|
350 |
with:self startingAt:srcIndex.
|
|
351 |
dstIndex := dstIndex + l
|
|
352 |
].
|
|
353 |
srcIndex := skipIndex + 1
|
|
354 |
].
|
|
355 |
l := sz - srcIndex.
|
|
356 |
copy replaceFrom:dstIndex to:(dstIndex + l)
|
|
357 |
with:self startingAt:srcIndex.
|
|
358 |
^ copy
|
|
359 |
|
44
|
360 |
"#($a $b $c $d $e $f $g) copyWithout:$d"
|
61
|
361 |
"#($a $b $c $d $e $f $g) copyWithout:$a"
|
|
362 |
"#($a $b $c $d $e $f $g) copyWithout:$g"
|
|
363 |
"#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a"
|
44
|
364 |
"#($a $b $c $d $e $f $g) copyWithout:$x"
|
61
|
365 |
"#(90 80 70 60 50) copyWithout:70"
|
|
366 |
"#(90 80 70 80 60 45 80 50) copyWithout:80"
|
|
367 |
!
|
|
368 |
|
|
369 |
copyWithoutIndex:omitIndex
|
|
370 |
"return a new collection consisting of receivers elements
|
|
371 |
without the argument stored at omitIndex"
|
|
372 |
|
|
373 |
|copy sz|
|
|
374 |
|
|
375 |
sz := self size.
|
|
376 |
copy := self species new:(sz - 1).
|
|
377 |
copy replaceFrom:1
|
|
378 |
to:(omitIndex - 1)
|
|
379 |
with:self
|
|
380 |
startingAt:1.
|
|
381 |
copy replaceFrom:omitIndex
|
|
382 |
to:sz - 1
|
|
383 |
with:self
|
|
384 |
startingAt:(omitIndex + 1).
|
|
385 |
^ copy
|
|
386 |
|
|
387 |
"#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3"
|
|
388 |
"'abcdefghijkl' copyWithoutIndex:5"
|
1
|
389 |
!
|
|
390 |
|
|
391 |
copyFrom:start to:stop
|
|
392 |
"return a new collection consisting of receivers elements
|
|
393 |
between start and stop"
|
|
394 |
|
|
395 |
|newCollection newSize|
|
|
396 |
|
|
397 |
newSize := stop - start + 1.
|
61
|
398 |
newCollection := self species new:newSize.
|
1
|
399 |
newCollection replaceFrom:1 to:newSize with:self startingAt:start.
|
|
400 |
^ newCollection
|
44
|
401 |
|
|
402 |
"#($a $b $c $d $e $f $g) copyFrom:2 to:5"
|
|
403 |
"'1234567890' copyFrom:2 to:5"
|
1
|
404 |
!
|
|
405 |
|
|
406 |
copyFrom:start
|
|
407 |
"return a new collection consisting of receivers elements
|
|
408 |
from start to the end of the collection"
|
|
409 |
|
|
410 |
^ self copyFrom:start to:(self size)
|
44
|
411 |
|
|
412 |
"#($a $b $c $d $e $f $g) copyFrom:2"
|
|
413 |
"'1234567890' copyFrom:2"
|
1
|
414 |
!
|
|
415 |
|
|
416 |
copyTo:stop
|
|
417 |
"return a new collection consisting of receivers elements
|
61
|
418 |
from 1 up to (including) index stop"
|
1
|
419 |
|
|
420 |
^ self copyFrom:1 to:stop
|
44
|
421 |
|
|
422 |
"#($a $b $c $d $e $f $g) copyTo:5"
|
|
423 |
"'1234567890' copyTo:4"
|
1
|
424 |
!
|
|
425 |
|
61
|
426 |
copyFirst:count
|
|
427 |
"return a new collection consisting of the receivers first count
|
|
428 |
elements - this is just a rename of copyTo: - for compatibility."
|
|
429 |
|
|
430 |
^ self copyFrom:1 to:count
|
|
431 |
|
|
432 |
"#($a $b $c $d $e $f $g) copyFirst:5"
|
|
433 |
"'1234567890' copyFirst:4"
|
|
434 |
!
|
|
435 |
|
|
436 |
copyLast:count
|
|
437 |
"return a new collection consisting of the receivers last count
|
|
438 |
elements."
|
|
439 |
|
|
440 |
|sz|
|
1
|
441 |
|
61
|
442 |
sz := self size.
|
|
443 |
^ self copyFrom:(sz - count + 1) to:sz
|
|
444 |
|
|
445 |
"#($a $b $c $d $e $f $g) copyLast:5"
|
|
446 |
"'1234567890' copyLast:4"
|
|
447 |
!
|
|
448 |
|
|
449 |
copyUpTo:element
|
|
450 |
"return a new collection consisting of the receiver elements
|
|
451 |
up-to (but excluding) the first occurence of element."
|
|
452 |
|
|
453 |
|idx|
|
|
454 |
|
|
455 |
idx := self indexOf:element.
|
|
456 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?"
|
|
457 |
idx == 1 ifTrue:[^ self species new].
|
|
458 |
^ self copyFrom:1 to:(idx-1)
|
1
|
459 |
|
61
|
460 |
"#($a $b $c $d $e $f $g) copyUpTo:$d"
|
|
461 |
"'1234567890' copyUpTo:$5"
|
|
462 |
"'1234567890' copyUpTo:$a"
|
|
463 |
"'1234567890' copyUpTo:$1"
|
|
464 |
!
|
|
465 |
|
|
466 |
copyThrough:element
|
|
467 |
"return a new collection consisting of the receiver elements
|
|
468 |
up-to (AND including) the first occurence of element."
|
|
469 |
|
|
470 |
|idx|
|
|
471 |
|
|
472 |
idx := self indexOf:element.
|
|
473 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?"
|
|
474 |
^ self copyFrom:1 to:idx
|
44
|
475 |
|
61
|
476 |
"#($a $b $c $d $e $f $g) copyThrough:$d"
|
|
477 |
"'1234567890' copyThrough:$5"
|
|
478 |
"'1234567890' copyThrough:$a"
|
|
479 |
"'1234567890' copyThrough:$1"
|
|
480 |
!
|
|
481 |
|
|
482 |
copyReplaceFrom:startIndex to:endIndex with:aCollection
|
|
483 |
"return a copy of the receiver, where the elements from startIndex to
|
|
484 |
endIndex have been replaced by the elements of aCollection"
|
|
485 |
|
|
486 |
|newColl replSize|
|
44
|
487 |
|
61
|
488 |
replSize := aCollection size.
|
|
489 |
newColl := self species new:(self size - (endIndex - startIndex + 1) + replSize).
|
|
490 |
newColl replaceFrom:1 to:(startIndex - 1) with:self.
|
|
491 |
newColl replaceFrom:startIndex with:aCollection.
|
|
492 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1).
|
|
493 |
^ newColl
|
|
494 |
|
|
495 |
"#(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)"
|
|
496 |
"'hello world' copyReplaceFrom:6 to:6 with:' there, '"
|
|
497 |
!
|
|
498 |
|
|
499 |
copyReplaceAll:startIndex to:endIndex with:aCollection
|
|
500 |
"return a copy of the receiver, where the elements from startIndex to
|
|
501 |
endIndex have been replaced by the elements of aCollection"
|
1
|
502 |
! !
|
|
503 |
|
|
504 |
!SequenceableCollection methodsFor:'filling and replacing'!
|
|
505 |
|
|
506 |
from:index1 to:index2 put:anObject
|
|
507 |
"replace the elements from index1 to index2 of the collection
|
44
|
508 |
by the argument, anObject.
|
|
509 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
510 |
therefore the change may affect all others referencing the receiver."
|
1
|
511 |
|
|
512 |
|index "{ Class: SmallInteger }"
|
|
513 |
end "{ Class: SmallInteger }"|
|
|
514 |
|
|
515 |
index := index1.
|
|
516 |
end := index2.
|
|
517 |
[index <= end] whileTrue:[
|
|
518 |
self at:index put:anObject.
|
|
519 |
index := index + 1
|
|
520 |
]
|
44
|
521 |
|
|
522 |
"#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X"
|
|
523 |
"'abcdefghijkl' from:3 to:6 put:$X"
|
1
|
524 |
!
|
|
525 |
|
|
526 |
atAllPut:anObject
|
44
|
527 |
"replace all elements of the collection by the argument, anObject.
|
|
528 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
529 |
therefore the change may affect all others referencing the receiver."
|
1
|
530 |
|
32
|
531 |
self from:1 to:(self size) put:anObject
|
1
|
532 |
!
|
|
533 |
|
|
534 |
atAll:indexCollection put:anObject
|
44
|
535 |
"put anObject into all indexes from indexCollection in the receiver.
|
|
536 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
537 |
therefore the change may affect all others referencing the receiver."
|
1
|
538 |
|
|
539 |
indexCollection do:[:index | self at:index put:anObject]
|
|
540 |
|
|
541 |
"(Array new:10) atAll:(1 to:5) put:0"
|
|
542 |
"(Array new:10) atAll:#(1 5 6 9) put:0"
|
|
543 |
!
|
|
544 |
|
|
545 |
replaceAll:oldObject by:newObject
|
44
|
546 |
"replace all oldObjects by newObject in the receiver.
|
|
547 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
548 |
therefore the change may affect all others referencing the receiver."
|
1
|
549 |
|
32
|
550 |
1 to:self size do:[:index |
|
1
|
551 |
(self at:index) = oldObject ifTrue:[
|
|
552 |
self at:index put:newObject
|
|
553 |
]
|
|
554 |
]
|
44
|
555 |
|
|
556 |
"'123123abc123' replaceAll:$1 by:$*"
|
|
557 |
"#($a $b $a $c $a $d $a $e) replaceAll:$a by:$A"
|
1
|
558 |
!
|
|
559 |
|
|
560 |
replaceFrom:start with:replacementCollection
|
44
|
561 |
"replace elements in the receiver starting at start,
|
|
562 |
with elements taken from replacementCollection starting at 1
|
|
563 |
to the end of replacementCollection.
|
|
564 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
565 |
therefore the change may affect all others referencing the receiver."
|
1
|
566 |
|
|
567 |
^ self replaceFrom:start
|
|
568 |
to:(start + replacementCollection size - 1)
|
|
569 |
with:replacementCollection
|
|
570 |
startingAt:1
|
44
|
571 |
|
|
572 |
"'1234567890' replaceFrom:5 with:'abc'"
|
|
573 |
"#($a $b $c $d $e) replaceFrom:2 with:'123'"
|
|
574 |
!
|
|
575 |
|
|
576 |
replaceFrom:start with:replacementCollection startingAt:offset
|
|
577 |
"replace elements in the receiver starting at start,
|
|
578 |
with elements taken from replacementCollection starting at offset
|
|
579 |
to the end of replacementCollection.
|
|
580 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
581 |
therefore the change may affect all others referencing the receiver."
|
|
582 |
|
|
583 |
^ self replaceFrom:start
|
|
584 |
to:(start + replacementCollection size - offset)
|
|
585 |
with:replacementCollection
|
|
586 |
startingAt:offset
|
|
587 |
|
|
588 |
"'1234567890' replaceFrom:5 with:'abcdef' startingAt:3"
|
|
589 |
"#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4"
|
1
|
590 |
!
|
|
591 |
|
|
592 |
replaceFrom:start to:stop with:replacementCollection
|
44
|
593 |
"replace elements in the receiver between index start and stop,
|
|
594 |
with elements taken from replacementCollection starting at 1.
|
|
595 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
596 |
therefore the change may affect all others referencing the receiver."
|
1
|
597 |
|
|
598 |
^ self replaceFrom:start
|
|
599 |
to:stop
|
|
600 |
with:replacementCollection
|
|
601 |
startingAt:1
|
61
|
602 |
|
|
603 |
"'1234567890' replaceFrom:5 to:7 with:'abcdef'"
|
|
604 |
"#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345'"
|
1
|
605 |
!
|
|
606 |
|
|
607 |
replaceFrom:start to:stop with:replacementCollection startingAt:repStart
|
44
|
608 |
"replace elements in the receiver between index start and stop,
|
|
609 |
with elements taken from replacementCollection starting at repStart.
|
|
610 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
611 |
therefore the change may affect all others referencing the receiver."
|
1
|
612 |
|
|
613 |
|srcIndex "{ Class: SmallInteger }"
|
|
614 |
dstIndex "{ Class: SmallInteger }"
|
|
615 |
end "{ Class: SmallInteger }" |
|
|
616 |
|
|
617 |
(replacementCollection == self) ifTrue:[
|
|
618 |
(repStart < start) ifTrue:[
|
|
619 |
" must do reverse copy "
|
|
620 |
srcIndex := repStart + (stop - start).
|
|
621 |
dstIndex := stop.
|
|
622 |
end := start.
|
|
623 |
[dstIndex >= end] whileTrue:[
|
|
624 |
self at:dstIndex put:(replacementCollection at:srcIndex).
|
|
625 |
srcIndex := srcIndex - 1.
|
|
626 |
dstIndex := dstIndex - 1
|
|
627 |
].
|
|
628 |
^ self
|
|
629 |
]
|
|
630 |
].
|
|
631 |
|
|
632 |
srcIndex := repStart.
|
|
633 |
dstIndex := start.
|
|
634 |
end := stop.
|
|
635 |
[dstIndex <= end] whileTrue:[
|
|
636 |
self at:dstIndex put:(replacementCollection at:srcIndex).
|
|
637 |
srcIndex := srcIndex + 1.
|
|
638 |
dstIndex := dstIndex + 1
|
|
639 |
]
|
61
|
640 |
|
|
641 |
"'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3"
|
|
642 |
"#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4"
|
|
643 |
!
|
|
644 |
|
|
645 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex
|
|
646 |
"replace elements in destColl with elements from the receiver.
|
|
647 |
Notice: This operation modifies the destination collection, NOT a copy;
|
|
648 |
therefore the change may affect all others referencing this object."
|
|
649 |
|
|
650 |
destColl replaceFrom:destStartIndex to:destEndIndex with:self startingAt:sourceStart
|
|
651 |
|
|
652 |
"
|
|
653 |
|s|
|
|
654 |
s := 'abcdefghijklmnop'.
|
|
655 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3.
|
|
656 |
s'123defghijklmnop'
|
|
657 |
"
|
1
|
658 |
!
|
|
659 |
|
|
660 |
withCRs
|
|
661 |
"return a new collection consisting of receivers elements
|
44
|
662 |
with all \-characters replaced by cr-characters.
|
|
663 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
664 |
therefore the change may affect all others referencing the receiver."
|
1
|
665 |
|
13
|
666 |
^ self copy replaceAll:$\ by:(Character cr)
|
1
|
667 |
!
|
|
668 |
|
|
669 |
withoutCRs
|
|
670 |
"return a new collection consisting of receivers elements
|
44
|
671 |
with all cr-characters replaced by \-characters.
|
|
672 |
Notice: This operation modifies the receiver, NOT a copy;
|
|
673 |
therefore the change may affect all others referencing the receiver."
|
1
|
674 |
|
13
|
675 |
^ self copy replaceAll:(Character cr) by:$\
|
1
|
676 |
! !
|
|
677 |
|
|
678 |
!SequenceableCollection methodsFor:'adding & removing'!
|
|
679 |
|
61
|
680 |
addFirst:anObject
|
|
681 |
"prepend the argument, anObject to the collection.
|
|
682 |
Return the argument, anObject.
|
|
683 |
|
|
684 |
Notice, that this is modifies the receiver NOT a copy.
|
|
685 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
686 |
(i.e. for Strings and Arrays it is not recommened)"
|
1
|
687 |
|
|
688 |
|newSize|
|
|
689 |
|
|
690 |
newSize := self size + 1.
|
|
691 |
self grow:newSize.
|
|
692 |
self replaceFrom:2 to:newSize with:self startingAt:1.
|
61
|
693 |
self at:1 put:anObject.
|
|
694 |
^ anObject
|
44
|
695 |
|
61
|
696 |
"
|
|
697 |
|a|
|
|
698 |
a:= #(1 2 3 4 5 6 7 8).
|
|
699 |
a addFirst:'hello'.
|
|
700 |
a
|
|
701 |
"
|
|
702 |
"
|
|
703 |
|c|
|
|
704 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection.
|
|
705 |
c addFirst:'hello'.
|
|
706 |
c
|
|
707 |
"
|
1
|
708 |
!
|
|
709 |
|
61
|
710 |
add:anObject
|
|
711 |
"append the argument, anObject to the collection.
|
|
712 |
Return the argument, anObject.
|
|
713 |
|
|
714 |
Notice, that this is modifies the receiver NOT a copy.
|
|
715 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
716 |
(i.e. for Strings and Arrays it is not recommened)"
|
1
|
717 |
|
|
718 |
|newSize|
|
|
719 |
|
|
720 |
newSize := self size + 1.
|
|
721 |
self grow:newSize.
|
61
|
722 |
self at:newSize put:anObject.
|
|
723 |
^ anObject
|
44
|
724 |
|
61
|
725 |
"
|
|
726 |
|a|
|
|
727 |
a := #(1 2 3 4 5 6 7 8).
|
|
728 |
a add:'hello'.
|
|
729 |
a
|
|
730 |
"
|
|
731 |
"
|
|
732 |
|c|
|
|
733 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection.
|
|
734 |
c add:'hello'.
|
|
735 |
c
|
|
736 |
"
|
1
|
737 |
!
|
|
738 |
|
|
739 |
add:anElement beforeIndex:index
|
61
|
740 |
"insert the first argument, anObject into the collection before slot index.
|
|
741 |
Return the receiver (sigh - ST-80 compatibility).
|
|
742 |
|
|
743 |
Notice, that this is modifies the receiver NOT a copy.
|
|
744 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
745 |
(i.e. for Strings and Arrays it is not recommened)"
|
1
|
746 |
|
|
747 |
|newSize|
|
|
748 |
|
|
749 |
newSize := self size + 1.
|
|
750 |
self grow:newSize.
|
|
751 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index.
|
|
752 |
self at:index put:anElement
|
44
|
753 |
|
|
754 |
"#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5"
|
1
|
755 |
!
|
|
756 |
|
|
757 |
remove:anElement ifAbsent:aBlock
|
61
|
758 |
"search for anElement and, if present remove and return it.
|
|
759 |
If not present, return the value of evaluating aBlock.
|
|
760 |
|
|
761 |
Notice, that this is modifies the receiver NOT a copy.
|
|
762 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
763 |
(i.e. for Strings and Arrays it is not recommened)"
|
1
|
764 |
|
|
765 |
|any
|
|
766 |
dstIndex "{ Class: SmallInteger }"
|
|
767 |
sz "{ Class: SmallInteger }"|
|
|
768 |
|
|
769 |
dstIndex := 1.
|
|
770 |
any := false.
|
|
771 |
sz := self size.
|
|
772 |
1 to:sz do:[:srcIndex |
|
|
773 |
(anElement = (self at:srcIndex)) ifTrue:[
|
|
774 |
any := true
|
|
775 |
] ifFalse:[
|
|
776 |
(dstIndex ~~ srcIndex) ifTrue:[
|
|
777 |
self at:dstIndex put:(self at:srcIndex)
|
|
778 |
].
|
|
779 |
dstIndex := dstIndex + 1
|
|
780 |
]
|
|
781 |
].
|
|
782 |
any ifTrue:[
|
61
|
783 |
self grow:dstIndex - 1.
|
|
784 |
^ anElement
|
|
785 |
].
|
|
786 |
^ aBlock value
|
44
|
787 |
|
|
788 |
"#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCr:'no']"
|
61
|
789 |
"#(1 2 3 4 5 6 7 8 9 0) remove:99 ifAbsent:[#oops]"
|
1
|
790 |
!
|
|
791 |
|
|
792 |
removeFromIndex:startIndex toIndex:endIndex
|
61
|
793 |
"remove the elements stored at indexes between startIndex and endIndex.
|
|
794 |
|
|
795 |
Notice, that this is modifies the receiver NOT a copy.
|
|
796 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
797 |
(i.e. for Strings and Arrays it is not recommened)"
|
1
|
798 |
|
|
799 |
|newSize|
|
|
800 |
|
|
801 |
newSize := self size - endIndex + startIndex - 1.
|
|
802 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1).
|
|
803 |
self grow:newSize
|
44
|
804 |
|
|
805 |
"#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5"
|
1
|
806 |
!
|
|
807 |
|
61
|
808 |
removeAtIndex:index
|
|
809 |
"remove the argument stored at index and return it.
|
|
810 |
|
|
811 |
Notice, that this is modifies the receiver NOT a copy.
|
|
812 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
813 |
(i.e. for Strings and Arrays it is not recommened)"
|
|
814 |
|
|
815 |
|element|
|
|
816 |
|
|
817 |
element := self at:index.
|
|
818 |
self removeIndex:index.
|
|
819 |
^ element
|
|
820 |
|
|
821 |
"#($a $b $c $d $e $f $g) removeAtIndex:3"
|
|
822 |
!
|
|
823 |
|
1
|
824 |
removeIndex:index
|
61
|
825 |
"remove the argument stored at index. Return the receiver.
|
|
826 |
|
|
827 |
Notice, that this is modifies the receiver NOT a copy.
|
|
828 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
829 |
(i.e. for Strings and Arrays it is not recommened)"
|
1
|
830 |
|
|
831 |
self removeFromIndex:index toIndex:index
|
44
|
832 |
|
61
|
833 |
"#($a $b $c $d $e $f $g) removeIndex:3"
|
|
834 |
!
|
|
835 |
|
|
836 |
removeFirst
|
|
837 |
"remove the first element of the receiver and return it.
|
|
838 |
|
|
839 |
Notice, that this is modifies the receiver NOT a copy.
|
|
840 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
841 |
(i.e. for Strings and Arrays it is not recommened)"
|
|
842 |
|
|
843 |
^ self removeAtIndex:1
|
|
844 |
|
|
845 |
"
|
|
846 |
|a|
|
|
847 |
a := #(1 2 3 4 5 6).
|
|
848 |
a removeFirst.
|
|
849 |
a
|
|
850 |
"
|
|
851 |
!
|
|
852 |
|
|
853 |
removeLast
|
|
854 |
"remove the last element of the receiver and return it.
|
|
855 |
|
|
856 |
Notice, that this is modifies the receiver NOT a copy.
|
|
857 |
Also note, that it may be a slow operation for some collections due to the grow:
|
|
858 |
(i.e. for Strings and Arrays it is not recommened)"
|
|
859 |
|
|
860 |
^ self removeAtIndex:(self size)
|
|
861 |
|
|
862 |
"
|
|
863 |
|a|
|
|
864 |
a := #(1 2 3 4 5 6).
|
|
865 |
a removeLast.
|
|
866 |
a
|
|
867 |
"
|
1
|
868 |
! !
|
|
869 |
|
|
870 |
!SequenceableCollection methodsFor:'searching'!
|
|
871 |
|
|
872 |
detect:aBlock ifNone:exceptionBlock
|
|
873 |
"find the first element, for which evaluation of the argument, aBlock
|
|
874 |
return true; if none does so, return the evaluation of exceptionBlock
|
|
875 |
|
|
876 |
reimplemented here for speed"
|
|
877 |
|
32
|
878 |
|stop "{ Class: SmallInteger }"
|
1
|
879 |
element|
|
|
880 |
|
32
|
881 |
stop := self size.
|
|
882 |
1 to:stop do:[:index |
|
1
|
883 |
element := self at:index.
|
|
884 |
(aBlock value:element) ifTrue:[
|
|
885 |
^ element
|
|
886 |
].
|
|
887 |
].
|
|
888 |
^ exceptionBlock value
|
|
889 |
!
|
|
890 |
|
|
891 |
indexOf:anElement
|
|
892 |
"search the collection for anElement;
|
|
893 |
if found, return the index otherwise return 0.
|
44
|
894 |
The comparison is done using =
|
|
895 |
(i.e. equality test - not identity test)."
|
1
|
896 |
|
32
|
897 |
^ self indexOf:anElement startingAt:1
|
44
|
898 |
|
|
899 |
"#(10 20 30 40 50 60 70) indexOf: 40"
|
|
900 |
"#(10 20 30 40 50 60 70) indexOf: 40.0"
|
1
|
901 |
!
|
|
902 |
|
|
903 |
indexOf:anElement ifAbsent:exceptionBlock
|
|
904 |
"search the collection for anElement;
|
|
905 |
if found, return the index otherwise return the value of the
|
|
906 |
exceptionBlock.
|
44
|
907 |
The comparison is done using =
|
|
908 |
(i.e. equality test - not identity test)."
|
1
|
909 |
|
|
910 |
|index|
|
|
911 |
|
|
912 |
index := self indexOf:anElement startingAt:1.
|
|
913 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
914 |
^ index
|
|
915 |
!
|
|
916 |
|
|
917 |
indexOf:anElement startingAt:start
|
|
918 |
"search the collection for anElement staring search at index start;
|
|
919 |
if found, return the index otherwise return 0.
|
44
|
920 |
The comparison is done using =
|
|
921 |
(i.e. equality test - not identity test)."
|
1
|
922 |
|
32
|
923 |
|startIndex "{ Class: SmallInteger }"
|
|
924 |
stop "{ Class: SmallInteger }" |
|
1
|
925 |
|
32
|
926 |
startIndex := start.
|
|
927 |
stop := self size.
|
|
928 |
startIndex to:stop do:[:index |
|
1
|
929 |
anElement = (self at:index) ifTrue:[^ index].
|
|
930 |
].
|
|
931 |
^ 0
|
|
932 |
!
|
|
933 |
|
|
934 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock
|
|
935 |
"search the collection for anElement starting search at start;
|
|
936 |
if found, return the index otherwise return the value of the
|
|
937 |
exceptionBlock.
|
44
|
938 |
The comparison is done using =
|
|
939 |
(i.e. equality test - not identity test)."
|
1
|
940 |
|
|
941 |
|index|
|
|
942 |
|
|
943 |
index := self indexOf:anElement startingAt:start.
|
|
944 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
945 |
^ index
|
|
946 |
!
|
|
947 |
|
|
948 |
identityIndexOf:anElement
|
|
949 |
"search the collection for anElement using identity compare (i.e. ==);
|
|
950 |
if found, return the index otherwise return 0."
|
|
951 |
|
32
|
952 |
^ self identityIndexOf:anElement startingAt:1
|
44
|
953 |
|
|
954 |
"#(10 20 30 40 50 60 70) indexOf: 40"
|
|
955 |
"#(10 20 30 40 50 60 70) indexOf: 40.0"
|
1
|
956 |
!
|
|
957 |
|
|
958 |
identityIndexOf:anElement ifAbsent:exceptionBlock
|
|
959 |
"search the collection for anElement using identity compare (i.e. ==);
|
|
960 |
if found, return the index otherwise return the value of the
|
|
961 |
exceptionBlock."
|
|
962 |
|
|
963 |
|index|
|
|
964 |
|
32
|
965 |
index := self identityIndexOf:anElement startingAt:1.
|
1
|
966 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
967 |
^ index
|
|
968 |
!
|
|
969 |
|
|
970 |
identityIndexOf:anElement startingAt:start
|
|
971 |
"search the collection for anElement staring search at index start
|
|
972 |
using identity compare (i.e. ==);
|
|
973 |
if found, return the index otherwise return 0."
|
|
974 |
|
32
|
975 |
|startIndex "{ Class: SmallInteger }"
|
|
976 |
stop "{ Class: SmallInteger }" |
|
1
|
977 |
|
32
|
978 |
startIndex := start.
|
|
979 |
stop := self size.
|
|
980 |
startIndex to:stop do:[:index |
|
1
|
981 |
anElement == (self at:index) ifTrue:[^ index].
|
|
982 |
].
|
|
983 |
^ 0
|
44
|
984 |
|
|
985 |
"#(10 20 30 40 50 60 70) indexOf: 40"
|
|
986 |
"#(10 20 30 40 50 60 70) indexOf: 40.0"
|
1
|
987 |
!
|
|
988 |
|
|
989 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock
|
|
990 |
"search the collection for anElement starting search at start;
|
|
991 |
if found, return the index otherwise return the value of the
|
|
992 |
exceptionBlock.
|
|
993 |
This one searches for identical objects (i.e. ==)."
|
|
994 |
|
|
995 |
|index|
|
|
996 |
|
|
997 |
index := self identityIndexOf:anElement startingAt:start.
|
|
998 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
999 |
^ index
|
|
1000 |
!
|
|
1001 |
|
|
1002 |
findFirst:aBlock
|
|
1003 |
"find the first element, for which evaluation of the argument, aBlock
|
|
1004 |
return true; return its index or 0 if none detected."
|
|
1005 |
|
32
|
1006 |
|stop "{ Class: SmallInteger }" |
|
1
|
1007 |
|
32
|
1008 |
stop := self size.
|
|
1009 |
1 to:stop do:[:index |
|
1
|
1010 |
(aBlock value:(self at:index)) ifTrue:[^ index].
|
|
1011 |
].
|
|
1012 |
^ 0
|
|
1013 |
|
13
|
1014 |
"#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)]"
|
|
1015 |
"#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]]"
|
|
1016 |
"'one.two.three' findFirst:[:c | (c == $.)]"
|
|
1017 |
!
|
|
1018 |
|
|
1019 |
findLast:aBlock
|
|
1020 |
"find the last element, for which evaluation of the argument, aBlock
|
|
1021 |
return true; return its index or 0 if none detected."
|
|
1022 |
|
32
|
1023 |
|start "{ Class: SmallInteger }"|
|
13
|
1024 |
|
32
|
1025 |
start := self size.
|
|
1026 |
start to:1 by:-1 do:[:index |
|
13
|
1027 |
(aBlock value:(self at:index)) ifTrue:[^ index].
|
|
1028 |
].
|
|
1029 |
^ 0
|
|
1030 |
|
|
1031 |
"#(1 99 3 99 5 6) findLast:[:x | (x == 99)]"
|
|
1032 |
"'one.two.three' findLast:[:c | (c == $.)]"
|
1
|
1033 |
!
|
|
1034 |
|
|
1035 |
includes:anElement
|
|
1036 |
"return true if the collection contains anElement; false otherwise.
|
|
1037 |
Comparison is done using equality compare (i.e. =)."
|
|
1038 |
|
32
|
1039 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false].
|
1
|
1040 |
^ true
|
|
1041 |
! !
|
|
1042 |
|
|
1043 |
!SequenceableCollection methodsFor:'sorting & reordering'!
|
|
1044 |
|
|
1045 |
reverse
|
2
|
1046 |
"reverse the order of the elements inplace"
|
1
|
1047 |
|
|
1048 |
|lowIndex "{ Class: SmallInteger }"
|
|
1049 |
hiIndex "{ Class: SmallInteger }"
|
|
1050 |
t|
|
|
1051 |
|
32
|
1052 |
hiIndex := self size.
|
|
1053 |
lowIndex := 1.
|
1
|
1054 |
[lowIndex < hiIndex] whileTrue:[
|
|
1055 |
t := self at:lowIndex.
|
|
1056 |
self at:lowIndex put:(self at:hiIndex).
|
|
1057 |
self at:hiIndex put:t.
|
|
1058 |
lowIndex := lowIndex + 1.
|
|
1059 |
hiIndex := hiIndex - 1
|
|
1060 |
]
|
|
1061 |
"#(4 5 6 7 7) reverse"
|
13
|
1062 |
"#(1 4 7 10 2 5) asOrderedCollection reverse"
|
1
|
1063 |
!
|
|
1064 |
|
|
1065 |
quickSortFrom:begin to:end
|
|
1066 |
"actual quicksort worker for sort-message"
|
|
1067 |
|
|
1068 |
|b "{ Class: SmallInteger }"
|
|
1069 |
e "{ Class: SmallInteger }"
|
|
1070 |
middleElement temp |
|
|
1071 |
|
|
1072 |
b := begin.
|
|
1073 |
e := end.
|
|
1074 |
middleElement := self at:((b + e) // 2).
|
|
1075 |
|
|
1076 |
[b < e] whileTrue:[
|
|
1077 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1].
|
|
1078 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1].
|
|
1079 |
|
|
1080 |
(b <= e) ifTrue:[
|
|
1081 |
(b == e) ifFalse:[
|
|
1082 |
temp := self at:b.
|
|
1083 |
self at:b put:(self at:e).
|
|
1084 |
self at:e put:temp
|
|
1085 |
].
|
|
1086 |
b := b + 1.
|
|
1087 |
e := e - 1
|
|
1088 |
]
|
|
1089 |
].
|
|
1090 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e].
|
|
1091 |
(b < end) ifTrue:[self quickSortFrom:b to:end]
|
|
1092 |
!
|
|
1093 |
|
|
1094 |
quickSortFrom:begin to:end with:aCollection
|
|
1095 |
"actual quicksort worker for sortWith-message"
|
|
1096 |
|
|
1097 |
|b "{ Class: SmallInteger }"
|
|
1098 |
e "{ Class: SmallInteger }"
|
|
1099 |
middleElement temp |
|
|
1100 |
|
|
1101 |
b := begin.
|
|
1102 |
e := end.
|
|
1103 |
middleElement := self at:((b + e) // 2).
|
|
1104 |
|
|
1105 |
[b < e] whileTrue:[
|
|
1106 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1].
|
|
1107 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1].
|
|
1108 |
|
|
1109 |
(b <= e) ifTrue:[
|
|
1110 |
(b == e) ifFalse:[
|
|
1111 |
temp := self at:b.
|
|
1112 |
self at:b put:(self at:e).
|
|
1113 |
self at:e put:temp.
|
|
1114 |
temp := aCollection at:b.
|
|
1115 |
aCollection at:b put:(aCollection at:e).
|
|
1116 |
aCollection at:e put:temp
|
|
1117 |
].
|
|
1118 |
b := b + 1.
|
|
1119 |
e := e - 1
|
|
1120 |
]
|
|
1121 |
].
|
|
1122 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e with:aCollection].
|
|
1123 |
(b < end) ifTrue:[self quickSortFrom:b to:end with:aCollection]
|
|
1124 |
!
|
|
1125 |
|
|
1126 |
quickSortFrom:begin to:end sortBlock:sortBlock
|
|
1127 |
"actual quicksort worker for sort:-message"
|
|
1128 |
|
|
1129 |
|b "{ Class: SmallInteger }"
|
|
1130 |
e "{ Class: SmallInteger }"
|
|
1131 |
middleElement temp |
|
|
1132 |
|
|
1133 |
b := begin.
|
|
1134 |
e := end.
|
|
1135 |
middleElement := self at:((b + e) // 2).
|
|
1136 |
|
|
1137 |
[b < e] whileTrue:[
|
|
1138 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1].
|
|
1139 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1].
|
|
1140 |
|
|
1141 |
(b <= e) ifTrue:[
|
|
1142 |
(b == e) ifFalse:[
|
|
1143 |
temp := self at:b.
|
|
1144 |
self at:b put:(self at:e).
|
|
1145 |
self at:e put:temp
|
|
1146 |
].
|
|
1147 |
b := b + 1.
|
|
1148 |
e := e - 1
|
|
1149 |
]
|
|
1150 |
].
|
|
1151 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock].
|
|
1152 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock]
|
|
1153 |
!
|
|
1154 |
|
|
1155 |
quickSortFrom:begin to:end sortBlock:sortBlock with:aCollection
|
|
1156 |
"actual quicksort worker for sort:with:-message"
|
|
1157 |
|
|
1158 |
|b "{ Class: SmallInteger }"
|
|
1159 |
e "{ Class: SmallInteger }"
|
|
1160 |
middleElement temp |
|
|
1161 |
|
|
1162 |
b := begin.
|
|
1163 |
e := end.
|
|
1164 |
middleElement := self at:((b + e) // 2).
|
|
1165 |
|
|
1166 |
[b < e] whileTrue:[
|
|
1167 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1].
|
|
1168 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1].
|
|
1169 |
|
|
1170 |
(b <= e) ifTrue:[
|
|
1171 |
(b == e) ifFalse:[
|
|
1172 |
temp := self at:b.
|
|
1173 |
self at:b put:(self at:e).
|
|
1174 |
self at:e put:temp.
|
|
1175 |
temp := aCollection at:b.
|
|
1176 |
aCollection at:b put:(aCollection at:e).
|
|
1177 |
aCollection at:e put:temp
|
|
1178 |
].
|
|
1179 |
b := b + 1.
|
|
1180 |
e := e - 1
|
|
1181 |
]
|
|
1182 |
].
|
|
1183 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock with:aCollection].
|
|
1184 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock with:aCollection]
|
|
1185 |
!
|
|
1186 |
|
69
|
1187 |
topologicalSort:sortBlock
|
|
1188 |
"sort the collection inplace using a sloooow sort algorithm.
|
|
1189 |
This algorithm has O-square runtime behavior ad should be used only
|
|
1190 |
in special situations.
|
|
1191 |
It compares all elements, thus can be used when a>b, b>c does NOT imply
|
|
1192 |
a>c (for example, to sort classes by inheritance)
|
1
|
1193 |
|
69
|
1194 |
In other situations, use #sort; which implements the quicksort algorithm.
|
|
1195 |
"
|
|
1196 |
|
|
1197 |
|smallestIndex "{ Class: SmallInteger }"
|
|
1198 |
end "{ Class: SmallInteger }"
|
|
1199 |
smallest thisOne|
|
1
|
1200 |
|
77
|
1201 |
"this is just a q&d hack - there must be better implementations for this ;-)"
|
|
1202 |
|
32
|
1203 |
end := self size.
|
69
|
1204 |
1 to:end do:[:index |
|
1
|
1205 |
smallest := self at:index.
|
|
1206 |
smallestIndex := index.
|
69
|
1207 |
|
|
1208 |
(index + 1) to:end do:[:index2 |
|
|
1209 |
thisOne := self at:index2.
|
|
1210 |
(sortBlock value:thisOne value:smallest) ifTrue:[
|
1
|
1211 |
smallestIndex := index2.
|
69
|
1212 |
smallest := thisOne
|
1
|
1213 |
].
|
|
1214 |
].
|
|
1215 |
(smallestIndex ~~ index) ifTrue:[
|
|
1216 |
thisOne := self at:index.
|
|
1217 |
self at:index put:smallest.
|
|
1218 |
self at:smallestIndex put:thisOne
|
|
1219 |
].
|
|
1220 |
]
|
|
1221 |
|
69
|
1222 |
"
|
77
|
1223 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] Array(0 1 3 4 7 16 19 98)
|
|
1224 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] Array(0 1 3 4 7 16 19 98)
|
|
1225 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a]
|
69
|
1226 |
"
|
1
|
1227 |
!
|
|
1228 |
|
|
1229 |
sort
|
|
1230 |
"sort the collection inplace. The elements are compared using
|
|
1231 |
> and < i.e. they should offer a magnitude-like protocol."
|
13
|
1232 |
|
32
|
1233 |
|stop|
|
1
|
1234 |
|
32
|
1235 |
stop := self size.
|
|
1236 |
(stop > 1) ifTrue:[
|
|
1237 |
self quickSortFrom:1 to:stop
|
1
|
1238 |
]
|
|
1239 |
|
|
1240 |
"#(1 16 7 98 3 19 4 0) sort"
|
|
1241 |
!
|
|
1242 |
|
|
1243 |
sort:sortBlock
|
|
1244 |
"sort the collection inplace using the 2-arg block sortBlock
|
|
1245 |
for comparison. This allows any sort criteria to be implemented."
|
|
1246 |
|
32
|
1247 |
|stop|
|
1
|
1248 |
|
32
|
1249 |
stop := self size.
|
|
1250 |
(stop > 1) ifTrue:[
|
|
1251 |
self quickSortFrom:1 to:stop sortBlock:sortBlock
|
1
|
1252 |
]
|
|
1253 |
|
|
1254 |
"#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b]"
|
|
1255 |
"#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b]"
|
|
1256 |
!
|
|
1257 |
|
69
|
1258 |
sortWith:aCollection
|
|
1259 |
"sort the receiver collection inplace, also sort aCollection with it.
|
|
1260 |
Use, when you have a key collection to sort another collection with."
|
|
1261 |
|
|
1262 |
|stop|
|
|
1263 |
|
|
1264 |
stop := self size.
|
|
1265 |
(stop > 1) ifTrue:[
|
|
1266 |
self quickSortFrom:1 to:stop with:aCollection
|
|
1267 |
]
|
|
1268 |
|
|
1269 |
"
|
|
1270 |
|c1 c2|
|
|
1271 |
c1 := #(1 16 7 9).
|
|
1272 |
c2 := #('one' 'sixteen' 'seven' 'nine').
|
|
1273 |
c1 sortWith:c2.
|
|
1274 |
c1 printNewline.
|
|
1275 |
c2 printNewline
|
|
1276 |
"
|
|
1277 |
!
|
|
1278 |
|
1
|
1279 |
sort:sortBlock with:aCollection
|
|
1280 |
"sort the collection inplace using the 2-arg block sortBlock
|
|
1281 |
for comparison. Also reorder the elements in aCollection"
|
|
1282 |
|
32
|
1283 |
|stop|
|
1
|
1284 |
|
32
|
1285 |
stop := self size.
|
|
1286 |
(stop > 1) ifTrue:[
|
|
1287 |
self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection
|
1
|
1288 |
]
|
|
1289 |
|
69
|
1290 |
"
|
|
1291 |
|c1 c2|
|
1
|
1292 |
c1 := #(1 16 7 9).
|
|
1293 |
c2 := #('one' 'sixteen' 'seven' 'nine').
|
|
1294 |
c1 sort:[:a :b | a > b] with:c2.
|
|
1295 |
c1 printNewline.
|
69
|
1296 |
c2 printNewline
|
|
1297 |
"
|
1
|
1298 |
! !
|
|
1299 |
|
|
1300 |
!SequenceableCollection methodsFor:'enumerating'!
|
|
1301 |
|
|
1302 |
do:aBlock
|
|
1303 |
"evaluate the argument, aBlock for every element in the collection."
|
|
1304 |
|
32
|
1305 |
|stop "{ Class:SmallInteger }"|
|
1
|
1306 |
|
32
|
1307 |
stop := self size.
|
|
1308 |
1 to:stop do:[:index |
|
1
|
1309 |
aBlock value:(self at:index).
|
|
1310 |
]
|
77
|
1311 |
"
|
|
1312 |
#(one two three four five six) do:[:element | Transcript showCr:element]
|
|
1313 |
"
|
1
|
1314 |
!
|
|
1315 |
|
3
|
1316 |
keysAndValuesDo:aTwoArgBlock
|
|
1317 |
"evaluate the argument, aBlock for every element in the collection,
|
|
1318 |
passing both index and element as arguments."
|
|
1319 |
|
32
|
1320 |
|stop "{ Class:SmallInteger }"|
|
3
|
1321 |
|
32
|
1322 |
stop := self size.
|
|
1323 |
1 to:stop do:[:index |
|
3
|
1324 |
aTwoArgBlock value:index value:(self at:index).
|
|
1325 |
]
|
77
|
1326 |
"
|
|
1327 |
#(one two three four five six) keysAndValuesDo:[:key :element | Transcript show:key; space; showCr:element]
|
|
1328 |
"
|
|
1329 |
!
|
|
1330 |
|
|
1331 |
with:aSequenceableCollection do:aTwoArgBlock
|
|
1332 |
"evaluate the argument, aBlock for successive elements from
|
|
1333 |
each the receiver and the argument, aSequenceableCollection.
|
|
1334 |
The second argument, aBlock must be a two-argument block.
|
|
1335 |
The collection argument must implement access via a numeric key."
|
|
1336 |
|
|
1337 |
|stop "{ Class: SmallInteger }" |
|
|
1338 |
|
|
1339 |
stop := self size.
|
|
1340 |
1 to:stop do:[:index |
|
|
1341 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index).
|
|
1342 |
]
|
|
1343 |
"
|
|
1344 |
#(one two three four five six) with:(1 to:10) do:[:el1 :el2 | Transcript show:el1; space; showCr:el2]
|
|
1345 |
"
|
3
|
1346 |
!
|
|
1347 |
|
1
|
1348 |
from:index1 to:index2 do:aBlock
|
|
1349 |
"evaluate the argument, aBlock for the elements with index index1 to
|
|
1350 |
index2 in the collection"
|
|
1351 |
|
32
|
1352 |
|start "{ Class:SmallInteger }"
|
1
|
1353 |
stop "{ Class:SmallInteger }" |
|
|
1354 |
|
32
|
1355 |
start := index1.
|
1
|
1356 |
stop := index2.
|
32
|
1357 |
start to:stop do:[:index |
|
1
|
1358 |
aBlock value:(self at:index).
|
|
1359 |
]
|
77
|
1360 |
|
|
1361 |
"
|
|
1362 |
#(one two three four five six) from:3 to:5 do:[:element | Transcript showCr:element]
|
|
1363 |
"
|
1
|
1364 |
!
|
|
1365 |
|
42
|
1366 |
from:index1 to:index2 reverseDo:aBlock
|
|
1367 |
"evaluate the argument, aBlock for the elements with index index1 to
|
|
1368 |
index2 in the collection. Step in reverse order"
|
|
1369 |
|
|
1370 |
|start "{ Class:SmallInteger }"
|
|
1371 |
stop "{ Class:SmallInteger }" |
|
|
1372 |
|
|
1373 |
start := index1.
|
|
1374 |
stop := index2.
|
|
1375 |
stop to:start by:-1 do:[:index |
|
|
1376 |
aBlock value:(self at:index).
|
|
1377 |
]
|
|
1378 |
|
77
|
1379 |
"
|
|
1380 |
#(one two three four five six) from:3 to:5 reverseDo:[:element | Transcript showCr:element]
|
|
1381 |
"
|
1
|
1382 |
!
|
|
1383 |
|
|
1384 |
reverseDo:aBlock
|
|
1385 |
"evaluate the argument, aBlock for every element in the collection
|
|
1386 |
in reverse order"
|
|
1387 |
|
32
|
1388 |
|sz "{ Class:SmallInteger }"|
|
1
|
1389 |
|
32
|
1390 |
sz := self size.
|
|
1391 |
sz to:1 by:-1 do:[:index |
|
1
|
1392 |
aBlock value:(self at:index).
|
|
1393 |
]
|
77
|
1394 |
"
|
|
1395 |
#(one two three four five six) reverseDo:[:element | Transcript showCr:element]
|
|
1396 |
"
|
1
|
1397 |
!
|
|
1398 |
|
|
1399 |
collect:aBlock
|
|
1400 |
"evaluate the argument, aBlock for every element in the collection
|
|
1401 |
and return a collection of the results"
|
|
1402 |
|
|
1403 |
|newCollection
|
32
|
1404 |
sz "{ Class:SmallInteger }"|
|
1
|
1405 |
|
32
|
1406 |
sz := self size.
|
|
1407 |
newCollection := self species new:sz.
|
|
1408 |
1 to:sz do:[:index |
|
1
|
1409 |
newCollection at:index put:(aBlock value:(self at:index)).
|
|
1410 |
].
|
|
1411 |
^ newCollection
|
77
|
1412 |
"
|
|
1413 |
#(one two three four five six) collect:[:element | element asUppercase]
|
|
1414 |
"
|
1
|
1415 |
!
|
|
1416 |
|
|
1417 |
select:aBlock
|
|
1418 |
"evaluate the argument, aBlock for every element in the collection
|
|
1419 |
and return a collection of all elements for which the block return
|
|
1420 |
true"
|
|
1421 |
|
|
1422 |
|element newColl
|
32
|
1423 |
sz "{ Class:SmallInteger }"|
|
1
|
1424 |
|
32
|
1425 |
sz := self size.
|
|
1426 |
newColl := OrderedCollection new:sz.
|
|
1427 |
1 to:sz do:[:index |
|
1
|
1428 |
element := self at:index.
|
|
1429 |
(aBlock value:element) ifTrue:[
|
|
1430 |
newColl add:element
|
|
1431 |
].
|
|
1432 |
].
|
|
1433 |
^ newColl
|
77
|
1434 |
"
|
|
1435 |
#(one two three four five six) select:[:element | element startsWith:'f']
|
|
1436 |
"
|
1
|
1437 |
! !
|