Re: remove elements during the iteration on NSMutableDictionary
Re: remove elements during the iteration on NSMutableDictionary
- Subject: Re: remove elements during the iteration on NSMutableDictionary
- From: "Michael Ash" <email@hidden>
- Date: Wed, 6 Aug 2008 22:29:33 -0400
On Wed, Aug 6, 2008 at 6:08 PM, Andrew Merenbach
<email@hidden> wrote:
>
> On Aug 6, 2008, at 3:01 PM, mm w wrote:
>
>> i will return the question why fast enumerator prevents against this?
>> maybe in-memory Pile?, you should have answered to the question alone
>
> Without knowing the workings of fast enumeration, I am at a loss as to why
> it would prevent it -- unless it has something to do with the fast
> enumerator not wanting to make an copy of the array before iterating, a copy
> which might slow things down.
It is extremely difficult, in the general case, to efficiently support
mutation during enumeration.
Take an array as a simple case. Let's say you delete every
odd-numbered element during enumeration. In an NSArray, the remaining
elements move down to occupy the newly emptied slots. If you want
sensible enumeration, you need to either decouple the enumeration from
the storage (which means copying the array, which is inefficient) or
you need to somehow track all of the enumerators currently active on
the array so that you can re-point them when you make a change (which
is difficult and potentially inefficient).
In the more general case, you even have open-ended semantic questions.
For example, let's say that during enumeration you add a new object to
the end of the array. Should that object be enumerated or not? How
about if you add the new object to the beginning of the array? If you
delete an object that you haven't enumerated yet, should it still be
enumerated or should it now be skipped?
For more complicated data structures, it goes from merely being
difficult to being completely nuts. Adding a new entry to a
dictionary, for example, can trigger a memory reallocation and a
subsequent rehash which completely shuffles the order of the objects
within. If you're in the middle of enumerating the dictionary when
that happens, how can you sensibly continue?
For one interesting technique, check out TransactionKit
(http://transactionkit.sourceforge.net/). With those collections,
mutation during enumeration doesn't affect the enumeration at all.
It's as if you were iterating a copy of the collection, but without
the cost of doing a full copy. However, this design does create
significant overhead for all operations, so once again, it's a
tradeoff.
Since there are multiple strategies for dealing with mutation during
enumeration and multiple desired semantics, Apple has decided that
rather than incurring a bunch of overhead and forcing semantics that
may be undesirable, they'll simply disallow it. For cases where you
really need it, it's relatively easy to simply enumerate a copy, or
accumulate changes for batch modification afterwards.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden