[squeak-dev] removing from a collection while enumerating it
Levente Uzonyi
leves at elte.hu
Thu Jul 4 08:20:49 UTC 2013
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
>
>
More information about the Squeak-dev
mailing list
|