MultiValueMap

A multi-valued mapping, where a key is mapped to one or more values. The map is sorted by keys for O(log(n)) lookup and retrieval, and O(n*log(n)) insertion.

KeyType is the type used for the map's keys. ValueType is the type used for the map's values. KeySort is a function that takes two operands and returns true if the first one is less than the second. KeyEquals is a function that takes two operands and returns true if they are equal.

Members

Functions

add
void add(KeyType k, ValueType v)

Adds a single key -> value pair to the map, with time complexity of O(n*log(n)) due to sorting the new entry by its key.

asAssociativeArray
ValueType[][KeyType] asAssociativeArray()

Gets this multivalue map as an associative array, where each key is mapped to a list of values.

clear
void clear()

Clears this map of all values.

contains
bool contains(KeyType k)

Determines if this map contains a value for the given key.

getAll
ValueType[] getAll(KeyType k)

Gets all values associated with a given key, allocated in a new array.

getFirst
Optional!ValueType getFirst(KeyType k)

Gets the first value associated with a given key, as per the order in which the values were inserted.

keys
KeyType[] keys()

Gets a list of all keys in this map, allocated in a new array.

length
size_t length()

Gets the number of unique keys in this map.

opApply
int opApply(int delegate(const ref KeyType, const ref ValueType) dg)

opApply implementation to allow iterating over this map by all pairs of keys and values.

opBinaryRight
ValueType[] opBinaryRight(string lhs)

Implements opBinaryRight for the "in" operator, such that k in m will resolve to the list of values for key k in the multivalue map m if that key exists, or null if not.

opIndex
inout(Entry)[] opIndex()

Implements the empty index operator, which just returns the entire list of entries in this map.

opIndex
ValueType opIndex(KeyType key)

Convenience overload to get the first value for a given key. Note: this will throw an exception if no values exist for the given key. To avoid this, use getFirst and deal with the missing value yourself.

remove
void remove(KeyType k)

Removes a key from the map, thus removing all values associated with that key.

toString
string toString()

Converts this map into a human-readable string which lists each key and all of the values for that key.

Static functions

fromAssociativeArray
MultiValueMap!(KeyType, ValueType, KeySort) fromAssociativeArray(ValueType[][KeyType] aa)

Constructs a multivalued map from an associative array.

fromAssociativeArray
MultiValueMap!(KeyType, ValueType, KeySort) fromAssociativeArray(ValueType[KeyType] aa)

Constructs a multivalued map from an associative array of single values.

Structs

Builder
struct Builder

An efficient builder that can be used to construct a multivalued map with successive add calls, which is more efficient than doing so directly due to the builder's deferred sorting.

Entry
struct Entry

The internal structure used to store each key and set of values.