dranges.associative

Functions acting upon associative arrays: filtering them, etc.

License:
Boost License 1.0.

Authors:
Philippe Sigaud

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

V[K] dup (K, V)(V[K] aa);
Simple helper function to duplicate an associative array.

struct AsRange (K,V);
AsRange!(K,V) asRange (K, V)(V[K] aa);
There may be a need for an 'asRange' function that takes a container and returns a range iterating over the entire content. In this particular case, asRange takes an associative array of type V[K] and returns (K,V) tuples. It's a random-access range, with a length. opIndexAssign is defined and will change the original associative array.

Note:
I'm a bit ambivalent on this one. front/back returns tuples, because that's the entire content and because if you only want keys or values, you can use the homonym properties. But for opIndex (indexed on keys) you already know the key, so it returns only a value. The same for opIndexApply. I'm wondering if that's OK or not.

Example:
auto aa = ["Hello":5, "World!":6, "a":99, "":0, "Howdy":6];
auto aa_range = aa.asRange;
assert(equal(aa_range, [tuple("a",99), tuple("",0), tuple("Hello",5), tuple("World!",6), tuple("Howdy",6)][]));
assert(aa_range.length == 5); // has a length
assert(aa_range["Hello"] == 5); // opIndex. Returns a value.
aa_range["Hello"] = 100; // opIndexAssign.
assert(aa["Hello"] == 100); // changes the original AA.
aa["Howdy"] = 66; // If we change the original AA after .asRange was called
assert(aa_range["Howdy"] == 66); // The range presents the udpated view.


Unqual!(ElementType!(VR))[ElementType!(KR)] toAssoc (KR, VR)(KR keysRange, VR valuesRange);


Unqual!(ElementType!(VR))[ElementType!(KR)] addToAssoc (KR, VR)(Unqual!(ElementType!(VR))[ElementType!(KR)] aa, KR keysRange, VR valuesRange);


Unqual!(ElementType!(VR))[ElementType!(KR)] addToAssoc (KR, VR)(Unqual!(ElementType!(VR))[ElementType!(KR)] aa, KR keysRange, VR valuesRange, ElementType!(VR) delegate(ElementType!(VR), ElementType!(VR)) onCollision);


V[K] append (K, V)(V[K] aa, K key, V value);


V[K] prepend (K, V)(V[K] aa, K key, V value);


V[K] filterOnKeys (alias pred, K, V)(V[K] aa);
Filtering on keys

template isKV (K,V)
A constraint template to detect associative arrays of a certain type. It's a curried (nested) template: isKV !(int,string) is itself a template.

Example:
alias isKV(int,string) IntString;
assert(IntString!(string[int]));
assert(!IntString!(double[int]));


V[K] merge (K, V, T...)(V[K] aa, T aas);
Merge AA.

V[K] mergeWith (alias fun = "c", K, V, T...)(V[K] aa, T aas);
Merge AA, with a merging function if some keys exist on more than one array.

ElementType!(R).Types[1][ElementType!(R).Types[0]] toAA (R)(R r);


ElementType!(VR)[ElementType!(KR)] toAA (KR, VR)(KR keyRange, VR valueRange);


Page was generated with on Fri Nov 12 11:55:08 2010