Struct UnsafeList<T> | Collections | 0.15.0-preview.21
docs.unity3d.com
    Show / Hide Table of Contents

    Struct UnsafeList<T>

    An managed, resizable list, without any thread safety check features.

    Namespace: Unity.Collections.LowLevel.Unsafe
    Syntax
    public struct UnsafeList<T> : INativeDisposable, IDisposable, INativeList<T>, IEnumerable<T>, IEnumerable where T : struct
    Type Parameters
    Name Description
    T

    Source type of elements

    Constructors

    UnsafeList(T*, Int32)

    Constructs list as view into memory.

    Declaration
    public UnsafeList(T*ptr, int length)
    Parameters
    Type Name Description
    T* ptr

    Pointer to data.

    Int32 length

    Lenght of data in bytes.

    UnsafeList(Int32, Allocator, NativeArrayOptions)

    Constructs a new list using the specified type of memory allocation.

    Declaration
    public UnsafeList(int initialCapacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
    Parameters
    Type Name Description
    Int32 initialCapacity

    The initial capacity of the list. If the list grows larger than its capacity, the internal array is copied to a new, larger array.

    Allocator allocator

    A member of the Unity.Collections.Allocator enumeration.

    NativeArrayOptions options

    Memory should be cleared on allocation or left uninitialized.

    Remarks

    The list initially has a capacity of one. To avoid reallocating memory for the list, specify sufficient capacity up front.

    UnsafeList(Int32, AllocatorManager.AllocatorHandle, NativeArrayOptions)

    Constructs a new list using the specified type of memory allocation.

    Declaration
    public UnsafeList(int initialCapacity, AllocatorManager.AllocatorHandle allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
    Parameters
    Type Name Description
    Int32 initialCapacity

    The initial capacity of the list. If the list grows larger than its capacity, the internal array is copied to a new, larger array.

    AllocatorManager.AllocatorHandle allocator

    A member of the Unity.Collections.Allocator enumeration.

    NativeArrayOptions options

    Memory should be cleared on allocation or left uninitialized.

    Remarks

    The list initially has a capacity of one. To avoid reallocating memory for the list, specify sufficient capacity up front.

    Fields

    Allocator

    Declaration
    public AllocatorManager.AllocatorHandle Allocator
    Field Value
    Type Description
    AllocatorManager.AllocatorHandle

    capacity

    Declaration
    public int capacity
    Field Value
    Type Description
    Int32

    length

    Declaration
    public int length
    Field Value
    Type Description
    Int32

    Ptr

    Declaration
    [NativeDisableUnsafePtrRestriction]
    public T*Ptr
    Field Value
    Type Description
    T*

    Properties

    Capacity

    Declaration
    public int Capacity { get; set; }
    Property Value
    Type Description
    Int32
    Implements
    INativeList<T>.Capacity

    IsCreated

    Reports whether memory for the container is allocated.

    Declaration
    public bool IsCreated { get; }
    Property Value
    Type Description
    Boolean

    True if this container object's internal storage has been allocated.

    Remarks

    Note that the container storage is not created if you use the default constructor. You must specify at least an allocation type to construct a usable container.

    Warning: the IsCreated property can't be used to determine whether a copy of a container is still valid. If you dispose any copy of the container, the container storage is deallocated. However, the properties of the other copies of the container (including the original) are not updated. As a result the IsCreated property of the copies still return true even though the container storage has been deallocated.

    IsEmpty

    Reports whether container is empty.

    Declaration
    public bool IsEmpty { get; }
    Property Value
    Type Description
    Boolean

    True if this container empty.

    Implements
    INativeList<T>.IsEmpty

    Item[Int32]

    Declaration
    public T this[int index] { get; set; }
    Parameters
    Type Name Description
    Int32 index
    Property Value
    Type Description
    T
    Implements
    INativeList<T>.Item[Int32]

    Length

    Declaration
    public int Length { get; set; }
    Property Value
    Type Description
    Int32
    Implements
    INativeList<T>.Length

    Methods

    Add(T)

    Adds an element to the list.

    Declaration
    public void Add(in T value)
    Parameters
    Type Name Description
    T value

    The struct to be added at the end of the list.

    AddNoResize(T)

    Adds an element to the container.

    Declaration
    public void AddNoResize(T value)
    Parameters
    Type Name Description
    T value

    The value to be added at the end of the container.

    Remarks

    If the list has reached its current capacity, internal array won't be resized, and exception will be thrown.

    AddRange(Void*, Int32)

    Adds elements from a buffer to this list.

    Declaration
    public void AddRange(void *ptr, int length)
    Parameters
    Type Name Description
    Void* ptr

    A pointer to the buffer.

    Int32 length

    The number of elements to add to the list.

    AddRange(UnsafeList<T>)

    Adds the elements of a UnsafePtrList to this list.

    Declaration
    public void AddRange(UnsafeList<T> list)
    Parameters
    Type Name Description
    UnsafeList<T> list

    Other container to copy elements from.

    AddRangeNoResize(Void*, Int32)

    Adds the elements to this container.

    Declaration
    public void AddRangeNoResize(void *ptr, int length)
    Parameters
    Type Name Description
    Void* ptr

    A pointer to the buffer.

    Int32 length

    The number of elements to add to the container.

    Remarks

    If the list has reached its current capacity, internal array won't be resized, and exception will be thrown.

    AddRangeNoResize(UnsafeList<T>)

    Adds elements from a list to this container.

    Declaration
    public void AddRangeNoResize(UnsafeList<T> list)
    Parameters
    Type Name Description
    UnsafeList<T> list

    Other container to copy elements from.

    Remarks

    If the list has reached its current capacity, internal array won't be resized, and exception will be thrown.

    AsParallelReader()

    Returns parallel reader instance.

    Declaration
    public UnsafeList<T>.ParallelReader AsParallelReader()
    Returns
    Type Description
    UnsafeList.ParallelReader<>

    Parallel reader instance.

    AsParallelWriter()

    Returns parallel writer instance.

    Declaration
    public UnsafeList<T>.ParallelWriter AsParallelWriter()
    Returns
    Type Description
    UnsafeList.ParallelWriter<>

    Parallel writer instance.

    Clear()

    Clears the container.

    Declaration
    public void Clear()
    Implements
    INativeList<T>.Clear()
    Remarks

    List Capacity remains unchanged.

    Dispose()

    Disposes of this container and deallocates its memory immediately.

    Declaration
    public void Dispose()

    Dispose(JobHandle)

    Safely disposes of this container and deallocates its memory when the jobs that use it have completed.

    Declaration
    public JobHandle Dispose(JobHandle inputDeps)
    Parameters
    Type Name Description
    JobHandle inputDeps

    The job handle or handles for any scheduled jobs that use this container.

    Returns
    Type Description
    JobHandle

    A new job handle containing the prior handles as well as the handle for the job that deletes the container.

    Implements
    INativeDisposable.Dispose(JobHandle)
    Remarks

    You can call this function dispose of the container immediately after scheduling the job. Pass the JobHandle returned by the Job.Schedule method using the jobHandle parameter so the job scheduler can dispose the container after all jobs using it have run.

    ElementAt(Int32)

    Declaration
    public T ElementAt(int index)
    Parameters
    Type Name Description
    Int32 index
    Returns
    Type Description
    T
    Implements
    INativeList<T>.ElementAt(Int32)

    GetEnumerator()

    Returns an IEnumerator interface for the container.

    Declaration
    public UnsafeList<T>.Enumerator GetEnumerator()
    Returns
    Type Description
    UnsafeList.Enumerator<>

    An IEnumerator interface for the container.

    InsertRangeWithBeginEnd(Int32, Int32)

    Inserts a number of items into a container at a specified zero-based index.

    Declaration
    public void InsertRangeWithBeginEnd(int begin, int end)
    Parameters
    Type Name Description
    Int32 begin

    The zero-based index at which the new elements should be inserted.

    Int32 end

    The zero-based index just after where the elements should be removed.

    Exceptions
    Type Condition
    ArgumentException

    Thrown if end argument is less than begin argument.

    ArgumentOutOfRangeException

    Thrown if begin or end arguments are not positive or out of bounds.

    RemoveAt(Int32)

    Truncates the list by removing the item at the specified index, and shifting all remaining items to replace removed item. The list is shortened by one.

    Declaration
    public void RemoveAt(int index)
    Parameters
    Type Name Description
    Int32 index

    The index of the item to delete.

    Remarks

    This method of removing item is useful only in case when list is ordered and user wants to preserve order in list after removal In majority of cases is not important and user should use more performant RemoveAtSwapBack.

    RemoveAtSwapBack(Int32)

    Truncates the list by replacing the item at the specified index with the last item in the list. The list is shortened by one.

    Declaration
    public void RemoveAtSwapBack(int index)
    Parameters
    Type Name Description
    Int32 index

    The index of the item to delete.

    RemoveRangeSwapBackWithBeginEnd(Int32, Int32)

    Truncates the list by replacing the item at the specified index range with the items from the end the list. The list is shortened by number of elements in range.

    Declaration
    public void RemoveRangeSwapBackWithBeginEnd(int begin, int end)
    Parameters
    Type Name Description
    Int32 begin

    The first index of the item to remove.

    Int32 end

    The index past-the-last item to remove.

    Exceptions
    Type Condition
    ArgumentException

    Thrown if end argument is less than begin argument.

    ArgumentOutOfRangeException

    Thrown if begin or end arguments are not positive or out of bounds.

    RemoveRangeWithBeginEnd(Int32, Int32)

    Truncates the list by removing the items at the specified index range, and shifting all remaining items to replace removed items. The list is shortened by number of elements in range.

    Declaration
    public void RemoveRangeWithBeginEnd(int begin, int end)
    Parameters
    Type Name Description
    Int32 begin

    The first intex of the item to remove.

    Int32 end

    The index past-the-last item to remove.

    Remarks

    This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order in list after removal In majority of cases is not important and user should use more performant RemoveRangeSwapBackWithBeginEnd.

    Exceptions
    Type Condition
    ArgumentException

    Thrown if end argument is less than begin argument.

    ArgumentOutOfRangeException

    Thrown if begin or end arguments are not positive or out of bounds.

    Resize(Int32, NativeArrayOptions)

    Changes the list length, resizing if necessary.

    Declaration
    public void Resize(int length, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
    Parameters
    Type Name Description
    Int32 length

    The new length of the list.

    NativeArrayOptions options

    Memory should be cleared on allocation or left uninitialized.

    SetCapacity(Int32)

    Set the number of items that can fit in the list.

    Declaration
    public void SetCapacity(int capacity)
    Parameters
    Type Name Description
    Int32 capacity

    The number of items that the list can hold before it resizes its internal storage.

    TrimExcess()

    Sets the capacity to the actual number of elements in the container.

    Declaration
    public void TrimExcess()

    Extension Methods

    NativeSortExtension.Sort<T, U>(UnsafeList<T>, U)
    NativeSortExtension.Sort<T, U>(UnsafeList<T>, U, JobHandle)
    NativeSortExtension.BinarySearch<T, U>(UnsafeList<T>, T, U)
    UnsafeListExtensions.IndexOf<T, U>(UnsafeList<T>, U)
    UnsafeListExtensions.Contains<T, U>(UnsafeList<T>, U)
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023