Table of Contents

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

T

The type of elements in the set.

Inheritance
SwiftHashSet<T>
Implements
IStateBacked<SwiftArrayState<T>>
ISet<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

state SwiftArrayState<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

collection IEnumerable<T>

The collection whose elements are copied to the new set.

comparer IEqualityComparer<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

comparer IEqualityComparer<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

capacity int
comparer IEqualityComparer<T>

Fields

DefaultCapacity

The default initial capacity of the set.

public const int DefaultCapacity = 8

Field Value

int

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

IEqualityComparer<T>

Count

Gets the number of elements contained in the set.

[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }

Property Value

int

this[T]

Gets the stored value that matches the specified key.

[JsonIgnore]
[MemoryPackIgnore]
public T this[T key] { get; }

Parameters

key T

The 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

SwiftArrayState<T>

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

item T

Returns

bool

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

items IEnumerable<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

item T

The 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

array T[]
arrayIndex int

EnsureCapacity(int)

Ensures that the set can hold up to the specified number of elements without resizing.

public void EnsureCapacity(int capacity)

Parameters

capacity int

ExceptWith(IEnumerable<T>)

public void ExceptWith(IEnumerable<T> other)

Parameters

other IEnumerable<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

match Predicate<T>

The predicate that defines the conditions of the element to search for.

Returns

bool

true if 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

match Predicate<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

SwiftHashSet<T>.SwiftHashSetEnumerator

IntersectWith(IEnumerable<T>)

public void IntersectWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>

IsProperSubsetOf(IEnumerable<T>)

public bool IsProperSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

IsProperSupersetOf(IEnumerable<T>)

public bool IsProperSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

IsSubsetOf(IEnumerable<T>)

public bool IsSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

IsSupersetOf(IEnumerable<T>)

public bool IsSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

Overlaps(IEnumerable<T>)

public bool Overlaps(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

Remove(T)

Removes the specified element from the set.

public bool Remove(T item)

Parameters

item T

The 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

comparer IEqualityComparer<T>

SetEquals(IEnumerable<T>)

public bool SetEquals(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

SymmetricExceptWith(IEnumerable<T>)

public void SymmetricExceptWith(IEnumerable<T> other)

Parameters

other IEnumerable<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

expected T
actual T

Returns

bool

UnionWith(IEnumerable<T>)

public void UnionWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>