Table of Contents

Class CircularBuffer<T>

Namespace
Ecng.Collections
Assembly
Ecng.Collections.dll

Circular buffer.

When writing to a full buffer: PushBack -> removes this[0] / Front() PushFront -> removes this[Size-1] / Back()

this implementation is inspired by http://www.boost.org/doc/libs/1_53_0/libs/circular_buffer/doc/circular_buffer.html because I liked their interface.

public class CircularBuffer<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable

Type Parameters

T
Inheritance
CircularBuffer<T>
Implements
Derived
Inherited Members
Extension Methods

Constructors

CircularBuffer(int)

Initializes a new instance of the CircularBuffer<T> class.

public CircularBuffer(int capacity)

Parameters

capacity int

Buffer capacity. Must be positive.

CircularBuffer(int, T[])

Initializes a new instance of the CircularBuffer<T> class.

public CircularBuffer(int capacity, T[] items)

Parameters

capacity int

Buffer capacity. Must be positive.

items T[]

Items to fill buffer with. Items length must be less than capacity. Suggestion: use Skip(x).Take(y).ToArray() to build this argument from any enumerable.

Properties

Capacity

Maximum capacity of the buffer. Elements pushed into the buffer after maximum capacity is reached (IsFull = true), will remove an element.

public virtual int Capacity { get; set; }

Property Value

int

Count

Current buffer size (the number of elements that the buffer has).

public int Count { get; }

Property Value

int

IsEmpty

True if has no elements.

public bool IsEmpty { get; }

Property Value

bool

IsFull

Boolean indicating if Circular is at full capacity. Adding more elements when the buffer is full will cause elements to be removed from the other end of the buffer.

public bool IsFull { get; }

Property Value

bool

this[int]

Index access to elements in buffer. Index does not loop around like when adding elements, valid interval is [0;Size[

public T this[int index] { get; set; }

Parameters

index int

Index of element to access.

Property Value

T

Exceptions

IndexOutOfRangeException

Thrown when index is outside of [; Size[ interval.

Methods

Back()

Element at the back of the buffer - this[Size - 1].

public T Back()

Returns

T

The value of the element of type T at the back of the buffer.

Clear()

Clears the contents of the array. Size = 0, Capacity is unchanged.

public virtual void Clear()

Exceptions

NotImplementedException

Front()

Element at the front of the buffer - this[0].

public T Front()

Returns

T

The value of the element of type T at the front of the buffer.

GetEnumerator()

Returns an enumerator that iterates through this buffer.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator that can be used to iterate this collection.

PopBack()

Removes the element at the back of the buffer. Decreasing the Buffer size by 1.

public void PopBack()

PopFront()

Removes the element at the front of the buffer. Decreasing the Buffer size by 1.

public void PopFront()

PushBack(T)

Pushes a new element to the back of the buffer. Back()/this[Size-1] will now return this element.

When the buffer is full, the element at Front()/this[0] will be popped to allow for this new element to fit.

public virtual void PushBack(T item)

Parameters

item T

Item to push to the back of the buffer

PushFront(T)

Pushes a new element to the front of the buffer. Front()/this[0] will now return this element.

When the buffer is full, the element at Back()/this[Size-1] will be popped to allow for this new element to fit.

public void PushFront(T item)

Parameters

item T

Item to push to the front of the buffer

ToArray()

Copies the buffer contents to an array, according to the logical contents of the buffer (i.e. independent of the internal order/contents)

public T[] ToArray()

Returns

T[]

A new array with a copy of the buffer contents.

ToArraySegments()

Get the contents of the buffer as 2 ArraySegments. Respects the logical contents of the buffer, where each segment and items in each segment are ordered according to insertion.

Fast: does not copy the array elements. Useful for methods like Send(IList<ArraySegment<Byte>>).

Segments may be empty.

public IList<ArraySegment<T>> ToArraySegments()

Returns

IList<ArraySegment<T>>

An IList with 2 segments corresponding to the buffer content.