Struct UnsafeChunkedList<T>
- Namespace
- Misaki.HighPerformance.LowLevel.Collections
- Assembly
- Misaki.HighPerformance.LowLevel.dll
A collection that stores elements in fixed-size chunks, enabling stable element addresses and eliminating large reallocation during growth. Adding elements never moves existing ones.
public struct UnsafeChunkedList<T> : IUnsafeCollection<T>, IUnsafeCollection, IDisposable where T : unmanaged
Type Parameters
TRepresents a type that can be stored in the collection, constrained to unmanaged types for performance and safety.
- Implements
- Inherited Members
Constructors
UnsafeChunkedList()
Invalid constructor, use UnsafeChunkedList(int, AllocationHandle, AllocationOption) instead.
public UnsafeChunkedList()
UnsafeChunkedList(int, AllocationHandle, AllocationOption)
Initializes a new instance with a specified chunk capacity and allocator.
public UnsafeChunkedList(int chunkCapacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
Parameters
chunkCapacityintThe maximum number of elements per chunk.
handleAllocationHandleA reference to an AllocationHandle that manages memory allocation.
allocationOptionAllocationOptionSpecifies how the memory should be allocated.
Fields
DEFAULT_CHUNK_SIZE_IN_BYTES
public const int DEFAULT_CHUNK_SIZE_IN_BYTES = 16384
Field Value
Properties
Capacity
public readonly int Capacity { get; }
Property Value
ChunkCapacity
public readonly int ChunkCapacity { get; }
Property Value
ChunkCount
public readonly int ChunkCount { 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.
this[int]
public readonly ref T this[int index] { get; }
Parameters
indexint
Property Value
- T
this[uint]
public readonly ref T this[uint index] { get; }
Parameters
indexuint
Property Value
- T
Methods
Add(scoped in T)
Adds a new element to the end of the list, allocating new chunks as needed.
public void Add(scoped in T value)
Parameters
valueT
AddNoResize(scoped in T)
Adds the specified value to the collection. For chunked lists, this is equivalent to Add(scoped in T), since allocating new chunks never moves existing elements.
public void AddNoResize(scoped in T value)
Parameters
valueT
AddRange(ReadOnlySpan<T>)
Adds a range of elements to the collection, allocating new chunks as needed.
public void AddRange(ReadOnlySpan<T> values)
Parameters
valuesReadOnlySpan<T>
AddRange(T*, int)
Adds a range of elements from a pointer to the collection, allocating new chunks as needed.
public void AddRange(T* ptr, int count)
Parameters
ptrT*countint
AddRangeNoResize(ReadOnlySpan<T>)
Adds a range of elements. For chunked lists, this is equivalent to AddRange(ReadOnlySpan<T>), since allocating new chunks never moves existing elements.
public void AddRangeNoResize(ReadOnlySpan<T> collection)
Parameters
collectionReadOnlySpan<T>
AddRangeNoResize(T*, int)
Adds a range of elements from a pointer. For chunked lists, this is equivalent to AddRange(T*, int), since allocating new chunks never moves existing elements.
public void AddRangeNoResize(T* ptr, int count)
Parameters
ptrT*countint
AsParallelReader()
Provides a parallel reader for the current list, enabling thread-safe read operations.
public UnsafeChunkedList<T>.ParallelReader AsParallelReader()
Returns
AsParallelWriter()
Provides a parallel writer for the current list, enabling thread-safe additions.
public UnsafeChunkedList<T>.ParallelWriter AsParallelWriter()
Returns
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 into the list, growing as needed.
public void CopyFrom(ReadOnlySpan<T> source)
Parameters
sourceReadOnlySpan<T>
CopyFrom(ReadOnlySpan<T>, int, int, int)
Copies a range of elements from a source span to the list.
public void CopyFrom(ReadOnlySpan<T> source, int sourceIndex, int destinationIndex, int length)
Parameters
sourceReadOnlySpan<T>sourceIndexintdestinationIndexintlengthint
CopyTo(Span<T>)
Copies all elements into a destination span.
public readonly void CopyTo(Span<T> destination)
Parameters
destinationSpan<T>
CopyTo(Span<T>, int, int, int)
Copies a range of elements from the list to a destination span.
public readonly void CopyTo(Span<T> destination, int sourceIndex, int destinationIndex, int length)
Parameters
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
EnsureCapacity(int)
Pre-allocates chunks to accommodate at least the specified number of elements.
public void EnsureCapacity(int capacity)
Parameters
capacityint
GetEnumerator()
[UnscopedRef]
public UnsafeChunkedList<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.
RemoveAt(int)
Removes the element at the specified index.
public void RemoveAt(int index)
Parameters
indexint
RemoveAtSwapBack(int)
Removes the element at the specified index by swapping it with the last element.
public void RemoveAtSwapBack(int index)
Parameters
indexint
RemoveRange(int, int)
Removes a range of elements from the list starting at the specified index.
public void RemoveRange(int start, int length)
Parameters
RemoveRangeSwapBack(int, int)
Removes a range of elements by swapping them with elements from the end of the list.
public void RemoveRangeSwapBack(int start, int length)
Parameters
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.
ToList()
Creates a new List<T> containing the elements.
public readonly List<T> ToList()
Returns
- List<T>