Table of Contents

Struct UnsafeList<T>

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

A collection that allows for unsafe operations on a list of unmanaged types.

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

Type Parameters

T

Represents a type that can be stored in the collection, constrained to unmanaged types for performance and safety.

Implements
Inherited Members

Constructors

UnsafeList()

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

public UnsafeList()

UnsafeList(int, AllocationHandle, AllocationOption)

Initializes a new instance of UnsafeList with a specified number of initial capacity and an allocation handle.

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

Parameters

capacity int

Specifies the number of initial capacity to allocate in the list, which must be greater than zero.

handle AllocationHandle

A reference to an AllocationHandle that manages the memory allocation for the array.

allocationOption AllocationOption

Specifies how the memory should be allocated.

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.

this[int]

public readonly ref T this[int index] { get; }

Parameters

index int

Property Value

T

this[uint]

public readonly ref T this[uint index] { get; }

Parameters

index uint

Property Value

T

Methods

Add(scoped in T)

Adds a new element to the end of the list, resizing the internal array if necessary.

public void Add(scoped in T value)

Parameters

value T

The element to be added to the list.

AddNoResize(scoped in T)

Adds the specified value to the collection without resizing the underlying storage.

public void AddNoResize(scoped in T value)

Parameters

value T

The value to add to the collection.

AddRange(ReadOnlySpan<T>)

Adds a range of elements to the collection.

public void AddRange(ReadOnlySpan<T> values)

Parameters

values ReadOnlySpan<T>

A span containing the elements to add.

AddRange(T*, int)

Adds a range of elements from a pointer to the collection.

public void AddRange(T* ptr, int count)

Parameters

ptr T*

Points to the source data to be copied into the collection.

count int

Indicates the number of elements to be added from the source data.

AddRangeNoResize(ReadOnlySpan<T>)

Adds the elements of the specified collection to the current list without resizing the underlying storage.

public void AddRangeNoResize(ReadOnlySpan<T> collection)

Parameters

collection ReadOnlySpan<T>

A read-only span containing the elements to add. The span must not exceed the available capacity.

AddRangeNoResize(T*, int)

Adds a range of elements from a pointer to the collection without resizing the underlying storage.

public void AddRangeNoResize(T* ptr, int count)

Parameters

ptr T*

Points to the source data to be copied into the collection.

count int

Indicates the number of elements to be added from the source data.

AsParallelReader()

Provides a parallel reader for the current list, enabling thread-safe read operations.

public UnsafeList<T>.ParallelReader AsParallelReader()

Returns

UnsafeList<T>.ParallelReader

A UnsafeList<T>.ParallelReader instance that can be used to read items from the list in a thread-safe manner.

Remarks

The list must live at least as long as the parallel reader, and the parallel reader must not be used after the list is disposed. For example, if you need to access the list in job system and wait that job in another stack frame, please always allocate the list struct itself on heap. Otherwise the parallel reader will be invalid after the stack frame that creates the list is popped, even if the list's internal array is still valid.

AsParallelWriter()

Provides a parallel writer for the current list, enabling thread-safe additions to the list.

public UnsafeList<T>.ParallelWriter AsParallelWriter()

Returns

UnsafeList<T>.ParallelWriter

A UnsafeList<T>.ParallelWriter instance that can be used to add items to the list in a thread-safe manner.

Remarks

The list must live at least as long as the parallel writer, and the parallel writer must not be used after the list is disposed. For example, if you need to access the list in job system and wait that job in another stack frame, please always allocate the list struct itself on heap. Otherwise the parallel writer will be invalid after the stack frame that creates the list is popped, even if the list's internal array is still valid.

AsReadOnly()

Converts the current list to a read-only collection that provides unsafe access to its elements.

public readonly ReadOnlyUnsafeCollection<T> AsReadOnly()

Returns

ReadOnlyUnsafeCollection<T>

A new ReadOnlyUnsafeCollection<T> instance that allows for read-only access to the list's elements without copying.

AsSpan()

public readonly Span<T> AsSpan()

Returns

Span<T>

AsSpan(int, int)

public readonly Span<T> AsSpan(int start, int length)

Parameters

start int
length int

Returns

Span<T>

AsUnsafeArray()

Converts the current list to an UnsafeArray representation.

public readonly UnsafeArray<T> AsUnsafeArray()

Returns

UnsafeArray<T>

A new UnsafeArray<T> instance.

Remarks

The returned UnsafeArray<T> shares the same underlying data as the list and does not own the memory.

Clear()

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

public void Clear()

CopyFrom(ReadOnlySpan<T>)

Copies elements from a source span to a destination unsafe collection, ensuring both have the same size.

public void CopyFrom(ReadOnlySpan<T> source)

Parameters

source ReadOnlySpan<T>

Represents the span containing the elements to be copied to the unsafe collection.

CopyFrom(ReadOnlySpan<T>, int, int, int)

Copies a specified range of elements from a source span to a destination collection.

public void CopyFrom(ReadOnlySpan<T> source, int sourceIndex, int destinationIndex, int length)

Parameters

source ReadOnlySpan<T>

The span containing the elements to be copied.

sourceIndex int

The starting index in the source span from which to begin copying.

destinationIndex int

The starting index in the destination collection where the elements will be placed.

length int

The number of elements to copy from the source span to the destination collection.

Exceptions

ArgumentOutOfRangeException

Thrown when the specified range exceeds the bounds of the source span or destination collection.

CopyTo(Span<T>)

Copies elements from a source UnsafeCollection to a destination Span, ensuring both have the same size.

public readonly void CopyTo(Span<T> destination)

Parameters

destination Span<T>

Represents the target span where elements are copied to.

CopyTo(Span<T>, int, int, int)

Copies a range of elements from a source collection to a destination span, ensuring both are adequately sized.

public readonly void CopyTo(Span<T> destination, int sourceIndex, int destinationIndex, int length)

Parameters

destination Span<T>

The span where the elements will be copied to.

sourceIndex int

The starting index in the source collection for the copy operation.

destinationIndex int

The starting index in the destination span where the elements will be placed.

length int

The number of elements to copy from the source to the destination.

Exceptions

ArgumentOutOfRangeException

Thrown when the specified range exceeds the bounds of the source collection or destination span.

Dispose()

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

public void Dispose()

GetEnumerator()

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

Returns

UnsafeList<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.

RemoveAt(int)

Removes the element at the specified index from the collection.

public void RemoveAt(int index)

Parameters

index int

The zero-based index of the element to remove.

RemoveAtSwapBack(int)

public void RemoveAtSwapBack(int index)

Parameters

index int

RemoveRange(int, int)

Removes a range of elements from the list starting at the specified index.

public void RemoveRange(int start, int length)

Parameters

start int

The zero-based index at which to start removing elements.

length int

The number of elements to remove.

RemoveRangeSwapBack(int, int)

Removes a range of elements from the list starting at the specified index by swapping them with the last elements.

public void RemoveRangeSwapBack(int start, int length)

Parameters

start int

The zero-based index at which to start removing elements.

length int

The number of elements to remove.

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.

ToList()

Creates a new List<T> containing the elements.

public readonly List<T> ToList()

Returns

List<T>

A List<T> containing all elements.

UnsafeSetCount(int)

Sets the count of the collection to a new value without modifying the underlying storage.

public void UnsafeSetCount(int newCount)

Parameters

newCount int

The new count value to set for the collection.

Remarks

This method will not initialize new elements, so it should be used with caution. The new count must be between 0 and the current capacity of the collection.

Exceptions

ArgumentOutOfRangeException

Thrown when the new count is outside the valid range.

Operators

implicit operator ReadOnlyUnsafeCollection<T>(UnsafeList<T>)

public static implicit operator ReadOnlyUnsafeCollection<T>(UnsafeList<T> list)

Parameters

list UnsafeList<T>

Returns

ReadOnlyUnsafeCollection<T>

implicit operator Span<T>(UnsafeList<T>)

public static implicit operator Span<T>(UnsafeList<T> list)

Parameters

list UnsafeList<T>

Returns

Span<T>