Class SwiftHashSet<T>
- Namespace
- SwiftCollections
- Assembly
- SwiftCollections.dll
Represents a high-performance set of unique values with efficient operations for addition, removal, and lookup
[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public sealed class SwiftHashSet<T> : IStateBacked<SwiftArrayState<T>>, ISet<T>, ICollection<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable, IMemoryPackable<SwiftHashSet<T>>, IMemoryPackFormatterRegister where T : notnull
Type Parameters
TThe type of elements in the set.
- Inheritance
-
SwiftHashSet<T>
- Implements
-
IStateBacked<SwiftArrayState<T>>ISet<T>ICollection<T>IEnumerable<T>IMemoryPackable<SwiftHashSet<T>>IMemoryPackFormatterRegister
- Inherited Members
- Extension Methods
Remarks
The comparer is not serialized. After deserialization the set reverts to the same default comparer selection used by a new instance. String values use SwiftCollections' deterministic default comparer. Object values use a SwiftCollections comparer that hashes strings deterministically, while other object-value determinism still depends on the underlying value type's GetHashCode() implementation. Other types use Default.
If a custom comparer is required it can be reapplied using SetComparer(IEqualityComparer<T>?).
Constructors
SwiftHashSet()
Initialize a new instance of SwiftHashSet<T> with customizable capacity and comparer for optimal performance tailored to your needs.
public SwiftHashSet()
SwiftHashSet(SwiftArrayState<T>)
Initializes a new instance of the SwiftHashSet<T> class with the specified SwiftArrayState<T>.
[MemoryPackConstructor]
public SwiftHashSet(SwiftArrayState<T> state)
Parameters
stateSwiftArrayState<T>The state containing the internal array, count, offset, and version for initialization.
SwiftHashSet(IEnumerable<T>, IEqualityComparer<T>?)
Initializes a new instance of the SwiftHashSet<T> class that contains elements copied from the specified collection.
public SwiftHashSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer = null)
Parameters
collectionIEnumerable<T>The collection whose elements are copied to the new set.
comparerIEqualityComparer<T>The comparer to use when comparing elements.
SwiftHashSet(IEqualityComparer<T>?)
Initialize a new instance of SwiftHashSet<T> with customizable capacity and comparer for optimal performance tailored to your needs.
public SwiftHashSet(IEqualityComparer<T>? comparer)
Parameters
comparerIEqualityComparer<T>
SwiftHashSet(int, IEqualityComparer<T>?)
Initializes a new instance of the SwiftHashSet<T> class that is empty and has the default initial capacity.
public SwiftHashSet(int capacity, IEqualityComparer<T>? comparer = null)
Parameters
capacityintcomparerIEqualityComparer<T>
Fields
DefaultCapacity
The default initial capacity of the set.
public const int DefaultCapacity = 8
Field Value
Properties
Comparer
Gets the IEqualityComparer<T> object that is used to determine equality for the values in the set.
[JsonIgnore]
[MemoryPackIgnore]
public IEqualityComparer<T> Comparer { get; }
Property Value
Count
Gets the number of elements contained in the set.
[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }
Property Value
this[T]
Gets the stored value that matches the specified key.
[JsonIgnore]
[MemoryPackIgnore]
public T this[T key] { get; }
Parameters
keyTThe lookup value used to find an equal element in the set.
Property Value
- T
Exceptions
- KeyNotFoundException
No matching value exists in the set.
State
Gets or sets the current state of the array, including its items and structure.
[JsonInclude]
[MemoryPackInclude]
public SwiftArrayState<T> State { get; }
Property Value
Remarks
Setting this property replaces the contents of the array with the items from the specified state. If the provided state is empty, the array is cleared. The setter is intended for internal use and may reset internal versioning.
Methods
Add(T)
public bool Add(T item)
Parameters
itemT
Returns
AddRange(IEnumerable<T>)
Adds the elements of the specified collection to the set, ignoring null values and duplicates.
public void AddRange(IEnumerable<T> items)
Parameters
itemsIEnumerable<T>The collection of elements to add to the set. Elements that are null or already present in the set are ignored. Cannot be null.
Remarks
If the source collection is the same instance as the set, the method returns without making any changes. The method preserves single-pass enumeration for sources that do not support multiple iterations.
Clear()
Removes all elements from the set.
public void Clear()
Contains(T)
Determines whether the set contains the specified element.
public bool Contains(T item)
Parameters
itemTThe element to locate in the set.
Returns
- bool
True if the set contains the specified element; otherwise, false.
CopyTo(T[], int)
Copies the elements of the set to an array, starting at the specified array index.
public void CopyTo(T[] array, int arrayIndex)
Parameters
arrayT[]arrayIndexint
EnsureCapacity(int)
Ensures that the set can hold up to the specified number of elements without resizing.
public void EnsureCapacity(int capacity)
Parameters
capacityint
ExceptWith(IEnumerable<T>)
public void ExceptWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
Exists(Predicate<T>)
Determines whether the SwiftHashSet<T> contains an element that matches the conditions defined by the specified predicate.
public bool Exists(Predicate<T> match)
Parameters
matchPredicate<T>The predicate that defines the conditions of the element to search for.
Returns
- bool
trueif the SwiftHashSet<T> contains one or more elements that match the specified predicate; otherwise,false.
Find(Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the first matching element.
public T Find(Predicate<T> match)
Parameters
matchPredicate<T>The predicate that defines the conditions of the element to search for.
Returns
- T
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type
T.
GetEnumerator()
Returns an enumerator that iterates through the set.
public SwiftHashSet<T>.SwiftHashSetEnumerator GetEnumerator()
Returns
IntersectWith(IEnumerable<T>)
public void IntersectWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
IsProperSubsetOf(IEnumerable<T>)
public bool IsProperSubsetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
Returns
IsProperSupersetOf(IEnumerable<T>)
public bool IsProperSupersetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
Returns
IsSubsetOf(IEnumerable<T>)
public bool IsSubsetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
Returns
IsSupersetOf(IEnumerable<T>)
public bool IsSupersetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
Returns
Overlaps(IEnumerable<T>)
public bool Overlaps(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
Returns
Remove(T)
Removes the specified element from the set.
public bool Remove(T item)
Parameters
itemTThe element to remove from the set.
Returns
- bool
True if the element is successfully found and removed; otherwise, false.
SetComparer(IEqualityComparer<T>?)
Switches the hash set's comparer and rehashes all entries using the new comparer to redistribute them across SwiftCollections.SwiftHashSet`1._entries.
public void SetComparer(IEqualityComparer<T>? comparer = null)
Parameters
comparerIEqualityComparer<T>
SetEquals(IEnumerable<T>)
public bool SetEquals(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
Returns
SymmetricExceptWith(IEnumerable<T>)
public void SymmetricExceptWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>
TrimExcess()
Sets the capacity of a SwiftHashSet<T> to the actual number of elements it contains, rounded up to a nearby next power of 2 value.
public void TrimExcess()
TryGetValue(T, out T)
Searches the set for a given value and returns the equal value it finds, if any.
public bool TryGetValue(T expected, out T actual)
Parameters
expectedTactualT
Returns
UnionWith(IEnumerable<T>)
public void UnionWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>