Struct UnsafeList<T>
An unmanaged, resizable list.
Namespace: Unity.Collections.LowLevel.Unsafe
Syntax
public struct UnsafeList<T> : INativeDisposable, IDisposable, INativeList<T>, IIndexable<T>, IEnumerable<T>, IEnumerable where T : struct
Type Parameters
Name | Description |
---|---|
T | The type of the elements. |
Constructors
UnsafeList(T*, Int32)
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. |
Int32 | length | The length. |
UnsafeList(Int32, AllocatorManager.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 |
---|---|---|
Int32 | initialCapacity | The initial capacity of the list. |
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
NativeArrayOptions | options | Whether newly allocated bytes should be zeroed out. |
Fields
Allocator
The allocator used to create the internal buffer.
Declaration
public AllocatorManager.AllocatorHandle Allocator
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle |
m_capacity
The number of elements that can fit in the internal buffer.
Declaration
public int m_capacity
Field Value
Type | Description |
---|---|
Int32 |
m_length
The number of elements.
Declaration
public int m_length
Field Value
Type | Description |
---|---|
Int32 |
Ptr
The internal buffer of this list.
Declaration
[NativeDisableUnsafePtrRestriction]
public T*Ptr
Field Value
Type | Description |
---|---|
T* |
Properties
Capacity
The number of elements that can fit in the internal buffer.
Declaration
public int Capacity { get; set; }
Property Value
Type | Description |
---|---|
Int32 | The number of elements that can fit in the internal buffer. |
Implements
IsCreated
Whether this list has been allocated (and not yet deallocated).
Declaration
public readonly bool IsCreated { get; }
Property Value
Type | Description |
---|---|
Boolean | True if this list has been allocated (and not yet deallocated). |
IsEmpty
Whether the list is empty.
Declaration
public readonly bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
Boolean | True if the list is empty or the list has not been constructed. |
Implements
Item[Int32]
The element at an index.
Declaration
public T this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | An index. |
Property Value
Type | Description |
---|---|
T | The element at the index. |
Implements
Length
The number of elements.
Declaration
public int Length { get; set; }
Property Value
Type | Description |
---|---|
Int32 | The number of elements. |
Implements
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*, Int32)
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. |
Int32 | 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*, Int32)
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. |
Int32 | 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.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.ParallelWriter<> | A parallel writer of this list. |
Clear()
Sets the length to 0.
Declaration
public void Clear()
Implements
Remarks
Does not change the capacity.
CopyFrom(UnsafeList<T>)
Overwrites the elements of this list with the elements of an equal-length array.
Declaration
public void CopyFrom(UnsafeList<T> array)
Parameters
Type | Name | Description |
---|---|---|
UnsafeList<T> | array | An array to copy into this list. |
Create(Int32, AllocatorManager.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 |
---|---|---|
Int32 | 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 |
Implements
ElementAt(Int32)
Returns a reference to the element at a given index.
Declaration
public ref T ElementAt(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | 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. |
Implements
GetEnumerator()
Returns an enumerator over the elements of the list.
Declaration
public UnsafeList<T>.Enumerator GetEnumerator()
Returns
Type | Description |
---|---|
UnsafeList.Enumerator<> | An enumerator over the elements of the list. |
InsertRangeWithBeginEnd(Int32, Int32)
Shifts elements toward the end of this list, increasing its length.
Declaration
public void InsertRangeWithBeginEnd(int begin, int end)
Parameters
Type | Name | Description |
---|---|---|
Int32 | begin | The index of the first element that will be shifted up. |
Int32 | 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(Int32)
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 |
---|---|---|
Int32 | index | The index of the element to remove. |
Remarks
If you don't care about preserving the order of the elements, RemoveAtSwapBack(Int32) is a more efficient way to remove elements.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if |
RemoveAtSwapBack(Int32)
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 |
---|---|---|
Int32 | 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(Int32, Int32)
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 |
---|---|---|
Int32 | index | The index of the first element to remove. |
Int32 | count | The number of elements to remove. |
Remarks
If you don't care about preserving the order of the elements, Unity.Collections.LowLevel.Unsafe.UnsafeList`1.RemoveRangeSwapBackWithBeginEnd(System.Int32,System.Int32) is a more efficient way to remove elements.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if |
RemoveRangeSwapBack(Int32, Int32)
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 |
---|---|---|
Int32 | index | The index of the first element to overwrite. |
Int32 | 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(Int32, NativeArrayOptions)
Sets the length, expanding the capacity if necessary.
Declaration
public void Resize(int length, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
Parameters
Type | Name | Description |
---|---|---|
Int32 | length | The new length. |
NativeArrayOptions | options | Whether newly allocated bytes should be zeroed out. |
SetCapacity(Int32)
Sets the capacity.
Declaration
public void SetCapacity(int capacity)
Parameters
Type | Name | Description |
---|---|---|
Int32 | capacity | The new capacity. |
TrimExcess()
Sets the capacity to match the length.
Declaration
public void TrimExcess()