using System; /* Circular Buffer Generic Implementation * By KDERazorback (http://twitter.com/kderazorback) * * Fast and Lightweight, Generic Circular Buffer Implementation, supports for bidirectional rotations * Free to use or modify! Just keep me on the comments! */ /// /// Used to store a Circular Buffer of objects with a particular size, that rotates when an item is added to the collection. /// class CircularBuffer { /// /// Creates a new Circular Buffer with the specified Capacity /// /// Total elements that can be stored inside the buffer before it starts discarding items public CircularBuffer(int capacity) { plainBuffer = new T[capacity]; _startIndex = 0; } private T[] plainBuffer; private int _startIndex; // Stores the start of the Circular Buffer /// /// Stores the current Capacity of this Buffer /// public int Capacity { get { return plainBuffer.Length; } } /// /// Returns the item that is stored on the specified Index inside the Circular Buffer /// /// Index of the Item to be returned /// The object value stored on the specified index public T ElementAt(int index) { if ((index >= plainBuffer.Length) || (index < 0)) throw new IndexOutOfRangeException(); index += _startIndex; if (index >= plainBuffer.Length) index -= plainBuffer.Length; return plainBuffer[index]; } /// /// Returns an array with the full content of the actual Circular Buffer /// /// public T[] ToArray() { int i; T[] output = new T[plainBuffer.Length]; for (i = 0; i < plainBuffer.Length; i++) { output[i] = ElementAt(i); } return output; } /// /// Inserts a new item inside the Circular Buffer and rotates the entire structure by one step forwards /// /// New item to be inserted into the Circular Buffer public void Insert(T newItem) { if (_startIndex == 0) _startIndex = plainBuffer.Length - 1; else _startIndex--; plainBuffer[_startIndex] = newItem; } /// /// Inserts a new item inside the Circular Buffer and rotates the entire structure by one step backwards /// /// New item to be inserted into the Circular Buffer public void InsertBackwards(T newItem) { plainBuffer[_startIndex] = newItem; if (_startIndex == plainBuffer.Length - 1) _startIndex = 0; else _startIndex++; } }