Queue.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 IEnumerator<T> GetEnumerator ()
  121. {
  122. return new Enumerator (this);
  123. }
  124. IEnumerator IEnumerable.GetEnumerator ()
  125. {
  126. return new Enumerator (this);
  127. }
  128. protected sealed class Node
  129. {
  130. public readonly T Item;
  131. public readonly Node Next;
  132. public Node (Node next, T item)
  133. {
  134. this.Next = next;
  135. this.Item = item;
  136. }
  137. }
  138. protected class Enumerator : IEnumerator<T>, IEnumerator
  139. {
  140. Queue<T> queue;
  141. int modified;
  142. Node current;
  143. public Enumerator (Queue<T> queue)
  144. {
  145. this.queue = queue;
  146. this.modified = queue.modified;
  147. this.current = queue.head;
  148. }
  149. public T Current {
  150. get {
  151. if (queue.modified != modified)
  152. throw new InvalidOperationException ();
  153. if (current == null)
  154. throw new ArgumentException ();
  155. return current.Item;
  156. }
  157. }
  158. object IEnumerator.Current {
  159. get {
  160. return Current;
  161. }
  162. }
  163. public bool MoveNext ()
  164. {
  165. if (queue.modified != modified)
  166. throw new InvalidOperationException ();
  167. if (current == null)
  168. throw new ArgumentException ();
  169. current = current.Next;
  170. return current != null;
  171. }
  172. public void Reset () {
  173. if (queue.modified != modified)
  174. throw new InvalidOperationException();
  175. current = queue.head;
  176. }
  177. public void Dispose ()
  178. {
  179. modified = -1;
  180. }
  181. }
  182. }
  183. }
  184. #endif