Associative array key order
bearophile
bearophileHUGS at lycos.com
Wed Jul 31 08:05:06 PDT 2013
Daniel Kozak:
> is there a way for AA to behave same as PHP?
D associative arrays are based on hashing, so they do not keep
the order of the items. This is true in Python too. The Python
standard library has a hash-based ordered dict that keeps the
insertion order of the keys. There is no such data structure in
Phobos.
You could write one yourself (wrapping an associative array and
using a linked list. I remember there is a trick to find the
address of a D associative array key given its value address, but
I don't remember it and it's not portable).
An alternative solution is to use the Phobos AVL-based tree as an
associative array, but it's less convenient and the search
complexity is logarithmic instead of constant in the average case.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list