Queue.cs 6.6 KB

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