KGy SOFT

ThreadSafeHashSetT Class

KGy SOFT Core Libraries Help
Inheritance Hierarchy

SystemObject
  KGySoft.CollectionsThreadSafeHashSetT

Namespace:  KGySoft.Collections
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 7.0.0-preview.3
Syntax

[SerializableAttribute]
public class ThreadSafeHashSet<T> : ICollection<T>, 
	IEnumerable<T>, IEnumerable, ICollection, ISerializable, IDeserializationCallback, 
	IReadOnlyCollection<T>

Type Parameters

T
Type of the items stored in the ThreadSafeHashSetT.

The ThreadSafeHashSetT type exposes the following members.

Constructors

  NameDescription
Public methodThreadSafeHashSetT
Initializes a new instance of the ThreadSafeHashSetT class that is empty and uses the default comparer and Auto hashing strategy.
Public methodThreadSafeHashSetT(IEqualityComparerT, HashingStrategy)
Initializes a new instance of the ThreadSafeHashSetT class that is empty and uses the specified comparer and hashing strategy.
Public methodThreadSafeHashSetT(Int32, HashingStrategy)
Initializes a new instance of the ThreadSafeHashSetT class that is empty and uses the default comparer and the specified hashing strategy.
Protected methodThreadSafeHashSetT(SerializationInfo, StreamingContext)
Initializes a new instance of the ThreadSafeHashSetT class from serialized data.
Public methodThreadSafeHashSetT(IEnumerableT, IEqualityComparerT, HashingStrategy)
Initializes a new instance of the ThreadSafeHashSetT class from the specified collection and uses the specified comparer and hashing strategy.
Public methodThreadSafeHashSetT(Int32, IEqualityComparerT, HashingStrategy)
Initializes a new instance of the ThreadSafeHashSetT class that is empty and uses the specified comparer and hashing strategy.
Top
Properties

  NameDescription
Public propertyComparer
Gets the IEqualityComparerT that is used to determine equality of items for this ThreadSafeHashSetT.
Public propertyCount
Gets the number of elements contained in this ThreadSafeHashSetT.
Public propertyIsEmpty
Gets whether this ThreadSafeHashSetT is empty.
Public propertyMergeInterval
Gets or sets the minimum lifetime for the temporarily created internal locking storage when adding new items to the ThreadSafeHashSetT.
Default value: 100 milliseconds.
See the Remarks section for details.
Public propertyPreserveMergedItems
Gets or sets whether items that have already been merged into the faster lock-free storage are preserved even when they are deleted.
Default value: .
See the Remarks section for details.
Top
Methods

  NameDescription
Public methodAdd
Adds the specified item to the ThreadSafeHashSetT.
Public methodClear
Removes all items from the ThreadSafeHashSetT.
See the Remarks section for details.
Public methodContains
Determines whether the ThreadSafeHashSetT contains the specified item.
Public methodEnsureMerged
Ensures that all elements in this ThreadSafeHashSetT are merged into the faster lock-free storage.
See the Remarks section of the MergeInterval property for details.
Public methodGetEnumerator
Returns an enumerator that iterates through the items of this ThreadSafeHashSetT.
See the Remarks section for details.
Protected methodGetObjectData
In a derived class populates a SerializationInfo with the additional data of the derived type needed to serialize the target object.
Protected methodOnDeserialization
In a derived class restores the state the deserialized instance.
Public methodRemove
Tries to remove the specified item from the ThreadSafeHashSetT.
Public methodReset
Removes all items from the ThreadSafeHashSetT.
See the Remarks section of the Clear method for details.
Public methodToArray
Copies the items stored in the ThreadSafeHashSetT to a new array.
Public methodTrimExcess
Forces to perform a merge while removing all possibly allocated but already deleted entries from the lock-free storage, even if the PreserveMergedItems property is .
See the Remarks section of the PreserveMergedItems property for details.
Public methodTryGetValue
Tries to get the actual stored item for the specified equalValue in the ThreadSafeHashSetT. It can be useful to obtain the actually stored reference when the Comparer can consider different instances equal.
Top
Extension Methods

  NameDescription
Public Extension MethodAddRangeT (Defined by CollectionExtensions.)
Public Extension MethodAsThreadSafeT (Defined by CollectionExtensions.)
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
See the Remarks section for details.
(Defined by ObjectExtensions.)
Public Extension MethodForEachT
Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementT(Boolean)Overloaded.
Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementT(Random, Boolean)Overloaded.
Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodIndexOf(FuncObject, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOf(Object)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfT(FuncT, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfT(T)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodIsNullOrEmptyOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodIsNullOrEmptyTOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodJoinT(Char)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodJoinT(String)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleTOverloaded.
Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleT(Guid)Overloaded.
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleT(Int32)Overloaded.
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleT(Random)Overloaded.
Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodToCircularListT (Defined by EnumerableExtensions.)
Public Extension MethodToStringKeyedDictionaryT(FuncT, String, StringSegmentComparer)Overloaded. (Defined by EnumerableExtensions.)
Public Extension MethodToStringKeyedDictionaryT, TValue(FuncT, String, FuncT, TValue, StringSegmentComparer)Overloaded. (Defined by EnumerableExtensions.)
Public Extension MethodTryAdd(Object, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddT(T, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRange(IEnumerable, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRangeT(IEnumerableT, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClear(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClearT(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryGetCount(Int32)Overloaded.
Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions.)
Public Extension MethodTryGetCountT(Int32)Overloaded.
Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions.)
Public Extension MethodTryGetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryGetElementAtT(Int32, T, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsert(Int32, Object, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertT(Int32, T, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRange(Int32, IEnumerable, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRangeT(Int32, IEnumerableT, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemove(Object, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveT(T, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAt(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAtT(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRange(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRangeT(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryReplaceRange(Int32, Int32, IEnumerable, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.)
Public Extension MethodTryReplaceRangeT(Int32, Int32, IEnumerableT, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.)
Public Extension MethodTrySetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTrySetElementAtT(Int32, T, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Top
Remarks

ThreadSafeHashSetT uses a very similar approach to ThreadSafeDictionaryTKey, TValue. It uses two separate internal storage: new items are added to a temporary storage using a single lock, which might regularly be merged into a faster lock-free storage, depending on the value of the MergeInterval property. Once the items are merged, their access (both read and write) becomes lock free. Even deleting and re-adding an item becomes faster after it has been merged into the lock-free storage.

Note Note
Therefore, ThreadSafeHashSetT is not always a good alternative of LockingCollectionT, or even a ConcurrentDictionaryTKey, TValue with ignored values. If new items are continuously added, then always a shared lock is used, in which case ConcurrentDictionaryTKey, TValue might perform better, unless you need to use some members, which are very slow in ConcurrentDictionaryTKey, TValue (see the performance comparison table at the Remarks section of the ThreadSafeDictionaryTKey, TValue class). If the newly added elements are regularly removed, make sure the PreserveMergedItems property is ; otherwise, the already merged items are just marked deleted when removed from the ThreadSafeHashSetT or when you call the Clear method. To remove even the merged items you must call the Reset method, or to remove the deleted items only you can explicitly call the TrimExcess method.

Comparison with other thread-safe collections.

When to prefer ThreadSafeHashSetT over ConcurrentDictionaryTKey, TValue:

Tip Tip
If T is string and it is safe to use a non-randomized string comparer, then you can pass StringSegmentComparer.Ordinal to the constructor for even better performance. Or, you can use StringSegmentComparer.OrdinalRandomized to use a comparer with randomized hash also on platforms where default string hashing is not randomized (eg. .NET Framework 3.5).

When to prefer LockingCollectionT over ThreadSafeHashSetT:

Incompatibilities with HashSetT:

Thread Safety

Instance members of this type are safe for multi-threaded operations.
See Also

Reference