Revamping associative arrays

bearophile bearophileHUGS at lycos.com
Sun Oct 18 14:47:54 PDT 2009


grauzone:

> In a perfect world, iterating over an AA would yield a tuple (key, 
> value). You could iterate over either the keys or values by iterating 
> over a "view" on the key or value list. I'm surprised Python doesn't do 
> that.

I don't know why Python has originally chosen to iterate on just keys, maybe it's a performance optimization (iterating on pairs is slower, because a single reference needs no allocation, while a tuple of 2 may need it, in practice CPython uses the same memory for the tuple, avoiding allocating it at every loop cycle), or maybe for one of the reasons I've explained.

I don't know if D can make looping on key-value of built-in AAs as fast as iterating on just keys or just values. Iterating on AA keys or values is a very common operation that must be fast.
 

> (I'm expecting that Andrei will replace the .key and .value properties 
> by "lazy" ranges, that don't allocate memory; so that aspect will be 
> alright.

At least, the range of the keys supports a O(1) opIn_r, I hope :-)

Bye,
bearophile



More information about the Digitalmars-d mailing list