Class TwoWayDictionary<LeftType, RightType>
This class implements a two-way associate container, that is, a dictionary-like collection that is indexable both by the left and right-hand values. This places key-like constraints on both sides (they must be comparable, hasheable, and unique), but provides fast lookup in both directions.
Inherited Members
Namespace: SuperOfficeUtil
Assembly: SoCore.dll
Syntax
public class TwoWayDictionary<LeftType, RightType>
Type Parameters
| Name | Description |
|---|---|
| LeftType | Left-hand type |
| RightType | Right-hand type |
Remarks
The implementation internally uses two generic dictionaries, so that all constraints, performance characteristics and other properties of the generic System.Collections.Generic.Dictionary class apply. LeftType and RightType can be the same type, which incidentally requires things like the TryGetValue methods to have different names for left and right...
Constructors
TwoWayDictionary()
Default constructor, allocates empty dictionaries. Insertions will take O(n) where n is the number of items already in the container; note that if you're populating in a loop that would give a total performance of O(n^2), worth avoiding by preallocating if at all possible.
Declaration
public TwoWayDictionary()
TwoWayDictionary(int)
Constructor that preallocates the given number of slots. Insertions will take O(1) up to this initial capacity, O(n^2) beyond.
Declaration
public TwoWayDictionary(int initialCapacity)
Parameters
| Type | Name | Description |
|---|---|---|
| int | initialCapacity | Initial capacity to allocate, this reserves memory but is not a hard limit |
TwoWayDictionary(int, IEqualityComparer<LeftType>)
Constructor that preallocates the given number of items, and uses the given comparer for the left-hand key type. Insertions will take O(1) up to this initial capacity, O(n^2) beyond.
Declaration
public TwoWayDictionary(int initialCapacity, IEqualityComparer<LeftType> LeftTypeEquality)
Parameters
| Type | Name | Description |
|---|---|---|
| int | initialCapacity | Initial capacity to allocate, this reserves memory but is not a hard limit |
| IEqualityComparer | LeftTypeEquality | Equality comparer for the given key type |
TwoWayDictionary(int, IEqualityComparer<LeftType>, IEqualityComparer<RightType>)
Constructor that preallocates the given number of items, and uses the given comparers for the left-hand and right-hand key types. Insertions will take O(1) up to this initial capacity, O(n^2) beyond.
Declaration
public TwoWayDictionary(int initialCapacity, IEqualityComparer<LeftType> LeftTypeEquality, IEqualityComparer<RightType> RightTypeEquality)
Parameters
| Type | Name | Description |
|---|---|---|
| int | initialCapacity | Initial capacity to allocate, this reserves memory but is not a hard limit |
| IEqualityComparer | LeftTypeEquality | Equality comparer for the left-hand key type |
| IEqualityComparer | RightTypeEquality | Equality comparer for the right-hand key type |
Properties
Count
Return the number of elements currently in the two-way dictionary
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int |
this[LeftType]
Get and set items similar to Dictionary using the first type as a key.
Declaration
public RightType this[LeftType left] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Key to use when looking up. |
Property Value
| Type | Description |
|---|---|
| RightType | Value from the given key. |
Keys
Wrapper for LeftKeys, so TwoWayDictionary can be used like a Dictionary.
Declaration
public Dictionary<LeftType, RightType>.KeyCollection Keys { get; }
Property Value
| Type | Description |
|---|---|
| DictionarySystem.Collections.Generic.Dictionary{`0,`1}.KeyCollection |
LeftKeys
Get Left keys.
Declaration
public Dictionary<LeftType, RightType>.KeyCollection LeftKeys { get; }
Property Value
| Type | Description |
|---|---|
| DictionarySystem.Collections.Generic.Dictionary{`0,`1}.KeyCollection |
LeftValues
Return an enumerator over the left-hand value type
Declaration
public IEnumerable<LeftType> LeftValues { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerable |
RightKeys
Get right keys.
Declaration
public Dictionary<RightType, LeftType>.KeyCollection RightKeys { get; }
Property Value
| Type | Description |
|---|---|
| DictionarySystem.Collections.Generic.Dictionary{`1,`0}.KeyCollection |
RightValues
Return an enumerator over the right-hand value type
Declaration
public IEnumerable<RightType> RightValues { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerable |
Values
Return an enumerator over the value pairs, with the left-hand value as the Key and the right-hand value as the Value element of the KeyValuePair
Declaration
public IEnumerable<KeyValuePair<LeftType, RightType>> Values { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerableKeyValuePair |
Methods
Add(LeftType, RightType)
Add a new pair of values. An exception will be thrown if either the left or right value already exists, or if any of the values is null.
Declaration
public void Add(LeftType left, RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Left-hand value |
| RightType | right | Right-hand value |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | An element with the same key (left- or right-hand) already exists |
| ArgumentNullException | Either one of the values is null |
AddOverwrite(LeftType, RightType)
Add or overwrite an existing pair of values. After this operation, the new values will be associated and any previous associations any of them might have will be lost. Both values must be non-null.
Declaration
public void AddOverwrite(LeftType left, RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Left-hand value |
| RightType | right | Right-hand value |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Either one of the values is null |
Clear()
Clear the dictionary.
Declaration
public void Clear()
ContainsKey(LeftType)
Wrapper for LeftContainsKey(LeftType), so TwoWayDictionary can be used like a Dictionary.
Declaration
public bool ContainsKey(LeftType left)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Left-hand key to look for |
Returns
| Type | Description |
|---|---|
| bool | true if left-hand key was found |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Left-hand key is null |
LeftContainsKey(LeftType)
Test if a left-hand key exists
Declaration
public bool LeftContainsKey(LeftType left)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Left-hand key to look for |
Returns
| Type | Description |
|---|---|
| bool | true if left-hand key was found |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Left-hand key is null |
LeftGetValue(LeftType)
Get the value using the left key.
Declaration
public RightType LeftGetValue(LeftType left)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | The left key. |
Returns
| Type | Description |
|---|---|
| RightType | The right value |
LeftGetValue(RightType)
Get the left-hand value corresponding to the given right-hand key.
Declaration
public LeftType LeftGetValue(RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| RightType | right | Right-hand key to look for |
Returns
| Type | Description |
|---|---|
| LeftType | Left-hand value corresponding to key |
Exceptions
| Type | Condition |
|---|---|
| KeyNotFoundException | Thrown if the right-hand key does not exist |
| ArgumentNullException | Thrown if the right-hand key is null |
LeftRemove(LeftType)
Remove item from the dictionary using the left key.
Declaration
public bool LeftRemove(LeftType left)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Key of item to remove. |
Returns
| Type | Description |
|---|---|
| bool | True if the item is successfully removed. |
LeftToArray()
Return the left-hand values as an array
Declaration
public LeftType[] LeftToArray()
Returns
| Type | Description |
|---|---|
Array of left-hand values |
LeftTryGetValue(LeftType, out RightType)
Get the right-hand value for the given left-hand key if it exists
Declaration
public bool LeftTryGetValue(LeftType left, out RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Left-hand key |
| RightType | right | Output: Right-hand value, or null/default if key was not found |
Returns
| Type | Description |
|---|---|
| bool | true if the left-hand key existed |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Left-hand key is null |
Remove(LeftType)
Wrapper for LeftRemove(LeftType), so TwoWayDictionary can be used like a Dictionary.
Declaration
public bool Remove(LeftType left)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Key of item to remove. |
Returns
| Type | Description |
|---|---|
| bool | True if the item is successfully removed. |
RightContainsKey(RightType)
Test if a right-hand key exists
Declaration
public bool RightContainsKey(RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| RightType | right | Right-hand key to look for |
Returns
| Type | Description |
|---|---|
| bool | true if the right-hand key was found |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Left-hand key is null |
RightGetValue(LeftType)
Get the right-hand value corresponding to the given left-hand key.
Declaration
public RightType RightGetValue(LeftType left)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | Left-hand key to look for |
Returns
| Type | Description |
|---|---|
| RightType | Right-hand value corresponding to key |
Exceptions
| Type | Condition |
|---|---|
| KeyNotFoundException | Thrown if the left-hand key does not exist |
| ArgumentNullException | Thrown if the left-hand key is null |
RightGetValue(RightType)
Get the value using the right key.
Declaration
public LeftType RightGetValue(RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| RightType | right | The right key. |
Returns
| Type | Description |
|---|---|
| LeftType | The left value. |
RightRemove(RightType)
Remove item from the dictionary using the right key.
Declaration
public bool RightRemove(RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| RightType | right | Key of item to remove. |
Returns
| Type | Description |
|---|---|
| bool | True if the item is successfully removed. |
RightToArray()
Return the right-hand values as an array
Declaration
public RightType[] RightToArray()
Returns
| Type | Description |
|---|---|
Array of right-hand values |
RightTryGetValue(RightType, out LeftType)
Get the left-hand value for the given right-hand key if it exists
Declaration
public bool RightTryGetValue(RightType right, out LeftType left)
Parameters
| Type | Name | Description |
|---|---|---|
| RightType | right | Right-hand key |
| LeftType | left | Output: left-hand value, or null/default if key was not found |
Returns
| Type | Description |
|---|---|
| bool | true if the right-hand key existed |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Right-hand key is null |
TryGetValue(LeftType, out RightType)
Wrapper for LeftTryGetValue(LeftType, out RightType), so TwoWayDictionary can be used like a Dictionary.
Declaration
public bool TryGetValue(LeftType left, out RightType right)
Parameters
| Type | Name | Description |
|---|---|---|
| LeftType | left | |
| RightType | right |
Returns
| Type | Description |
|---|---|
| bool |