CircularBuffer`1.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace SRDebugger
  5. {
  6. public class CircularBuffer<T> : IEnumerable<T>, IReadOnlyList<T>, IEnumerable
  7. {
  8. public CircularBuffer(int capacity) : this(capacity, new T[0])
  9. {
  10. }
  11. public CircularBuffer(int capacity, T[] items)
  12. {
  13. if (capacity < 1)
  14. {
  15. throw new ArgumentException("Circular buffer cannot have negative or zero capacity.", "capacity");
  16. }
  17. if (items == null)
  18. {
  19. throw new ArgumentNullException("items");
  20. }
  21. if (items.Length > capacity)
  22. {
  23. throw new ArgumentException("Too many items to fit circular buffer", "items");
  24. }
  25. this._buffer = new T[capacity];
  26. Array.Copy(items, this._buffer, items.Length);
  27. this._count = items.Length;
  28. this._start = 0;
  29. this._end = ((this._count != capacity) ? this._count : 0);
  30. }
  31. public int Capacity
  32. {
  33. get
  34. {
  35. return this._buffer.Length;
  36. }
  37. }
  38. public bool IsFull
  39. {
  40. get
  41. {
  42. return this.Count == this.Capacity;
  43. }
  44. }
  45. public bool IsEmpty
  46. {
  47. get
  48. {
  49. return this.Count == 0;
  50. }
  51. }
  52. public int Count
  53. {
  54. get
  55. {
  56. return this._count;
  57. }
  58. }
  59. public T this[int index]
  60. {
  61. get
  62. {
  63. if (this.IsEmpty)
  64. {
  65. throw new IndexOutOfRangeException(string.Format("Cannot access index {0}. Buffer is empty", index));
  66. }
  67. if (index >= this._count)
  68. {
  69. throw new IndexOutOfRangeException(string.Format("Cannot access index {0}. Buffer size is {1}", index, this._count));
  70. }
  71. int num = this.InternalIndex(index);
  72. return this._buffer[num];
  73. }
  74. set
  75. {
  76. if (this.IsEmpty)
  77. {
  78. throw new IndexOutOfRangeException(string.Format("Cannot access index {0}. Buffer is empty", index));
  79. }
  80. if (index >= this._count)
  81. {
  82. throw new IndexOutOfRangeException(string.Format("Cannot access index {0}. Buffer size is {1}", index, this._count));
  83. }
  84. int num = this.InternalIndex(index);
  85. this._buffer[num] = value;
  86. }
  87. }
  88. public IEnumerator<T> GetEnumerator()
  89. {
  90. ArraySegment<T>[] segments = new ArraySegment<T>[]
  91. {
  92. this.ArrayOne(),
  93. this.ArrayTwo()
  94. };
  95. foreach (ArraySegment<T> segment in segments)
  96. {
  97. for (int i = 0; i < segment.Count; i++)
  98. {
  99. yield return segment.Array[segment.Offset + i];
  100. }
  101. }
  102. yield break;
  103. }
  104. IEnumerator IEnumerable.GetEnumerator()
  105. {
  106. return this.GetEnumerator();
  107. }
  108. public T Front()
  109. {
  110. this.ThrowIfEmpty("Cannot access an empty buffer.");
  111. return this._buffer[this._start];
  112. }
  113. public T Back()
  114. {
  115. this.ThrowIfEmpty("Cannot access an empty buffer.");
  116. return this._buffer[((this._end == 0) ? this._count : this._end) - 1];
  117. }
  118. public void PushBack(T item)
  119. {
  120. if (this.IsFull)
  121. {
  122. this._buffer[this._end] = item;
  123. this.Increment(ref this._end);
  124. this._start = this._end;
  125. }
  126. else
  127. {
  128. this._buffer[this._end] = item;
  129. this.Increment(ref this._end);
  130. this._count++;
  131. }
  132. }
  133. public void PushFront(T item)
  134. {
  135. if (this.IsFull)
  136. {
  137. this.Decrement(ref this._start);
  138. this._end = this._start;
  139. this._buffer[this._start] = item;
  140. }
  141. else
  142. {
  143. this.Decrement(ref this._start);
  144. this._buffer[this._start] = item;
  145. this._count++;
  146. }
  147. }
  148. public void PopBack()
  149. {
  150. this.ThrowIfEmpty("Cannot take elements from an empty buffer.");
  151. this.Decrement(ref this._end);
  152. this._buffer[this._end] = default(T);
  153. this._count--;
  154. }
  155. public void PopFront()
  156. {
  157. this.ThrowIfEmpty("Cannot take elements from an empty buffer.");
  158. this._buffer[this._start] = default(T);
  159. this.Increment(ref this._start);
  160. this._count--;
  161. }
  162. public T[] ToArray()
  163. {
  164. T[] array = new T[this.Count];
  165. int num = 0;
  166. ArraySegment<T>[] array2 = new ArraySegment<T>[]
  167. {
  168. this.ArrayOne(),
  169. this.ArrayTwo()
  170. };
  171. foreach (ArraySegment<T> arraySegment in array2)
  172. {
  173. Array.Copy(arraySegment.Array, arraySegment.Offset, array, num, arraySegment.Count);
  174. num += arraySegment.Count;
  175. }
  176. return array;
  177. }
  178. private void ThrowIfEmpty(string message = "Cannot access an empty buffer.")
  179. {
  180. if (this.IsEmpty)
  181. {
  182. throw new InvalidOperationException(message);
  183. }
  184. }
  185. private void Increment(ref int index)
  186. {
  187. if (++index == this.Capacity)
  188. {
  189. index = 0;
  190. }
  191. }
  192. private void Decrement(ref int index)
  193. {
  194. if (index == 0)
  195. {
  196. index = this.Capacity;
  197. }
  198. index--;
  199. }
  200. private int InternalIndex(int index)
  201. {
  202. return this._start + ((index >= this.Capacity - this._start) ? (index - this.Capacity) : index);
  203. }
  204. private ArraySegment<T> ArrayOne()
  205. {
  206. if (this._start < this._end)
  207. {
  208. return new ArraySegment<T>(this._buffer, this._start, this._end - this._start);
  209. }
  210. return new ArraySegment<T>(this._buffer, this._start, this._buffer.Length - this._start);
  211. }
  212. private ArraySegment<T> ArrayTwo()
  213. {
  214. if (this._start < this._end)
  215. {
  216. return new ArraySegment<T>(this._buffer, this._end, 0);
  217. }
  218. return new ArraySegment<T>(this._buffer, 0, this._end);
  219. }
  220. private readonly T[] _buffer;
  221. private int _end;
  222. private int _count;
  223. private int _start;
  224. }
  225. }