Table of Contents

Class CircularBuffer<T>

Namespace
Balsam.Utility
Assembly
Balsam.Backtester.dll

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

int

_start

The Index into the _store where the first element if the buffer resides.

protected int _start

Field Value

int

_store

The backing store

protected T[] _store

Field Value

T[]

Properties

Capacity

Gets the capacity of the buffer.

public int Capacity { get; }

Property Value

int

Count

Gets the number of items in the buffer.

public int Count { get; }

Property Value

int

IsFull

Returns true if the buffer is filled to capacity.

public bool IsFull { get; }

Property Value

bool

IsReadOnly

Is this buffer read only?

public bool IsReadOnly { get; }

Property Value

bool

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

DateTime?

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 DateTime
item 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

bool

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

Returns

bool