Class CircularBuffer<T>
A Circular Buffer holds as many elements as the capacity it was created with. Adding more items overwrites the oldest items.
public class CircularBuffer<T> : ICollection<T>, IEnumerable<T>, IEnumerable
Type Parameters
T
- Inheritance
-
CircularBuffer<T>
- Implements
-
ICollection<T>IEnumerable<T>
- Derived
- Inherited Members
- Extension Methods
Remarks
A CircularBuffer{T} can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Constructors
CircularBuffer(int)
Create a new Circular Buffer with the given capacity
public CircularBuffer(int capacity)
Parameters
capacity
int
Fields
_size
How many items we currently have (important because _store is bigger than the circular buffer initially)
protected int _size
Field Value
_start
The Index into the _store where the first element if the buffer resides.
protected int _start
Field Value
_store
The backing store
protected T[] _store
Field Value
- T[]
Properties
Capacity
Gets the capacity of the buffer.
public int Capacity { get; }
Property Value
Count
Gets the number of items in the buffer.
public int Count { get; }
Property Value
IsFull
Returns true if the buffer is filled to capacity.
public bool IsFull { get; }
Property Value
IsReadOnly
Is this buffer read only?
public bool IsReadOnly { get; }
Property Value
this[int]
Gets an item based on the specified index. 0 returns the most recent item, 1 the second most recent, etc.
public T this[int index] { get; }
Parameters
index
int
Property Value
- T
Last
Gets the last item.
public T Last { get; }
Property Value
- T
LastDate
Gets the date of the last observation added to the buffer or null if a date wasn't specified when adding the observation.
public DateTime? LastDate { get; }
Property Value
Previous
Gets the second to last item.
public T Previous { get; }
Property Value
- T
Methods
Add(DateTime, T)
Adds a new item to the buffer using the specified date.
public void Add(DateTime date, T item)
Parameters
date
DateTimeitem
T
Add(T)
Add a new item to the buffer. If the buffer is at its capacity, the oldest item will be discarded.
public void Add(T item)
Parameters
item
T
Clear()
Clear the Buffer, removing all elements
public void Clear()
Contains(T)
Does the buffer contain the given item?
public bool Contains(T item)
Parameters
item
T
Returns
CopyTo(T[], int)
Copies the buffer to the specified array starting at the specified index.
public void CopyTo(T[] array, int arrayIndex)
Parameters
array
T[]arrayIndex
int
GetEnumerator()
Get an Enumerator to enumerator over the buffer.
public IEnumerator<T> GetEnumerator()
Returns
- IEnumerator<T>
Remarks
If the buffer is modified while enumerating, the enumerator throws an exception
OnAdd(T)
Adds the specified item. Overrides should call the base implementation.
protected virtual void OnAdd(T item)
Parameters
item
T
Remove(T)
Removes an item from the collection. If the item removed occurs at or before the current position the current position is moved back by one.
public bool Remove(T item)
Parameters
item
T