Enhanced C#
Loyc library documentation
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Nested classes | Public fields | Properties | Public Member Functions | List of all members
Loyc.Collections.MultiMap< K, V > Class Template Reference

A multimap is a dictionary that allows more than one value to be associated with each key. This is much more convenient than a Dictionary of Lists of values, because the indexer always returns a collection (empty if there are no values associated with a key) and its methods automatically create or destroy, as appropriate, the list associated with each key. More...


Source file:
Inheritance diagram for Loyc.Collections.MultiMap< K, V >:
Loyc.Collections.ICollectionImpl< T > Loyc.Collections.IIndexed< in K, out V > Loyc.ICloneable< out T > Loyc.Collections.ICollectionSource< T > Loyc.Collections.ICollectionSink< in T > Loyc.Collections.ICollectionAndReadOnly< T > Loyc.Collections.IContains< in T > Loyc.Collections.IAdd< in T > Loyc.Collections.MultiMap< K, V >.ValueList

Remarks

A multimap is a dictionary that allows more than one value to be associated with each key. This is much more convenient than a Dictionary of Lists of values, because the indexer always returns a collection (empty if there are no values associated with a key) and its methods automatically create or destroy, as appropriate, the list associated with each key.

This class is implemented using a Dictionary whose entries have lists of values.

This class keeps track of the number of values as well as the number of keys. The Count property returns the number of values, while KeyCount returns the number of keys. The number of values is never less than the number of keys, because the collection associated with a key is always removed when its last item is removed.

Type Constraints
K :notnull 

Nested classes

struct  ValueList
 

Public fields

IEnumerable< K > Keys => _dict.Keys
 
IEnumerable< V > Values => _dict.SelectMany(p => p.Value)
 
int KeyCount => _dict.Count
 Gets the number of keys in the underlying dictionary. More...
 
int Count => _valueCount
 Gets the number of values in all value collections. More...
 

Properties

MultiMap< K, V >.ValueList this[K key] [get]
 
- Properties inherited from Loyc.Collections.IIndexed< in K, out V >
this[K key] [get]
 Gets the value associated with the specified key. More...
 

Public Member Functions

 MultiMap (IEnumerable< KeyValuePair< K, V >> pairs, IEqualityComparer< K >?comp=null)
 
 MultiMap (Dictionary< K, List< V >> dict)
 
 MultiMap (MultiMap< K, V > map)
 
 MultiMap (IEqualityComparer< K >?comp)
 
bool ContainsKey (K key)
 
IEnumerator< KeyValuePair< K, V > > GetEnumerator ()
 
bool TryGetValue (K key, [MaybeNullWhen(false)] out ValueList value)
 The collection returned is always valid, but the method returns true only if the collection is not empty. More...
 
void Add (KeyValuePair< K, V > item)
 
void Clear ()
 
bool Contains (KeyValuePair< K, V > item)
 
void CopyTo (KeyValuePair< K, V >[] array, int arrayIndex)
 
bool Remove (KeyValuePair< K, V > item)
 
bool Remove (K key)
 
MultiMap< K, V > Clone ()
 
- Public Member Functions inherited from Loyc.Collections.ICollectionSource< T >
void CopyTo (T[] array, int arrayIndex)
 Copies the elements of the collection to an Array, starting at a particular array index. More...
 
- Public Member Functions inherited from Loyc.Collections.IContains< in T >
bool Contains (T item)
 Returns true if and only if the collection contains the specified item. More...
 
- Public Member Functions inherited from Loyc.Collections.ICollectionSink< in T >
bool Remove (T item)
 
- Public Member Functions inherited from Loyc.Collections.IAdd< in T >
void Add (T item)
 

Member Function Documentation

bool Loyc.Collections.MultiMap< K, V >.TryGetValue ( key,
[MaybeNullWhen(false)] out ValueList  value 
)
inline

The collection returned is always valid, but the method returns true only if the collection is not empty.

Member Data Documentation

int Loyc.Collections.MultiMap< K, V >.Count => _valueCount

Gets the number of values in all value collections.

int Loyc.Collections.MultiMap< K, V >.KeyCount => _dict.Count

Gets the number of keys in the underlying dictionary.