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