Class MultiDictionaryBase<TKey, TValue>
MultiDictionaryBase is a base class that can be used to more easily implement a class that associates multiple values to a single key. The class implements the generic IDictionary<TKey, ICollection<TValue>> interface.
Inheritance
Implements
Inherited Members
Namespace: Wintellect.PowerCollections
Assembly: CADability.dll
Syntax
[Serializable]
public abstract class MultiDictionaryBase<TKey, TValue> : CollectionBase<KeyValuePair<TKey, ICollection<TValue>>>, ICollection, IDictionary<TKey, ICollection<TValue>>, ICollection<KeyValuePair<TKey, ICollection<TValue>>>, IEnumerable<KeyValuePair<TKey, ICollection<TValue>>>, IEnumerable
Type Parameters
| Name | Description |
|---|---|
| TKey | The key type of the dictionary. |
| TValue | The value type of the dictionary. |
Remarks
To use MultiDictionaryBase as a base class, the derived class must override Count, Clear, Add, Remove(TKey), Remove(TKey,TValue), Contains(TKey,TValue), EnumerateKeys, and TryEnumerateValuesForKey.
It may wish consider overriding CountValues, CountAllValues, ContainsKey, and EqualValues, but these are not required.
Constructors
| Improve this Doc View SourceMultiDictionaryBase()
Creates a new MultiDictionaryBase.
Declaration
protected MultiDictionaryBase()
Properties
| Improve this Doc View SourceCount
Gets the number of keys in the dictionary. This property must be overridden in the derived class.
Declaration
public abstract override int Count { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
Overrides
Item[TKey]
Returns a collection of all of the values in the dictionary associated with key,
or changes the set of values associated with key.
If the key is not present in the dictionary, an ICollection enumerating no
values is returned. The returned collection of values is read-write, and can be used to
modify the collection of values associated with the key.
Declaration
public virtual ICollection<TValue> this[TKey key] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to get the values associated with. |
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<TValue> | An ICollection<TValue> with all the values associated with |
Keys
Gets a read-only collection all the keys in this dictionary.
Declaration
public virtual ICollection<TKey> Keys { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<TKey> | An readonly ICollection<TKey> of all the keys in this dictionary. |
KeyValuePairs
Gets a read-only collection of all key-value pairs in the dictionary. If a key has multiple values associated with it, then a key-value pair is present for each value associated with the key.
Declaration
public virtual ICollection<KeyValuePair<TKey, TValue>> KeyValuePairs { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>> |
Values
Gets a read-only collection of all the values in the dictionary.
Declaration
public virtual ICollection<TValue> Values { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<TValue> | A read-only ICollection<TValue> of all the values in the dictionary. |
Methods
| Improve this Doc View SourceAdd(TKey, TValue)
Adds a new key-value pair to the dictionary. This method must be overridden in the derived class.
Declaration
public abstract void Add(TKey key, TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | Key to add. |
| TValue | value | Value to associated with the key. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | key is already present in the dictionary |
Add(KeyValuePair<TKey, ICollection<TValue>>)
Adds a key-value pair to the collection. The value part of the pair must be a collection of values to associate with the key. If values are already associated with the given key, the new values are added to the ones associated with that key.
Declaration
public override void Add(KeyValuePair<TKey, ICollection<TValue>> item)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.KeyValuePair<TKey, System.Collections.Generic.ICollection<TValue>> | item | A KeyValuePair contains the Key and Value collection to add. |
Overrides
AddMany(TKey, IEnumerable<TValue>)
Adds new values to be associated with a key. If duplicate values are permitted, this method always adds new key-value pairs to the dictionary.
If duplicate values are not permitted, and key already has a value
equal to one of values associated with it, then that value is replaced,
and the number of values associate with key is unchanged.
Declaration
public virtual void AddMany(TKey key, IEnumerable<TValue> values)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to associate with. |
| System.Collections.Generic.IEnumerable<TValue> | values | A collection of values to associate with |
Clear()
Clears the dictionary. This method must be overridden in the derived class.
Declaration
public abstract override void Clear()
Overrides
Contains(TKey, TValue)
Determines if this dictionary contains a key-value pair equal to key and
value. The dictionary is not changed. This method must be overridden in the derived class.
Declaration
public abstract bool Contains(TKey key, TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to search for. |
| TValue | value | The value to search for. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the dictionary has associated |
Contains(KeyValuePair<TKey, ICollection<TValue>>)
Determines if this dictionary contains the given key and all of the values associated with that key..
Declaration
public override bool Contains(KeyValuePair<TKey, ICollection<TValue>> pair)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.KeyValuePair<TKey, System.Collections.Generic.ICollection<TValue>> | pair | A key and collection of values to search for. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the dictionary has associated all of the values in |
Overrides
ContainsKey(TKey)
Determines whether a given key is found in the dictionary.
Declaration
public virtual bool ContainsKey(TKey key)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | Key to look for in the dictionary. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the key is present in the dictionary. |
Remarks
The default implementation simply calls TryEnumerateValuesForKey. It may be appropriate to override this method to provide a more efficient implementation.
CountAllValues()
Gets a total count of values in the collection. This default implementation is slow; it enumerates all of the keys in the dictionary and calls CountValues on each. A derived class may be able to supply a more efficient implementation.
Declaration
protected virtual int CountAllValues()
Returns
| Type | Description |
|---|---|
| System.Int32 | The total number of values associated with all keys in the dictionary. |
CountValues(TKey)
Gets a count of the number of values associated with a key. The default implementation is slow; it enumerators all of the values (using TryEnumerateValuesForKey) to count them. A derived class may be able to supply a more efficient implementation.
Declaration
protected virtual int CountValues(TKey key)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to count values for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of values associated with |
EnumerateKeys()
Enumerate all the keys in the dictionary. This method must be overridden by a derived class.
Declaration
protected abstract IEnumerator<TKey> EnumerateKeys()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<TKey> | An IEnumerator<TKey> that enumerates all of the keys in the collection that have at least one value associated with them. |
EqualValues(TValue, TValue)
If the derived class does not use the default comparison for values, this methods should be overridden to compare two values for equality. This is used for the correct implementation of ICollection.Contains on the Values and KeyValuePairs collections.
Declaration
protected virtual bool EqualValues(TValue value1, TValue value2)
Parameters
| Type | Name | Description |
|---|---|---|
| TValue | value1 | First value to compare. |
| TValue | value2 | Second value to compare. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the values are equal. |
GetEnumerator()
Enumerate all the keys in the dictionary, and for each key, the collection of values for that key.
Declaration
public override IEnumerator<KeyValuePair<TKey, ICollection<TValue>>> GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<TKey, System.Collections.Generic.ICollection<TValue>>> | An enumerator to enumerate all the key, ICollection<value> pairs in the dictionary. |
Overrides
Remove(TKey)
Removes a key from the dictionary. This method must be overridden in the derived class.
Declaration
public abstract bool Remove(TKey key)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | Key to remove from the dictionary. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the key was found, false otherwise. |
Remove(TKey, TValue)
Removes a key-value pair from the dictionary. This method must be overridden in the derived class.
Declaration
public abstract bool Remove(TKey key, TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | Key to remove from the dictionary. |
| TValue | value | Associated value to remove from the dictionary. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the key-value pair was found, false otherwise. |
Remove(KeyValuePair<TKey, ICollection<TValue>>)
Removes a set of values from a given key. If all values associated with a key are removed, then the key is removed also.
Declaration
public override bool Remove(KeyValuePair<TKey, ICollection<TValue>> pair)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.KeyValuePair<TKey, System.Collections.Generic.ICollection<TValue>> | pair | A KeyValuePair contains a key and a set of values to remove from that key. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if at least one values was found and removed. |
Overrides
RemoveMany(TKey, IEnumerable<TValue>)
Removes a collection of values from the values associated with a key. If the last value is removed from a key, the key is removed also.
Declaration
public virtual int RemoveMany(TKey key, IEnumerable<TValue> values)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | A key to remove values from. |
| System.Collections.Generic.IEnumerable<TValue> | values | A collection of values to remove. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of values that were present and removed. |
RemoveMany(IEnumerable<TKey>)
Remove all of the keys (and any associated values) in a collection of keys. If a key is not present in the dictionary, nothing happens.
Declaration
public int RemoveMany(IEnumerable<TKey> keyCollection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<TKey> | keyCollection | A collection of key values to remove. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of keys from the collection that were present and removed. |
Replace(TKey, TValue)
Replaces all values associated with key with the single value value.
Declaration
public virtual bool Replace(TKey key, TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to associate with. |
| TValue | value | The new values to be associated with |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns true if some values were removed. Returns false if |
Remarks
This implementation simply calls Remove, followed by Add.
ReplaceMany(TKey, IEnumerable<TValue>)
Replaces all values associated with key with a new collection
of values. If the collection does not permit duplicate values, and values has duplicate
items, then only the last of duplicates is added.
Declaration
public bool ReplaceMany(TKey key, IEnumerable<TValue> values)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to associate with. |
| System.Collections.Generic.IEnumerable<TValue> | values | The new values to be associated with |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns true if some values were removed. Returns false if |
ToString()
Shows the string representation of the dictionary. The string representation contains a list of the mappings in the dictionary.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | The string representation of the dictionary. |
Overrides
TryEnumerateValuesForKey(TKey, out IEnumerator<TValue>)
Enumerate all of the values associated with a given key. This method must be overridden
by the derived class. If the key exists and has values associated with it, an enumerator for those
values is returned throught values. If the key does not exist, false is returned.
Declaration
protected abstract bool TryEnumerateValuesForKey(TKey key, out IEnumerator<TValue> values)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to get values for. |
| System.Collections.Generic.IEnumerator<TValue> | values | If true is returned, this parameter receives an enumerators that enumerates the values associated with that key. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the key exists and has values associated with it. False otherwise. |
Explicit Interface Implementations
| Improve this Doc View SourceIDictionary<TKey, ICollection<TValue>>.Add(TKey, ICollection<TValue>)
Implements IDictionary<TKey, IEnumerable<TValue>>.Add. If the key is already present, and ArgumentException is thrown. Otherwise, a new key is added, and new values are associated with that key.
Declaration
void IDictionary<TKey, ICollection<TValue>>.Add(TKey key, ICollection<TValue> values)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | Key to add. |
| System.Collections.Generic.ICollection<TValue> | values | Values to associate with that key. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The key is already present in the dictionary. |
IDictionary<TKey, ICollection<TValue>>.Item[TKey]
Gets a collection of all the values in the dictionary associated with key,
or changes the set of values associated with key.
If the key is not present in the dictionary, a KeyNotFound exception is thrown.
Declaration
ICollection<TValue> IDictionary<TKey, ICollection<TValue>>.this[TKey key] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to get the values associated with. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<TValue> | An IEnumerable<TValue> that enumerates all the values associated with |
Exceptions
| Type | Condition |
|---|---|
| System.Collections.Generic.KeyNotFoundException | The given key is not present in the dictionary. |
IDictionary<TKey, ICollection<TValue>>.TryGetValue(TKey, out ICollection<TValue>)
Determines if this dictionary contains a key equal to key. If so, all the values
associated with that key are returned through the values parameter. This method must be
overridden by the derived class.
Declaration
bool IDictionary<TKey, ICollection<TValue>>.TryGetValue(TKey key, out ICollection<TValue> values)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key to search for. |
| System.Collections.Generic.ICollection<TValue> | values | Returns all values associated with key, if true was returned. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the dictionary contains key. False if the dictionary does not contain key. |
IDictionary<TKey, ICollection<TValue>>.Values
Gets a read-only collection of all the value collections in the dictionary.
Declaration
ICollection<ICollection<TValue>> IDictionary<TKey, ICollection<TValue>>.Values { get; }
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<System.Collections.Generic.ICollection<TValue>> | A read-only ICollection<IEnumerable<TValue>> of all the values in the dictionary. |