Well I hope this class will helps You:
Internally the Circular FIFO Buffer use a Queue with the specified size.
Once the size of the buffer is reached, it will replaces older items with new ones.
NOTĂ: Nu puteți să ștergeți articole aleatoriu. Am setat metoda Remove (item T) pentru a reveni false.
dacă doriți Puteți modifica pentru a elimina articolele în mod aleatoriu
public class CircularFIFO : ICollection , IDisposable
{
public Queue CircularBuffer;
///
/// The default initial capacity.
///
private int capacity = 32;
///
/// Gets the actual capacity of the FIFO.
///
public int Capacity
{
get { return capacity; }
}
///
/// Initialize a new instance of FIFO class that is empty and has the default initial capacity.
///
public CircularFIFO()
{
CircularBuffer = new Queue();
}
///
/// Initialize a new instance of FIFO class that is empty and has the specified initial capacity.
///
/// Initial capacity of the FIFO.
public CircularFIFO(int size)
{
capacity = size;
CircularBuffer = new Queue(capacity);
}
///
/// Adds an item to the end of the FIFO.
///
/// The item to add to the end of the FIFO.
public void Add(T item)
{
if (this.Count >= this.Capacity)
Remove();
CircularBuffer.Enqueue(item);
}
///
/// Adds array of items to the end of the FIFO.
///
/// The array of items to add to the end of the FIFO.
public void Add(T[] item)
{
int enqueuedSize = 0;
int remainEnqueueSize = this.Capacity - this.Count;
for (; (enqueuedSize < item.Length && enqueuedSize < remainEnqueueSize); enqueuedSize++)
CircularBuffer.Enqueue(item[enqueuedSize]);
if ((item.Length - enqueuedSize) != 0)
{
Remove((item.Length - enqueuedSize));//remaining item size
for (; enqueuedSize < item.Length; enqueuedSize++)
CircularBuffer.Enqueue(item[enqueuedSize]);
}
}
///
/// Removes and Returns an item from the FIFO.
///
/// Item removed.
public T Remove()
{
T removedItem = CircularBuffer.Peek();
CircularBuffer.Dequeue();
return removedItem;
}
///
/// Removes and Returns the array of items form the FIFO.
///
/// The size of item to be removed from the FIFO.
/// Removed array of items
public T[] Remove(int size)
{
if (size > CircularBuffer.Count)
size = CircularBuffer.Count;
T[] removedItems = new T[size];
for (int i = 0; i < size; i++)
{
removedItems[i] = CircularBuffer.Peek();
CircularBuffer.Dequeue();
}
return removedItems;
}
///
/// Returns the item at the beginning of the FIFO with out removing it.
///
/// Item Peeked.
public T Peek()
{
return CircularBuffer.Peek();
}
///
/// Returns the array of item at the beginning of the FIFO with out removing it.
///
/// The size of the array items.
/// Array of peeked items.
public T[] Peek(int size)
{
T[] arrayItems = new T[CircularBuffer.Count];
CircularBuffer.CopyTo(arrayItems, 0);
if (size > CircularBuffer.Count)
size = CircularBuffer.Count;
T[] peekedItems = new T[size];
Array.Copy(arrayItems, 0, peekedItems, 0, size);
return peekedItems;
}
///
/// Gets the actual number of items presented in the FIFO.
///
public int Count
{
get
{
return CircularBuffer.Count;
}
}
///
/// Removes all the contents of the FIFO.
///
public void Clear()
{
CircularBuffer.Clear();
}
///
/// Resets and Initialize the instance of FIFO class that is empty and has the default initial capacity.
///
public void Reset()
{
Dispose();
CircularBuffer = new Queue(capacity);
}
#region ICollection Members
///
/// Determines whether an element is in the FIFO.
///
/// The item to locate in the FIFO.
///
public bool Contains(T item)
{
return CircularBuffer.Contains(item);
}
///
/// Copies the FIFO elements to an existing one-dimensional array.
///
/// The one-dimensional array that have at list a size of the FIFO
///
public void CopyTo(T[] array, int arrayIndex)
{
if (array.Length >= CircularBuffer.Count)
CircularBuffer.CopyTo(array, 0);
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
return false;
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return CircularBuffer.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return CircularBuffer.GetEnumerator();
}
#endregion
#region IDisposable Members
///
/// Releases all the resource used by the FIFO.
///
public void Dispose()
{
CircularBuffer.Clear();
CircularBuffer = null;
GC.Collect();
}
#endregion
}