Queue.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 == contents.Length)
  131. grow ();
  132. contents[(head + count) % contents.Length] = 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. head = 0;
  164. }
  165. // private classes
  166. private class SyncQueue : Queue {
  167. Queue queue;
  168. internal SyncQueue (Queue queue) {
  169. this.queue = queue;
  170. }
  171. public override int Count {
  172. get {
  173. lock (queue) {
  174. return queue.count;
  175. }
  176. }
  177. }
  178. public override bool IsSynchronized {
  179. get {
  180. lock (queue) {
  181. return queue.IsSynchronized;
  182. }
  183. }
  184. }
  185. public override object SyncRoot {
  186. get {
  187. return queue.SyncRoot;
  188. }
  189. }
  190. public override void CopyTo (Array array, int index) {
  191. lock (queue) {
  192. queue.CopyTo (array, index);
  193. }
  194. }
  195. public override IEnumerator GetEnumerator () {
  196. lock (queue) {
  197. return queue.GetEnumerator ();
  198. }
  199. }
  200. public override object Clone () {
  201. lock (queue) {
  202. return queue.Clone ();
  203. }
  204. }
  205. /*
  206. public override bool IsReadOnly {
  207. get {
  208. lock (queue) {
  209. return queue.IsReadOnly;
  210. }
  211. }
  212. }
  213. */
  214. public override void Clear () {
  215. lock (queue) {
  216. queue.Clear ();
  217. }
  218. }
  219. public override bool Contains (object obj) {
  220. lock (queue) {
  221. return queue.Contains (obj);
  222. }
  223. }
  224. public override object Dequeue () {
  225. lock (queue) {
  226. return queue.Dequeue ();
  227. }
  228. }
  229. public override void Enqueue (object obj) {
  230. lock (queue) {
  231. queue.Enqueue (obj);
  232. }
  233. }
  234. public override object Peek () {
  235. lock (queue) {
  236. return queue.Peek ();
  237. }
  238. }
  239. public override object[] ToArray () {
  240. lock (queue) {
  241. return queue.ToArray ();
  242. }
  243. }
  244. }
  245. [Serializable]
  246. private class QueueEnumerator : IEnumerator {
  247. Queue queue;
  248. private int modCount;
  249. private int current;
  250. internal QueueEnumerator (Queue q) {
  251. queue = q;
  252. modCount = q.modCount;
  253. current = -1; // one element before the head
  254. }
  255. public virtual object Current {
  256. get {
  257. if (modCount != queue.modCount
  258. || current < 0
  259. || current >= queue.count)
  260. throw new InvalidOperationException ();
  261. return queue.contents[(queue.head + current) % queue.contents.Length];
  262. }
  263. }
  264. public virtual bool MoveNext () {
  265. if (modCount != queue.modCount) {
  266. throw new InvalidOperationException ();
  267. }
  268. if (current >= queue.count - 1) {
  269. return false;
  270. } else {
  271. current++;
  272. return true;
  273. }
  274. }
  275. public virtual void Reset () {
  276. if (modCount != queue.modCount) {
  277. throw new InvalidOperationException();
  278. }
  279. current = -1;
  280. }
  281. }
  282. }
  283. }