UnityUtilities
|
A container that always stores the last arraySize elements added. New elements are added via Append(), which automatically rolls over once the maximum number of elements is reached, overwriting the oldest element. array[i] always returns the oldest element that still exists + i. That way, this container always stores the last arraySize elements added. More...
Public Member Functions | |
RollingArray (int arraySize) | |
Creates a new rolling array with the defined arraySize. More... | |
void | Append (T element) |
Appends an element to the RollingArray. Automatically rolls over once the maximum number of elements is reached, overwriting the oldest element More... | |
void | Clear () |
Clears the RollingArray. More... | |
IEnumerator< T > | GetEnumerator () |
Returns an enumerator that iterates through the collection from oldest to newest element. More... | |
Properties | |
int | Count [get] |
The current count of filled elements. More... | |
bool | IsEmpty [get] |
Is the array empty? More... | |
T | OldestElement [get] |
Gets the oldest element. More... | |
T | LatestElement [get] |
Gets the latest added element. More... | |
T | this[int i] [get, set] |
Gets/sets an element from/in the array. 0 is always the oldest element that is still in the array, [Count-1] is always the latest added element. More... | |
A container that always stores the last arraySize elements added. New elements are added via Append(), which automatically rolls over once the maximum number of elements is reached, overwriting the oldest element. array[i] always returns the oldest element that still exists + i. That way, this container always stores the last arraySize elements added.
Adding is O(1), retrieving is O(1) and (with the exception of GetEnumerator()) no new memory is allocated after the initial creation.
T | The collection element type. |
UnityUtilities.RollingArray< T >.RollingArray | ( | int | arraySize | ) |
Creates a new rolling array with the defined arraySize.
arraySize |
void UnityUtilities.RollingArray< T >.Append | ( | T | element | ) |
Appends an element to the RollingArray. Automatically rolls over once the maximum number of elements is reached, overwriting the oldest element
element | The element to be added. |
void UnityUtilities.RollingArray< T >.Clear | ( | ) |
Clears the RollingArray.
IEnumerator<T> UnityUtilities.RollingArray< T >.GetEnumerator | ( | ) |
Returns an enumerator that iterates through the collection from oldest to newest element.
Caveat: Due to the outdated Mono version used in Unity3D, this allocates a small amount of memory, leading to memory fragmentation if called often. You might want to use array[i] instead.
Read more about this under "Should you avoid foreach loops?" at: http://www.gamasutra.com/blogs/WendelinReich/20131109/203841/C_Memory_Management_for_Unity_Developers_part_1_of_3.php
|
get |
The current count of filled elements.
|
get |
Is the array empty?
|
get |
Gets the latest added element.
IndexOutOfRangeException | Thrown when the array is empty. |
|
get |
Gets the oldest element.
IndexOutOfRangeException | Thrown when the array is empty. |
|
getset |
Gets/sets an element from/in the array. 0 is always the oldest element that is still in the array, [Count-1] is always the latest added element.
This method should NOT be used to add elements - used Append() for that. Only read and write elements that are already in the array.
i | The index. 0 is the oldest, [Count-1] the newest element. |
IndexOutOfRangeException | Thrown when accessing an element outside of [0..Count-1]. |