Safe to remove AA elements while iterating over it via .byKeyValue?
igor.khasilev at gmail.com
Mon Sep 28 06:10:58 UTC 2020
On Sunday, 27 September 2020 at 14:23:11 UTC, H. S. Teoh wrote:
> On Sun, Sep 27, 2020 at 01:02:04PM +0000, Per Nordlöw via
> Digitalmars-d-learn wrote:
>> Is it safe to remove AA-elements from an `aa` I'm iterating
>> over via aa.byKeyValue?
> No. Modifying a container while iterating over it is, in
> general, a bad idea (unless the container is designed to be
> used that way, but even then, such removal is generally
> restricted), because it often leads to highly counterintuitive
> results. In the case of AA's, removing an element may lead to
> a rehashing, which reorders elements, and your iteration may
> miss some elements or repeat some elements or terminate
> prematurely. Even without a rehashing, you may encounter
> inconsistent behaviours, like some elements going "missing".
I think sane rule for AA iterator cane be like this:
1. you must not visit keys removed during iteration regardless of
2. you must be able to visit all keys available when iteration
3. you may not visit keys added during iteration (as AA is
Looks like this can be implemented for open addressing hash map
without performance loss (in terms of speed). My has hmap
implementation support rules 2 and 3, I'll try to add rule 1 to
the list (right now it iterate over immutable copy of keys).
More information about the Digitalmars-d-learn