www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Has AA .keys the same order as .values?

reply "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
Hi,

I need to interleave multiple arrays stored in an AA into a 
single array, and the keys of the AA need to be stored separately 
in the same order as the interleaved data. As an example:

auto aa = [
   "1" : [1, 2],
   "2" : [3, 4],
   "3" : [5, 6],
   "4" : [7, 8]
]

auto values = aa.values;
auto keys =  aa.keys;

//Store keys, and interleave values

I did a quick test and values has the same order as keys, but is 
this guaranteed to be the case?
Jul 30 2012
next sibling parent =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
On 30-07-2012 18:00, Rene Zwanenburg wrote:
 Hi,

 I need to interleave multiple arrays stored in an AA into a single
 array, and the keys of the AA need to be stored separately in the same
 order as the interleaved data. As an example:

 auto aa = [
    "1" : [1, 2],
    "2" : [3, 4],
    "3" : [5, 6],
    "4" : [7, 8]
 ]

 auto values = aa.values;
 auto keys =  aa.keys;

 //Store keys, and interleave values

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

Ordering is not guaranteed at all in AAs. -- Alex Rønne Petersen alex lycus.org http://lycus.org
Jul 30 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 30 2012
prev sibling next sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
 Ordering is not guaranteed at all in AAs.

I don't want .keys and .values to have the same order as the order in which it was filled, I understand this is impossible. What I'd like is to have aa.values[i] == aa[aa.keys[i]]. I think this is easy to guarantee with typical AA implementations.
Jul 30 2012
prev sibling next sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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.
Jul 30 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Rene Zwanenburg:

 Great, thanks.

See: http://d.puremagic.com/issues/show_bug.cgi?id=8473 http://forum.dlang.org/thread/wztazusqjoispivajifh forum.dlang.org Bye, bearophile
Jul 30 2012