Class SparseSet<T>
- Namespace
- Misaki.HighPerformance.Collections
- Assembly
- Misaki.HighPerformance.dll
A sparse set data structure that provides O(1) insertion, deletion, and lookup operations. The sparse set uses three arrays: a dense array for storing values, a sparse array for mapping indices, and a reverse array for mapping dense indices back to sparse indices. Sparse indices work like entity IDs and are automatically generated.
public class SparseSet<T> : IEnumerable<T>, IEnumerable where T : notnull
Type Parameters
TRepresents a type that can be stored in the sparse set.
- Inheritance
-
SparseSet<T>
- Implements
-
IEnumerable<T>
- Inherited Members
Constructors
SparseSet(int)
Initializes a new instance of SparseSet with a specified capacity and an allocation handle.
public SparseSet(int capacity = 4)
Parameters
capacityintSpecifies the initial capacity of the sparse set, which must be greater than zero.
Exceptions
- ArgumentOutOfRangeException
Thrown when the specified capacity is less than or equal to zero.
Properties
Capacity
public int Capacity { get; }
Property Value
Count
public int Count { get; }
Property Value
Methods
Add(T, out int)
Adds a value to the sparse set and returns a unique sparse index for the value.
public int Add(T value, out int generation)
Parameters
valueTThe value to add to the sparse set.
generationintOutputs the generation number associated with the added value.
Returns
- int
A unique sparse index that can be used to reference this value.
Clear()
Clears the sparse set, removing all values and resetting internal state.
public void Clear()
Contains(int, int)
Checks if the sparse set contains a value at the specified sparse index.
public bool Contains(int sparseIndex, int generation)
Parameters
sparseIndexintThe sparse index to check.
generationintThe generation number to validate against the stored generation.
Returns
- bool
True if the sparse index is valid and contains a value, false otherwise.
GetEnumerator()
public SparseSet<T>.Enumerator GetEnumerator()
Returns
GetValue(int, int)
Gets the value at the specified sparse index and generation.
public T GetValue(int sparseIndex, int generation)
Parameters
sparseIndexintThe sparse index to retrieve the value from.
generationintThe generation number to validate against the stored generation.
Returns
- T
The value at the specified sparse index.
Exceptions
- ArgumentOutOfRangeException
Thrown when the sparse index is not found.
GetValueReference(int, int, out bool)
Gets reference of the value at the specified sparse index and generation.
public ref T GetValueReference(int sparseIndex, int generation, out bool exist)
Parameters
sparseIndexintThe sparse index to retrieve the value from.
generationintThe generation number to validate against the stored generation.
existboolOutputs whether the sparse index exists in the set.
Returns
- T
Reference of the value at the specified sparse index.
Exceptions
- ArgumentOutOfRangeException
Thrown when the sparse index is not found.
Remove(int, int)
Removes the value at the specified sparse index.
public bool Remove(int sparseIndex, int generation)
Parameters
sparseIndexintThe sparse index of the value to remove.
generationintThe generation number associated with the sparse index to validate.
Returns
- bool
True if the value was removed, false if the sparse index was not found.
Resize(int)
Resizes the sparse set to accommodate more elements.
public void Resize(int newSize)
Parameters
newSizeintThe new size of the sparse set.
Exceptions
- ArgumentOutOfRangeException
Thrown when the new size is not positive.
SetValue(int, int, T)
Updates the value at the specified sparse index.
public bool SetValue(int sparseIndex, int generation, T value)
Parameters
sparseIndexintThe sparse index of the value to update.
generationintThe generation number to validate against the stored generation.
valueTThe new value.
Returns
- bool
True if the value was updated, false if the sparse index was not found.
TryGetValue(int, int, out T)
Gets the value at the specified sparse index and generation.
public bool TryGetValue(int sparseIndex, int generation, out T value)
Parameters
sparseIndexintThe sparse index to retrieve the value from.
generationintThe generation number to validate against the stored generation.
valueTWhen this method returns, contains the value at the specified sparse index, if found.
Returns
- bool
True if the sparse index contains a value, false otherwise.