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
T1
First value type.
T2
Second 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
forwardComparer
IEqualityComparer<T1>Forward key equality comparer.
reverseComparer
IEqualityComparer<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
first
T1First value.
second
T2Second 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
item
Pair<T1, T2>The object to locate in the ICollection<T>.
Returns
- bool
true if
item
is 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
first
T1First value.
second
T2Second 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
value
T1Entry'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
value
T1Entry's key.
second
T2out 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
value
T2Entry'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
value
T2Entry's key.
first
T1out 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
first
T1First value.
second
T2Second 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
first
T1Entry's key.
second
T2Second 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
second
T2Entry's key.
first
T1First 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
first
T1Entry's key.
second
T2Second value to set.
Exceptions
- ArgumentException
When the provided
second
already exists as a key.
UpdateReverse(T2, T1)
Updates the value associated with the specified second
key.
public void UpdateReverse(T2 second, T1 first)
Parameters
second
T2Entry's key.
first
T1First value to set.
Exceptions
- ArgumentException
When the provided
first
already exists as a key.