Has AA .keys the same order as .values?

bearophile bearophileHUGS at lycos.com
Mon Jul 30 09:21:16 PDT 2012


Rene Zwanenburg:

> I did a quick test and values has the same order as keys, but 
> is this guaranteed to be the case?

In Python they they are guaranteed to have the same order (if you 
don't modify the associative array in the loops).

In D the current implementation gives them in the same order 
(again if you don't modify the associative array in the 
meantime), because this is the most natural way to implement the 
algorithm. But as far as I know there is no guarantee this is 
always true.

To solve this problem I have suggested to introduce another AA 
pseudomethod, named byPair, that returns a lazy range of 
key-value tuples, similar to the dict.iteritems() method of 
Python2:

http://d.puremagic.com/issues/show_bug.cgi?id=5466

But Andrei has not introduced this enhancement because it 
requires to import the std.typecons module from the object.d, 
tying it even more to Phobos.

Until a solution like byPair is introduced, I think the D docs 
should be updated, to state that the byKey and byValue yield 
corresponding keys-values.

---------------------

Alex Rønne Petersen:

> Ordering is not guaranteed at all in AAs.

This is a different thing.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list