Safe to remove AA elements while iterating over it via .byKeyValue?

Mike Parker aldacron at gmail.com
Tue Sep 29 06:30:59 UTC 2020


On Sunday, 27 September 2020 at 13:02:04 UTC, Per Nordlöw wrote:
> Is it safe to remove AA-elements from an `aa` I'm iterating 
> over via aa.byKeyValue?
>
> I'm currently doing this:
>
>     foreach (ref kv; aa.byKeyValue)
>     {
>         if (pred(kv.key))
>             aa.remove(kv.key); // ok?
>     }
>     if (aa.length == 0)
>         aa = null;
>
> Is there a better way?

If you're okay with the allocation that comes with it:

foreach(k; aa.keys)
{
     if(pred(key)) aa.remove(key);
}





More information about the Digitalmars-d-learn mailing list