[BUG]Collection>>removeAll:
Brian Keefer
mgomes21 at cox.net
Sat Sep 7 03:12:48 UTC 2002
Torge Husfeldt wrote:
>
> Brian,
>
> If i still can read this last version reintroduces the slack problem?!
> One more point: do you really think dowithindex would be so problematic?
>
The head slack is left alone (in 4.cs) by using OrderedCollection's normal
access methods (self at:). In the .3.cs, I was *thinking* about
manipulating the array instvar directly, and that resulted in a muddled
#truncate:. Man, that was full of stupid.
Bonus chocolate goes to whoever can find a way to 1.) Maintain the
approximate head/tail slack ratio 2.) without extra passes through the
array (or other untowardly overhead).
I specifically avoided #doWithIndex: because I never heard of it. It was
slightly easier to use #to:do: than look up a replacement.
This version (too many versions for such a simple thing!) follows the
fixes reccommended by Mr. O'Keefe. I'm even going along with the #=
sentinel comparison in removeOccurancesOf:.
On a completely different matter, is Object>>hash safe? It seems to use
the OOP address (as in a C pointer), which could change with each
incremental GC.
-------------- next part --------------
'From Squeak3.2gamma of 15 January 2002 [latest update: #4881] on 6 September 2002 at 11:11:08 pm'!
!OrderedCollection methodsFor: 'removing' stamp: 'bmk 9/6/2002 23:10'!
removeAll: aCollection
|removalSet sentinel|
removalSet := aCollection as: Bag. "#asBag is unfriendly to streams"
sentinel := Object new.
self withIndexDo: [:value :index|
(removalSet includes:(self at:index)) ifTrue:[
self at:index put:sentinel.
].
].
self removeOccurancesOf:sentinel.
^aCollection. "don't know why. Nobody wants it"! !
!OrderedCollection methodsFor: 'removing' stamp: 'bmk 9/6/2002 23:10'!
removeOccurancesOf: sentinel "Though this was made with consideration towards sentinels, any regular container member could be sent. It just reads better using the name sentinel than anItem. What would ``valueOrAnItem'' mean?"
|valuesOnlyIndex|
valuesOnlyIndex := 1.
self withIndexDo: [:valueOrSentinel :valuesOrSentinelsIndex| "VOSIndex unnecessary"
sentinel = valueOrSentinel ifFalse:[ "As long as sentinel class -> Object, #= means #==."
self at:valuesOnlyIndex put: valueOrSentinel.
valuesOnlyIndex := valuesOnlyIndex + 1.
].
].
self truncate:valuesOnlyIndex - 1
! !
!OrderedCollection methodsFor: 'removing' stamp: 'bmk 9/5/2002 15:45'!
truncate:aSize
|newLastIndex|
aSize > self size ifTrue:[ self error: 'cannot truncate larger']
ifFalse:[
newLastIndex := firstIndex+aSize-1.
newLastIndex+1 to: lastIndex do: [:position| array at: position put: nil].
lastIndex := newLastIndex]! !
More information about the Squeak-dev
mailing list
|