Struct UnsafeList<T>
An unmanaged, resizable list.
Namespace: Unity.Collections.LowLevel.Unsafe
Assembly: Unity.Collections.dll
Syntax
public struct UnsafeList<T> : INativeDisposable, INativeList<T>, IIndexable<T> where T : unmanaged
Type Parameters
Name | Description |
---|---|
T | The type of the elements. |
Constructors
UnsafeList(int, AllocatorHandle, NativeArrayOptions)
Initializes and returns an instance of UnsafeList.
Declaration
public UnsafeList(int initialCapacity, AllocatorManager.AllocatorHandle allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
Parameters
Type | Name | Description |
---|---|---|
int | initialCapacity | The initial capacity of the list. |
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
NativeArrayOptions | options | Whether newly allocated bytes should be zeroed out. |
UnsafeList(T*, int)
Initializes and returns an instance of UnsafeList.
Declaration
public UnsafeList(T* ptr, int length)
Parameters
Type | Name | Description |
---|---|---|
T* | ptr | An existing byte array to set as the internal buffer. |
int | length | The length. |
Fields
Allocator
The allocator used to create the internal buffer.
Declaration
public AllocatorManager.AllocatorHandle Allocator
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle |
Ptr
The internal buffer of this list.
Declaration
[NativeDisableUnsafePtrRestriction]
public T* Ptr
Field Value
Type | Description |
---|---|
T* |
m_capacity
The number of elements that can fit in the internal buffer.
Declaration
public int m_capacity
Field Value
Type | Description |
---|---|
int |
m_length
The number of elements.
Declaration
public int m_length
Field Value
Type | Description |
---|---|
int |
Properties
Capacity
The number of elements that can fit in the internal buffer.
Declaration
public int Capacity { get; set; }
Property Value
Type | Description |
---|---|
int | The number of elements that can fit in the internal buffer. |
IsCreated
Whether this list has been allocated (and not yet deallocated).
Declaration
public bool IsCreated { get; }
Property Value
Type | Description |
---|---|
bool | True if this list has been allocated (and not yet deallocated). |
IsEmpty
Whether the list is empty.
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
bool | True if the list is empty. |
this[int]
The element at an index.
Declaration
public T this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | index | An index. |
Property Value
Type | Description |
---|---|
T | The element at the index. |
Length
The number of elements.
Declaration
public int Length { get; set; }
Property Value
Type | Description |
---|---|
int | The number of elements. |
Methods
Add(in T)
Adds an element to the end of the list.
Declaration
public void Add(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to add to the end of this list. |
Remarks
Increments the length by 1. Increases the capacity if necessary.
AddNoResize(T)
Adds an element to the end of this list.
Declaration
public void AddNoResize(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to add to the end of the list. |
Remarks
Increments the length by 1. Never increases the capacity.
Exceptions
Type | Condition |
---|---|
Exception | Thrown if incrementing the length would exceed the capacity. |
AddRange(void*, int)
Copies the elements of a buffer to the end of this list.
Declaration
public void AddRange(void* ptr, int count)
Parameters
Type | Name | Description |
---|---|---|
void* | ptr | The buffer to copy from. |
int | count | The number of elements to copy from the buffer. |
Remarks
Increments the length by count
. Increases the capacity if necessary.
AddRange(UnsafeList<T>)
Copies the elements of another list to the end of the list.
Declaration
public void AddRange(UnsafeList<T> list)
Parameters
Type | Name | Description |
---|---|---|
UnsafeList<T> | list | The list to copy from. |
Remarks
The length is increased by the length of the other list. Increases the capacity if necessary.
AddRangeNoResize(void*, int)
Copies elements from a buffer to the end of this list.
Declaration
public void AddRangeNoResize(void* ptr, int count)
Parameters
Type | Name | Description |
---|---|---|
void* | ptr | The buffer to copy from. |
int | count | The number of elements to copy from the buffer. |
Remarks
Increments the length by count
. Never increases the capacity.
Exceptions
Type | Condition |
---|---|
Exception | Thrown if the increased length would exceed the capacity. |
AddRangeNoResize(UnsafeList<T>)
Copies the elements of another list to the end of this list.
Declaration
public void AddRangeNoResize(UnsafeList<T> list)
Parameters
Type | Name | Description |
---|---|---|
UnsafeList<T> | list | The other list to copy from. |
Remarks
Increments the length by the length of the other list. Never increases the capacity.
Exceptions
Type | Condition |
---|---|
Exception | Thrown if the increased length would exceed the capacity. |
AsParallelReader()
Returns a parallel reader of this list.
Declaration
public UnsafeList<T>.ParallelReader AsParallelReader()
Returns
Type | Description |
---|---|
UnsafeList<T>.ParallelReader | A parallel reader of this list. |
AsParallelWriter()
Returns a parallel writer of this list.
Declaration
public UnsafeList<T>.ParallelWriter AsParallelWriter()
Returns
Type | Description |
---|---|
UnsafeList<T>.ParallelWriter | A parallel writer of this list. |
Clear()
Sets the length to 0.
Declaration
public void Clear()
Remarks
Does not change the capacity.
Create(int, AllocatorHandle, NativeArrayOptions)
Returns a new list.
Declaration
public static UnsafeList<T>* Create(int initialCapacity, AllocatorManager.AllocatorHandle allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
Parameters
Type | Name | Description |
---|---|---|
int | initialCapacity | The initial capacity of the list. |
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
NativeArrayOptions | options | Whether newly allocated bytes should be zeroed out. |
Returns
Type | Description |
---|---|
UnsafeList<T>* | A pointer to the new list. |
Destroy(UnsafeList<T>*)
Destroys the list.
Declaration
public static void Destroy(UnsafeList<T>* listData)
Parameters
Type | Name | Description |
---|---|---|
UnsafeList<T>* | listData | The list to destroy. |
Dispose()
Releases all resources (memory).
Declaration
public void Dispose()
Dispose(JobHandle)
Creates and schedules a job that frees the memory of this list.
Declaration
public JobHandle Dispose(JobHandle inputDeps)
Parameters
Type | Name | Description |
---|---|---|
JobHandle | inputDeps | The dependency for the new job. |
Returns
Type | Description |
---|---|
JobHandle | The handle of the new job. The job depends upon |
ElementAt(int)
Returns a reference to the element at a given index.
Declaration
public ref T ElementAt(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index to access. Must be in the range of [0..Length). |
Returns
Type | Description |
---|---|
T | A reference to the element at the index. |
GetEnumerator()
Returns an enumerator over the elements of the list.
Declaration
public UnsafeList<T>.Enumerator GetEnumerator()
Returns
Type | Description |
---|---|
UnsafeList<T>.Enumerator | An enumerator over the elements of the list. |
InsertRangeWithBeginEnd(int, int)
Shifts elements toward the end of this list, increasing its length.
Declaration
public void InsertRangeWithBeginEnd(int begin, int end)
Parameters
Type | Name | Description |
---|---|---|
int | begin | The index of the first element that will be shifted up. |
int | end | The index where the first shifted element will end up. |
Remarks
Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
The length is increased by end - begin
. If necessary, the capacity will be increased accordingly.
If end
equals begin
, the method does nothing.
The element at index begin
will be copied to index end
, the element at index begin + 1
will be copied to end + 1
, and so forth.
The indexes begin
up to end
are not cleared: they will contain whatever values they held prior.
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if |
ArgumentOutOfRangeException | Thrown if |
RemoveAt(int)
Removes the element at an index, shifting everything above it down by one. Decrements the length by 1.
Declaration
public void RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index of the element to remove. |
Remarks
If you don't care about preserving the order of the elements, RemoveAtSwapBack(int) is a more efficient way to remove elements.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if |
RemoveAtSwapBack(int)
Copies the last element of this list to the specified index. Decrements the length by 1.
Declaration
public void RemoveAtSwapBack(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index to overwrite with the last element. |
Remarks
Useful as a cheap way to remove an element from this list when you don't care about preserving order.
Exceptions
Type | Condition |
---|---|
IndexOutOfRangeException | Thrown if |
RemoveRange(int, int)
Removes N elements in a range, shifting everything above the range down by N. Decrements the length by N.
Declaration
public void RemoveRange(int index, int count)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index of the first element to remove. |
int | count | The number of elements to remove. |
Remarks
If you don't care about preserving the order of the elements, RemoveRangeSwapBackWithBeginEnd(int, int) is a more efficient way to remove elements.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if |
RemoveRangeSwapBack(int, int)
Copies the last N elements of this list to a range in this list. Decrements the length by N.
Declaration
public void RemoveRangeSwapBack(int index, int count)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index of the first element to overwrite. |
int | count | The number of elements to copy and remove. |
Remarks
Copies the last count
elements to the indexes index
up to index + count
.
Useful as a cheap way to remove elements from a list when you don't care about preserving order.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if |
Resize(int, NativeArrayOptions)
Sets the length, expanding the capacity if necessary.
Declaration
public void Resize(int length, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
Parameters
Type | Name | Description |
---|---|---|
int | length | The new length. |
NativeArrayOptions | options | Whether newly allocated bytes should be zeroed out. |
SetCapacity(int)
Sets the capacity.
Declaration
public void SetCapacity(int capacity)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The new capacity. |
TrimExcess()
Sets the capacity to match the length.
Declaration
public void TrimExcess()