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: SuperOffice.Util
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()
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...
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 |
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...
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<LeftType> | LeftTypeEquality | Equality comparer for the given key 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...
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<LeftType> | LeftTypeEquality | Equality comparer for the left-hand key type |
IEqualityComparer<RightType> | RightTypeEquality | Equality comparer for the right-hand key 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...
Properties
Count
Return the number of elements currently in the two-way dictionary
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
int |
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...
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. |
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...
Keys
Wrapper for LeftKeys, so TwoWayDictionary can be used like a Dictionary.
Declaration
public Dictionary<LeftType, RightType>.KeyCollection Keys { get; }
Property Value
Type | Description |
---|---|
Dictionary<LeftType, RightType>.KeyCollection |
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...
LeftKeys
Get Left keys.
Declaration
public Dictionary<LeftType, RightType>.KeyCollection LeftKeys { get; }
Property Value
Type | Description |
---|---|
Dictionary<LeftType, RightType>.KeyCollection |
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...
LeftValues
Return an enumerator over the left-hand value type
Declaration
public IEnumerable<LeftType> LeftValues { get; }
Property Value
Type | Description |
---|---|
IEnumerable<LeftType> |
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...
RightKeys
Get right keys.
Declaration
public Dictionary<RightType, LeftType>.KeyCollection RightKeys { get; }
Property Value
Type | Description |
---|---|
Dictionary<RightType, LeftType>.KeyCollection |
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...
RightValues
Return an enumerator over the right-hand value type
Declaration
public IEnumerable<RightType> RightValues { get; }
Property Value
Type | Description |
---|---|
IEnumerable<RightType> |
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...
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 |
---|---|
IEnumerable<KeyValuePair<LeftType, RightType>> |
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...
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 |
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...
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 |
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...
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Either one of the values is null |
Clear()
Clear the dictionary.
Declaration
public void Clear()
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...
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 |
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...
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 |
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...
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 |
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...
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 |
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...
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. |
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...
LeftToArray()
Return the left-hand values as an array
Declaration
public LeftType[] LeftToArray()
Returns
Type | Description |
---|---|
LeftType[] | Array of left-hand values |
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...
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 |
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...
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. |
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...
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 |
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...
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 |
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...
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. |
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...
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. |
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...
RightToArray()
Return the right-hand values as an array
Declaration
public RightType[] RightToArray()
Returns
Type | Description |
---|---|
RightType[] | Array of right-hand values |
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...
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 |
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...
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 |
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...