Class TwoWayDictionary<T1, T2>
- Namespace
- LfrlAnvil.Collections
- Assembly
- LfrlAnvil.Collections.dll
Represents a generic collection of two-way (forward, reverse) pairs.
public class TwoWayDictionary<T1, T2> : ITwoWayDictionary<T1, T2>, IReadOnlyTwoWayDictionary<T1, T2>, IReadOnlyCollection<Pair<T1, T2>>, ICollection<Pair<T1, T2>>, IEnumerable<Pair<T1, T2>>, IEnumerable where T1 : notnull where T2 : notnull
Type Parameters
T1First value type.
T2Second value type.
- Inheritance
-
TwoWayDictionary<T1, T2>
- Implements
-
ITwoWayDictionary<T1, T2>IReadOnlyTwoWayDictionary<T1, T2>IReadOnlyCollection<Pair<T1, T2>>ICollection<Pair<T1, T2>>IEnumerable<Pair<T1, T2>>
- Inherited Members
- Extension Methods
Constructors
TwoWayDictionary()
Creates a new empty TwoWayDictionary<T1, T2> instance with Default forward and reverse comparer.
public TwoWayDictionary()
TwoWayDictionary(IEqualityComparer<T1>, IEqualityComparer<T2>)
Creates a new empty TwoWayDictionary<T1, T2> instance.
public TwoWayDictionary(IEqualityComparer<T1> forwardComparer, IEqualityComparer<T2> reverseComparer)
Parameters
forwardComparerIEqualityComparer<T1>Forward key equality comparer.
reverseComparerIEqualityComparer<T2>Reverse key equality comparer.
Properties
Count
Gets the number of elements contained in the ICollection<T>.
public int Count { get; }
Property Value
- int
The number of elements contained in the ICollection<T>.
Forward
Represents the T1 => T2 read-only dictionary.
public IReadOnlyDictionary<T1, T2> Forward { get; }
Property Value
- IReadOnlyDictionary<T1, T2>
ForwardComparer
Forward key equality comparer.
public IEqualityComparer<T1> ForwardComparer { get; }
Property Value
Reverse
Represents the T2 => T1 read-only dictionary.
public IReadOnlyDictionary<T2, T1> Reverse { get; }
Property Value
- IReadOnlyDictionary<T2, T1>
ReverseComparer
Reverse key equality comparer.
public IEqualityComparer<T2> ReverseComparer { get; }
Property Value
Methods
Add(T1, T2)
Adds the provided pair.
public void Add(T1 first, T2 second)
Parameters
firstT1First value.
secondT2Second value.
Exceptions
- ArgumentException
When either element already exists as key in its respective dictionary.
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Contains(Pair<T1, T2>)
Determines whether the ICollection<T> contains a specific value.
[Pure]
public bool Contains(Pair<T1, T2> item)
Parameters
itemPair<T1, T2>The object to locate in the ICollection<T>.
Returns
- bool
true if
itemis found in the ICollection<T>; otherwise, false.
Contains(T1, T2)
Checks whether or not the provided pair exists.
[Pure]
public bool Contains(T1 first, T2 second)
Parameters
firstT1First value.
secondT2Second value.
Returns
- bool
true when pair exists, otherwise false.
GetEnumerator()
Returns an enumerator that iterates through the collection.
[Pure]
public IEnumerator<Pair<T1, T2>> GetEnumerator()
Returns
- IEnumerator<Pair<T1, T2>>
An enumerator that can be used to iterate through the collection.
RemoveForward(T1)
Attempts to remove a pair by its first value.
public bool RemoveForward(T1 value)
Parameters
valueT1Entry's key.
Returns
- bool
true when pair was removed, otherwise false.
RemoveForward(T1, out T2)
Attempts to remove a pair by its first value.
public bool RemoveForward(T1 value, out T2 second)
Parameters
valueT1Entry's key.
secondT2out parameter that returns second value associated with the removed pair.
Returns
- bool
true when pair was removed, otherwise false.
RemoveReverse(T2)
Attempts to remove a pair by its second value.
public bool RemoveReverse(T2 value)
Parameters
valueT2Entry's key.
Returns
- bool
true when pair was removed, otherwise false.
RemoveReverse(T2, out T1)
Attempts to remove a pair by its second value.
public bool RemoveReverse(T2 value, out T1 first)
Parameters
valueT2Entry's key.
firstT1out parameter that returns first value associated with the removed pair.
Returns
- bool
true when pair was removed, otherwise false.
TryAdd(T1, T2)
Attempts to add the provided pair.
public bool TryAdd(T1 first, T2 second)
Parameters
firstT1First value.
secondT2Second value.
Returns
- bool
true when pair was added, otherwise false.
TryUpdateForward(T1, T2)
Attempts to update the value associated with the specified first key.
public bool TryUpdateForward(T1 first, T2 second)
Parameters
firstT1Entry's key.
secondT2Second value to set.
Returns
- bool
true when pair was updated, otherwise false.
TryUpdateReverse(T2, T1)
Attempts to update the value associated with the specified second key.
public bool TryUpdateReverse(T2 second, T1 first)
Parameters
secondT2Entry's key.
firstT1First value to set.
Returns
- bool
true when pair was updated, otherwise false.
UpdateForward(T1, T2)
Updates the value associated with the specified first key.
public void UpdateForward(T1 first, T2 second)
Parameters
firstT1Entry's key.
secondT2Second value to set.
Exceptions
- ArgumentException
When the provided
secondalready exists as a key.
UpdateReverse(T2, T1)
Updates the value associated with the specified second key.
public void UpdateReverse(T2 second, T1 first)
Parameters
secondT2Entry's key.
firstT1First value to set.
Exceptions
- ArgumentException
When the provided
firstalready exists as a key.