[BUG]Collection>>removeAll:
Richard A. O'Keefe
squeak-dev at lists.squeakfoundation.org
Fri Sep 6 04:06:25 UTC 2002
Brian Keefer <mgomes21 at cox.net> wrote:
!OrderedCollection methodsFor: 'adding' 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]! !
This might be a nice method to have around anyway.
However, surely it's a method for _removing_ things?
!OrderedCollection methodsFor: 'removing' stamp: 'bmk 9/5/2002 15:47'!
squeegee: sentinel "remove all instances of the sentinel, and squish down the array"
|tightIndex|
tightIndex := firstIndex.
1 to: self size do: [:fluffedIndex| "probably safer to manually iterate here than trust #do:"
(self at: fluffedIndex) == sentinel ifFalse:[
self at:tightIndex put: (self at:fluffedIndex).
tightIndex := tightIndex+1.
].
].
self truncate:tightIndex -1
The problem here is that #truncate: wants a *size* for the OrderedCollection
but it is given an *index* in the array. For example, suppose originally
firstIndex = 42.
lastIndex = 178.
self occurrencesOf: sentinel = 20.
136 elements will be checked, 116 moved down, and tightIndex will end
up as 42 + 116 + 1 = 159. So there will be a call to
self truncate: 158.
Unfortunately, there are not that many elements in the sequence.
removeOccurrencesOf: anItem
|target|
target := firstIndex - 1.
firstIndex to: lastIndex do: [:index |
(self at: index) = anItem ifFalse: [
array at: (target := target + 1) put: (array at: index)]].
target + 1 to: lastIndex do: [:index |
array at: index put: nil].
lastIndex := target
would seem to do the job, and could be useful in its own right.
More information about the Squeak-dev
mailing list
|