| |
| Posted by H. S. Teoh in reply to Jethro | PermalinkReply |
|
H. S. Teoh
Posted in reply to Jethro
| On Fri, Apr 14, 2017 at 12:29:34AM +0000, Jethro via Digitalmars-d-learn wrote:
> Is there a way to retain the ordering of an associative array? When the elements are added then looped over using a foreach, the order is different.
An AA is implemented as an unordered hash. So the insertion order is usually unrelated to the foreach order. If you want keys to be sorted according to some order, consider using std.container.rbtree instead.
If you want to retain insertion order, you could wrap an AA inside a custom container that keeps track of insertion order. Something along these lines:
/* Warning: untested, proof-of-concept code. This assumes you
* never remove stuff from the AA. If you want to support
* .remove you'll have to do a bit more work, obviously.
*/
struct InsertionOrderAA(K,V) {
V[K] _impl;
K[] keyOrder;
void opIndexAssign(V value, K key) {
_impl[key] = value;
keyOrder ~= key;
}
Value opIndex(K key) {
return _impl[key];
}
void opApply(scope void delegate(K,V) dg) {
foreach (key; keyOrder) {
auto ret = dg(key, _impl[key]);
if (ret != 0) return ret;
}
}
}
T
--
I am not young enough to know everything. -- Oscar Wilde
|