Revamping associative arrays
BCS
none at anon.com
Sat Oct 17 12:15:33 PDT 2009
Hello Moritz,
> On Sat, 17 Oct 2009 18:58:08 +0000, BCS wrote:
>
>> Hello Chris Nicholson-Sauls,
>>
>>> Idea: the .keys and .values properties, rather than creating arrays,
>>> could create iterable ranges with the smallest possible footprint,
>>> internally walking the tree structure.
>>>
>> what will this do?
>>
>> foreach(key; aa.keys)
>> if(Test(key))
>> aa.remove(key);
>
> It's undefined behavior.
> You shouldn't try to mutate the aa while iterating.
> I hope that will be fixed.
> It took me some time to find this out.
That's the reason for the above idiom. Under the current spec, the above
is totally valid and, as far as I know, the only valid way to remove all
elements fitting some test with only a single pass.
Whatever is chosen, the above code had better work or at a minimum, requiter
nothing more than the addition of a .dup
OTOH building .keys must do a pass of it's own so all that's gained is sugar.
But still, switching to a .keys that gets invalidated on updating the AA
will break existing code so making the fix simple should be kept in mind.
More information about the Digitalmars-d
mailing list