Queue.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. using System.Runtime.InteropServices;
  34. namespace System.Collections {
  35. #if NET_2_0
  36. [ComVisible(true)]
  37. #endif
  38. [Serializable]
  39. public class Queue : ICollection, IEnumerable, ICloneable {
  40. private object[] _array;
  41. private int _head = 0; // points to the first used slot
  42. private int _size = 0;
  43. private int _tail = 0;
  44. private int _growFactor;
  45. private int _version = 0;
  46. public Queue () : this (32, 2.0F) {}
  47. public Queue (int initialCapacity) : this (initialCapacity, 2.0F) {}
  48. public Queue(ICollection col) : this (col == null ? 32 : col.Count)
  49. {
  50. if (col == null)
  51. throw new ArgumentNullException ("col");
  52. // We have to do this because msft seems to call the
  53. // enumerator rather than CopyTo. This affects classes
  54. // like bitarray.
  55. foreach (object o in col)
  56. Enqueue (o);
  57. }
  58. public Queue (int initialCapacity, float growFactor) {
  59. if (initialCapacity < 0)
  60. throw new ArgumentOutOfRangeException("capacity", "Needs a non-negative number");
  61. if (!(growFactor >= 1.0F && growFactor <= 10.0F))
  62. throw new ArgumentOutOfRangeException("growFactor", "Queue growth factor must be between 1.0 and 10.0, inclusive");
  63. _array = new object[initialCapacity];
  64. this._growFactor = (int)(growFactor * 100);
  65. }
  66. // from ICollection
  67. public virtual int Count {
  68. get { return _size; }
  69. }
  70. public virtual bool IsSynchronized {
  71. get { return false; }
  72. }
  73. public virtual object SyncRoot {
  74. get { return this; }
  75. }
  76. public virtual void CopyTo (Array array, int index)
  77. {
  78. if (array == null)
  79. throw new ArgumentNullException ("array");
  80. if (index < 0)
  81. throw new ArgumentOutOfRangeException ("index");
  82. if (array.Rank > 1
  83. || (index != 0 && index >= array.Length)
  84. || _size > array.Length - index)
  85. throw new ArgumentException ();
  86. int contents_length = _array.Length;
  87. int length_from_head = contents_length - _head;
  88. // copy the _array of the circular array
  89. Array.Copy (_array, _head, array, index,
  90. Math.Min (_size, length_from_head));
  91. if (_size > length_from_head)
  92. Array.Copy (_array, 0, array,
  93. index + length_from_head,
  94. _size - length_from_head);
  95. }
  96. // from IEnumerable
  97. public virtual IEnumerator GetEnumerator () {
  98. return new QueueEnumerator (this);
  99. }
  100. // from ICloneable
  101. public virtual object Clone () {
  102. Queue newQueue;
  103. newQueue = new Queue (this._array.Length);
  104. newQueue._growFactor = _growFactor;
  105. Array.Copy (this._array, 0, newQueue._array, 0,
  106. this._array.Length);
  107. newQueue._head = this._head;
  108. newQueue._size = this._size;
  109. newQueue._tail = this._tail;
  110. return newQueue;
  111. }
  112. public virtual void Clear () {
  113. _version++;
  114. _head = 0;
  115. _size = 0;
  116. _tail = 0;
  117. for (int length = _array.Length - 1; length >= 0; length--)
  118. _array [length] = null;
  119. }
  120. public virtual bool Contains (object obj) {
  121. int tail = _head + _size;
  122. if (obj == null) {
  123. for (int i = _head; i < tail; i++) {
  124. if (_array[i % _array.Length] == null)
  125. return true;
  126. }
  127. } else {
  128. for (int i = _head; i < tail; i++) {
  129. if (obj.Equals (_array[i % _array.Length]))
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. public virtual object Dequeue ()
  136. {
  137. _version++;
  138. if (_size < 1)
  139. throw new InvalidOperationException ();
  140. object result = _array[_head];
  141. _array [_head] = null;
  142. _head = (_head + 1) % _array.Length;
  143. _size--;
  144. return result;
  145. }
  146. public virtual void Enqueue (object obj) {
  147. _version++;
  148. if (_size == _array.Length)
  149. grow ();
  150. _array[_tail] = obj;
  151. _tail = (_tail+1) % _array.Length;
  152. _size++;
  153. }
  154. public virtual object Peek () {
  155. if (_size < 1)
  156. throw new InvalidOperationException ();
  157. return _array[_head];
  158. }
  159. public static Queue Synchronized (Queue queue) {
  160. if (queue == null) {
  161. throw new ArgumentNullException ("queue");
  162. }
  163. return new SyncQueue (queue);
  164. }
  165. public virtual object[] ToArray () {
  166. object[] ret = new object[_size];
  167. CopyTo (ret, 0);
  168. return ret;
  169. }
  170. public virtual void TrimToSize() {
  171. _version++;
  172. object[] trimmed = new object [_size];
  173. CopyTo (trimmed, 0);
  174. _array = trimmed;
  175. _head = 0;
  176. _tail = _head + _size;
  177. }
  178. // private methods
  179. private void grow () {
  180. int newCapacity = (_array.Length * _growFactor) / 100;
  181. if (newCapacity < _array.Length + 1)
  182. newCapacity = _array.Length + 1;
  183. object[] newContents = new object[newCapacity];
  184. CopyTo (newContents, 0);
  185. _array = newContents;
  186. _head = 0;
  187. _tail = _head + _size;
  188. }
  189. // private classes
  190. private class SyncQueue : Queue {
  191. Queue queue;
  192. internal SyncQueue (Queue queue) {
  193. this.queue = queue;
  194. }
  195. public override int Count {
  196. get {
  197. lock (queue) {
  198. return queue.Count;
  199. }
  200. }
  201. }
  202. public override bool IsSynchronized {
  203. get {
  204. return true;
  205. }
  206. }
  207. public override object SyncRoot {
  208. get {
  209. return queue.SyncRoot;
  210. }
  211. }
  212. public override void CopyTo (Array array, int index) {
  213. lock (queue) {
  214. queue.CopyTo (array, index);
  215. }
  216. }
  217. public override IEnumerator GetEnumerator () {
  218. lock (queue) {
  219. return queue.GetEnumerator ();
  220. }
  221. }
  222. public override object Clone () {
  223. lock (queue) {
  224. return new SyncQueue((Queue) queue.Clone ());
  225. }
  226. }
  227. /*
  228. public override bool IsReadOnly {
  229. get {
  230. lock (queue) {
  231. return queue.IsReadOnly;
  232. }
  233. }
  234. }
  235. */
  236. public override void Clear () {
  237. lock (queue) {
  238. queue.Clear ();
  239. }
  240. }
  241. public override void TrimToSize () {
  242. lock (queue) {
  243. queue.TrimToSize ();
  244. }
  245. }
  246. public override bool Contains (object obj) {
  247. lock (queue) {
  248. return queue.Contains (obj);
  249. }
  250. }
  251. public override object Dequeue () {
  252. lock (queue) {
  253. return queue.Dequeue ();
  254. }
  255. }
  256. public override void Enqueue (object obj) {
  257. lock (queue) {
  258. queue.Enqueue (obj);
  259. }
  260. }
  261. public override object Peek () {
  262. lock (queue) {
  263. return queue.Peek ();
  264. }
  265. }
  266. public override object[] ToArray () {
  267. lock (queue) {
  268. return queue.ToArray ();
  269. }
  270. }
  271. }
  272. [Serializable]
  273. private class QueueEnumerator : IEnumerator, ICloneable {
  274. Queue queue;
  275. private int _version;
  276. private int current;
  277. internal QueueEnumerator (Queue q) {
  278. queue = q;
  279. _version = q._version;
  280. current = -1; // one element before the _head
  281. }
  282. public object Clone ()
  283. {
  284. QueueEnumerator q = new QueueEnumerator (queue);
  285. q._version = _version;
  286. q.current = current;
  287. return q;
  288. }
  289. public virtual object Current {
  290. get {
  291. if (_version != queue._version
  292. || current < 0
  293. || current >= queue._size)
  294. throw new InvalidOperationException ();
  295. return queue._array[(queue._head + current) % queue._array.Length];
  296. }
  297. }
  298. public virtual bool MoveNext () {
  299. if (_version != queue._version) {
  300. throw new InvalidOperationException ();
  301. }
  302. if (current >= queue._size - 1) {
  303. current = Int32.MaxValue; // to late!
  304. return false;
  305. } else {
  306. current++;
  307. return true;
  308. }
  309. }
  310. public virtual void Reset () {
  311. if (_version != queue._version) {
  312. throw new InvalidOperationException();
  313. }
  314. current = -1;
  315. }
  316. }
  317. }
  318. }