SynchronizedPairSet
Represents a thread-safe collection that maintains key/value pairs and allows bidirectional lookups.
Inherits: SynchronizedKeyedCollection<T, T>
Constructors
public SynchronizedPairSet(IEqualityComparer<TKey> comparer)
synchronizedPairSet = SynchronizedPairSet(comparer)
Initializes a new instance of the SynchronizedPairSet class with a specified comparer for keys.
- comparer
- An equality comparer for keys.
public SynchronizedPairSet(IEqualityComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
synchronizedPairSet = SynchronizedPairSet(keyComparer, valueComparer)
Initializes a new instance of the SynchronizedPairSet class with specified comparers for keys and values.
- keyComparer
- An equality comparer for keys.
- valueComparer
- An equality comparer for values.
public SynchronizedPairSet()
synchronizedPairSet = SynchronizedPairSet()
Initializes a new instance of the SynchronizedPairSet class.
Properties
public TKey Item { get; }
value = synchronizedPairSet.Item
Gets the key associated with the specified value.
- value
- The value for which to retrieve the key.
Returns: The key corresponding to the specified value.
Methods
public override void Add(TKey key, TValue value)
synchronizedPairSet.Add(key, value)
Adds the specified key and value to the collection.
- key
- The key to insert.
- value
- The value to insert.
protected override bool CanAdd(TKey key, TValue value)
result = synchronizedPairSet.CanAdd(key, value)
Determines whether a key/value pair can be added based on whether the value is already present.
- key
- The key to be added.
- value
- The value to be added.
Returns: true if the value is not already contained; otherwise false.
public bool ContainsValue(TValue value)
result = synchronizedPairSet.ContainsValue(value)
Determines whether the collection contains an entry with the specified value.
- value
- The value to locate.
Returns: true if the value is found; otherwise false.
public TKey GetKey(TValue value)
result = synchronizedPairSet.GetKey(value)
Retrieves the key associated with the specified value.
- value
- The value whose key is requested.
Returns: The key associated with the given value.
public TValue GetValue(TKey key)
result = synchronizedPairSet.GetValue(key)
Retrieves the value associated with the specified key.
- key
- The key whose value is requested.
Returns: The value associated with the given key.
protected override void OnAdding(TKey key, TValue value)
synchronizedPairSet.OnAdding(key, value)
Called when an item is being added. Adds the value/key mapping to the internal dictionary.
- key
- The key being added.
- value
- The value being added.
protected override void OnClearing()
synchronizedPairSet.OnClearing()
Called when the collection is being cleared. Clears the internal dictionary.
protected override void OnRemoving(TKey key, TValue value)
synchronizedPairSet.OnRemoving(key, value)
Called when an item is being removed. Removes the value/key mapping from the internal dictionary.
- key
- The key being removed.
- value
- The value being removed.
protected override void OnSetting(TKey key, TValue value)
synchronizedPairSet.OnSetting(key, value)
Called when an existing item is being set. Updates the value/key mapping in the internal dictionary. Maintains bijective property by swapping values when a collision occurs.
- key
- The key being set.
- value
- The value being set.
public bool RemoveByValue(TValue value)
result = synchronizedPairSet.RemoveByValue(value)
Removes the element with the specified value if it is found.
- value
- The value to remove.
Returns: true if the item was removed; otherwise false.
public void SetKey(TValue value, TKey key)
synchronizedPairSet.SetKey(value, key)
Sets a new key for an existing value, replacing any previous association.
- value
- The value to associate with the new key.
- key
- The new key to associate with the value.
public void SetValue(TKey key, TValue value)
synchronizedPairSet.SetValue(key, value)
Updates the value associated with a key.
- key
- The key to update.
- value
- The new value.
public bool TryAdd(TKey key, TValue value)
result = synchronizedPairSet.TryAdd(key, value)
Attempts to add a new key/value pair if neither key nor value is already present.
- key
- The key to add.
- value
- The value to add.
Returns: true if the pair was added; otherwise false.
public TKey TryGetKey(TValue value)
result = synchronizedPairSet.TryGetKey(value)
Attempts to get a key for the specified value.
- value
- The value whose key is requested.
Returns: The key if found; otherwise the default.
public bool TryGetKey(TValue value, TKey key)
result = synchronizedPairSet.TryGetKey(value, key)
Attempts to get a key for the specified value, returning a boolean indicating success.
- value
- The value whose key is requested.
- key
- When this method returns, contains the key if found.
Returns: true if the key was found; otherwise false.
public bool TryGetKeyAndRemove(TValue value, TKey key)
result = synchronizedPairSet.TryGetKeyAndRemove(value, key)
Attempts to get and remove the key associated with the specified value.
- value
- The value for which to retrieve and remove the key.
- key
- When this method returns, contains the removed key if found.
Returns: true if the key was removed successfully; otherwise false.