Queue.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // System.Collections.Generic.Queue
  3. //
  4. // Author:
  5. // Martin Baulig ([email protected])
  6. // Ben Maurer ([email protected])
  7. //
  8. // (C) 2003, 2004 Novell, Inc.
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. #if NET_2_0
  33. using System;
  34. using System.Runtime.InteropServices;
  35. namespace System.Collections.Generic
  36. {
  37. [ComVisible(false)]
  38. public class Queue<T> : IEnumerable <T>, ICollection, IEnumerable
  39. {
  40. T [] data;
  41. int head;
  42. int tail;
  43. int size;
  44. int version;
  45. public Queue ()
  46. {
  47. }
  48. public Queue (int count)
  49. {
  50. if (count < 0)
  51. throw new ArgumentOutOfRangeException ("count");
  52. data = new T [count];
  53. }
  54. public Queue (IEnumerable <T> collection)
  55. {
  56. if (collection == null)
  57. throw new ArgumentNullException ("collection");
  58. foreach (T t in collection)
  59. Enqueue (t);
  60. }
  61. public void Clear ()
  62. {
  63. if (data != null)
  64. Array.Clear (data, 0, data.Length);
  65. head = tail = size = 0;
  66. }
  67. public bool Contains (T item)
  68. {
  69. if (item == null) {
  70. foreach (T t in this)
  71. if (t == null)
  72. return true;
  73. } else {
  74. foreach (T t in this)
  75. if (item.Equals (t))
  76. return true;
  77. }
  78. return false;
  79. }
  80. public void CopyTo (T [] array, int idx)
  81. {
  82. if (array == null)
  83. throw new ArgumentNullException ();
  84. if ((uint) idx > (uint) array.Length)
  85. throw new ArgumentOutOfRangeException ();
  86. if (array.Length - idx < size)
  87. throw new ArgumentOutOfRangeException ();
  88. if (size == 0)
  89. return;
  90. int contents_length = data.Length;
  91. int length_from_head = contents_length - head;
  92. Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
  93. if (size > length_from_head)
  94. Array.Copy (data, 0, array,
  95. idx + length_from_head,
  96. size - length_from_head);
  97. }
  98. void ICollection.CopyTo (Array array, int idx)
  99. {
  100. if (array == null)
  101. throw new ArgumentNullException ();
  102. if ((uint) idx < (uint) array.Length)
  103. throw new ArgumentOutOfRangeException ();
  104. if (array.Length - idx < size)
  105. throw new ArgumentOutOfRangeException ();
  106. if (size == 0)
  107. return;
  108. try {
  109. int contents_length = data.Length;
  110. int length_from_head = contents_length - head;
  111. Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
  112. if (size > length_from_head)
  113. Array.Copy (data, 0, array,
  114. idx + length_from_head,
  115. size - length_from_head);
  116. } catch (ArrayTypeMismatchException) {
  117. throw new ArgumentException ();
  118. }
  119. }
  120. public T Dequeue ()
  121. {
  122. T ret = Peek ();
  123. // clear stuff out to make the GC happy
  124. data [head] = default (T);
  125. if (++head == data.Length)
  126. head = 0;
  127. size --;
  128. version ++;
  129. return ret;
  130. }
  131. public T Peek ()
  132. {
  133. if (size == 0)
  134. throw new InvalidOperationException ();
  135. return data [head];
  136. }
  137. public void Enqueue (T item)
  138. {
  139. if (data == null || size == data.Length)
  140. SetCapacity (Math.Max (size * 2, 4));
  141. data [tail] = item;
  142. if (++tail == data.Length)
  143. tail = 0;
  144. size ++;
  145. version ++;
  146. }
  147. public T [] ToArray ()
  148. {
  149. T [] t = new T [size];
  150. CopyTo (t, 0);
  151. return t;
  152. }
  153. public void TrimToSize ()
  154. {
  155. SetCapacity (size);
  156. }
  157. void SetCapacity (int new_size)
  158. {
  159. if (data != null && new_size == data.Length)
  160. return;
  161. if (new_size < size)
  162. throw new InvalidOperationException ("shouldnt happen");
  163. T [] new_data = new T [new_size];
  164. if (size > 0)
  165. CopyTo (new_data, 0);
  166. data = new_data;
  167. tail = size;
  168. head = 0;
  169. version ++;
  170. }
  171. public int Count {
  172. get { return size; }
  173. }
  174. bool ICollection.IsSynchronized {
  175. get { return false; }
  176. }
  177. object ICollection.SyncRoot {
  178. get { return this; }
  179. }
  180. public Enumerator GetEnumerator ()
  181. {
  182. return new Enumerator (this);
  183. }
  184. IEnumerator <T> IEnumerable<T>.GetEnumerator ()
  185. {
  186. return GetEnumerator ();
  187. }
  188. IEnumerator IEnumerable.GetEnumerator ()
  189. {
  190. return GetEnumerator ();
  191. }
  192. public struct Enumerator : IEnumerator <T>, IEnumerator, IDisposable {
  193. const int NOT_STARTED = -2;
  194. // this MUST be -1, because we depend on it in move next.
  195. // we just decr the size, so, 0 - 1 == FINISHED
  196. const int FINISHED = -1;
  197. Queue <T> q;
  198. int idx;
  199. int ver;
  200. internal Enumerator (Queue <T> q)
  201. {
  202. this.q = q;
  203. idx = NOT_STARTED;
  204. ver = q.version;
  205. }
  206. // for some fucked up reason, MSFT added a useless dispose to this class
  207. // It means that in foreach, we must still do a try/finally. Broken, very
  208. // broken.
  209. public void Dispose ()
  210. {
  211. idx = NOT_STARTED;
  212. }
  213. public bool MoveNext ()
  214. {
  215. if (ver != q.version)
  216. throw new InvalidOperationException ();
  217. if (idx == NOT_STARTED)
  218. idx = q.size;
  219. return idx != FINISHED && -- idx != FINISHED;
  220. }
  221. public T Current {
  222. get {
  223. if (idx < 0)
  224. throw new InvalidOperationException ();
  225. return q.data [(q.size - 1 - idx + q.head) % q.data.Length];
  226. }
  227. }
  228. void IEnumerator.Reset ()
  229. {
  230. if (ver != q.version)
  231. throw new InvalidOperationException ();
  232. idx = NOT_STARTED;
  233. }
  234. object IEnumerator.Current {
  235. get { return Current; }
  236. }
  237. }
  238. }
  239. }
  240. #endif