Table of Contents

Class SwiftBiDictionary<T1, T2>

Namespace
SwiftCollections
Assembly
SwiftCollections.dll

Represents a bidirectional dictionary that allows for efficient lookups in both directions, mapping keys to values and values back to keys. Both keys and values must be unique to maintain the integrity of the bidirectional relationship. Inherits from SwiftDictionary<TKey, TValue> and maintains a reverse map for reverse lookups.

[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public class SwiftBiDictionary<T1, T2> : SwiftDictionary<T1, T2>, IDictionary<T1, T2>, ICollection<KeyValuePair<T1, T2>>, IEnumerable<KeyValuePair<T1, T2>>, IDictionary, ICollection, IEnumerable, IMemoryPackable<SwiftDictionary<T1, T2>>, IStateBacked<SwiftDictionaryState<T1, T2>>, IMemoryPackable<SwiftBiDictionary<T1, T2>>, IMemoryPackFormatterRegister where T1 : notnull where T2 : notnull

Type Parameters

T1

The type of the keys in the forward dictionary.

T2

The type of the values in the forward dictionary.

Inheritance
SwiftBiDictionary<T1, T2>
Implements
IDictionary<T1, T2>
IMemoryPackable<SwiftDictionary<T1, T2>>
IStateBacked<SwiftDictionaryState<T1, T2>>
IMemoryPackable<SwiftBiDictionary<T1, T2>>
IMemoryPackFormatterRegister
Inherited Members
Extension Methods

Remarks

The comparer is not serialized. After deserialization the dictionary reverts to the same default comparer selection used by a new instance for both the forward and reverse maps. String keys use SwiftCollections' deterministic default comparer. Object keys use a SwiftCollections comparer that hashes strings deterministically, while other object-key determinism still depends on the underlying key type's GetHashCode() implementation. Other key types use Default and Default.

If a custom comparer is required it can be reapplied using SetComparer(IEqualityComparer<T1>, IEqualityComparer<T2>).

Constructors

SwiftBiDictionary()

Initializes a new instance of the SwiftBiDictionary<T1, T2> class that is empty and uses the default equality comparers for the key and value types.

public SwiftBiDictionary()

SwiftBiDictionary(SwiftDictionaryState<T1, T2>)

Initializes a new instance of the SwiftBiDictionary<T1, T2> class with the specified SwiftDictionaryState<TKey, TValue>.

[MemoryPackConstructor]
public SwiftBiDictionary(SwiftDictionaryState<T1, T2> state)

Parameters

state SwiftDictionaryState<T1, T2>

The state containing the internal array, count, offset, and version for initialization.

SwiftBiDictionary(IDictionary<T1, T2>)

Initializes a new instance of the SwiftBiDictionary<T1, T2> class that contains elements copied from the specified dictionary and uses the default equality comparers for the key and value types.

public SwiftBiDictionary(IDictionary<T1, T2> dictionary)

Parameters

dictionary IDictionary<T1, T2>

The dictionary whose elements are copied to the new SwiftBiDictionary<T1, T2>.

SwiftBiDictionary(IDictionary<T1, T2>, IEqualityComparer<T1>?, IEqualityComparer<T2>?)

Initializes a new instance of the SwiftBiDictionary<T1, T2> class that contains elements copied from the specified dictionary and uses the specified equality comparers for the key and value types.

public SwiftBiDictionary(IDictionary<T1, T2> dictionary, IEqualityComparer<T1>? comparer1, IEqualityComparer<T2>? comparer2)

Parameters

dictionary IDictionary<T1, T2>

The dictionary whose elements are copied to the new SwiftBiDictionary<T1, T2>.

comparer1 IEqualityComparer<T1>

The comparer to use for the keys.

comparer2 IEqualityComparer<T2>

The comparer to use for the values.

SwiftBiDictionary(IEqualityComparer<T1>?, IEqualityComparer<T2>?)

Initializes a new instance of the SwiftBiDictionary<T1, T2> class that is empty and uses the specified equality comparers for the key and value types.

public SwiftBiDictionary(IEqualityComparer<T1>? comparer1, IEqualityComparer<T2>? comparer2)

Parameters

comparer1 IEqualityComparer<T1>

The comparer to use for the keys.

comparer2 IEqualityComparer<T2>

The comparer to use for the values.

Properties

this[T1]

Gets or sets the value associated with the specified key. Ensures that each value is unique within the collection.

[JsonIgnore]
[MemoryPackIgnore]
public T2 this[T1 key] { get; set; }

Parameters

key T1

The key whose value to get or set. Cannot be null.

Property Value

T2

The value associated with the specified key.

Remarks

Setting a value that already exists in the collection will result in an exception, as each value must be unique. If the key does not exist, a new key-value pair is added. If the key exists and the value is unchanged, no operation is performed.

Exceptions

ArgumentException

Thrown when the specified value already exists in the collection.

ReverseSyncRoot

Gets an object that can be used to synchronize access to the reverse collection.

[JsonIgnore]
[MemoryPackIgnore]
public object ReverseSyncRoot { get; }

Property Value

object

Remarks

Use this object to lock the reverse collection during multithreaded operations to ensure thread safety. This property is intended for advanced scenarios where manual synchronization is required.

State

Gets or sets the current state of the dictionary, including its items and configuration.

[JsonInclude]
[MemoryPackInclude]
public SwiftDictionaryState<T1, T2> State { get; }

Property Value

SwiftDictionaryState<T1, T2>

Remarks

Setting this property updates the internal reverse mapping and comparer to reflect the new state. The reverse mapping is rebuilt to ensure consistency with the updated state. This property is intended for serialization and deserialization scenarios.

Methods

Add(T1, T2)

Adds the specified key and value to the SwiftBiDictionary<T1, T2>. Also adds the value-key pair to the reverse map.

public override bool Add(T1 key, T2 value)

Parameters

key T1

The key of the element to add.

value T2

The value of the element to add.

Returns

bool

Exceptions

ArgumentException

An element with the same key or value already exists.

Clear()

Removes all keys and values from the SwiftBiDictionary<T1, T2>. Also clears the reverse map.

public override void Clear()

ContainsValue(T2)

Determines whether the SwiftBiDictionary<T1, T2> contains the specified value.

public bool ContainsValue(T2 value)

Parameters

value T2

The value to locate in the dictionary.

Returns

bool

true if the dictionary contains an element with the specified value; otherwise, false.

GetKey(T2)

Gets the key associated with the specified value.

public T1 GetKey(T2 value)

Parameters

value T2

The value whose associated key is to be retrieved.

Returns

T1

The key associated with the specified value.

Exceptions

KeyNotFoundException

The property is retrieved and value does not exist in the reverse map.

Remove(T1)

Removes the value with the specified key from the SwiftBiDictionary<T1, T2>. Also removes the corresponding key from the reverse map.

public override bool Remove(T1 key)

Parameters

key T1

The key of the element to remove.

Returns

bool

true if the element is successfully found and removed; otherwise, false.

Remove(T1, T2)

Removes the key-value pair from the SwiftBiDictionary<T1, T2>. Also removes the corresponding value-key pair from the reverse map.

public bool Remove(T1 key, T2 value)

Parameters

key T1

The key of the element to remove.

value T2

The value of the element to remove.

Returns

bool

true if the element is successfully found and removed; otherwise, false.

SetComparer(IEqualityComparer<T1>, IEqualityComparer<T2>)

Sets the comparers used to determine equality for keys and values in the dictionary.

public void SetComparer(IEqualityComparer<T1> comparer1, IEqualityComparer<T2> comparer2)

Parameters

comparer1 IEqualityComparer<T1>

The equality comparer to use for keys of type T1. Cannot be null.

comparer2 IEqualityComparer<T2>

The equality comparer to use for values of type T2. Cannot be null.

Remarks

This method updates the internal comparers and rebuilds the reverse mapping using the specified value comparer. The operation is thread-safe and locks the internal state during the update. Changing comparers may affect key and value lookup behavior.

TryGetKey(T2, out T1)

Attempts to get the key associated with the specified value.

public bool TryGetKey(T2 value, out T1 key)

Parameters

value T2

The value whose associated key is to be retrieved.

key T1

When this method returns, contains the key associated with the specified value, if the key is found; otherwise, the default value for the type of the key.

Returns

bool

true if the key was found; otherwise, false.