Class SwiftSparseSet
- Namespace
- SwiftCollections
- Assembly
- SwiftCollections.dll
Represents a high-performance sparse set for externally supplied non-negative integer IDs. Provides O(1) Add, Remove, Contains, and densely packed iteration.
[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public sealed class SwiftSparseSet : IStateBacked<SwiftArrayState<int>>, ISwiftCloneable<int>, ISet<int>, ICollection<int>, IReadOnlyCollection<int>, IEnumerable<int>, ICollection, IEnumerable, IMemoryPackable<SwiftSparseSet>, IMemoryPackFormatterRegister
- Inheritance
-
SwiftSparseSet
- Implements
-
IMemoryPackable<SwiftSparseSet>IMemoryPackFormatterRegister
- Inherited Members
- Extension Methods
Remarks
SwiftSparseSet is intended for membership workloads where the caller already owns compact integer IDs, such as entity handles, body IDs, or slot indices.
Internally, IDs are stored in a dense array for cache-friendly iteration while a sparse lookup table maps each ID directly to its dense position. Removal uses swap-back, so iteration order is not stable.
Memory usage scales with the highest stored ID rather than only the number of IDs. For arbitrary,
huge, or widely spaced keys, prefer SwiftHashSet<T> with int keys.
Constructors
SwiftSparseSet()
Initializes a new instance of the SwiftSparseSet class with default sparse and dense capacities.
public SwiftSparseSet()
SwiftSparseSet(SwiftArrayState<int>)
Initializes a new instance of the SwiftSparseSet class using the specified state.
[MemoryPackConstructor]
public SwiftSparseSet(SwiftArrayState<int> state)
Parameters
stateSwiftArrayState<int>The state object that provides the initial IDs. Cannot be null.
SwiftSparseSet(int)
Initializes a new instance of the SwiftSparseSet class with matching sparse and dense capacities.
public SwiftSparseSet(int capacity)
Parameters
capacityintThe initial sparse and dense capacity.
SwiftSparseSet(int, int)
Initializes a new instance of the SwiftSparseSet class with explicit sparse and dense capacities.
public SwiftSparseSet(int sparseCapacity, int denseCapacity)
Parameters
sparseCapacityintInitial sparse lookup capacity. This should track the highest expected ID plus one, not just the number of stored IDs.
denseCapacityintInitial dense storage capacity for IDs.
Fields
DefaultDenseCapacity
Represents the default initial capacity for dense ID storage.
public const int DefaultDenseCapacity = 8
Field Value
DefaultSparseCapacity
Represents the default initial capacity for sparse ID lookup.
public const int DefaultSparseCapacity = 8
Field Value
Properties
Count
Gets the number of IDs contained in the set.
[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }
Property Value
DenseCapacity
Capacity of the dense ID storage.
[JsonIgnore]
[MemoryPackIgnore]
public int DenseCapacity { get; }
Property Value
DenseKeys
Returns the dense ID array. Only the range [0..Count) is populated.
[JsonIgnore]
[MemoryPackIgnore]
public int[] DenseKeys { get; }
Property Value
- int[]
Remarks
Prefer collection APIs. Direct key mutation must preserve the dense/sparse lookup invariants; invalid edits may fail fast.
IsReadOnly
[JsonIgnore]
[MemoryPackIgnore]
public bool IsReadOnly { get; }
Property Value
IsSynchronized
Gets a value indicating whether access to the collection is synchronized.
[JsonIgnore]
[MemoryPackIgnore]
public bool IsSynchronized { get; }
Property Value
Keys
Gets a span containing the current IDs in dense iteration order.
[JsonIgnore]
[MemoryPackIgnore]
public Span<int> Keys { get; }
Property Value
Remarks
Prefer collection APIs. Direct key mutation must preserve the dense/sparse lookup invariants; invalid edits may fail fast.
SparseCapacity
Capacity of the sparse lookup table.
[JsonIgnore]
[MemoryPackIgnore]
public int SparseCapacity { get; }
Property Value
State
Gets or sets the current state of the sparse set.
[JsonInclude]
[MemoryPackInclude]
public SwiftArrayState<int> State { get; }
Property Value
SyncRoot
Gets an object that can be used to synchronize access to the collection.
[JsonIgnore]
[MemoryPackIgnore]
public object SyncRoot { get; }
Property Value
Methods
Add(int)
Adds the specified ID if it is not already present.
public bool Add(int item)
Parameters
itemint
Returns
- bool
true if the ID was added; false if it was already present.
AsReadOnlySpan()
Returns a read-only span over the populated dense ID range.
public ReadOnlySpan<int> AsReadOnlySpan()
Returns
Clear()
Removes all IDs from the set without reducing capacity.
public void Clear()
CloneTo(ICollection<int>)
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<int> output)
Parameters
outputICollection<int>
Contains(int)
Determines whether the set contains the specified ID.
public bool Contains(int item)
Parameters
itemint
Returns
ContainsKey(int)
Determines whether the set contains the specified key. Alias for Contains(int).
public bool ContainsKey(int key)
Parameters
keyint
Returns
CopyKeysTo(SwiftList<int>)
Replaces the destination list contents with this set's keys in dense iteration order.
public void CopyKeysTo(SwiftList<int> destination)
Parameters
Remarks
The destination list is reused and only grows when its current capacity is smaller than Count. Use CopySortedKeysTo(SwiftList<int>) when stable ascending key order is required.
CopySortedKeysTo(SwiftList<int>)
Replaces the destination list contents with this set's keys sorted in ascending order.
public void CopySortedKeysTo(SwiftList<int> destination)
Parameters
Remarks
This method is intended for reusable hot-path scratch buffers that need deterministic key order without constructing a persistent sorted collection.
CopyTo(int[], int)
public void CopyTo(int[] array, int arrayIndex)
Parameters
EnsureDenseCapacity(int)
Ensures that dense storage can hold at least the specified number of IDs.
public void EnsureDenseCapacity(int capacity)
Parameters
capacityint
EnsureSparseCapacity(int)
Ensures that the sparse lookup table has at least the specified capacity.
public void EnsureSparseCapacity(int capacity)
Parameters
capacityint
ExceptWith(IEnumerable<int>)
public void ExceptWith(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
GetDense(out int[], out int)
Retrieves the backing dense ID array and current count.
public void GetDense(out int[] keys, out int count)
Parameters
GetEnumerator()
public SwiftSparseSet.SwiftSparseSetEnumerator GetEnumerator()
Returns
IntersectWith(IEnumerable<int>)
public void IntersectWith(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
IsProperSubsetOf(IEnumerable<int>)
public bool IsProperSubsetOf(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
Returns
IsProperSupersetOf(IEnumerable<int>)
public bool IsProperSupersetOf(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
Returns
IsSubsetOf(IEnumerable<int>)
public bool IsSubsetOf(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
Returns
IsSupersetOf(IEnumerable<int>)
public bool IsSupersetOf(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
Returns
Overlaps(IEnumerable<int>)
public bool Overlaps(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
Returns
Remove(int)
Removes the specified ID from the set.
public bool Remove(int item)
Parameters
itemint
Returns
- bool
true if the ID was found and removed; otherwise, false.
SetEquals(IEnumerable<int>)
public bool SetEquals(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
Returns
SymmetricExceptWith(IEnumerable<int>)
public void SymmetricExceptWith(IEnumerable<int> other)
Parameters
otherIEnumerable<int>
TrimExcess()
Reduces unused dense and sparse capacity while preserving all IDs.
public void TrimExcess()
TryAdd(int)
Adds the specified ID if it is not already present.
public bool TryAdd(int item)
Parameters
itemint
Returns
UnionWith(IEnumerable<int>)
public void UnionWith(IEnumerable<int> other)
Parameters
otherIEnumerable<int>