Queue.cs 8.6 KB

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