2
0

Queue.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. {
  52. if (array == null)
  53. throw new ArgumentNullException ("array");
  54. if (index < 0)
  55. throw new ArgumentOutOfRangeException ("index");
  56. if (array.Rank > 1
  57. || (index != 0 && index >= array.Length)
  58. || count > array.Length - index)
  59. throw new ArgumentException ();
  60. int contents_length = contents.Length;
  61. int length_from_head = contents_length - head;
  62. // copy the contents of the circular array
  63. Array.Copy (contents, head, array, index,
  64. Math.Min (count, length_from_head));
  65. if (count > length_from_head)
  66. Array.Copy (contents, 0, array,
  67. index + length_from_head,
  68. count - length_from_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. public virtual void TrimToSize() {
  150. object[] trimmed = new object [count];
  151. CopyTo (trimmed, 0);
  152. contents = trimmed;
  153. }
  154. // private methods
  155. private void grow () {
  156. int newCapacity = (int) Math.Ceiling
  157. (contents.Length * growFactor);
  158. object[] newContents = new object[newCapacity];
  159. CopyTo (newContents, 0);
  160. contents = newContents;
  161. capacity = newCapacity;
  162. head = 0;
  163. }
  164. // private classes
  165. private class SyncQueue : Queue {
  166. Queue queue;
  167. internal SyncQueue (Queue queue) {
  168. this.queue = queue;
  169. }
  170. public override int Count {
  171. get {
  172. lock (queue) {
  173. return queue.count;
  174. }
  175. }
  176. }
  177. public override bool IsSynchronized {
  178. get {
  179. return true;
  180. }
  181. }
  182. public override object SyncRoot {
  183. get {
  184. return queue.SyncRoot;
  185. }
  186. }
  187. public override void CopyTo (Array array, int index) {
  188. lock (queue) {
  189. queue.CopyTo (array, index);
  190. }
  191. }
  192. public override IEnumerator GetEnumerator () {
  193. lock (queue) {
  194. return queue.GetEnumerator ();
  195. }
  196. }
  197. public override object Clone () {
  198. lock (queue) {
  199. return queue.Clone ();
  200. }
  201. }
  202. /*
  203. public override bool IsReadOnly {
  204. get {
  205. lock (queue) {
  206. return queue.IsReadOnly;
  207. }
  208. }
  209. }
  210. */
  211. public override void Clear () {
  212. lock (queue) {
  213. queue.Clear ();
  214. }
  215. }
  216. public override bool Contains (object obj) {
  217. lock (queue) {
  218. return queue.Contains (obj);
  219. }
  220. }
  221. public override object Dequeue () {
  222. lock (queue) {
  223. return queue.Dequeue ();
  224. }
  225. }
  226. public override void Enqueue (object obj) {
  227. lock (queue) {
  228. queue.Enqueue (obj);
  229. }
  230. }
  231. public override object Peek () {
  232. lock (queue) {
  233. return queue.Peek ();
  234. }
  235. }
  236. public override object[] ToArray () {
  237. lock (queue) {
  238. return queue.ToArray ();
  239. }
  240. }
  241. }
  242. [Serializable]
  243. private class QueueEnumerator : IEnumerator, ICloneable {
  244. Queue queue;
  245. private int modCount;
  246. private int current;
  247. internal QueueEnumerator (Queue q) {
  248. queue = q;
  249. modCount = q.modCount;
  250. current = -1; // one element before the head
  251. }
  252. public object Clone ()
  253. {
  254. QueueEnumerator q = new QueueEnumerator (queue);
  255. q.modCount = modCount;
  256. q.current = current;
  257. return q;
  258. }
  259. public virtual object Current {
  260. get {
  261. if (modCount != queue.modCount
  262. || current < 0
  263. || current >= queue.count)
  264. throw new InvalidOperationException ();
  265. return queue.contents[(queue.head + current) % queue.contents.Length];
  266. }
  267. }
  268. public virtual bool MoveNext () {
  269. if (modCount != queue.modCount) {
  270. throw new InvalidOperationException ();
  271. }
  272. if (current >= queue.count - 1) {
  273. return false;
  274. } else {
  275. current++;
  276. return true;
  277. }
  278. }
  279. public virtual void Reset () {
  280. if (modCount != queue.modCount) {
  281. throw new InvalidOperationException();
  282. }
  283. current = -1;
  284. }
  285. }
  286. }
  287. }