Queue.cs 6.8 KB

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