2
0

Queue.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. [CLSCompliant(false)]
  38. [ComVisible(false)]
  39. public class Queue<T> : ICollection<T>, ICollection
  40. {
  41. T [] data;
  42. int head;
  43. int tail;
  44. int size;
  45. int version;
  46. public Queue ()
  47. {
  48. }
  49. public Queue (int count)
  50. {
  51. if (count < 0)
  52. throw new ArgumentOutOfRangeException ("count");
  53. data = new T [count];
  54. }
  55. public Queue (IEnumerable <T> collection)
  56. {
  57. if (collection == null)
  58. throw new ArgumentNullException ("collection");
  59. foreach (T t in collection)
  60. Enqueue (t);
  61. }
  62. public void Clear ()
  63. {
  64. if (data != null)
  65. Array.Clear (data, 0, data.Length);
  66. head = tail = size = 0;
  67. }
  68. public bool Contains (T item)
  69. {
  70. if (item == null) {
  71. foreach (T t in this)
  72. if (t == null)
  73. return true;
  74. } else {
  75. foreach (T t in this)
  76. if (item.Equals (t))
  77. return true;
  78. }
  79. return false;
  80. }
  81. public void CopyTo (T [] array, int idx)
  82. {
  83. if (array == null)
  84. throw new ArgumentNullException ();
  85. if ((uint) idx > (uint) array.Length)
  86. throw new ArgumentOutOfRangeException ();
  87. if (array.Length - idx < size)
  88. throw new ArgumentOutOfRangeException ();
  89. if (size == 0)
  90. return;
  91. int contents_length = data.Length;
  92. int length_from_head = contents_length - head;
  93. Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
  94. if (size > length_from_head)
  95. Array.Copy (data, 0, array,
  96. idx + length_from_head,
  97. size - length_from_head);
  98. }
  99. void ICollection.CopyTo (Array array, int idx)
  100. {
  101. if (array == null)
  102. throw new ArgumentNullException ();
  103. if ((uint) idx < (uint) array.Length)
  104. throw new ArgumentOutOfRangeException ();
  105. if (array.Length - idx < size)
  106. throw new ArgumentOutOfRangeException ();
  107. if (size == 0)
  108. return;
  109. try {
  110. int contents_length = data.Length;
  111. int length_from_head = contents_length - head;
  112. Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
  113. if (size > length_from_head)
  114. Array.Copy (data, 0, array,
  115. idx + length_from_head,
  116. size - length_from_head);
  117. } catch (ArrayTypeMismatchException) {
  118. throw new ArgumentException ();
  119. }
  120. }
  121. public T Dequeue ()
  122. {
  123. T ret = Peek ();
  124. // clear stuff out to make the GC happy
  125. data [head] = default (T);
  126. if (++head == data.Length)
  127. head = 0;
  128. size --;
  129. version ++;
  130. return ret;
  131. }
  132. public T Peek ()
  133. {
  134. if (size == 0)
  135. throw new InvalidOperationException ();
  136. return data [head];
  137. }
  138. public void Enqueue (T item)
  139. {
  140. if (data == null || size == data.Length)
  141. SetCapacity (Math.Max (size * 2, 4));
  142. data [tail] = item;
  143. if (++tail == data.Length)
  144. tail = 0;
  145. size ++;
  146. version ++;
  147. }
  148. public T [] ToArray ()
  149. {
  150. T [] t = new T [size];
  151. CopyTo (t, 0);
  152. return t;
  153. }
  154. public void TrimToSize ()
  155. {
  156. SetCapacity (size);
  157. }
  158. void SetCapacity (int new_size)
  159. {
  160. if (data != null && new_size == data.Length)
  161. return;
  162. if (new_size < size)
  163. throw new InvalidOperationException ("shouldnt happen");
  164. T [] new_data = new T [new_size];
  165. if (size > 0)
  166. CopyTo (new_data, 0);
  167. data = new_data;
  168. tail = size;
  169. head = 0;
  170. version ++;
  171. }
  172. public int Count {
  173. get { return size; }
  174. }
  175. bool ICollection <T>.IsReadOnly {
  176. get { return false; }
  177. }
  178. bool ICollection.IsSynchronized {
  179. get { return false; }
  180. }
  181. object ICollection.SyncRoot {
  182. get { return this; }
  183. }
  184. void ICollection <T>.Add (T t)
  185. {
  186. Enqueue (t);
  187. }
  188. bool ICollection <T>.Remove (T t)
  189. {
  190. throw new InvalidOperationException ("");
  191. }
  192. public Enumerator <T> GetEnumerator ()
  193. {
  194. return new Enumerator <T> (this);
  195. }
  196. IEnumerator <T> IEnumerable<T>.GetEnumerator ()
  197. {
  198. return GetEnumerator ();
  199. }
  200. IEnumerator IEnumerable.GetEnumerator ()
  201. {
  202. return GetEnumerator ();
  203. }
  204. public struct Enumerator <T> : IEnumerator <T>, IEnumerator, IDisposable {
  205. const int NOT_STARTED = -2;
  206. // this MUST be -1, because we depend on it in move next.
  207. // we just decr the size, so, 0 - 1 == FINISHED
  208. const int FINISHED = -1;
  209. Queue <T> q;
  210. int idx;
  211. int ver;
  212. internal Enumerator (Queue <T> q)
  213. {
  214. this.q = q;
  215. idx = NOT_STARTED;
  216. ver = q.version;
  217. }
  218. // for some fucked up reason, MSFT added a useless dispose to this class
  219. // It means that in foreach, we must still do a try/finally. Broken, very
  220. // broken.
  221. public void Dispose ()
  222. {
  223. idx = NOT_STARTED;
  224. }
  225. public bool MoveNext ()
  226. {
  227. if (ver != q.version)
  228. throw new InvalidOperationException ();
  229. if (idx == NOT_STARTED)
  230. idx = q.size;
  231. return idx != FINISHED && -- idx != FINISHED;
  232. }
  233. public T Current {
  234. get {
  235. if (idx < 0)
  236. throw new InvalidOperationException ();
  237. return q.data [(q.size - 1 - idx + q.head) % q.data.Length];
  238. }
  239. }
  240. void IEnumerator.Reset ()
  241. {
  242. if (ver != q.version)
  243. throw new InvalidOperationException ();
  244. idx = NOT_STARTED;
  245. }
  246. object IEnumerator.Current {
  247. get { return Current; }
  248. }
  249. }
  250. }
  251. }
  252. #endif