Enhanced C#
Language of your choice: library documentation
|
An sorted dictionary that allows multiple values to be associated with a single key. More...
An sorted dictionary that allows multiple values to be associated with a single key.
The keys must be comparable. By default, the values must also be comparable, but if the values do not support IComparable, you can set the comparison function to (a,b) => 0
to pretend all values are equal. A shorthand way to do this is to call the constructor with parameters of (null, null) which means "use default key comparison and treat all values as equal". If you do this, you should avoid calling this[key].Remove(value) or this[key].Contains(value), or the Remove method in the base class, since these methods will treat any value as a match.
An article about the BList classes is available.
Often when people want to be able to associate multiple values with a single key, they use a Dictionary with values of type List<T>. This approach is very inefficient (in terms of memory use) if most keys are only associated with one or two values; this class solves the problem using a single sorted B+ tree for all keys and all values. It requires, however, that both the keys and values are totally ordered (i.e. are sortable).
By default, keys and values are sorted using Comparer<T>.Default. This will work provided that the keys and values both implement the IComparable<T> interface. If they don't, you can pass custom comparison functions to the constructor instead (one comparison function for keys, and a second one for values).
Since it is derived from BList<T>, this class enjoys the space efficiency of a B+ tree and capabilities of a AListBase<K,V>, although it tends to be slower than Dictionary<K,V>.
Nested classes | |
struct | ValueList |
Represents the set of values associated with a particular key in a BMultiMap<K,V> collection. More... | |
Properties | |
ValueList | this[K key] [get] |
Gets a collection associated with the specified key. More... | |
IEnumerable< K > | Keys [get] |
IEnumerable< ValueList > | Values [get] |
Gets a list of collections of ValueList in this object. More... | |
Properties inherited from Loyc.Collections.IIndexed< K, BMultiMap< K, V >.ValueList > | |
V | this[K key] [get] |
Gets the value associated with the specified key. More... | |
Public Member Functions | |
BMultiMap (int maxNodeSize) | |
BMultiMap (int maxLeafSize, int maxInnerSize) | |
BMultiMap (Func< K, K, int > compareKeys) | |
Initializes the map with the specified key-comparer and default value comparer. This constructor can only be used if values are comparible with IComparable<V>. More... | |
BMultiMap (Func< K, K, int > compareKeys, Func< V, V, int > compareValues) | |
Initializes the map with the specified key and value comparers. More... | |
BMultiMap (Func< K, K, int > compareKeys, Func< V, V, int > compareValues, int maxNodeSize) | |
BMultiMap (Func< K, K, int > compareKeys, Func< V, V, int > compareValues, int maxLeafSize, int maxInnerSize) | |
bool | AddIfUnique (K key, V value) |
Adds a key-value pair if there is not already a pair that compares equal to the new one. More... | |
bool | ContainsKey (K key) |
Finds out whether the specified key is present. More... | |
int | FirstIndexOf (K key) |
Finds the lowest index of an item with the specified key. More... | |
bool | RemoveAny (K key) |
Removes one pair from the collection that matches the specified key. More... | |
int | Remove (K key, int maxToRemove) |
Removes up to a specified number of items from the collections that have the specified key. More... | |
int | RemoveAll (K key) |
Removes all the items from the collection whose key compares equal to the specified key. More... | |
bool | TryGetValue (K key, out V value) |
Finds a value associated with the specified key. More... | |
void | Add (K key, V value) |
int | FindLowerBound (K key) |
int | FindLowerBound (K key, out bool found) |
int | FindLowerBound (K key, out V value, out bool found) |
Finds the lowest index of an item that is equal to or greater than the specified item. More... | |
int | FindLowerBound (ref K key, out V value, out bool found) |
int | FindUpperBound (K key) |
Finds the index of the first item in the list whose key is greater than the specified key. More... | |
int | FindLowerBoundExact (ref K key, out V value, out bool found) |
Does the same thing as IndexOfExact, but with the same set of arguments as FindLowerBound including the value associated with the matching key. More... | |
int | IndexOfExact (K key) |
Specialized search function that finds the first index of an item whose key compares equal to the specified key, not only according to the comparison function for this collection, but also according to Object.Equals. This function works properly even if duplicate keys exist in addition that do NOT compare equal according to Object.Equals. More... | |
override long | CountSizeInBytes (int sizeOfElement, int sizeOfKey=8) |
Protected fields | |
readonly Func< K, K, int > | _compareKeys |
readonly Func< V, V, int > | _compareValues |
|
inline |
Initializes the map with the specified key-comparer and default value comparer. This constructor can only be used if values are comparible with IComparable<V>.
compareKeys | Key comparer, or null to use the default key comparer. |
|
inline |
Initializes the map with the specified key and value comparers.
compareKeys | Key comparer, or null to use the default key comparer. |
compareValues | Value comparer, or null to pretend all values are equal. It is useful to use null for this parameter when V does not implement IComparable. However, keep in mind that the Contains() and Remove() methods won't work in the normal way when all values are considered equal! |
|
inline |
Adds a key-value pair if there is not already a pair that compares equal to the new one.
|
inline |
Finds out whether the specified key is present.
key | Key to search for |
|
inline |
Finds the lowest index of an item that is equal to or greater than the specified item.
key | The key to find. |
value | The first value associated with the specified key, if the key was found, or default(V) if not. |
found | Set to true if the item was found, false if not. |
|
inline |
Does the same thing as IndexOfExact, but with the same set of arguments as FindLowerBound including the value associated with the matching key.
Referenced by Loyc.Collections.BMultiMap< Loyc.Collections.Impl.AListNode< K, T >, Loyc.Collections.Impl.AListInnerBase< K, T > >.IndexOfExact(), and Loyc.Collections.Impl.AListIndexer< int, T >.ReconstructIndex().
|
inline |
Finds the index of the first item in the list whose key is greater than the specified key.
key | The key to find. If passed by reference, when this method returns, item is set to the next greater item than the item you searched for, or left unchanged if there is no greater item. |
Referenced by Loyc.Collections.BMultiMap< Loyc.Collections.Impl.AListNode< K, T >, Loyc.Collections.Impl.AListInnerBase< K, T > >.Remove().
|
inline |
Finds the lowest index of an item with the specified key.
key | Key to search for |
This method is like FindLowerBound except that it returns -1 if the key was not found.
|
inline |
Specialized search function that finds the first index of an item whose key compares equal to the specified key, not only according to the comparison function for this collection, but also according to Object.Equals. This function works properly even if duplicate keys exist in addition that do NOT compare equal according to Object.Equals.
This method is useful when the items in this collection are sorted by hashcode (which is usually a bad idea, but occasionally useful).
Referenced by Loyc.Collections.Impl.AListIndexer< int, T >.NodeRemoved(), and Loyc.Collections.Impl.AListIndexer< int, T >.VerifyCorrectness().
|
inline |
Removes up to a specified number of items from the collections that have the specified key.
key | The key to remove. |
maxToRemove | Maximum number of items to remove. |
Referenced by Loyc.Collections.BMultiMap< Loyc.Collections.Impl.AListNode< K, T >, Loyc.Collections.Impl.AListInnerBase< K, T > >.RemoveAll().
|
inline |
Removes all the items from the collection whose key compares equal to the specified key.
key | The key to remove. |
|
inline |
Removes one pair from the collection that matches the specified key.
|
inline |
Finds a value associated with the specified key.
key | Key to find |
value | Set to the first (lowest) value associated with the key, or default(V) if the key was not found. |
Referenced by LeMP.Compiler.ProcessArguments().
|
staticprotected |
|
get |
Gets a collection associated with the specified key.
key |
This property always succeeds and does not actually search for the key you requested. It returns an object that represents the set of values associated with a key, but those values are not actually retrieved from the collection until you enumerate the collection.
|
get |
Gets a list of collections of ValueList in this object.