Class DynamicArray<T>
Generic growable array.
Inherited Members
Namespace: UnityEngine.Rendering
Assembly: Unity.RenderPipelines.Core.Runtime.dll
Syntax
public class DynamicArray<T> where T : new()
Type Parameters
Name | Description |
---|---|
T | Type of the array. |
Constructors
DynamicArray()
Constructor. Defaults to a capacity of 32 elements. The size will be 0.
Declaration
public DynamicArray()
DynamicArray(int)
Constructor. This constructor allocates memory and sets the size of the array to the specified number of elements.
Declaration
public DynamicArray(int size)
Parameters
Type | Name | Description |
---|---|---|
int | size | Number of elements. The elements are initialized to the default value of the element type, 0 for integers. |
DynamicArray(int, bool)
Constructor. This overload allows you to only allocate memory without setting the size.
Declaration
public DynamicArray(int capacity, bool resize)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The number of elements to allocate. |
bool | resize | If true, also set the size of the array to the passed in capacity. If false, only allocate data but keep the size at 0. |
DynamicArray(DynamicArray<T>)
Constructor. This constructor allocates memory and does a deep copy of the provided array.
Declaration
public DynamicArray(DynamicArray<T> deepCopy)
Parameters
Type | Name | Description |
---|---|---|
DynamicArray<T> | deepCopy | Array to be copied |
Fields
m_Array
The C# array memory used to store the DynamicArray values in. This array's Length may be longer than the DynamicArrayLength. Objects beyond the length should not be referenced.
Declaration
protected T[] m_Array
Field Value
Type | Description |
---|---|
T[] |
Properties
this[int]
ref access to an element.
Declaration
public ref T this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
int | index | Element index |
Property Value
Type | Description |
---|---|
T | The requested element. |
capacity
Allocated size of the array.
Declaration
public int capacity { get; }
Property Value
Type | Description |
---|---|
int |
size
Number of elements in the array. There may be more elements allocated. Use capacity
to query the number of allocated items.
Declaration
public int size { get; protected set; }
Property Value
Type | Description |
---|---|
int |
version
This keeps track of structural modifications to this array and allows us to raise exceptions when modifying during enumeration
Declaration
protected int version { get; set; }
Property Value
Type | Description |
---|---|
int |
Methods
Add(in T)
Add an element to the array.
Declaration
public int Add(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | Element to add to the array. |
Returns
Type | Description |
---|---|
int | The index of the element. |
AddRange(DynamicArray<T>)
Adds the elements of the specified collection to the end of the DynamicArray.
Declaration
public void AddRange(DynamicArray<T> array)
Parameters
Type | Name | Description |
---|---|---|
DynamicArray<T> | array | The array whose elements should be added to the end of the DynamicArray. The array itself cannot be null, but it can contain elements that are null, if type T is a reference type. |
BumpVersion()
Increments the internal version counter.
Declaration
protected void BumpVersion()
Clear()
Clear the array of all elements.
Declaration
public void Clear()
Contains(T)
Determines whether the DynamicArray contains a specific value.
Declaration
public bool Contains(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The object to locate in the DynamicArray. |
Returns
Type | Description |
---|---|
bool | true if item is found in the DynamicArray; otherwise, false. |
FindIndex(int, int, Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the DynamicArray that starts at the specified index and contains the specified number of elements.
Declaration
public int FindIndex(int startIndex, int count, Predicate<T> match)
Parameters
Type | Name | Description |
---|---|---|
int | startIndex | The zero-based starting index of the search. |
int | count | The number of elements in the section to search. |
Predicate<T> | match | The Predicate delegate that defines the conditions of the element to search for. |
Returns
Type | Description |
---|---|
int | The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1. |
GetEnumerator()
Returns an enumerator that iterates through of this array. See the IEnumerable docs for more info: IEnumerable
Declaration
public DynamicArray<T>.Iterator GetEnumerator()
Returns
Type | Description |
---|---|
DynamicArray<T>.Iterator | Iterator pointing before the first element in the array. |
Remarks
The returned struct intentionally does not explicitly implement the IEnumarable/IEnumerator interfaces it just follows
the same function signatures. This means the duck typing used by foreach
on the compiler level will
pick it up as IEnumerable but at the same time avoids generating Garbage.
For more info, see the C# language specification of the foreach
statement.
IndexOf(T)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire DynamicArray.
Declaration
public int IndexOf(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The object to locate in the DynamicArray. The value can be null for reference types. |
Returns
Type | Description |
---|---|
int | he zero-based index of the first occurrence of item within the entire DynamicArray, if found; otherwise, -1. |
IndexOf(T, int)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the DynamicArray that extends from the specified index to the last element.
Declaration
public int IndexOf(T item, int index)
Parameters
Type | Name | Description |
---|---|---|
T | item | The object to locate in the DynamicArray. The value can be null for reference types. |
int | index | The zero-based starting index of the search. 0 (zero) is valid in an empty list. |
Returns
Type | Description |
---|---|
int | The zero-based index of the first occurrence of item within the range of elements in the DynamicArray that extends from index to the last element, if found; otherwise, -1. |
IndexOf(T, int, int)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the DynamicArray that starts at the specified index and contains the specified number of elements.
Declaration
public int IndexOf(T item, int index, int count)
Parameters
Type | Name | Description |
---|---|---|
T | item | The object to locate in the DynamicArray. The value can be null for reference types. |
int | index | The zero-based starting index of the search. 0 (zero) is valid in an empty list. |
int | count | The number of elements in the section to search. |
Returns
Type | Description |
---|---|
int | The index of the first occurrence of the object within the range of elements, or -1 if not found. |
Insert(int, T)
Insert an item in the DynamicArray.
Declaration
public void Insert(int index, T item)
Parameters
Type | Name | Description |
---|---|---|
int | index | Index where the item should be inserted. |
T | item | Item to be inserted in the DynamicArray. |
Remove(T)
Removes the first occurrence of a specific object from the DynamicArray.
Declaration
public bool Remove(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The object to remove from the DynamicArray. The value can be null for reference types. |
Returns
Type | Description |
---|---|
bool | true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the DynamicArray. |
RemoveAt(int)
Removes the element at the specified index of the DynamicArray.
Declaration
public void RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The zero-based index of the element to remove. |
RemoveRange(int, int)
Removes a range of elements from the DynamicArray.
Declaration
public void RemoveRange(int index, int count)
Parameters
Type | Name | Description |
---|---|---|
int | index | The zero-based starting index of the range of elements to remove. |
int | count | The number of elements to remove. |
Reserve(int, bool)
Sets the total number of elements the internal data structure can hold without resizing.
Declaration
public void Reserve(int newCapacity, bool keepContent = false)
Parameters
Type | Name | Description |
---|---|---|
int | newCapacity | New capacity for the array. |
bool | keepContent | Set to true if you want the current content of the array to be kept. |
Resize(int, bool)
Resize the Dynamic Array. This will reallocate memory if necessary and set the current size of the array to the provided size. Note: The memory is not cleared so the elements may contain invalid data.
Declaration
public void Resize(int newSize, bool keepContent = false)
Parameters
Type | Name | Description |
---|---|---|
int | newSize | New size for the array. |
bool | keepContent | Set to true if you want the current content of the array to be kept. |
ResizeAndClear(int)
Resize the Dynamic Array. This will reallocate memory if necessary and set the current size of the array to the provided size. The elements are initialized to the default value of the element type, e.g. 0 for integers.
Declaration
public void ResizeAndClear(int newSize)
Parameters
Type | Name | Description |
---|---|---|
int | newSize | New size for the array. |
SubRange(int, int)
Returns an IEnumeralbe-Like object that iterates through a subsection of this array.
Declaration
public DynamicArray<T>.RangeEnumerable SubRange(int first, int numItems)
Parameters
Type | Name | Description |
---|---|---|
int | first | The index of the first item |
int | numItems | The number of items to iterate |
Returns
Type | Description |
---|---|
DynamicArray<T>.RangeEnumerable |
|
Remarks
The returned struct intentionally does not explicitly implement the IEnumarable/IEnumerator interfaces it just follows
the same function signatures. This means the duck typing used by foreach
on the compiler level will
pick it up as IEnumerable but at the same time avoids generating Garbage.
For more info, see the C# language specification of the foreach
statement.
Operators
implicit operator ReadOnlySpan<T>(DynamicArray<T>)
Implicit conversion to ReadOnlySpan.
Declaration
public static implicit operator ReadOnlySpan<T>(DynamicArray<T> array)
Parameters
Type | Name | Description |
---|---|---|
DynamicArray<T> | array | Input DynamicArray. |
Returns
Type | Description |
---|---|
ReadOnlySpan<T> | The internal array. |
implicit operator Span<T>(DynamicArray<T>)
Implicit conversion to Span.
Declaration
public static implicit operator Span<T>(DynamicArray<T> array)
Parameters
Type | Name | Description |
---|---|---|
DynamicArray<T> | array | Input DynamicArray. |
Returns
Type | Description |
---|---|
Span<T> | The internal array. |
implicit operator T[](DynamicArray<T>)
Implicit conversion to regular array.
Declaration
[Obsolete("This is deprecated because it returns an incorrect value. It may returns an array with elements beyond the size. Please use Span/ReadOnly if you want safe raw access to the DynamicArray memory.", false)]
public static implicit operator T[](DynamicArray<T> array)
Parameters
Type | Name | Description |
---|---|---|
DynamicArray<T> | array | Input DynamicArray. |
Returns
Type | Description |
---|---|
T[] | The internal array. |