Struct UnsafeSparseSet<T>
- Namespace
- Misaki.HighPerformance.LowLevel.Collections
- Assembly
- Misaki.HighPerformance.LowLevel.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 struct UnsafeSparseSet<T> : IUnsafeCollection<T>, IUnsafeCollection, IDisposable where T : unmanaged
Type Parameters
TRepresents a type that can be stored in the sparse set, constrained to unmanaged types for performance and safety.
- Implements
- Inherited Members
Constructors
UnsafeSparseSet()
Constructs an UnsafeSparseSet with a default size of 1 and uses the Persistent allocator.
public UnsafeSparseSet()
UnsafeSparseSet(int, AllocationHandle, AllocationOption)
Initializes a new instance of UnsafeSparseSet with a specified capacity and an allocation handle.
public UnsafeSparseSet(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
Parameters
capacityintSpecifies the initial capacity of the sparse set, which must be greater than zero.
handleAllocationHandleA reference to an AllocationHandle that manages the memory allocation for the sparse set.
allocationOptionAllocationOptionSpecifies how the memory should be allocated.
Exceptions
- ArgumentOutOfRangeException
Thrown when the specified capacity is less than or equal to zero.
Properties
Capacity
public readonly int Capacity { get; }
Property Value
Count
Gets the number of elements in a collection.
public readonly int Count { get; }
Property Value
IsCreated
Indicates whether the object has been created. Returns true if the object is created, otherwise false.
public readonly bool IsCreated { get; }
Property Value
Remarks
If MHP_ENABLE_STACKTRACE is not defined, this property will only check if the underlying pointer is not null, which may not be sufficient to determine if the collection is fully initialized and ready for use.
Methods
Add(scoped in T, out int)
Adds a value to the sparse set and returns a unique sparse index for the value.
public int Add(scoped in 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.
AsUnsafeArray()
Converts the current sparse set to an UnsafeArray representation using its dense array.
public readonly UnsafeArray<T> AsUnsafeArray()
Returns
- UnsafeArray<T>
Returns a new UnsafeArray instance initialized with the dense array's pointer and count.
Clear()
Removes all elements from the collection. The collection will be empty after this operation.
public void Clear()
Contains(int, int)
Checks if the sparse set contains a value at the specified sparse index.
public readonly 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.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetEnumerator()
[UnscopedRef]
public UnsafeSparseSet<T>.Enumerator GetEnumerator()
Returns
GetUnsafePtr()
Returns a pointer to an unmanaged memory location. This pointer can be used for low-level memory operations.
public readonly void* GetUnsafePtr()
Returns
- void*
The method returns a void pointer to the unsafe memory location.
GetValue(int, int)
Gets the value at the specified sparse index and generation.
public readonly 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 readonly 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.
Remove(int, int, out T)
Removes the value at the specified sparse index.
public bool Remove(int sparseIndex, int generation, out T item)
Parameters
sparseIndexintThe sparse index of the value to remove.
generationintThe generation number associated with the sparse index to validate.
itemTWhen this method returns, contains the item that was removed if the removal was successful; otherwise, the default value for type
T.
Returns
- bool
True if the value was removed, false if the sparse index was not found.
Resize(int, AllocationOption)
Changes the size of a collection to the specified value.
public void Resize(int newSize, AllocationOption option = AllocationOption.None)
Parameters
newSizeintSpecifies the new size to which the collection should be adjusted.
optionAllocationOptionSpecifies allocation options that may affect how memory is managed during the resize operation.
SetValue(int, int, T)
Updates the value at the specified sparse index.
public readonly 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 readonly 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.