Here's a little C sharp implementation of a cyclic/ring buffer, though the name is not good probably it's more of an "keep the last n" buffer, because it does not cycle as you enumerate it (like in the C++ implementation in the article)
public class CyclicBuffer<T> : IEnumerable<T>
{
readonly int size;
readonly T[] values;
int start = 0;
int count = 0;
public CyclicBuffer( int size )
{
this.size = size;
this.values = new T[size];
}
public int Size { get { return this.size; } }
public int Count { get { return this.count; } }
public bool isFull { get { return this.size == this.count; } }
public T First { get { return this.values[this.start]; } }
public T Last { get { return this.values[Increment( this.start, this.count - 1 )]; } }
public void Clear()
{
this.start = 0;
this.count = 0;
}
public void Add( T obj )
{
int end = Increment( this.start, this.count );
this.values[end] = obj;
if (isFull)
this.start = Increment( this.start, 1 );
else
this.count++;
}
public IEnumerator<T> GetEnumerator()
{
int index = this.start;
for (int n = 0; n < this.count; ++n)
{
yield return values[index];
index = Increment( index, 1 );
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private int Increment( int index, int count )
{
return (index + count) % this.size;
}
}