[ENH] SequenceableCollection>>forceTo:paddingWith:
Stephan Rudlof
sr at evolgo.de
Tue Apr 11 00:47:16 UTC 2000
Andres,
I think your implementation of
!SequenceableCollection methodsFor: 'copying' stamp: 'SqR!!!! 4/10/2000
09:20'!
forceTo: length paddingWith: elem
"Force the length of the collection to length, padding
if necessary with elem. Note that this makes a copy.
Much much faster, SqR!! 4/10/2000 09:19"
| newCollection copyLen padded toPad thisPad |
newCollection _ self species new: length.
copyLen _ self size min: length.
"Copy bulk"
newCollection replaceFrom: 1 to: copyLen
with: self startingAt: 1.
"Fast out"
copyLen = length ifTrue: [^newCollection].
"Fill at the end. Tres cool method"
newCollection at: copyLen + 1 put: elem.
toPad _ length - copyLen. padded _ 1.
[padded = toPad] whileFalse:
[
thisPad _ padded min: toPad - padded.
newCollection
replaceFrom: copyLen + padded + 1 to:
copyLen + padded + thisPad
with: newCollection startingAt: copyLen
+ 1.
padded _ padded + thisPad.
].
^newCollection! !
is fast (assumed it works), because >>replaceFrom:to:startingAt: is
realized by calling primitive: 105 for many subclasses. But it is
complicated.
I think it would be better to create a new method like
SequenceableCollection>>
from: start to: stop put: anElem
"Replaces elements from start to stop by anElem for all valid indices."
start to: (stop min: self size) do: [:ix | self at: ix put: anElem].
^self
, which *could* be implemented as a primitive for the subclasses with
fast >>replace:... primitive,
and then change >>forceTo:paddingWith: to
SequenceableCollection>>
forceTo: length paddingWith: elem
"Force the length of the collection to length, padding if necissary
with elem. Note that this makes a copy."
| newCollection copyLen |
newCollection _ self species new: length.
length > self size ifTrue: [self from: size + 1 to: length put:
anElem].
copyLen _ self size min: length.
"Copy bulk"
newCollection replaceFrom: 1 to: copyLen
with: self startingAt: 1.
^ newCollection
.
Note: I haven't compiled this code.
Regards,
Stephan
P.S.: Your Squeak (2.6) is not very up to date...
--
Stephan Rudlof (sr at evolgo.de)
"Genius doesn't work on an assembly line basis.
You can't simply say, 'Today I will be brilliant.'"
-- Kirk, "The Ultimate Computer", stardate 4731.3
More information about the Squeak-dev
mailing list
|