Queue.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. int contents_length = contents.Length;
  63. int length_from_head = contents_length - head;
  64. // copy the contents of the circular array
  65. Array.Copy (contents, head, array, index,
  66. Math.Min (count, length_from_head));
  67. if (count > length_from_head)
  68. Array.Copy (contents, 0, array,
  69. index + length_from_head,
  70. count - length_from_head);
  71. }
  72. // from IEnumerable
  73. public virtual IEnumerator GetEnumerator () {
  74. return new QueueEnumerator (this);
  75. }
  76. // from ICloneable
  77. public virtual object Clone () {
  78. Queue newQueue;
  79. newQueue = new Queue (); // FIXME: improve this...
  80. newQueue.contents = new object[this.contents.Length];
  81. Array.Copy (this.contents, 0, newQueue.contents, 0,
  82. this.contents.Length);
  83. newQueue.head = this.head;
  84. newQueue.count = this.count;
  85. newQueue.capacity = this.capacity;
  86. newQueue.growFactor = this.growFactor;
  87. return newQueue;
  88. }
  89. // FIXME: should override Equals?
  90. // from Queue spec
  91. /*
  92. public virtual bool IsReadOnly {
  93. get { return false; }
  94. }
  95. */
  96. public virtual void Clear () {
  97. modCount++;
  98. head = 0;
  99. count = 0;
  100. // FIXME: Should allocate a new contents array?
  101. // Should null the current array?
  102. }
  103. public virtual bool Contains (object obj) {
  104. int tail = head + count;
  105. if (obj == null) {
  106. for (int i = head; i < tail; i++) {
  107. if (contents[i % capacity] == null)
  108. return true;
  109. }
  110. } else {
  111. for (int i = head; i < tail; i++) {
  112. if (obj.Equals (contents[i % capacity]))
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. public virtual object Dequeue ()
  119. {
  120. modCount++;
  121. if (count < 1)
  122. throw new InvalidOperationException ();
  123. object result = contents[head];
  124. head = (head + 1) % capacity;
  125. count--;
  126. return result;
  127. }
  128. public virtual void Enqueue (object obj) {
  129. modCount++;
  130. if (count == capacity)
  131. grow ();
  132. contents[(head + count) % capacity] = obj;
  133. count++;
  134. }
  135. public virtual object Peek () {
  136. if (count < 1)
  137. throw new InvalidOperationException ();
  138. return contents[head];
  139. }
  140. public static Queue Synchronized (Queue queue) {
  141. if (queue == null) {
  142. throw new ArgumentNullException ();
  143. }
  144. return new SyncQueue (queue);
  145. }
  146. public virtual object[] ToArray () {
  147. object[] ret = new object[count];
  148. CopyTo (ret, 0);
  149. return ret;
  150. }
  151. public virtual void TrimToSize() {
  152. object[] trimmed = new object [count];
  153. CopyTo (trimmed, 0);
  154. contents = trimmed;
  155. }
  156. // private methods
  157. private void grow () {
  158. int newCapacity = (int) Math.Ceiling
  159. (contents.Length * growFactor);
  160. object[] newContents = new object[newCapacity];
  161. CopyTo (newContents, 0);
  162. contents = newContents;
  163. capacity = newCapacity;
  164. head = 0;
  165. }
  166. // private classes
  167. private class SyncQueue : Queue {
  168. Queue queue;
  169. internal SyncQueue (Queue queue) {
  170. this.queue = queue;
  171. }
  172. public override int Count {
  173. get {
  174. lock (queue) {
  175. return queue.count;
  176. }
  177. }
  178. }
  179. public override bool IsSynchronized {
  180. get {
  181. return true;
  182. }
  183. }
  184. public override object SyncRoot {
  185. get {
  186. return queue.SyncRoot;
  187. }
  188. }
  189. public override void CopyTo (Array array, int index) {
  190. lock (queue) {
  191. queue.CopyTo (array, index);
  192. }
  193. }
  194. public override IEnumerator GetEnumerator () {
  195. lock (queue) {
  196. return queue.GetEnumerator ();
  197. }
  198. }
  199. public override object Clone () {
  200. lock (queue) {
  201. return queue.Clone ();
  202. }
  203. }
  204. /*
  205. public override bool IsReadOnly {
  206. get {
  207. lock (queue) {
  208. return queue.IsReadOnly;
  209. }
  210. }
  211. }
  212. */
  213. public override void Clear () {
  214. lock (queue) {
  215. queue.Clear ();
  216. }
  217. }
  218. public override bool Contains (object obj) {
  219. lock (queue) {
  220. return queue.Contains (obj);
  221. }
  222. }
  223. public override object Dequeue () {
  224. lock (queue) {
  225. return queue.Dequeue ();
  226. }
  227. }
  228. public override void Enqueue (object obj) {
  229. lock (queue) {
  230. queue.Enqueue (obj);
  231. }
  232. }
  233. public override object Peek () {
  234. lock (queue) {
  235. return queue.Peek ();
  236. }
  237. }
  238. public override object[] ToArray () {
  239. lock (queue) {
  240. return queue.ToArray ();
  241. }
  242. }
  243. }
  244. [Serializable]
  245. private class QueueEnumerator : IEnumerator, ICloneable {
  246. Queue queue;
  247. private int modCount;
  248. private int current;
  249. internal QueueEnumerator (Queue q) {
  250. queue = q;
  251. modCount = q.modCount;
  252. current = -1; // one element before the head
  253. }
  254. public object Clone ()
  255. {
  256. QueueEnumerator q = new QueueEnumerator (queue);
  257. q.modCount = modCount;
  258. q.current = current;
  259. return q;
  260. }
  261. public virtual object Current {
  262. get {
  263. if (modCount != queue.modCount
  264. || current < 0
  265. || current >= queue.count)
  266. throw new InvalidOperationException ();
  267. return queue.contents[(queue.head + current) % queue.contents.Length];
  268. }
  269. }
  270. public virtual bool MoveNext () {
  271. if (modCount != queue.modCount) {
  272. throw new InvalidOperationException ();
  273. }
  274. if (current >= queue.count - 1) {
  275. return false;
  276. } else {
  277. current++;
  278. return true;
  279. }
  280. }
  281. public virtual void Reset () {
  282. if (modCount != queue.modCount) {
  283. throw new InvalidOperationException();
  284. }
  285. current = -1;
  286. }
  287. }
  288. }
  289. }