[ENH] SequenceableCollection>>forceTo:paddingWith:

Andres Valloud avalloud at entrypoint.com
Tue Apr 11 19:31:52 UTC 2000


Hi Stephan.

> I think your implementation of forceTo:paddingWith:
> is fast (assumed it works), because >>replaceFrom:to:startingAt: is
> realized by calling primitive: 105 for many subclasses.

That's right. I like using primitives like that because it is fast and it's still
sending a message.

> But it is complicated. I think it would be better to [put the general padding
> stuff in SequenceableCollection and then use it in forceTo:paddingWith:].

I agree, and then we could also reimplement #atAllPut: among others. The other
stuff we could take care of is when a to:do: construct is used to fill a
collection with the same element. I like the from:to:put: name.

Andres.
-------------- next part --------------
'From Squeak2.6 of 11 October 1999 [latest update: #1578] on 11 April 2000 at 12:31:15 pm'!
"Change Set:		Fast SeqCollection Replaces
Date:			11 April 2000
Author:			Andres Valloud

Fast implementations of methods related to
copying a SequenceableCollection, padding
it with anObject, etc."!


!SequenceableCollection methodsFor: 'accessing' stamp: 'SqR!!!! 4/11/2000 12:10'!
atAll: anInterval put: anObject 
	"Put anObject at every index specified by the integer elements of 
	anInterval."

	anInterval increment == 1
		ifTrue: [self from: anInterval first to: anInterval last put: anObject]
		ifFalse: [anInterval do: [:index | self at: index put: anObject]]! !

!SequenceableCollection methodsFor: 'accessing' stamp: 'SqR!!!! 4/11/2000 12:06'!
atAllPut: anObject 
	"Put anObject at every one of the receiver's indices."

	| selfSize |

	selfSize _ self size.
	selfSize > 26 "First method faster from 27 accesses and on"
		ifTrue: [self from: 1 to: selfSize put: anObject]
		ifFalse: [1 to: selfSize do: [:index | self at: index put: anObject]]

! !

!SequenceableCollection methodsFor: 'accessing' stamp: 'SqR!!!! 4/11/2000 11:56'!
from: startIndex to: endIndex put: anObject
	"Put anObject in all indexes between startIndex 
	and endIndex. Very fast. Faster than to:do: for
	more than 26 writes. No range checks are 
	performed. Answer anObject"

	| written toWrite thisWrite |

	toWrite _ endIndex - startIndex + 1.
	toWrite <= 0 ifTrue: [^self].
	self at: startIndex put: anObject.
	written _ 1.
	[written = toWrite] whileFalse:
		[
			thisWrite _ written min: toWrite - written.
			self 
				replaceFrom: startIndex + written to: startIndex + written + thisWrite - 1
				with: self startingAt: startIndex.
			written _ written + thisWrite.
		].
	^anObject! !

!SequenceableCollection methodsFor: 'copying' stamp: 'SqR!!!! 4/11/2000 12:26'!
copyAt: anIndex put: anElement
	"Answer a copy of the receiver with anElement inserted at anIndex."

	"^self copyReplaceFrom: anIndex to: anIndex with: (Array with: anElement)"
	^(self copyFrom: 1 to: self size) at: anIndex put: anElement; yourself
! !

!SequenceableCollection methodsFor: 'copying' stamp: 'SqR!!!! 4/11/2000 12:07'!
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 |

	newCollection _ self species new: length.
	copyLen _ self size min: length.

	"Copy bulk"
	newCollection replaceFrom: 1 to: copyLen with: self startingAt: 1.

	"Fill at the end"
	newCollection from: copyLen + 1 to: length put: elem.

	^newCollection! !




More information about the Squeak-dev mailing list