Has AA .keys the same order as .values?

Rene Zwanenburg renezwanenburg at gmail.com
Mon Jul 30 09:26:05 PDT 2012


On Monday, 30 July 2012 at 16:21:17 UTC, bearophile wrote:
> 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

Great, thanks.


More information about the Digitalmars-d-learn mailing list