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

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Sep 28 21:05:31 UTC 2020


On Mon, Sep 28, 2020 at 08:04:49PM +0000, ikod via Digitalmars-d-learn wrote:
> On Monday, 28 September 2020 at 19:18:20 UTC, H. S. Teoh wrote:
[...]
> > The problem with arbitrary, unrestricted modification of a container
> > while iterating over it, is that it inevitably leads to
> > counterintuitive results.
> 
> Thanks for your reply, and sorry if I was not clear, but I meant AA.
> AA is unordered container, so for me intuitive behavior for mutations
> visibility during iteration is next:
> 
> 1) you must not see removed keys. Let our keys are (unordered) A D C E
> F, you stay on D and remove E. Then E must not be seen on any future
> iteration steps.
> 
> 2) you may or may not see any inserted keys. As AA is unordered
> container there is no reason to expect anything about position of key
> you just inserted - it can fall before or after current iteration
> position, so implementation is free to show inserted keys or not. I
> prefer not to see new keys.
> 
> 3) you expect to visit all not removed keys exactly once.
> 
> Is it sane?
[...]

It sounds reasonable, but it does constrain your implementation. For
example, (3) is likely to break during a rehash. But not rehashing may
lead to other problems, depending on your hashing implementation.


T

-- 
Time flies like an arrow. Fruit flies like a banana.


More information about the Digitalmars-d-learn mailing list