Class CircularBuffer<T>
A queue-like data structure that allows read-only access to all values in the queue.
Namespace: Unity.LiveCapture
Syntax
public class CircularBuffer<T> : IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
Type Parameters
Name | Description |
---|---|
T | The type of data stored in the buffer. |
Constructors
CircularBuffer(Int32, Action<T>)
Constructs a new CircularBuffer<T> instance with an initial capacity.
Declaration
public CircularBuffer(int capacity, Action<T> elementDiscarded = null)
Parameters
Type | Name | Description |
---|---|---|
Int32 | capacity | The maximum number of elements which can be stored in the collection. |
Action<T> | elementDiscarded | A callback invoked for each element that is discarded from the buffer. |
Properties
Capacity
Gets the maximum number of elements which can be stored in the collection.
Declaration
public int Capacity { get; }
Property Value
Type | Description |
---|---|
Int32 |
Count
Gets the number of elements in the collection.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
Int32 |
ElementDiscarded
A callback invoked for each element that is discarded from the buffer.
Declaration
public Action<T> ElementDiscarded { get; set; }
Property Value
Type | Description |
---|---|
Action<T> |
Item[Int32]
Declaration
public T this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index |
Property Value
Type | Description |
---|---|
T |
Methods
Add(in T)
Adds an element to the back of the buffer.
Declaration
public void Add(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The element to add. |
Remarks
If the buffer is full, the element at the front of the buffer will be discarded.
Clear()
Removes all items in the collection.
Declaration
public void Clear()
GetEnumerator()
Declaration
public IEnumerator<T> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<T> |
PeekBack()
Get the element at the back of the buffer.
Declaration
public ref readonly T PeekBack()
Returns
Type | Description |
---|---|
T | The element at the back of the buffer. |
PeekFront()
Get the element at the front of the buffer.
Declaration
public ref readonly T PeekFront()
Returns
Type | Description |
---|---|
T | The element at the front of the buffer. |
PeekIndex(Int32)
Gets the element at the specified index in the buffer.
Declaration
public ref readonly T PeekIndex(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index counting from the front of the buffer. |
Returns
Type | Description |
---|---|
T | The element at the specified index in the buffer. |
PopBack()
Removes an element from the back of the buffer.
Declaration
public T PopBack()
Returns
Type | Description |
---|---|
T | The removed element. |
PopFront()
Removes an element from the front of the buffer.
Declaration
public T PopFront()
Returns
Type | Description |
---|---|
T | The removed element. |
PushBack(in T)
Adds an element to the back of the buffer.
Declaration
public void PushBack(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The element to add. |
Remarks
If the buffer is full, the element at the front of the buffer will be discarded.
PushFront(in T)
Adds an element to the front of the buffer.
Declaration
public void PushFront(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The element to add. |
Remarks
If the buffer is full, the element at the back of the buffer will be discarded.
PushIndex(Int32, in T)
Insets an element into the buffer at the specified index.
Declaration
public void PushIndex(int index, in T value)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index counting from the front of the buffer. |
T | value | The element to add. |
Remarks
If the buffer is full, the element at the front of the buffer will be discarded.
SetCapacity(Int32)
Sets the Capacity of the circular buffer.
Declaration
public void SetCapacity(int capacity)
Parameters
Type | Name | Description |
---|---|---|
Int32 | capacity | The desired capacity of the collection. |
Remarks
If the new size is smaller than the current Count, elements will be truncated from the front.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if the capacity is not greater than zero. |