Class CircularBuffer<T>
Supports all classes in the .NET class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all .NET classes; it is the root of the type hierarchy.
public class CircularBuffer<T> : IEnumerable<T>, IEnumerable
Type Parameters
T
- Inheritance
-
CircularBuffer<T>
- Implements
-
IEnumerable<T>
- Inherited Members
Constructors
CircularBuffer(int)
Initializes a new instance of the CircularBuffer<T> class.
public CircularBuffer(int capacity)
Parameters
capacityintBuffer capacity. Must be positive.
CircularBuffer(int, T[])
Initializes a new instance of the CircularBuffer<T> class.
public CircularBuffer(int capacity, T[] items)
Parameters
capacityintBuffer capacity. Must be positive.
itemsT[]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 int Capacity { get; }
Property Value
IsEmpty
True if has no elements.
public bool IsEmpty { get; }
Property Value
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
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
indexintIndex of element to access.
Property Value
- T
Exceptions
- IndexOutOfRangeException
Thrown when index is outside of [; Size[ interval.
Size
Current buffer size (the number of elements that the buffer has).
public int Size { get; }
Property Value
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 void Clear()
Exceptions
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 void PushBack(T item)
Parameters
itemTItem 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
itemTItem 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>>).
public IList<ArraySegment<T>> ToArraySegments()
Returns
- IList<ArraySegment<T>>
An IList with 2 segments corresponding to the buffer content.