Queue.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // System.Collections.Queue
  3. //
  4. // Author:
  5. // Ricardo Fernández Pascual
  6. //
  7. // (C) 2001 Ricardo Fernández Pascual
  8. //
  9. using System;
  10. using System.Collections;
  11. namespace System.Collections {
  12. public class Queue : ICollection, IEnumerable, ICloneable {
  13. private object[] contents;
  14. private int head = 0; // points to the first used slot
  15. private int count = 0;
  16. private int capacity = 16;
  17. private float growFactor = 2.0F;
  18. private int modCount = 0;
  19. public Queue () {
  20. contents = new object[capacity];
  21. }
  22. public Queue (ICollection collection) {
  23. capacity = collection.Count;
  24. contents = new object[capacity];
  25. count = capacity;
  26. collection.CopyTo (contents, 0);
  27. }
  28. public Queue (int initialCapacity) {
  29. capacity = initialCapacity;
  30. contents = new object[capacity];
  31. }
  32. public Queue (int initialCapacity, float growFactor) {
  33. capacity = initialCapacity;
  34. contents = new object[capacity];
  35. // LAMESPEC: The spec says nothing, but I think this
  36. // should throw an exception if growFactor <= 1.0
  37. this.growFactor = growFactor;
  38. }
  39. // from ICollection
  40. public virtual int Count {
  41. get { return count; }
  42. }
  43. public virtual bool IsSynchronized {
  44. get { return false; }
  45. }
  46. public virtual object SyncRoot {
  47. get { return this; }
  48. }
  49. public virtual void CopyTo (Array array, int index) {
  50. if (array == null) {
  51. throw new ArgumentNullException ();
  52. }
  53. if (index < 0) {
  54. throw new ArgumentOutOfRangeException ();
  55. }
  56. if (array.Rank > 1
  57. || index >= array.Length
  58. || count > array.Length - index) {
  59. throw new ArgumentException ();
  60. }
  61. // copy the contents of the circular array
  62. Array.Copy (contents, head, array, index,
  63. Math.Max (count, capacity - head));
  64. if (count > capacity - head)
  65. Array.Copy (contents, 0, array,
  66. index + capacity - head,
  67. count - (capacity - head));
  68. }
  69. // from IEnumerable
  70. public virtual IEnumerator GetEnumerator () {
  71. return new QueueEnumerator (this);
  72. }
  73. // from ICloneable
  74. public virtual object Clone () {
  75. Queue newQueue;
  76. newQueue = new Queue (); // FIXME: improve this...
  77. newQueue.contents = (object[]) this.contents.clone ();
  78. newQueue.head = this.head;
  79. newQueue.count = this.count;
  80. newQueue.capacity = this.capacity;
  81. newQueue.growFactor = this.growFactor;
  82. return newQueue;
  83. }
  84. // FIXME: should override Equals?
  85. // from Queue spec
  86. public virtual bool IsReadOnly {
  87. get { return false; }
  88. }
  89. public virtual void Clear () {
  90. modCount++;
  91. head = 0;
  92. count = 0;
  93. // FIXME: Should allocate a new contents array?
  94. // Should null the current array?
  95. }
  96. public virtual bool Contains (object obj) {
  97. int tail = head + count;
  98. if (obj == null) {
  99. for (int i = head; i < tail; i++) {
  100. if (contents[i % capacity] == null)
  101. return true;
  102. }
  103. } else {
  104. for (int i = head; i < tail; i++) {
  105. if (obj.Equals (contents[i % capacity]))
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. public virtual object Dequeue ()
  112. {
  113. modCount++;
  114. if (count < 1)
  115. throw new InvalidOperationException ();
  116. object result = contents[head];
  117. head = (head + 1) % capacity;
  118. count--;
  119. return result;
  120. }
  121. public virtual void Enqueue (object obj) {
  122. modCount++;
  123. if (count == capacity)
  124. grow ();
  125. contents[(head + count) % capacity] = obj;
  126. count++;
  127. }
  128. public virtual object Peek () {
  129. if (count < 1)
  130. throw new InvalidOperationException ();
  131. return contents[head];
  132. }
  133. public static Queue Synchronized (Queue queue) {
  134. if (queue == null) {
  135. throw new ArgumentNullException ();
  136. }
  137. return new SyncQueue (queue);
  138. }
  139. public virtual object[] ToArray () {
  140. object[] ret = new object[count];
  141. CopyTo (ret, 0);
  142. return ret;
  143. }
  144. // private methods
  145. private void grow () {
  146. int newCapacity = capacity * 2;
  147. int newContents = new object[newCapacity];
  148. CopyTo (newContents, 0);
  149. contents = newContents;
  150. head = 0;
  151. }
  152. // private classes
  153. private class SyncQueue : Queue {
  154. Queue queue;
  155. internal SyncQueue (Queue queue) {
  156. this.queue = queue;
  157. }
  158. public override int Count {
  159. get {
  160. lock (queue) {
  161. return queue.count;
  162. }
  163. }
  164. }
  165. public override bool IsSynchronized {
  166. get {
  167. lock (queue) {
  168. return queue.IsSynchronized;
  169. }
  170. }
  171. }
  172. public override object SyncRoot {
  173. get {
  174. return queue.SyncRoot;
  175. }
  176. }
  177. public override void CopyTo (Array array, int index) {
  178. lock (queue) {
  179. queue.CopyTo (array, index);
  180. }
  181. }
  182. public override IEnumerator GetEnumerator () {
  183. lock (queue) {
  184. return queue.GetEnumerator ();
  185. }
  186. }
  187. public override object Clone () {
  188. lock (queue) {
  189. return queue.Clone ();
  190. }
  191. }
  192. public override bool IsReadOnly {
  193. get {
  194. lock (queue) {
  195. return queue.IsReadOnly;
  196. }
  197. }
  198. }
  199. public override void Clear () {
  200. lock (queue) {
  201. queue.Clear ();
  202. }
  203. }
  204. public override bool Contains (object obj) {
  205. lock (queue) {
  206. return queue.Contains (obj);
  207. }
  208. }
  209. public override object Dequeue () {
  210. lock (queue) {
  211. return queue.Dequeue ();
  212. }
  213. }
  214. public override void Enqueue (object obj) {
  215. lock (queue) {
  216. queue.Enqueue (obj);
  217. }
  218. }
  219. public override object Peek () {
  220. lock (queue) {
  221. return queue.Peek ();
  222. }
  223. }
  224. public override object[] ToArray () {
  225. lock (queue) {
  226. return queue.ToArray ();
  227. }
  228. }
  229. }
  230. private class QueueEnumerator : IEnumerator {
  231. Queue queue;
  232. private int modCount;
  233. private int current;
  234. internal QueueEnumerator (Queue q) {
  235. queue = q;
  236. modCount = q.modCount;
  237. current = -1; // one element before the head
  238. }
  239. public virtual object Current {
  240. get {
  241. if (modCount != queue.modCount
  242. || current < 0
  243. || current >= queue.count)
  244. throw new InvalidOperationException ();
  245. return queue.contents[(queue.head + current) % queue.capacity];
  246. }
  247. }
  248. public virtual bool MoveNext () {
  249. if (modCount != queue.modCount) {
  250. throw new InvalidOperationException ();
  251. }
  252. if (current >= queue.count) {
  253. return false;
  254. } else {
  255. current++;
  256. return true;
  257. }
  258. }
  259. public virtual void Reset () {
  260. if (modCount != queue.modCount) {
  261. throw new InvalidOperationException();
  262. }
  263. current = -1;
  264. }
  265. }
  266. }
  267. }