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:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
SequenceableCollections have ordered elements which can be accessed via
|
|
26 |
an index. SequenceableCollection is an abstract class - there are no
|
|
27 |
instances of it in the system.
|
|
28 |
|
42
|
29 |
$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.7 1994-01-12 19:10:37 claus Exp $
|
1
|
30 |
|
|
31 |
written spring 89 by claus
|
|
32 |
'!
|
|
33 |
|
|
34 |
!SequenceableCollection class methodsFor:'instance creation'!
|
|
35 |
|
|
36 |
new:size withAll:element
|
|
37 |
"return a new Collection of size, where all elements are
|
|
38 |
initialized to element"
|
|
39 |
|
|
40 |
|newCollection|
|
|
41 |
|
|
42 |
newCollection := self new:size.
|
|
43 |
newCollection atAllPut:element.
|
|
44 |
^ newCollection
|
|
45 |
! !
|
|
46 |
|
|
47 |
!SequenceableCollection methodsFor:'accessing'!
|
|
48 |
|
|
49 |
first
|
|
50 |
"return the first element"
|
|
51 |
|
|
52 |
^ self at:1
|
|
53 |
!
|
|
54 |
|
|
55 |
last
|
|
56 |
"return the last element"
|
|
57 |
|
|
58 |
^ self at:(self size)
|
32
|
59 |
!
|
|
60 |
|
|
61 |
at:index ifAbsent:exceptionBlock
|
|
62 |
"return the element at index if valid. If the index is invalid,
|
|
63 |
return the result of evaluating the exceptionblock"
|
|
64 |
|
|
65 |
((index < 1) or:[index > self size]) ifTrue:[
|
|
66 |
^ exceptionBlock value
|
|
67 |
].
|
|
68 |
^ self at:index
|
|
69 |
|
|
70 |
"#(1 2 3) at:4 ifAbsent:['no such index']"
|
1
|
71 |
! !
|
|
72 |
|
13
|
73 |
!SequenceableCollection methodsFor:'queries'!
|
|
74 |
|
|
75 |
firstIndex
|
|
76 |
"return the first elements index"
|
|
77 |
|
|
78 |
^ 1
|
|
79 |
!
|
|
80 |
|
|
81 |
lastIndex
|
|
82 |
"return the last elements index"
|
|
83 |
|
|
84 |
^ self size
|
|
85 |
!
|
|
86 |
|
|
87 |
size
|
|
88 |
"return the number of elements in the collection.
|
|
89 |
concrete implementations must define this"
|
|
90 |
|
|
91 |
^ self subclassResponsibility
|
|
92 |
! !
|
|
93 |
|
1
|
94 |
!SequenceableCollection methodsFor:'comparing'!
|
|
95 |
|
|
96 |
= aCollection
|
|
97 |
"return true if the receiver and aCollection represent collections
|
|
98 |
with equal contents."
|
|
99 |
|
|
100 |
|index "{ Class: SmallInteger }"
|
|
101 |
stop "{ Class: SmallInteger }" |
|
|
102 |
|
|
103 |
(aCollection == self) ifTrue:[^true].
|
|
104 |
(aCollection isKindOf:SequenceableCollection) ifFalse:[^false].
|
|
105 |
|
|
106 |
stop := self size.
|
|
107 |
stop == (aCollection size) ifFalse:[^false].
|
|
108 |
index := 1.
|
|
109 |
[index <= stop] whileTrue:[
|
|
110 |
(self at:index) = (aCollection at:index) ifFalse:[^false].
|
|
111 |
index := index + 1
|
|
112 |
].
|
|
113 |
^ true
|
|
114 |
!
|
|
115 |
|
|
116 |
startsWith:aCollection
|
|
117 |
"return true, if the receivers first elements match those
|
|
118 |
of aCollection"
|
|
119 |
|
|
120 |
|index "{ Class: SmallInteger }"
|
|
121 |
stop "{ Class: SmallInteger }" |
|
|
122 |
|
|
123 |
(aCollection == self) ifTrue:[^true].
|
|
124 |
(aCollection isKindOf:SequenceableCollection) ifFalse:[^false].
|
|
125 |
|
|
126 |
stop := aCollection size.
|
|
127 |
stop > self size ifTrue:[^false].
|
|
128 |
|
|
129 |
index := 1.
|
|
130 |
[index <= stop] whileTrue:[
|
|
131 |
(self at:index) = (aCollection at:index) ifFalse:[^false].
|
|
132 |
index := index + 1
|
|
133 |
].
|
|
134 |
^ true
|
|
135 |
|
|
136 |
"'abcde' startsWith:#($a $b $c)"
|
|
137 |
"#[1 2 3 4] startsWith:#(1 2 3)"
|
|
138 |
"#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3)"
|
|
139 |
!
|
|
140 |
|
|
141 |
endsWith:aCollection
|
|
142 |
"return true, if the receivers last elements match those
|
|
143 |
of aCollection"
|
|
144 |
|
|
145 |
|index1 "{ Class: SmallInteger }"
|
|
146 |
index2 "{ Class: SmallInteger }"
|
|
147 |
stop "{ Class: SmallInteger }" |
|
|
148 |
|
|
149 |
(aCollection == self) ifTrue:[^true].
|
|
150 |
(aCollection isKindOf:SequenceableCollection) ifFalse:[^false].
|
|
151 |
|
|
152 |
stop := aCollection size.
|
|
153 |
stop > self size ifTrue:[^false].
|
|
154 |
|
|
155 |
index1 := self size.
|
13
|
156 |
index2 := stop.
|
1
|
157 |
[index2 > 0] whileTrue:[
|
|
158 |
(self at:index1) = (aCollection at:index2) ifFalse:[^false].
|
|
159 |
index1 := index1 - 1.
|
|
160 |
index2 := index2 - 1
|
|
161 |
].
|
|
162 |
^ true
|
|
163 |
|
|
164 |
"'abcde' endsWith:#($d $e)"
|
|
165 |
"#[1 2 3 4] endsWith:#(3 4)"
|
|
166 |
"#(1 2 3 4) asOrderedCollection endsWith:#(3 4)"
|
|
167 |
! !
|
|
168 |
|
|
169 |
!SequenceableCollection methodsFor:'copying'!
|
|
170 |
|
|
171 |
, aCollection
|
|
172 |
"return a new collection formed from concatenating the receiver with
|
|
173 |
the argument"
|
|
174 |
|
|
175 |
|newCollection
|
|
176 |
mySize "{ Class: SmallInteger }"
|
|
177 |
newSize "{ Class: SmallInteger }"
|
|
178 |
otherSize "{ Class: SmallInteger }"
|
|
179 |
dstIndex "{ Class: SmallInteger }"|
|
|
180 |
|
|
181 |
mySize := self size.
|
|
182 |
otherSize := aCollection size.
|
|
183 |
newSize := mySize + otherSize.
|
|
184 |
newCollection := self species new:newSize.
|
|
185 |
|
|
186 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1.
|
|
187 |
dstIndex := mySize + 1.
|
|
188 |
(aCollection isKindOf:SequenceableCollection) ifTrue:[
|
|
189 |
"yes, aCollection has indexed elements"
|
|
190 |
newCollection replaceFrom:dstIndex to:newSize
|
|
191 |
with:aCollection startingAt:1.
|
|
192 |
^ newCollection
|
|
193 |
] ifFalse:[
|
|
194 |
"no, enumerate aCollection"
|
|
195 |
aCollection do:[:element |
|
|
196 |
newCollection at:dstIndex put:element.
|
|
197 |
dstIndex := dstIndex + 1
|
|
198 |
]
|
|
199 |
].
|
|
200 |
^ newCollection
|
|
201 |
!
|
|
202 |
|
|
203 |
copyWith:newElement
|
|
204 |
"return a new collection consisting of receivers elements
|
|
205 |
plus the argument"
|
|
206 |
|
|
207 |
|newCollection mySize newSize|
|
|
208 |
|
|
209 |
mySize := self size.
|
|
210 |
newSize := mySize + 1.
|
|
211 |
newCollection := self species new:newSize.
|
|
212 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1.
|
|
213 |
newCollection at:newSize put:newElement.
|
|
214 |
^newCollection
|
|
215 |
!
|
|
216 |
|
|
217 |
copyWithout:anElement
|
|
218 |
"return a new collection consisting of receivers elements
|
|
219 |
without anElement (if it was present)"
|
|
220 |
|
|
221 |
|newCollection skipIndex
|
|
222 |
dstIndex "{ Class: SmallInteger }"
|
|
223 |
index "{ Class: SmallInteger }"
|
|
224 |
stop "{ Class: SmallInteger }" |
|
|
225 |
|
|
226 |
skipIndex := self indexOf:anElement startingAt:1.
|
|
227 |
(skipIndex == 0) ifTrue:[^ self copy].
|
|
228 |
stop := self size.
|
|
229 |
newCollection := self class new:(stop - 1).
|
|
230 |
dstIndex := 1.
|
|
231 |
index := 1.
|
|
232 |
[index <= stop] whileTrue:[
|
|
233 |
(index ~~ skipIndex) ifTrue:[
|
|
234 |
newCollection at:dstIndex put:(self at:index).
|
|
235 |
dstIndex := dstIndex + 1
|
|
236 |
].
|
|
237 |
index := index + 1
|
|
238 |
].
|
|
239 |
^ newCollection
|
|
240 |
!
|
|
241 |
|
|
242 |
copyFrom:start to:stop
|
|
243 |
"return a new collection consisting of receivers elements
|
|
244 |
between start and stop"
|
|
245 |
|
|
246 |
|newCollection newSize|
|
|
247 |
|
|
248 |
newSize := stop - start + 1.
|
|
249 |
newCollection := self class new:newSize.
|
|
250 |
newCollection replaceFrom:1 to:newSize with:self startingAt:start.
|
|
251 |
^ newCollection
|
|
252 |
!
|
|
253 |
|
|
254 |
copyFrom:start
|
|
255 |
"return a new collection consisting of receivers elements
|
|
256 |
from start to the end of the collection"
|
|
257 |
|
|
258 |
^ self copyFrom:start to:(self size)
|
|
259 |
!
|
|
260 |
|
|
261 |
copyTo:stop
|
|
262 |
"return a new collection consisting of receivers elements
|
|
263 |
from 1 up to index stop"
|
|
264 |
|
|
265 |
^ self copyFrom:1 to:stop
|
|
266 |
!
|
|
267 |
|
|
268 |
copyWithoutIndex:omitIndex
|
|
269 |
"return a new collection consisting of receivers elements
|
|
270 |
without the argument stored at omitIndex"
|
|
271 |
|
|
272 |
|copy|
|
|
273 |
|
|
274 |
copy := self class new:(self size - 1).
|
|
275 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1.
|
|
276 |
copy replaceFrom:omitIndex to:(copy size)
|
|
277 |
with:self startingAt:(omitIndex + 1).
|
|
278 |
^ copy
|
|
279 |
! !
|
|
280 |
|
|
281 |
!SequenceableCollection methodsFor:'filling and replacing'!
|
|
282 |
|
|
283 |
from:index1 to:index2 put:anObject
|
|
284 |
"replace the elements from index1 to index2 of the collection
|
|
285 |
by the argument, anObject"
|
|
286 |
|
|
287 |
|index "{ Class: SmallInteger }"
|
|
288 |
end "{ Class: SmallInteger }"|
|
|
289 |
|
|
290 |
index := index1.
|
|
291 |
end := index2.
|
|
292 |
[index <= end] whileTrue:[
|
|
293 |
self at:index put:anObject.
|
|
294 |
index := index + 1
|
|
295 |
]
|
|
296 |
!
|
|
297 |
|
|
298 |
atAllPut:anObject
|
|
299 |
"replace all elements of the collection by the argument, anObject"
|
|
300 |
|
32
|
301 |
self from:1 to:(self size) put:anObject
|
1
|
302 |
!
|
|
303 |
|
|
304 |
atAll:indexCollection put:anObject
|
|
305 |
"put anObject into all indexes from indexCollection in the receiver"
|
|
306 |
|
|
307 |
indexCollection do:[:index | self at:index put:anObject]
|
|
308 |
|
|
309 |
"(Array new:10) atAll:(1 to:5) put:0"
|
|
310 |
"(Array new:10) atAll:#(1 5 6 9) put:0"
|
|
311 |
!
|
|
312 |
|
|
313 |
replaceAll:oldObject by:newObject
|
|
314 |
"replace all oldObjects by newObject in the receiver"
|
|
315 |
|
32
|
316 |
1 to:self size do:[:index |
|
1
|
317 |
(self at:index) = oldObject ifTrue:[
|
|
318 |
self at:index put:newObject
|
|
319 |
]
|
|
320 |
]
|
|
321 |
!
|
|
322 |
|
|
323 |
replaceFrom:start with:replacementCollection
|
|
324 |
"replace elements starting at start with elements
|
|
325 |
taken from replacementCollection (starting at 1)"
|
|
326 |
|
|
327 |
^ self replaceFrom:start
|
|
328 |
to:(start + replacementCollection size - 1)
|
|
329 |
with:replacementCollection
|
|
330 |
startingAt:1
|
|
331 |
!
|
|
332 |
|
|
333 |
replaceFrom:start to:stop with:replacementCollection
|
|
334 |
"replace elements between index start and stop with elements
|
|
335 |
taken from replacementCollection (starting at 1)"
|
|
336 |
|
|
337 |
^ self replaceFrom:start
|
|
338 |
to:stop
|
|
339 |
with:replacementCollection
|
|
340 |
startingAt:1
|
|
341 |
!
|
|
342 |
|
|
343 |
replaceFrom:start to:stop with:replacementCollection startingAt:repStart
|
|
344 |
"replace elements between index start and stop with elements
|
|
345 |
taken from replacementCollection (starting at repStart)"
|
|
346 |
|
|
347 |
|srcIndex "{ Class: SmallInteger }"
|
|
348 |
dstIndex "{ Class: SmallInteger }"
|
|
349 |
end "{ Class: SmallInteger }" |
|
|
350 |
|
|
351 |
(replacementCollection == self) ifTrue:[
|
|
352 |
(repStart < start) ifTrue:[
|
|
353 |
" must do reverse copy "
|
|
354 |
srcIndex := repStart + (stop - start).
|
|
355 |
dstIndex := stop.
|
|
356 |
end := start.
|
|
357 |
[dstIndex >= end] whileTrue:[
|
|
358 |
self at:dstIndex put:(replacementCollection at:srcIndex).
|
|
359 |
srcIndex := srcIndex - 1.
|
|
360 |
dstIndex := dstIndex - 1
|
|
361 |
].
|
|
362 |
^ self
|
|
363 |
]
|
|
364 |
].
|
|
365 |
|
|
366 |
srcIndex := repStart.
|
|
367 |
dstIndex := start.
|
|
368 |
end := stop.
|
|
369 |
[dstIndex <= end] whileTrue:[
|
|
370 |
self at:dstIndex put:(replacementCollection at:srcIndex).
|
|
371 |
srcIndex := srcIndex + 1.
|
|
372 |
dstIndex := dstIndex + 1
|
|
373 |
]
|
|
374 |
!
|
|
375 |
|
|
376 |
withCRs
|
|
377 |
"return a new collection consisting of receivers elements
|
|
378 |
with all \-characters replaced by cr-characters"
|
|
379 |
|
13
|
380 |
^ self copy replaceAll:$\ by:(Character cr)
|
1
|
381 |
!
|
|
382 |
|
|
383 |
withoutCRs
|
|
384 |
"return a new collection consisting of receivers elements
|
|
385 |
with all cr-characters replaced by \-characters"
|
|
386 |
|
13
|
387 |
^ self copy replaceAll:(Character cr) by:$\
|
1
|
388 |
! !
|
|
389 |
|
|
390 |
!SequenceableCollection methodsFor:'adding & removing'!
|
|
391 |
|
|
392 |
addFirst:anElement
|
|
393 |
"prepend the argument, anElement to the collection"
|
|
394 |
|
|
395 |
|newSize|
|
|
396 |
|
|
397 |
newSize := self size + 1.
|
|
398 |
self grow:newSize.
|
|
399 |
self replaceFrom:2 to:newSize with:self startingAt:1.
|
|
400 |
self at:1 put:anElement
|
|
401 |
!
|
|
402 |
|
|
403 |
add:anElement
|
|
404 |
"append the argument, anElement to the collection"
|
|
405 |
|
|
406 |
|newSize|
|
|
407 |
|
|
408 |
newSize := self size + 1.
|
|
409 |
self grow:newSize.
|
|
410 |
self at:newSize put:anElement
|
|
411 |
!
|
|
412 |
|
|
413 |
add:anElement beforeIndex:index
|
|
414 |
"insert the first argument, anObject into the collection before slot index"
|
|
415 |
|
|
416 |
|newSize|
|
|
417 |
|
|
418 |
newSize := self size + 1.
|
|
419 |
self grow:newSize.
|
|
420 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index.
|
|
421 |
self at:index put:anElement
|
|
422 |
!
|
|
423 |
|
|
424 |
remove:anElement ifAbsent:aBlock
|
|
425 |
"search for anElement and, if present remove it; if not present
|
|
426 |
return the value of evaluating aBlock"
|
|
427 |
|
|
428 |
|any
|
|
429 |
dstIndex "{ Class: SmallInteger }"
|
|
430 |
sz "{ Class: SmallInteger }"|
|
|
431 |
|
|
432 |
dstIndex := 1.
|
|
433 |
any := false.
|
|
434 |
sz := self size.
|
|
435 |
1 to:sz do:[:srcIndex |
|
|
436 |
(anElement = (self at:srcIndex)) ifTrue:[
|
|
437 |
any := true
|
|
438 |
] ifFalse:[
|
|
439 |
(dstIndex ~~ srcIndex) ifTrue:[
|
|
440 |
self at:dstIndex put:(self at:srcIndex)
|
|
441 |
].
|
|
442 |
dstIndex := dstIndex + 1
|
|
443 |
]
|
|
444 |
].
|
|
445 |
any ifTrue:[
|
|
446 |
self grow:dstIndex - 1
|
|
447 |
] ifFalse:[
|
|
448 |
aBlock value
|
|
449 |
]
|
|
450 |
!
|
|
451 |
|
|
452 |
removeFromIndex:startIndex toIndex:endIndex
|
|
453 |
"remove the elements stored at indexes between startIndex and endIndex"
|
|
454 |
|
|
455 |
|newSize|
|
|
456 |
|
|
457 |
newSize := self size - endIndex + startIndex - 1.
|
|
458 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1).
|
|
459 |
self grow:newSize
|
|
460 |
!
|
|
461 |
|
|
462 |
removeIndex:index
|
|
463 |
"remove the argument stored at index"
|
|
464 |
|
|
465 |
self removeFromIndex:index toIndex:index
|
|
466 |
! !
|
|
467 |
|
|
468 |
!SequenceableCollection methodsFor:'searching'!
|
|
469 |
|
|
470 |
detect:aBlock ifNone:exceptionBlock
|
|
471 |
"find the first element, for which evaluation of the argument, aBlock
|
|
472 |
return true; if none does so, return the evaluation of exceptionBlock
|
|
473 |
|
|
474 |
reimplemented here for speed"
|
|
475 |
|
32
|
476 |
|stop "{ Class: SmallInteger }"
|
1
|
477 |
element|
|
|
478 |
|
32
|
479 |
stop := self size.
|
|
480 |
1 to:stop do:[:index |
|
1
|
481 |
element := self at:index.
|
|
482 |
(aBlock value:element) ifTrue:[
|
|
483 |
^ element
|
|
484 |
].
|
|
485 |
].
|
|
486 |
^ exceptionBlock value
|
|
487 |
!
|
|
488 |
|
|
489 |
indexOf:anElement
|
|
490 |
"search the collection for anElement;
|
|
491 |
if found, return the index otherwise return 0.
|
|
492 |
The comparison is done using = (i.e. equality test)."
|
|
493 |
|
32
|
494 |
^ self indexOf:anElement startingAt:1
|
1
|
495 |
!
|
|
496 |
|
|
497 |
indexOf:anElement ifAbsent:exceptionBlock
|
|
498 |
"search the collection for anElement;
|
|
499 |
if found, return the index otherwise return the value of the
|
|
500 |
exceptionBlock.
|
|
501 |
The comparison is done using = (i.e. equality test)."
|
|
502 |
|
|
503 |
|index|
|
|
504 |
|
|
505 |
index := self indexOf:anElement startingAt:1.
|
|
506 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
507 |
^ index
|
|
508 |
!
|
|
509 |
|
|
510 |
indexOf:anElement startingAt:start
|
|
511 |
"search the collection for anElement staring search at index start;
|
|
512 |
if found, return the index otherwise return 0.
|
|
513 |
The comparison is done using = (i.e. equality test)."
|
|
514 |
|
32
|
515 |
|startIndex "{ Class: SmallInteger }"
|
|
516 |
stop "{ Class: SmallInteger }" |
|
1
|
517 |
|
32
|
518 |
startIndex := start.
|
|
519 |
stop := self size.
|
|
520 |
startIndex to:stop do:[:index |
|
1
|
521 |
anElement = (self at:index) ifTrue:[^ index].
|
|
522 |
].
|
|
523 |
^ 0
|
|
524 |
!
|
|
525 |
|
|
526 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock
|
|
527 |
"search the collection for anElement starting search at start;
|
|
528 |
if found, return the index otherwise return the value of the
|
|
529 |
exceptionBlock.
|
|
530 |
The comparison is done using = (i.e. equality test)."
|
|
531 |
|
|
532 |
|index|
|
|
533 |
|
|
534 |
index := self indexOf:anElement startingAt:start.
|
|
535 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
536 |
^ index
|
|
537 |
!
|
|
538 |
|
|
539 |
identityIndexOf:anElement
|
|
540 |
"search the collection for anElement using identity compare (i.e. ==);
|
|
541 |
if found, return the index otherwise return 0."
|
|
542 |
|
32
|
543 |
^ self identityIndexOf:anElement startingAt:1
|
1
|
544 |
!
|
|
545 |
|
|
546 |
identityIndexOf:anElement ifAbsent:exceptionBlock
|
|
547 |
"search the collection for anElement using identity compare (i.e. ==);
|
|
548 |
if found, return the index otherwise return the value of the
|
|
549 |
exceptionBlock."
|
|
550 |
|
|
551 |
|index|
|
|
552 |
|
32
|
553 |
index := self identityIndexOf:anElement startingAt:1.
|
1
|
554 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
555 |
^ index
|
|
556 |
!
|
|
557 |
|
|
558 |
identityIndexOf:anElement startingAt:start
|
|
559 |
"search the collection for anElement staring search at index start
|
|
560 |
using identity compare (i.e. ==);
|
|
561 |
if found, return the index otherwise return 0."
|
|
562 |
|
32
|
563 |
|startIndex "{ Class: SmallInteger }"
|
|
564 |
stop "{ Class: SmallInteger }" |
|
1
|
565 |
|
32
|
566 |
startIndex := start.
|
|
567 |
stop := self size.
|
|
568 |
startIndex to:stop do:[:index |
|
1
|
569 |
anElement == (self at:index) ifTrue:[^ index].
|
|
570 |
].
|
|
571 |
^ 0
|
|
572 |
!
|
|
573 |
|
|
574 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock
|
|
575 |
"search the collection for anElement starting search at start;
|
|
576 |
if found, return the index otherwise return the value of the
|
|
577 |
exceptionBlock.
|
|
578 |
This one searches for identical objects (i.e. ==)."
|
|
579 |
|
|
580 |
|index|
|
|
581 |
|
|
582 |
index := self identityIndexOf:anElement startingAt:start.
|
|
583 |
(index == 0) ifTrue:[^ exceptionBlock value].
|
|
584 |
^ index
|
|
585 |
!
|
|
586 |
|
|
587 |
findFirst:aBlock
|
|
588 |
"find the first element, for which evaluation of the argument, aBlock
|
|
589 |
return true; return its index or 0 if none detected."
|
|
590 |
|
32
|
591 |
|stop "{ Class: SmallInteger }" |
|
1
|
592 |
|
32
|
593 |
stop := self size.
|
|
594 |
1 to:stop do:[:index |
|
1
|
595 |
(aBlock value:(self at:index)) ifTrue:[^ index].
|
|
596 |
].
|
|
597 |
^ 0
|
|
598 |
|
13
|
599 |
"#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)]"
|
|
600 |
"#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]]"
|
|
601 |
"'one.two.three' findFirst:[:c | (c == $.)]"
|
|
602 |
!
|
|
603 |
|
|
604 |
findLast:aBlock
|
|
605 |
"find the last element, for which evaluation of the argument, aBlock
|
|
606 |
return true; return its index or 0 if none detected."
|
|
607 |
|
32
|
608 |
|start "{ Class: SmallInteger }"|
|
13
|
609 |
|
32
|
610 |
start := self size.
|
|
611 |
start to:1 by:-1 do:[:index |
|
13
|
612 |
(aBlock value:(self at:index)) ifTrue:[^ index].
|
|
613 |
].
|
|
614 |
^ 0
|
|
615 |
|
|
616 |
"#(1 99 3 99 5 6) findLast:[:x | (x == 99)]"
|
|
617 |
"'one.two.three' findLast:[:c | (c == $.)]"
|
1
|
618 |
!
|
|
619 |
|
|
620 |
includes:anElement
|
|
621 |
"return true if the collection contains anElement; false otherwise.
|
|
622 |
Comparison is done using equality compare (i.e. =)."
|
|
623 |
|
32
|
624 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false].
|
1
|
625 |
^ true
|
|
626 |
! !
|
|
627 |
|
|
628 |
!SequenceableCollection methodsFor:'sorting & reordering'!
|
|
629 |
|
|
630 |
reverse
|
2
|
631 |
"reverse the order of the elements inplace"
|
1
|
632 |
|
|
633 |
|lowIndex "{ Class: SmallInteger }"
|
|
634 |
hiIndex "{ Class: SmallInteger }"
|
|
635 |
t|
|
|
636 |
|
32
|
637 |
hiIndex := self size.
|
|
638 |
lowIndex := 1.
|
1
|
639 |
[lowIndex < hiIndex] whileTrue:[
|
|
640 |
t := self at:lowIndex.
|
|
641 |
self at:lowIndex put:(self at:hiIndex).
|
|
642 |
self at:hiIndex put:t.
|
|
643 |
lowIndex := lowIndex + 1.
|
|
644 |
hiIndex := hiIndex - 1
|
|
645 |
]
|
|
646 |
"#(4 5 6 7 7) reverse"
|
13
|
647 |
"#(1 4 7 10 2 5) asOrderedCollection reverse"
|
1
|
648 |
!
|
|
649 |
|
|
650 |
quickSortFrom:begin to:end
|
|
651 |
"actual quicksort worker for sort-message"
|
|
652 |
|
|
653 |
|b "{ Class: SmallInteger }"
|
|
654 |
e "{ Class: SmallInteger }"
|
|
655 |
middleElement temp |
|
|
656 |
|
|
657 |
b := begin.
|
|
658 |
e := end.
|
|
659 |
middleElement := self at:((b + e) // 2).
|
|
660 |
|
|
661 |
[b < e] whileTrue:[
|
|
662 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1].
|
|
663 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1].
|
|
664 |
|
|
665 |
(b <= e) ifTrue:[
|
|
666 |
(b == e) ifFalse:[
|
|
667 |
temp := self at:b.
|
|
668 |
self at:b put:(self at:e).
|
|
669 |
self at:e put:temp
|
|
670 |
].
|
|
671 |
b := b + 1.
|
|
672 |
e := e - 1
|
|
673 |
]
|
|
674 |
].
|
|
675 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e].
|
|
676 |
(b < end) ifTrue:[self quickSortFrom:b to:end]
|
|
677 |
!
|
|
678 |
|
|
679 |
quickSortFrom:begin to:end with:aCollection
|
|
680 |
"actual quicksort worker for sortWith-message"
|
|
681 |
|
|
682 |
|b "{ Class: SmallInteger }"
|
|
683 |
e "{ Class: SmallInteger }"
|
|
684 |
middleElement temp |
|
|
685 |
|
|
686 |
b := begin.
|
|
687 |
e := end.
|
|
688 |
middleElement := self at:((b + e) // 2).
|
|
689 |
|
|
690 |
[b < e] whileTrue:[
|
|
691 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1].
|
|
692 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1].
|
|
693 |
|
|
694 |
(b <= e) ifTrue:[
|
|
695 |
(b == e) ifFalse:[
|
|
696 |
temp := self at:b.
|
|
697 |
self at:b put:(self at:e).
|
|
698 |
self at:e put:temp.
|
|
699 |
temp := aCollection at:b.
|
|
700 |
aCollection at:b put:(aCollection at:e).
|
|
701 |
aCollection at:e put:temp
|
|
702 |
].
|
|
703 |
b := b + 1.
|
|
704 |
e := e - 1
|
|
705 |
]
|
|
706 |
].
|
|
707 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e with:aCollection].
|
|
708 |
(b < end) ifTrue:[self quickSortFrom:b to:end with:aCollection]
|
|
709 |
!
|
|
710 |
|
|
711 |
quickSortFrom:begin to:end sortBlock:sortBlock
|
|
712 |
"actual quicksort worker for sort:-message"
|
|
713 |
|
|
714 |
|b "{ Class: SmallInteger }"
|
|
715 |
e "{ Class: SmallInteger }"
|
|
716 |
middleElement temp |
|
|
717 |
|
|
718 |
b := begin.
|
|
719 |
e := end.
|
|
720 |
middleElement := self at:((b + e) // 2).
|
|
721 |
|
|
722 |
[b < e] whileTrue:[
|
|
723 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1].
|
|
724 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1].
|
|
725 |
|
|
726 |
(b <= e) ifTrue:[
|
|
727 |
(b == e) ifFalse:[
|
|
728 |
temp := self at:b.
|
|
729 |
self at:b put:(self at:e).
|
|
730 |
self at:e put:temp
|
|
731 |
].
|
|
732 |
b := b + 1.
|
|
733 |
e := e - 1
|
|
734 |
]
|
|
735 |
].
|
|
736 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock].
|
|
737 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock]
|
|
738 |
!
|
|
739 |
|
|
740 |
quickSortFrom:begin to:end sortBlock:sortBlock with:aCollection
|
|
741 |
"actual quicksort worker for sort:with:-message"
|
|
742 |
|
|
743 |
|b "{ Class: SmallInteger }"
|
|
744 |
e "{ Class: SmallInteger }"
|
|
745 |
middleElement temp |
|
|
746 |
|
|
747 |
b := begin.
|
|
748 |
e := end.
|
|
749 |
middleElement := self at:((b + e) // 2).
|
|
750 |
|
|
751 |
[b < e] whileTrue:[
|
|
752 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1].
|
|
753 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1].
|
|
754 |
|
|
755 |
(b <= e) ifTrue:[
|
|
756 |
(b == e) ifFalse:[
|
|
757 |
temp := self at:b.
|
|
758 |
self at:b put:(self at:e).
|
|
759 |
self at:e put:temp.
|
|
760 |
temp := aCollection at:b.
|
|
761 |
aCollection at:b put:(aCollection at:e).
|
|
762 |
aCollection at:e put:temp
|
|
763 |
].
|
|
764 |
b := b + 1.
|
|
765 |
e := e - 1
|
|
766 |
]
|
|
767 |
].
|
|
768 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock with:aCollection].
|
|
769 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock with:aCollection]
|
|
770 |
!
|
|
771 |
|
|
772 |
bubbleSort
|
|
773 |
"sort the collection inplace using bubbleSort (sloooow)
|
|
774 |
- this one makes only sense to sort after inserting an element into
|
|
775 |
an already sorted collection (if at all)"
|
|
776 |
|
|
777 |
|index "{ Class: SmallInteger }"
|
|
778 |
index2 "{ Class: SmallInteger }"
|
|
779 |
end "{ Class: SmallInteger }"
|
|
780 |
smallest smallestIndex thisOne|
|
|
781 |
|
32
|
782 |
end := self size.
|
|
783 |
index := 1.
|
1
|
784 |
[index <= end] whileTrue:[
|
|
785 |
smallest := self at:index.
|
|
786 |
smallestIndex := index.
|
|
787 |
index2 := index + 1.
|
|
788 |
[index2 <= end] whileTrue:[
|
|
789 |
(self at:index2) < smallest ifTrue:[
|
|
790 |
smallestIndex := index2.
|
|
791 |
smallest := self at:index2
|
|
792 |
].
|
|
793 |
index2 := index2 + 1
|
|
794 |
].
|
|
795 |
(smallestIndex ~~ index) ifTrue:[
|
|
796 |
thisOne := self at:index.
|
|
797 |
self at:index put:smallest.
|
|
798 |
self at:smallestIndex put:thisOne
|
|
799 |
].
|
|
800 |
index := index + 1
|
|
801 |
]
|
|
802 |
|
|
803 |
"#(1 16 7 98 3 19 4 0) bubbleSort"
|
|
804 |
!
|
|
805 |
|
|
806 |
sort
|
|
807 |
"sort the collection inplace. The elements are compared using
|
|
808 |
> and < i.e. they should offer a magnitude-like protocol."
|
13
|
809 |
|
32
|
810 |
|stop|
|
1
|
811 |
|
32
|
812 |
stop := self size.
|
|
813 |
(stop > 1) ifTrue:[
|
|
814 |
self quickSortFrom:1 to:stop
|
1
|
815 |
]
|
|
816 |
|
|
817 |
"#(1 16 7 98 3 19 4 0) sort"
|
|
818 |
!
|
|
819 |
|
|
820 |
sortWith:aCollection
|
|
821 |
"sort the receiver collection inplace, also sort aCollection with it.
|
|
822 |
Use, when you have a key collection to sort another collection with."
|
|
823 |
|
32
|
824 |
|stop|
|
1
|
825 |
|
32
|
826 |
stop := self size.
|
|
827 |
(stop > 1) ifTrue:[
|
|
828 |
self quickSortFrom:1 to:stop with:aCollection
|
1
|
829 |
]
|
|
830 |
|
|
831 |
"|c1 c2|
|
|
832 |
c1 := #(1 16 7 9).
|
|
833 |
c2 := #('one' 'sixteen' 'seven' 'nine').
|
|
834 |
c1 sortWith:c2.
|
|
835 |
c1 printNewline.
|
|
836 |
c2 printNewline"
|
|
837 |
!
|
|
838 |
|
|
839 |
sort:sortBlock
|
|
840 |
"sort the collection inplace using the 2-arg block sortBlock
|
|
841 |
for comparison. This allows any sort criteria to be implemented."
|
|
842 |
|
32
|
843 |
|stop|
|
1
|
844 |
|
32
|
845 |
stop := self size.
|
|
846 |
(stop > 1) ifTrue:[
|
|
847 |
self quickSortFrom:1 to:stop sortBlock:sortBlock
|
1
|
848 |
]
|
|
849 |
|
|
850 |
"#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b]"
|
|
851 |
"#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b]"
|
|
852 |
!
|
|
853 |
|
|
854 |
sort:sortBlock with:aCollection
|
|
855 |
"sort the collection inplace using the 2-arg block sortBlock
|
|
856 |
for comparison. Also reorder the elements in aCollection"
|
|
857 |
|
32
|
858 |
|stop|
|
1
|
859 |
|
32
|
860 |
stop := self size.
|
|
861 |
(stop > 1) ifTrue:[
|
|
862 |
self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection
|
1
|
863 |
]
|
|
864 |
|
|
865 |
"|c1 c2|
|
|
866 |
c1 := #(1 16 7 9).
|
|
867 |
c2 := #('one' 'sixteen' 'seven' 'nine').
|
|
868 |
c1 sort:[:a :b | a > b] with:c2.
|
|
869 |
c1 printNewline.
|
|
870 |
c2 printNewline"
|
|
871 |
! !
|
|
872 |
|
|
873 |
!SequenceableCollection methodsFor:'enumerating'!
|
|
874 |
|
|
875 |
do:aBlock
|
|
876 |
"evaluate the argument, aBlock for every element in the collection."
|
|
877 |
|
32
|
878 |
|stop "{ Class:SmallInteger }"|
|
1
|
879 |
|
32
|
880 |
stop := self size.
|
|
881 |
1 to:stop do:[:index |
|
1
|
882 |
aBlock value:(self at:index).
|
|
883 |
]
|
|
884 |
!
|
|
885 |
|
3
|
886 |
keysAndValuesDo:aTwoArgBlock
|
|
887 |
"evaluate the argument, aBlock for every element in the collection,
|
|
888 |
passing both index and element as arguments."
|
|
889 |
|
32
|
890 |
|stop "{ Class:SmallInteger }"|
|
3
|
891 |
|
32
|
892 |
stop := self size.
|
|
893 |
1 to:stop do:[:index |
|
3
|
894 |
aTwoArgBlock value:index value:(self at:index).
|
|
895 |
]
|
|
896 |
!
|
|
897 |
|
1
|
898 |
from:index1 to:index2 do:aBlock
|
|
899 |
"evaluate the argument, aBlock for the elements with index index1 to
|
|
900 |
index2 in the collection"
|
|
901 |
|
32
|
902 |
|start "{ Class:SmallInteger }"
|
1
|
903 |
stop "{ Class:SmallInteger }" |
|
|
904 |
|
32
|
905 |
start := index1.
|
1
|
906 |
stop := index2.
|
32
|
907 |
start to:stop do:[:index |
|
1
|
908 |
aBlock value:(self at:index).
|
|
909 |
]
|
|
910 |
!
|
|
911 |
|
42
|
912 |
from:index1 to:index2 reverseDo:aBlock
|
|
913 |
"evaluate the argument, aBlock for the elements with index index1 to
|
|
914 |
index2 in the collection. Step in reverse order"
|
|
915 |
|
|
916 |
|start "{ Class:SmallInteger }"
|
|
917 |
stop "{ Class:SmallInteger }" |
|
|
918 |
|
|
919 |
start := index1.
|
|
920 |
stop := index2.
|
|
921 |
stop to:start by:-1 do:[:index |
|
|
922 |
aBlock value:(self at:index).
|
|
923 |
]
|
|
924 |
!
|
|
925 |
|
32
|
926 |
with:aCollection do:aTwoArgBlock
|
1
|
927 |
"evaluate the argument, aBlock for successive elements from
|
|
928 |
each of the two collections self and aCollection.
|
|
929 |
aBlock must be a two-argument block"
|
|
930 |
|
32
|
931 |
|stop "{ Class: SmallInteger }" |
|
1
|
932 |
|
32
|
933 |
stop := self size.
|
|
934 |
1 to:stop do:[:index |
|
|
935 |
aTwoArgBlock value:(self at:index) value:(aCollection at:index).
|
1
|
936 |
]
|
|
937 |
!
|
|
938 |
|
|
939 |
reverseDo:aBlock
|
|
940 |
"evaluate the argument, aBlock for every element in the collection
|
|
941 |
in reverse order"
|
|
942 |
|
32
|
943 |
|sz "{ Class:SmallInteger }"|
|
1
|
944 |
|
32
|
945 |
sz := self size.
|
|
946 |
sz to:1 by:-1 do:[:index |
|
1
|
947 |
aBlock value:(self at:index).
|
|
948 |
]
|
|
949 |
!
|
|
950 |
|
|
951 |
collect:aBlock
|
|
952 |
"evaluate the argument, aBlock for every element in the collection
|
|
953 |
and return a collection of the results"
|
|
954 |
|
|
955 |
|newCollection
|
32
|
956 |
sz "{ Class:SmallInteger }"|
|
1
|
957 |
|
32
|
958 |
sz := self size.
|
|
959 |
newCollection := self species new:sz.
|
|
960 |
1 to:sz do:[:index |
|
1
|
961 |
newCollection at:index put:(aBlock value:(self at:index)).
|
|
962 |
].
|
|
963 |
^ newCollection
|
|
964 |
!
|
|
965 |
|
|
966 |
select:aBlock
|
|
967 |
"evaluate the argument, aBlock for every element in the collection
|
|
968 |
and return a collection of all elements for which the block return
|
|
969 |
true"
|
|
970 |
|
|
971 |
|element newColl
|
32
|
972 |
sz "{ Class:SmallInteger }"|
|
1
|
973 |
|
32
|
974 |
sz := self size.
|
|
975 |
newColl := OrderedCollection new:sz.
|
|
976 |
1 to:sz do:[:index |
|
1
|
977 |
element := self at:index.
|
|
978 |
(aBlock value:element) ifTrue:[
|
|
979 |
newColl add:element
|
|
980 |
].
|
|
981 |
].
|
|
982 |
^ newColl
|
|
983 |
! !
|