CircularBuffer.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. Copyright (c) 2012, Alex Regueiro
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  5. following conditions are met:
  6. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
  8. in the documentation and/or other materials provided with the distribution.
  9. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  10. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  12. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  13. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  14. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  15. POSSIBILITY OF SUCH DAMAGE.
  16. */
  17. using System;
  18. using System.Collections;
  19. using System.Collections.Generic;
  20. using System.Threading;
  21. namespace OpenSim.Framework
  22. {
  23. public class CircularBuffer<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable
  24. {
  25. private int capacity;
  26. private int size;
  27. private int head;
  28. private int tail;
  29. private T[] buffer;
  30. [NonSerialized()]
  31. private object syncRoot;
  32. public CircularBuffer(int capacity)
  33. : this(capacity, false)
  34. {
  35. }
  36. public CircularBuffer(int capacity, bool allowOverflow)
  37. {
  38. if (capacity < 0)
  39. throw new ArgumentException("Needs to have at least 1","capacity");
  40. this.capacity = capacity;
  41. size = 0;
  42. head = 0;
  43. tail = 0;
  44. buffer = new T[capacity];
  45. AllowOverflow = allowOverflow;
  46. }
  47. public bool AllowOverflow
  48. {
  49. get;
  50. set;
  51. }
  52. public int Capacity
  53. {
  54. get { return capacity; }
  55. set
  56. {
  57. if (value == capacity)
  58. return;
  59. if (value < size)
  60. throw new ArgumentOutOfRangeException("value","Capacity is too small.");
  61. var dst = new T[value];
  62. if (size > 0)
  63. CopyTo(dst);
  64. buffer = dst;
  65. capacity = value;
  66. }
  67. }
  68. public int Size
  69. {
  70. get { return size; }
  71. }
  72. public bool Contains(T item)
  73. {
  74. int bufferIndex = head;
  75. var comparer = EqualityComparer<T>.Default;
  76. for (int i = 0; i < size; i++, bufferIndex++)
  77. {
  78. if (bufferIndex == capacity)
  79. bufferIndex = 0;
  80. if (item == null && buffer[bufferIndex] == null)
  81. return true;
  82. else if ((buffer[bufferIndex] != null) &&
  83. comparer.Equals(buffer[bufferIndex], item))
  84. return true;
  85. }
  86. return false;
  87. }
  88. public void Clear()
  89. {
  90. size = 0;
  91. head = 0;
  92. tail = 0;
  93. }
  94. public int Put(T[] src)
  95. {
  96. return Put(src, 0, src.Length);
  97. }
  98. public int Put(T[] src, int offset, int count)
  99. {
  100. if (!AllowOverflow && count > capacity - size)
  101. throw new InvalidOperationException("Buffer Overflow");
  102. int srcIndex = offset;
  103. for (int i = 0; i < count; i++, tail++, srcIndex++)
  104. {
  105. if (tail == capacity)
  106. tail = 0;
  107. buffer[tail] = src[srcIndex];
  108. }
  109. size = Math.Min(size + count, capacity);
  110. return count;
  111. }
  112. public void Put(T item)
  113. {
  114. if (!AllowOverflow && size == capacity)
  115. throw new InvalidOperationException("Buffer Overflow");
  116. buffer[tail] = item;
  117. if (++tail == capacity)
  118. tail = 0;
  119. size++;
  120. }
  121. public void Skip(int count)
  122. {
  123. head += count;
  124. if (head >= capacity)
  125. head -= capacity;
  126. }
  127. public T[] Get(int count)
  128. {
  129. var dst = new T[count];
  130. Get(dst);
  131. return dst;
  132. }
  133. public int Get(T[] dst)
  134. {
  135. return Get(dst, 0, dst.Length);
  136. }
  137. public int Get(T[] dst, int offset, int count)
  138. {
  139. int realCount = Math.Min(count, size);
  140. int dstIndex = offset;
  141. for (int i = 0; i < realCount; i++, head++, dstIndex++)
  142. {
  143. if (head == capacity)
  144. head = 0;
  145. dst[dstIndex] = buffer[head];
  146. }
  147. size -= realCount;
  148. return realCount;
  149. }
  150. public T Get()
  151. {
  152. if (size == 0)
  153. throw new InvalidOperationException("Buffer Empty");
  154. var item = buffer[head];
  155. if (++head == capacity)
  156. head = 0;
  157. size--;
  158. return item;
  159. }
  160. public void CopyTo(T[] array)
  161. {
  162. CopyTo(array, 0);
  163. }
  164. public void CopyTo(T[] array, int arrayIndex)
  165. {
  166. CopyTo(0, array, arrayIndex, size);
  167. }
  168. public void CopyTo(int index, T[] array, int arrayIndex, int count)
  169. {
  170. if (count > size)
  171. throw new ArgumentOutOfRangeException("count", "Count Too Large");
  172. int bufferIndex = head;
  173. for (int i = 0; i < count; i++, bufferIndex++, arrayIndex++)
  174. {
  175. if (bufferIndex == capacity)
  176. bufferIndex = 0;
  177. array[arrayIndex] = buffer[bufferIndex];
  178. }
  179. }
  180. public IEnumerator<T> GetEnumerator()
  181. {
  182. int bufferIndex = head;
  183. for (int i = 0; i < size; i++, bufferIndex++)
  184. {
  185. if (bufferIndex == capacity)
  186. bufferIndex = 0;
  187. yield return buffer[bufferIndex];
  188. }
  189. }
  190. public T[] GetBuffer()
  191. {
  192. return buffer;
  193. }
  194. public T[] ToArray()
  195. {
  196. var dst = new T[size];
  197. CopyTo(dst);
  198. return dst;
  199. }
  200. #region ICollection<T> Members
  201. int ICollection<T>.Count
  202. {
  203. get { return Size; }
  204. }
  205. bool ICollection<T>.IsReadOnly
  206. {
  207. get { return false; }
  208. }
  209. void ICollection<T>.Add(T item)
  210. {
  211. Put(item);
  212. }
  213. bool ICollection<T>.Remove(T item)
  214. {
  215. if (size == 0)
  216. return false;
  217. Get();
  218. return true;
  219. }
  220. #endregion
  221. #region IEnumerable<T> Members
  222. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  223. {
  224. return GetEnumerator();
  225. }
  226. #endregion
  227. #region ICollection Members
  228. int ICollection.Count
  229. {
  230. get { return Size; }
  231. }
  232. bool ICollection.IsSynchronized
  233. {
  234. get { return false; }
  235. }
  236. object ICollection.SyncRoot
  237. {
  238. get
  239. {
  240. if (syncRoot == null)
  241. Interlocked.CompareExchange(ref syncRoot, new object(), null);
  242. return syncRoot;
  243. }
  244. }
  245. void ICollection.CopyTo(Array array, int arrayIndex)
  246. {
  247. CopyTo((T[])array, arrayIndex);
  248. }
  249. #endregion
  250. #region IEnumerable Members
  251. IEnumerator IEnumerable.GetEnumerator()
  252. {
  253. return (IEnumerator)GetEnumerator();
  254. }
  255. #endregion
  256. }
  257. }