Table of Contents

Class SwiftPackedSet<T>

Namespace
SwiftCollections
Assembly
SwiftCollections.dll

Represents a high-performance set that stores unique values in a densely packed array while providing O(1) lookups via an internal hash map.

[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public sealed class SwiftPackedSet<T> : IStateBacked<SwiftArrayState<T>>, ISwiftCloneable<T>, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IMemoryPackable<SwiftPackedSet<T>>, IMemoryPackFormatterRegister where T : notnull

Type Parameters

T

The type of elements contained in the set.

Inheritance
SwiftPackedSet<T>
Implements
IStateBacked<SwiftArrayState<T>>
ISet<T>
IMemoryPackable<SwiftPackedSet<T>>
IMemoryPackFormatterRegister
Inherited Members
Extension Methods

Remarks

SwiftPackedSet<T> maintains values in a contiguous array for extremely cache-friendly iteration while using a hash-based lookup table to guarantee fast membership tests and removals.

Removal uses a swap-back strategy that keeps the dense storage contiguous but does not preserve ordering. As a result, iteration order is not guaranteed to remain stable.

This structure is commonly used in high-performance systems such as ECS (Entity Component Systems) where dense iteration speed is critical.

Constructors

SwiftPackedSet()

Initializes a new instance of the SwiftPackedSet class with the default capacity.

public SwiftPackedSet()

SwiftPackedSet(SwiftArrayState<T>)

Initializes a new instance of the SwiftPackedSet class with the specified array state.

[MemoryPackConstructor]
public SwiftPackedSet(SwiftArrayState<T> state)

Parameters

state SwiftArrayState<T>

The state object that provides the initial data and configuration for the set. Cannot be null.

SwiftPackedSet(int)

Initializes a new instance of the SwiftPackedSet class with the specified initial capacity.

public SwiftPackedSet(int capacity)

Parameters

capacity int

The initial number of elements that the set can contain before resizing. If less than or equal to the default capacity, the default capacity is used. Must be non-negative.

Remarks

The actual capacity is rounded up to the next power of two greater than or equal to the specified value, unless the specified value is less than or equal to the default capacity.

Fields

DefaultCapacity

Represents the default initial capacity value used when no specific capacity is provided.

public const int DefaultCapacity = 8

Field Value

int

Properties

Capacity

Gets the total number of elements that the collection can hold without resizing.

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

Property Value

int

Count

Gets the number of elements contained in the collection.

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

Property Value

int

Dense

Gets the underlying dense array of elements.

[JsonIgnore]
[MemoryPackIgnore]
public T[] Dense { get; }

Property Value

T[]

IsReadOnly

[JsonIgnore]
[MemoryPackIgnore]
public bool IsReadOnly { get; }

Property Value

bool

IsSynchronized

Gets a value indicating whether access to the collection is synchronized (thread safe).

[JsonIgnore]
[MemoryPackIgnore]
public bool IsSynchronized { get; }

Property Value

bool

State

Gets or sets the current state of the array, including its items and order.

[JsonInclude]
[MemoryPackInclude]
public SwiftArrayState<T> State { get; }

Property Value

SwiftArrayState<T>

Remarks

Use this property to capture or restore the array's contents and structure. Setting this property replaces the entire array with the provided state.

SyncRoot

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

Property Value

object

Methods

Add(T)

public bool Add(T value)

Parameters

value T

Returns

bool

AsReadOnlySpan()

Returns a read-only span over the populated dense portion of the set.

public ReadOnlySpan<T> AsReadOnlySpan()

Returns

ReadOnlySpan<T>

Clear()

public void Clear()

CloneTo(ICollection<T>)

Clones the entire ISwiftCloneable<T> into a new target ICollection<T>, ensuring that the target list is an exact copy. Clears the target list first to match the structure and state of the source list exactly.

public void CloneTo(ICollection<T> output)

Parameters

output ICollection<T>

Contains(T)

public bool Contains(T value)

Parameters

value T

Returns

bool

CopyTo(T[], int)

public void CopyTo(T[] array, int arrayIndex)

Parameters

array T[]
arrayIndex int

EnsureCapacity(int)

Ensures that the internal storage has at least the specified capacity, expanding it if necessary.

public void EnsureCapacity(int capacity)

Parameters

capacity int

The minimum number of elements that the internal storage should be able to hold. Must be non-negative.

Remarks

If the current capacity is less than the specified value, the internal storage is resized to the next power of two greater than or equal to the specified capacity. Existing elements are preserved.

ExceptWith(IEnumerable<T>)

public void ExceptWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Exists(Predicate<T>)

Determines whether the SwiftPackedSet<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 SwiftPackedSet<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 in dense iteration order.

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()

public SwiftPackedSet<T>.SwiftPackedSetEnumerator GetEnumerator()

Returns

SwiftPackedSet<T>.SwiftPackedSetEnumerator

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)

public bool Remove(T value)

Parameters

value T

Returns

bool

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>

UnionWith(IEnumerable<T>)

public void UnionWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>