[squeak-dev] removing from a collection while enumerating it

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Thu Jul 4 17:15:23 UTC 2013


For example, another strategy for removing would be to move elements on the
shortest side, that is increment firstIndex if removedIndex < (firstIndex +
lastIndex // 2).
This is currently not the case because the replaceFrom:to:with:startingAt:
did use a fragile memcopy instead of memmove, but I swear I saw such
implementation once in my life...
But if someone changes that someday, the reverseDo: strategy will break.

Nicolas


2013/7/4 Levente Uzonyi <leves at elte.hu>

> On Wed, 3 Jul 2013, Chris Muller wrote:
>
>  I have a case where I need to remove certain entries from an
>> OrderedCollection that will be called very frequently.  I therefore
>> want to avoid copying the collection (to safely remove while
>> enumerating) but it seems #reverseDo: can do what I want.
>>
>>  myCollection reverseDo: [ : each | each meetsSomeCriteria ifTrue: [
>> myCollection remove: each ] ]
>>
>> Does anyone see any problem with doing this?
>>
>
> Yes. Once the implementation of #remove:ifAbsent: changes, your code might
> break. You should use #removeAllSuchThat: as Bert suggested, which was
> designed to do exactly what you want. It has guaranteed O(n) runtime,
> unlike your #reverseDo: "hack", which has O(n*n).
>
> If your OrderedCollection is large, then you should probably use another
> data structure.
>
>
> Levente
>
>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20130704/d0943a9a/attachment.htm


More information about the Squeak-dev mailing list