Queue.cs 6.5 KB

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