123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #region License
- #endregion
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace RingBuffer {
-
-
-
-
- public class RingBuffer<T> : IEnumerable<T>, IEnumerable, ICollection<T>,
- ICollection {
- protected int head = 0;
- protected int tail = 0;
- protected int size = 0;
- protected T[] buffer;
- private bool allowOverflow;
- public bool AllowOverflow { get { return allowOverflow; } }
-
-
-
- public int Capacity { get { return buffer.Length; } }
-
-
-
- public int Size { get { return size; } }
-
-
-
-
- public T Get() {
- if(size == 0) throw new System.InvalidOperationException("Buffer is empty.");
- T _item = buffer[head];
- head = (head + 1) % Capacity;
- size--;
- return _item;
- }
-
-
-
-
- public void Put(T item) {
-
-
- if(tail == head && size != 0) {
- if(allowOverflow) {
- addToBuffer(item, true);
- }
- else {
- throw new System.InvalidOperationException("The RingBuffer is full");
- }
- }
-
- else {
- addToBuffer(item, false);
- }
- }
- public void Put(T[] item)
- {
- foreach (var t in item)
- {
- Put(t);
- }
- }
- protected void addToBuffer(T toAdd, bool overflow) {
- if(overflow) {
- head = (head + 1) % Capacity;
- }
- else {
- size++;
- }
- buffer[tail] = toAdd;
- tail = (tail + 1) % Capacity;
- }
- #region Constructors
-
- public RingBuffer() : this(4) { }
- public RingBuffer(int capacity) : this(capacity, false) { }
- public RingBuffer(int capacity, bool overflow) {
- buffer = new T[capacity];
- allowOverflow = overflow;
- }
- #endregion
- #region IEnumerable Members
- public IEnumerator<T> GetEnumerator() {
- int _index = head;
- for(int i = 0; i < size; i++, _index = (_index + 1) % Capacity) {
- yield return buffer[_index];
- }
- }
- IEnumerator<T> IEnumerable<T>.GetEnumerator() {
- return GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator() {
- return (IEnumerator)GetEnumerator();
- }
- #endregion
- #region ICollection<T> Members
- public int Count { get { return size; } }
- public bool IsReadOnly { get { return false; } }
- public void Add(T item) {
- Put(item);
- }
-
-
-
-
-
-
-
-
- public bool Contains(T item) {
- EqualityComparer<T> comparer = EqualityComparer<T>.Default;
- int _index = head;
- for(int i = 0; i < size; i++, _index = (_index + 1) % Capacity) {
- if(comparer.Equals(item, buffer[_index])) return true;
- }
- return false;
- }
-
-
-
- public void Clear() {
- for(int i = 0; i < Capacity; i++) {
- buffer[i] = default(T);
- }
- head = 0;
- tail = 0;
- size = 0;
- }
-
-
-
-
-
-
-
- public void CopyTo(T[] array, int arrayIndex) {
- int _index = head;
- for(int i = 0; i < size; i++, arrayIndex++, _index = (_index + 1) %
- Capacity) {
- array[arrayIndex] = buffer[_index];
- }
- }
-
-
-
-
-
-
-
-
- public bool Remove(T item) {
- int _index = head;
- int _removeIndex = 0;
- bool _foundItem = false;
- EqualityComparer<T> _comparer = EqualityComparer<T>.Default;
- for(int i = 0; i < size; i++, _index = (_index + 1) % Capacity) {
- if(_comparer.Equals(item, buffer[_index])) {
- _removeIndex = _index;
- _foundItem = true;
- break;
- }
- }
- if(_foundItem) {
- T[] _newBuffer = new T[size - 1];
- _index = head;
- bool _pastItem = false;
- for(int i = 0; i < size - 1; i++, _index = (_index + 1) % Capacity) {
- if(_index == _removeIndex) {
- _pastItem = true;
- }
- if(_pastItem) {
- _newBuffer[_index] = buffer[(_index + 1) % Capacity];
- }
- else {
- _newBuffer[_index] = buffer[_index];
- }
- }
- size--;
- buffer = _newBuffer;
- return true;
- }
- return false;
- }
- #endregion
- #region ICollection Members
-
-
-
-
- public Object SyncRoot { get { return this; } }
-
-
-
-
- public bool IsSynchronized { get { return false; } }
-
-
-
-
-
-
-
-
-
- void ICollection.CopyTo(Array array, int index) {
- CopyTo((T[])array, index);
- }
- #endregion
- }
- }
|