Queue.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // System.Collections.Queue
  3. //
  4. // Author:
  5. // Ricardo Fernández Pascual
  6. //
  7. // (C) 2001 Ricardo Fernández Pascual
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. namespace System.Collections {
  34. [Serializable]
  35. public class Queue : ICollection, IEnumerable, ICloneable {
  36. private object[] _array;
  37. private int _head = 0; // points to the first used slot
  38. private int _size = 0;
  39. private int _tail = 0;
  40. private int _growFactor;
  41. private int _version = 0;
  42. public Queue () : this (32, 2.0F) {}
  43. public Queue (int initialCapacity) : this (initialCapacity, 2.0F) {}
  44. public Queue(ICollection col) : this (col == null ? 32 : col.Count)
  45. {
  46. if (col == null)
  47. throw new ArgumentNullException ("col");
  48. _size = _array.Length;
  49. _tail = _size;
  50. col.CopyTo (_array, 0);
  51. }
  52. public Queue (int initialCapacity, float growFactor) {
  53. if (initialCapacity < 0)
  54. throw new ArgumentOutOfRangeException("capacity", "Needs a non-negative number");
  55. if (!(growFactor >= 1.0F && growFactor <= 10.0F))
  56. throw new ArgumentOutOfRangeException("growFactor", "Queue growth factor must be between 1.0 and 10.0, inclusive");
  57. _array = new object[initialCapacity];
  58. this._growFactor = (int)(growFactor * 100);
  59. }
  60. // from ICollection
  61. public virtual int Count {
  62. get { return _size; }
  63. }
  64. public virtual bool IsSynchronized {
  65. get { return false; }
  66. }
  67. public virtual object SyncRoot {
  68. get { return this; }
  69. }
  70. public virtual void CopyTo (Array array, int index)
  71. {
  72. if (array == null)
  73. throw new ArgumentNullException ("array");
  74. if (index < 0)
  75. throw new ArgumentOutOfRangeException ("index");
  76. if (array.Rank > 1
  77. || (index != 0 && index >= array.Length)
  78. || _size > array.Length - index)
  79. throw new ArgumentException ();
  80. int contents_length = _array.Length;
  81. int length_from_head = contents_length - _head;
  82. // copy the _array of the circular array
  83. Array.Copy (_array, _head, array, index,
  84. Math.Min (_size, length_from_head));
  85. if (_size > length_from_head)
  86. Array.Copy (_array, 0, array,
  87. index + length_from_head,
  88. _size - length_from_head);
  89. }
  90. // from IEnumerable
  91. public virtual IEnumerator GetEnumerator () {
  92. return new QueueEnumerator (this);
  93. }
  94. // from ICloneable
  95. public virtual object Clone () {
  96. Queue newQueue;
  97. newQueue = new Queue (this._array.Length);
  98. newQueue._growFactor = _growFactor;
  99. Array.Copy (this._array, 0, newQueue._array, 0,
  100. this._array.Length);
  101. newQueue._head = this._head;
  102. newQueue._size = this._size;
  103. newQueue._tail = this._tail;
  104. return newQueue;
  105. }
  106. public virtual void Clear () {
  107. _version++;
  108. _head = 0;
  109. _size = 0;
  110. _tail = 0;
  111. for (int length = _array.Length - 1; length >= 0; length--)
  112. _array [length] = null;
  113. }
  114. public virtual bool Contains (object obj) {
  115. int tail = _head + _size;
  116. if (obj == null) {
  117. for (int i = _head; i < tail; i++) {
  118. if (_array[i % _array.Length] == null)
  119. return true;
  120. }
  121. } else {
  122. for (int i = _head; i < tail; i++) {
  123. if (obj.Equals (_array[i % _array.Length]))
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. public virtual object Dequeue ()
  130. {
  131. _version++;
  132. if (_size < 1)
  133. throw new InvalidOperationException ();
  134. object result = _array[_head];
  135. _array [_head] = null;
  136. _head = (_head + 1) % _array.Length;
  137. _size--;
  138. return result;
  139. }
  140. public virtual void Enqueue (object obj) {
  141. _version++;
  142. if (_size == _array.Length)
  143. grow ();
  144. _array[_tail] = obj;
  145. _tail = (_tail+1) % _array.Length;
  146. _size++;
  147. }
  148. public virtual object Peek () {
  149. if (_size < 1)
  150. throw new InvalidOperationException ();
  151. return _array[_head];
  152. }
  153. public static Queue Synchronized (Queue queue) {
  154. if (queue == null) {
  155. throw new ArgumentNullException ("queue");
  156. }
  157. return new SyncQueue (queue);
  158. }
  159. public virtual object[] ToArray () {
  160. object[] ret = new object[_size];
  161. CopyTo (ret, 0);
  162. return ret;
  163. }
  164. public virtual void TrimToSize() {
  165. _version++;
  166. object[] trimmed = new object [_size];
  167. CopyTo (trimmed, 0);
  168. _array = trimmed;
  169. _head = 0;
  170. _tail = _head + _size;
  171. }
  172. // private methods
  173. private void grow () {
  174. int newCapacity = (_array.Length * _growFactor) / 100;
  175. object[] newContents = new object[newCapacity];
  176. CopyTo (newContents, 0);
  177. _array = newContents;
  178. _head = 0;
  179. _tail = _head + _size;
  180. }
  181. // private classes
  182. private class SyncQueue : Queue {
  183. Queue queue;
  184. internal SyncQueue (Queue queue) {
  185. this.queue = queue;
  186. }
  187. public override int Count {
  188. get {
  189. lock (queue) {
  190. return queue.Count;
  191. }
  192. }
  193. }
  194. public override bool IsSynchronized {
  195. get {
  196. return true;
  197. }
  198. }
  199. public override object SyncRoot {
  200. get {
  201. return queue.SyncRoot;
  202. }
  203. }
  204. public override void CopyTo (Array array, int index) {
  205. lock (queue) {
  206. queue.CopyTo (array, index);
  207. }
  208. }
  209. public override IEnumerator GetEnumerator () {
  210. lock (queue) {
  211. return queue.GetEnumerator ();
  212. }
  213. }
  214. public override object Clone () {
  215. lock (queue) {
  216. return new SyncQueue((Queue) queue.Clone ());
  217. }
  218. }
  219. /*
  220. public override bool IsReadOnly {
  221. get {
  222. lock (queue) {
  223. return queue.IsReadOnly;
  224. }
  225. }
  226. }
  227. */
  228. public override void Clear () {
  229. lock (queue) {
  230. queue.Clear ();
  231. }
  232. }
  233. public override void TrimToSize () {
  234. lock (queue) {
  235. queue.TrimToSize ();
  236. }
  237. }
  238. public override bool Contains (object obj) {
  239. lock (queue) {
  240. return queue.Contains (obj);
  241. }
  242. }
  243. public override object Dequeue () {
  244. lock (queue) {
  245. return queue.Dequeue ();
  246. }
  247. }
  248. public override void Enqueue (object obj) {
  249. lock (queue) {
  250. queue.Enqueue (obj);
  251. }
  252. }
  253. public override object Peek () {
  254. lock (queue) {
  255. return queue.Peek ();
  256. }
  257. }
  258. public override object[] ToArray () {
  259. lock (queue) {
  260. return queue.ToArray ();
  261. }
  262. }
  263. }
  264. [Serializable]
  265. private class QueueEnumerator : IEnumerator, ICloneable {
  266. Queue queue;
  267. private int _version;
  268. private int current;
  269. internal QueueEnumerator (Queue q) {
  270. queue = q;
  271. _version = q._version;
  272. current = -1; // one element before the _head
  273. }
  274. public object Clone ()
  275. {
  276. QueueEnumerator q = new QueueEnumerator (queue);
  277. q._version = _version;
  278. q.current = current;
  279. return q;
  280. }
  281. public virtual object Current {
  282. get {
  283. if (_version != queue._version
  284. || current < 0
  285. || current >= queue._size)
  286. throw new InvalidOperationException ();
  287. return queue._array[(queue._head + current) % queue._array.Length];
  288. }
  289. }
  290. public virtual bool MoveNext () {
  291. if (_version != queue._version) {
  292. throw new InvalidOperationException ();
  293. }
  294. if (current >= queue._size - 1) {
  295. current = Int32.MaxValue; // to late!
  296. return false;
  297. } else {
  298. current++;
  299. return true;
  300. }
  301. }
  302. public virtual void Reset () {
  303. if (_version != queue._version) {
  304. throw new InvalidOperationException();
  305. }
  306. current = -1;
  307. }
  308. }
  309. }
  310. }