Table of Contents

Struct UnsafeSlotMap<T>

Namespace
Misaki.HighPerformance.LowLevel.Collections
Assembly
Misaki.HighPerformance.LowLevel.dll

Provides an unsafe, high-performance slot map for storing and managing unmanaged values, supporting fast insertion, removal, and lookup by slot index and generation.

public struct UnsafeSlotMap<T> : IUnsafeCollection<T>, IUnsafeCollection, IDisposable where T : unmanaged

Type Parameters

T

The type of value to store in the slot map. Must be unmanaged.

Implements
Inherited Members

Constructors

UnsafeSlotMap()

Initializes a new instance of UnsafeSlotMap with a default size of 1 and a persistent allocation handle.

public UnsafeSlotMap()

UnsafeSlotMap(int, AllocationHandle, AllocationOption)

Initializes a new instance of the UnsafeSlotMap class with the specified capacity, allocation handle, and allocation options.

public UnsafeSlotMap(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)

Parameters

capacity int

The number of slots to allocate for the map. Must be greater than zero.

handle AllocationHandle

A reference to the allocation handle used to manage memory for the slot map.

allocationOption AllocationOption

The allocation options to use when creating internal data structures. The default is AllocationOption.None.

Exceptions

ArgumentOutOfRangeException

Thrown when capacity is less than or equal to zero.

Properties

Capacity

public readonly int Capacity { get; }

Property Value

int

Count

Gets the number of elements in a collection.

public readonly int Count { get; }

Property Value

int

IsCreated

Indicates whether the object has been created. Returns true if the object is created, otherwise false.

public readonly bool IsCreated { get; }

Property Value

bool

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 the specified item to the collection and returns the index of the slot where it was stored.

public int Add(scoped in T item, out int generation)

Parameters

item T

The item to add to the collection.

generation int

When this method returns, contains the generation number associated with the slot where the item was stored.

Returns

int

The index of the slot in which the item was stored.

Clear()

Removes all elements from the collection. The collection will be empty after this operation.

public void Clear()

Contains(int, int)

Determines whether the specified slot index contains a valid entry with the given generation.

public readonly bool Contains(int slotIndex, int generation)

Parameters

slotIndex int

The zero-based index of the slot to check. Must be greater than or equal to 0 and less than the current capacity.

generation int

The generation value to compare against the slot's generation.

Returns

bool

true if the slot at the specified index is valid and its generation matches the specified value; otherwise, false.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetElementAt(int, int)

Retrieves the element stored at the specified slot index and generation.

public readonly T GetElementAt(int slotIndex, int generation)

Parameters

slotIndex int

The zero-based index of the slot from which to retrieve the element. Must be within the valid range of allocated slots.

generation int

The generation identifier associated with the slot. Used to ensure the element has not been replaced or removed since allocation.

Returns

T

The element stored at the specified slot and generation.

Exceptions

ArgumentOutOfRangeException

Thrown when slotIndex is less than zero or greater than or equal to the capacity.

InvalidOperationException

Thrown when the specified slot is not occupied or the generation does not match.

GetElementReferenceAt(int, int, out bool)

Returns a reference to the element at the specified slot index and generation, if it exists; otherwise, returns a null reference.

public readonly ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist)

Parameters

slotIndex int

The zero-based index of the slot to retrieve. Must be within the valid range of allocated slots.

generation int

The expected generation value for the slot. Used to verify that the slot has not been recycled or replaced.

exist bool

When this method returns, contains true if a valid element exists at the specified slot and generation; otherwise, false.

Returns

T

A reference to the element of type T at the specified slot and generation if it exists; otherwise, a null reference.

GetEnumerator()

[UnscopedRef]
public UnsafeSlotMap<T>.Enumerator GetEnumerator()

Returns

UnsafeSlotMap<T>.Enumerator

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.

Remove(int, int)

Attempts to remove the item at the specified slot index and generation from the collection.

public bool Remove(int slotIndex, int generation)

Parameters

slotIndex int

The zero-based index of the slot to remove. Must be within the valid range of slot indices.

generation int

The generation value associated with the slot. Removal succeeds only if this matches the current generation of the slot.

Returns

bool

true if the item was successfully removed; otherwise, false.

Remove(int, int, out T)

Attempts to remove the item at the specified slot index and generation from the collection.

public bool Remove(int slotIndex, int generation, out T item)

Parameters

slotIndex int

The zero-based index of the slot to remove. Must be within the valid range of slot indices.

generation int

The generation value associated with the slot. Removal succeeds only if this matches the current generation of the slot.

item T

When 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 item was successfully removed; otherwise, false.

Resize(int, AllocationOption)

Changes the size of a collection to the specified value.

public void Resize(int newSize, AllocationOption option = AllocationOption.None)

Parameters

newSize int

Specifies the new size to which the collection should be adjusted.

option AllocationOption

Specifies allocation options that may affect how memory is managed during the resize operation.

TryGetElementAt(int, int, out T)

Attempts to retrieve the element at the specified slot index and generation.

public readonly bool TryGetElementAt(int slotIndex, int generation, out T value)

Parameters

slotIndex int

The zero-based index of the slot to retrieve. Must be within the valid range of slots.

generation int

The generation identifier associated with the slot. Used to verify that the slot has not been replaced or invalidated.

value T

When this method returns, contains the element at the specified slot and generation if found; otherwise, the default value for type T.

Returns

bool

true if the element at the specified slot index and generation is found; otherwise, false.