Struct UnsafeStack<T>
- Namespace
- Misaki.HighPerformance.LowLevel.Collections
- Assembly
- Misaki.HighPerformance.LowLevel.dll
Provides a high-performance, unsafe stack data structure for unmanaged types, supporting manual memory management and allocation control.
public struct UnsafeStack<T> : IUnsafeCollection<T>, IUnsafeCollection, IDisposable where T : unmanaged
Type Parameters
TThe type of elements stored in the stack. Must be an unmanaged type.
- Implements
- Inherited Members
Constructors
UnsafeStack()
Initializes a new instance of UnsafeStack with a default size of 1 and a persistent allocation handle.
public UnsafeStack()
UnsafeStack(int, AllocationHandle, AllocationOption)
Initializes a new instance of the UnsafeStack class with the specified initial capacity and allocation options.
public UnsafeStack(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
Parameters
capacityintThe number of elements the stack can initially hold. Must be greater than zero.
handleAllocationHandleA reference to an AllocationHandle used to manage the underlying memory allocation for the stack.
allocationOptionAllocationOptionSpecifies additional options for memory allocation. The default is AllocationOption.None.
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
Clear()
Removes all elements from the collection. The collection will be empty after this operation.
public void Clear()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetEnumerator()
[UnscopedRef]
public UnsafeStack<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.
Peek()
Returns the item at the top of the stack without removing it.
public readonly T Peek()
Returns
- T
The item of type
Tat the top of the stack.
Exceptions
- InvalidOperationException
Thrown when the stack is empty.
Pop()
Removes and returns the object at the top of the stack.
public T Pop()
Returns
- T
The object removed from the top of the stack.
Exceptions
- InvalidOperationException
Thrown when the stack is empty.
Push(scoped in T)
Adds an element to the top of the stack.
public void Push(scoped in T value)
Parameters
valueTThe element to add to the stack.
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.
TryPeek(out T)
Attempts to return the item at the top of the stack without removing it.
public readonly bool TryPeek(out T value)
Parameters
valueTWhen this method returns, contains the item at the top of the stack if the stack is not empty; otherwise, the default value of
T.
Returns
TryPop(out T)
Attempts to remove and return the object at the top of the stack.
public bool TryPop(out T value)
Parameters
valueTWhen this method returns, contains the object removed from the top of the stack, if the operation succeeded; otherwise, the default value of
T.
Returns
- bool
true if an object was successfully removed and returned from the stack; otherwise, false.