UnityUtilities
UnityUtilities.RollingArray< T > Class Template Reference

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...

Inheritance diagram for UnityUtilities.RollingArray< T >:

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...
 
OldestElement [get]
 Gets the oldest element. More...
 
LatestElement [get]
 Gets the latest added element. More...
 
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...
 

Detailed Description

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.

Template Parameters
TThe collection element type.

Constructor & Destructor Documentation

§ RollingArray()

UnityUtilities.RollingArray< T >.RollingArray ( int  arraySize)

Creates a new rolling array with the defined arraySize.

Parameters
arraySize

Member Function Documentation

§ Append()

void UnityUtilities.RollingArray< T >.Append ( element)

Appends an element to the RollingArray. Automatically rolls over once the maximum number of elements is reached, overwriting the oldest element

Parameters
elementThe element to be added.

§ Clear()

void UnityUtilities.RollingArray< T >.Clear ( )

Clears the RollingArray.

§ GetEnumerator()

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

Returns
An enumerator that iterates through the collection from oldest to newest element.

Property Documentation

§ Count

int UnityUtilities.RollingArray< T >.Count
get

The current count of filled elements.

§ IsEmpty

bool UnityUtilities.RollingArray< T >.IsEmpty
get

Is the array empty?

§ LatestElement

T UnityUtilities.RollingArray< T >.LatestElement
get

Gets the latest added element.

Exceptions
IndexOutOfRangeExceptionThrown when the array is empty.

§ OldestElement

T UnityUtilities.RollingArray< T >.OldestElement
get

Gets the oldest element.

Exceptions
IndexOutOfRangeExceptionThrown when the array is empty.

§ this[int i]

T UnityUtilities.RollingArray< T >.this[int i]
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.

Parameters
iThe index. 0 is the oldest, [Count-1] the newest element.
Returns
The accessed elements.
Exceptions
IndexOutOfRangeExceptionThrown when accessing an element outside of [0..Count-1].

The documentation for this class was generated from the following file: