Queue.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.Generic.Queue
  4. //
  5. // Author:
  6. // Martin Baulig ([email protected])
  7. //
  8. // (C) 2003 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>, IEnumerable<T>,
  40. ICollection, IEnumerable
  41. {
  42. int count;
  43. protected int modified;
  44. protected Node head;
  45. Node tail;
  46. public void Clear ()
  47. {
  48. head = tail = null;
  49. count = 0;
  50. modified++;
  51. }
  52. public void Enqueue (T item)
  53. {
  54. tail = new Node (tail, item);
  55. if (head == null)
  56. head = tail;
  57. count++;
  58. modified++;
  59. }
  60. public T Peek ()
  61. {
  62. if (head == null)
  63. throw new ArgumentException ();
  64. return head.Item;
  65. }
  66. public T Dequeue ()
  67. {
  68. if (head == null)
  69. throw new ArgumentException ();
  70. T retval = head.Item;
  71. head = head.Next;
  72. if (head == null)
  73. tail = null;
  74. count--;
  75. modified++;
  76. return retval;
  77. }
  78. public bool Contains (T item)
  79. {
  80. for (Node node = head; node != null; node = node.Next)
  81. if (node.Item == item)
  82. return true;
  83. return false;
  84. }
  85. public virtual void CopyTo (T[] array, int start)
  86. {
  87. // re-ordered to avoid possible integer overflow
  88. if (start >= array.Length - count)
  89. throw new ArgumentException ();
  90. for (Node node = head; node != null; node = node.Next)
  91. array [start++] = node.Item;
  92. }
  93. void ICollection.CopyTo (Array array, int start)
  94. {
  95. // re-ordered to avoid possible integer overflow
  96. if (start >= array.Length - count)
  97. throw new ArgumentException ();
  98. for (Node node = head; node != null; node = node.Next)
  99. array.SetValue (node.Item, start++);
  100. }
  101. public T[] ToArray ()
  102. {
  103. int pos = 0;
  104. T[] retval = new T [count];
  105. for (Node node = head; node != null; node = node.Next)
  106. retval [pos++] = node.Item;
  107. return retval;
  108. }
  109. public void TrimToSize ()
  110. { }
  111. public int Count {
  112. get { return count; }
  113. }
  114. public bool IsSynchronized {
  115. get { return false; }
  116. }
  117. public object SyncRoot {
  118. get { return this; }
  119. }
  120. public bool IsReadOnly {
  121. get { return false; }
  122. }
  123. public void Add (T item)
  124. {
  125. Enqueue (item);
  126. }
  127. public bool Remove (T item)
  128. {
  129. throw new NotImplementedException ();
  130. }
  131. public IEnumerator<T> GetEnumerator ()
  132. {
  133. return new Enumerator (this);
  134. }
  135. IEnumerator IEnumerable.GetEnumerator ()
  136. {
  137. return new Enumerator (this);
  138. }
  139. protected sealed class Node
  140. {
  141. public readonly T Item;
  142. public readonly Node Next;
  143. public Node (Node next, T item)
  144. {
  145. this.Next = next;
  146. this.Item = item;
  147. }
  148. }
  149. protected class Enumerator : IEnumerator<T>, IEnumerator
  150. {
  151. Queue<T> queue;
  152. int modified;
  153. Node current;
  154. public Enumerator (Queue<T> queue)
  155. {
  156. this.queue = queue;
  157. this.modified = queue.modified;
  158. this.current = queue.head;
  159. }
  160. public T Current {
  161. get {
  162. if (queue.modified != modified)
  163. throw new InvalidOperationException ();
  164. if (current == null)
  165. throw new ArgumentException ();
  166. return current.Item;
  167. }
  168. }
  169. object IEnumerator.Current {
  170. get {
  171. return Current;
  172. }
  173. }
  174. public bool MoveNext ()
  175. {
  176. if (queue.modified != modified)
  177. throw new InvalidOperationException ();
  178. if (current == null)
  179. throw new ArgumentException ();
  180. current = current.Next;
  181. return current != null;
  182. }
  183. public void Reset () {
  184. if (queue.modified != modified)
  185. throw new InvalidOperationException();
  186. current = queue.head;
  187. }
  188. public void Dispose ()
  189. {
  190. modified = -1;
  191. }
  192. }
  193. }
  194. }
  195. #endif