Queue.cs 7.1 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[] _array;
  15. private int _head = 0; // points to the first used slot
  16. private int _size = 0;
  17. private int _tail = 0;
  18. private int _growFactor;
  19. private int _version = 0;
  20. public Queue () : this (32, 2.0F) {}
  21. public Queue (int initialCapacity) : this (initialCapacity, 2.0F) {}
  22. public Queue(ICollection col) : this (col == null ? 32 : col.Count)
  23. {
  24. if (col == null)
  25. throw new ArgumentNullException ("col");
  26. _size = _array.Length;
  27. _tail = _size;
  28. col.CopyTo (_array, 0);
  29. }
  30. public Queue (int initialCapacity, float growFactor) {
  31. if (initialCapacity < 0)
  32. throw new ArgumentOutOfRangeException("capacity", "Needs a non-negative number");
  33. if (!(growFactor >= 1.0F && growFactor <= 10.0F))
  34. throw new ArgumentOutOfRangeException("growFactor", "Queue growth factor must be between 1.0 and 10.0, inclusive");
  35. _array = new object[initialCapacity];
  36. this._growFactor = (int)(growFactor * 100);
  37. }
  38. // from ICollection
  39. public virtual int Count {
  40. get { return _size; }
  41. }
  42. public virtual bool IsSynchronized {
  43. get { return false; }
  44. }
  45. public virtual object SyncRoot {
  46. get { return this; }
  47. }
  48. public virtual void CopyTo (Array array, int index)
  49. {
  50. if (array == null)
  51. throw new ArgumentNullException ("array");
  52. if (index < 0)
  53. throw new ArgumentOutOfRangeException ("index");
  54. if (array.Rank > 1
  55. || (index != 0 && index >= array.Length)
  56. || _size > array.Length - index)
  57. throw new ArgumentException ();
  58. int contents_length = _array.Length;
  59. int length_from_head = contents_length - _head;
  60. // copy the _array of the circular array
  61. Array.Copy (_array, _head, array, index,
  62. Math.Min (_size, length_from_head));
  63. if (_size > length_from_head)
  64. Array.Copy (_array, 0, array,
  65. index + length_from_head,
  66. _size - length_from_head);
  67. }
  68. // from IEnumerable
  69. public virtual IEnumerator GetEnumerator () {
  70. return new QueueEnumerator (this);
  71. }
  72. // from ICloneable
  73. public virtual object Clone () {
  74. Queue newQueue;
  75. newQueue = new Queue (this._array.Length);
  76. newQueue._growFactor = _growFactor;
  77. Array.Copy (this._array, 0, newQueue._array, 0,
  78. this._array.Length);
  79. newQueue._head = this._head;
  80. newQueue._size = this._size;
  81. newQueue._tail = this._tail;
  82. return newQueue;
  83. }
  84. public virtual void Clear () {
  85. _version++;
  86. _head = 0;
  87. _size = 0;
  88. _tail = 0;
  89. for (int length = _array.Length - 1; length >= 0; length--)
  90. _array [length] = null;
  91. }
  92. public virtual bool Contains (object obj) {
  93. int tail = _head + _size;
  94. if (obj == null) {
  95. for (int i = _head; i < tail; i++) {
  96. if (_array[i % _array.Length] == null)
  97. return true;
  98. }
  99. } else {
  100. for (int i = _head; i < tail; i++) {
  101. if (obj.Equals (_array[i % _array.Length]))
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public virtual object Dequeue ()
  108. {
  109. _version++;
  110. if (_size < 1)
  111. throw new InvalidOperationException ();
  112. object result = _array[_head];
  113. _array [_head] = null;
  114. _head = (_head + 1) % _array.Length;
  115. _size--;
  116. return result;
  117. }
  118. public virtual void Enqueue (object obj) {
  119. _version++;
  120. if (_size == _array.Length)
  121. grow ();
  122. _array[_tail] = obj;
  123. _tail = (_tail+1) % _array.Length;
  124. _size++;
  125. }
  126. public virtual object Peek () {
  127. if (_size < 1)
  128. throw new InvalidOperationException ();
  129. return _array[_head];
  130. }
  131. public static Queue Synchronized (Queue queue) {
  132. if (queue == null) {
  133. throw new ArgumentNullException ("queue");
  134. }
  135. return new SyncQueue (queue);
  136. }
  137. public virtual object[] ToArray () {
  138. object[] ret = new object[_size];
  139. CopyTo (ret, 0);
  140. return ret;
  141. }
  142. public virtual void TrimToSize() {
  143. _version++;
  144. object[] trimmed = new object [_size];
  145. CopyTo (trimmed, 0);
  146. _array = trimmed;
  147. _head = 0;
  148. _tail = _head + _size;
  149. }
  150. // private methods
  151. private void grow () {
  152. int newCapacity = (_array.Length * _growFactor) / 100;
  153. object[] newContents = new object[newCapacity];
  154. CopyTo (newContents, 0);
  155. _array = newContents;
  156. _head = 0;
  157. _tail = _head + _size;
  158. }
  159. // private classes
  160. private class SyncQueue : Queue {
  161. Queue queue;
  162. internal SyncQueue (Queue queue) {
  163. this.queue = queue;
  164. }
  165. public override int Count {
  166. get {
  167. lock (queue) {
  168. return queue._size;
  169. }
  170. }
  171. }
  172. public override bool IsSynchronized {
  173. get {
  174. return true;
  175. }
  176. }
  177. public override object SyncRoot {
  178. get {
  179. return queue.SyncRoot;
  180. }
  181. }
  182. public override void CopyTo (Array array, int index) {
  183. lock (queue) {
  184. queue.CopyTo (array, index);
  185. }
  186. }
  187. public override IEnumerator GetEnumerator () {
  188. lock (queue) {
  189. return queue.GetEnumerator ();
  190. }
  191. }
  192. public override object Clone () {
  193. lock (queue) {
  194. return new SyncQueue((Queue) queue.Clone ());
  195. }
  196. }
  197. /*
  198. public override bool IsReadOnly {
  199. get {
  200. lock (queue) {
  201. return queue.IsReadOnly;
  202. }
  203. }
  204. }
  205. */
  206. public override void Clear () {
  207. lock (queue) {
  208. queue.Clear ();
  209. }
  210. }
  211. public override void TrimToSize () {
  212. lock (queue) {
  213. queue.TrimToSize ();
  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 _version;
  246. private int current;
  247. internal QueueEnumerator (Queue q) {
  248. queue = q;
  249. _version = q._version;
  250. current = -1; // one element before the _head
  251. }
  252. public object Clone ()
  253. {
  254. QueueEnumerator q = new QueueEnumerator (queue);
  255. q._version = _version;
  256. q.current = current;
  257. return q;
  258. }
  259. public virtual object Current {
  260. get {
  261. if (_version != queue._version
  262. || current < 0
  263. || current >= queue._size)
  264. throw new InvalidOperationException ();
  265. return queue._array[(queue._head + current) % queue._array.Length];
  266. }
  267. }
  268. public virtual bool MoveNext () {
  269. if (_version != queue._version) {
  270. throw new InvalidOperationException ();
  271. }
  272. if (current >= queue._size - 1) {
  273. current = -1; // to late!
  274. return false;
  275. } else {
  276. current++;
  277. return true;
  278. }
  279. }
  280. public virtual void Reset () {
  281. if (_version != queue._version) {
  282. throw new InvalidOperationException();
  283. }
  284. current = -1;
  285. }
  286. }
  287. }
  288. }