associative arrays: iteration is finally here

dsimcha dsimcha at yahoo.com
Wed Oct 28 08:41:48 PDT 2009


== Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> Walter has magically converted his work on T[new] into work on making
> associative arrays true templates defined in druntime and not considered
> very special by the compiler.
> This is very exciting because it opens up or simplifies a number of
> possibilities. One is that of implementing true iteration. I actually
> managed to implement last night something that allows you to do:
> int[int] aa = [ 1:1 ];
> auto iter = aa.each;
> writeln(iter.front.key);
> writeln(iter.front.value);
> Two other iterations are possible: by key and by value (in those cases
> iter.front just returns a key or a value).
> One question is, what names should these bear? I am thinking of makign
> opSlice() a universal method of getting the "all" iterator, a default
> that every container must implement.
> For AAs, there would be a "iterate keys" and "iterate values" properties
> or functions. How should they be called?
> Thanks,
> Andrei

Awesome, this definitely improves the interface, but how about the implementation?
 The current implementation, while fast for reading, is unbelievably slow for
adding elements, requires a heap allocation (read:  a global lock) on *every*
insertion, and generates an insane amount of false pointers.  Even if I succeed in
making heap scanning (mostly) precise, it's not clear if the current AA impl.
could easily be made to benefit, since it isn't template based.  It uses RTTI
internally instead, and the types it's operating on aren't known to the
implementation at compile time, so I wouldn't be able to use templates to generate
the bitmask at compile time.  The structs it uses internally would therefor have
to be scanned conservatively.



More information about the Digitalmars-d mailing list