Robustness of the built in associative array
Walter Bright
newshound at digitalmars.com
Mon Mar 27 22:02:06 PST 2006
"Oskar Linde" <oskar.lindeREM at OVEgmail.com> wrote in message
news:e00v0m$te2$3 at digitaldaemon.com...
> It turns out it is neither. It is definitely not a value type. The above
> function will in most cases alter the original associative array and work
> as expected. But it is not a reference type either -- in the few cases
> where adding a new key to the AA invokes a rehash, all hell breaks loose.
> The original associative array is suddenly *corrupted*! (The way to make
> the function work as expected is to make the map an inout argument, but
> that is beside my point.)
I hadn't realized that could happen, and it clearly needs to be fixed.
> A simple fix is to change the AA implementation so that AAs become MyAA*
> instead of aaA*[], with MyAA defined as:
>
> struct MyAA {
> AANode[] buckets;
> uint numberOfNodes;
> }
>
> You get a very slight additional lookup cost (double dereferencing to get
> to the buckets), but make the AA a pure reference type without any caveats
> and problems.
I'll make that change.
More information about the Digitalmars-d
mailing list