2 // System.Collections.Queue
5 // Ricardo Fernández Pascual
7 // (C) 2001 Ricardo Fernández Pascual
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
36 namespace System.Collections {
39 public class Queue : ICollection, IEnumerable, ICloneable {
41 private object[] _array;
42 private int _head = 0; // points to the first used slot
43 private int _size = 0;
44 private int _tail = 0;
45 private int _growFactor;
46 private int _version = 0;
48 public Queue () : this (32, 2.0F) {}
49 public Queue (int initialCapacity) : this (initialCapacity, 2.0F) {}
50 public Queue(ICollection col) : this (col == null ? 32 : col.Count)
53 throw new ArgumentNullException ("col");
55 // We have to do this because msft seems to call the
56 // enumerator rather than CopyTo. This affects classes
58 foreach (object o in col)
62 public Queue (int initialCapacity, float growFactor) {
63 if (initialCapacity < 0)
64 throw new ArgumentOutOfRangeException("capacity", "Needs a non-negative number");
65 if (!(growFactor >= 1.0F && growFactor <= 10.0F))
66 throw new ArgumentOutOfRangeException("growFactor", "Queue growth factor must be between 1.0 and 10.0, inclusive");
68 _array = new object[initialCapacity];
70 this._growFactor = (int)(growFactor * 100);
75 public virtual int Count {
79 public virtual bool IsSynchronized {
83 public virtual object SyncRoot {
87 public virtual void CopyTo (Array array, int index)
90 throw new ArgumentNullException ("array");
93 throw new ArgumentOutOfRangeException ("index");
96 || (index != 0 && index >= array.Length)
97 || _size > array.Length - index)
98 throw new ArgumentException ();
100 int contents_length = _array.Length;
101 int length_from_head = contents_length - _head;
102 // copy the _array of the circular array
103 Array.Copy (_array, _head, array, index,
104 Math.Min (_size, length_from_head));
105 if (_size > length_from_head)
106 Array.Copy (_array, 0, array,
107 index + length_from_head,
108 _size - length_from_head);
113 public virtual IEnumerator GetEnumerator () {
114 return new QueueEnumerator (this);
119 public virtual object Clone () {
122 newQueue = new Queue (this._array.Length);
123 newQueue._growFactor = _growFactor;
125 Array.Copy (this._array, 0, newQueue._array, 0,
127 newQueue._head = this._head;
128 newQueue._size = this._size;
129 newQueue._tail = this._tail;
134 public virtual void Clear () {
139 for (int length = _array.Length - 1; length >= 0; length--)
140 _array [length] = null;
143 public virtual bool Contains (object obj) {
144 int tail = _head + _size;
146 for (int i = _head; i < tail; i++) {
147 if (_array[i % _array.Length] == null)
151 for (int i = _head; i < tail; i++) {
152 if (obj.Equals (_array[i % _array.Length]))
159 public virtual object Dequeue ()
163 throw new InvalidOperationException ();
164 object result = _array[_head];
165 _array [_head] = null;
166 _head = (_head + 1) % _array.Length;
171 public virtual void Enqueue (object obj) {
173 if (_size == _array.Length)
176 _tail = (_tail+1) % _array.Length;
181 public virtual object Peek () {
183 throw new InvalidOperationException ();
184 return _array[_head];
187 public static Queue Synchronized (Queue queue) {
189 throw new ArgumentNullException ("queue");
191 return new SyncQueue (queue);
194 public virtual object[] ToArray () {
195 object[] ret = new object[_size];
200 public virtual void TrimToSize() {
202 object[] trimmed = new object [_size];
206 _tail = _head + _size;
211 private void grow () {
212 int newCapacity = (_array.Length * _growFactor) / 100;
213 if (newCapacity < _array.Length + 1)
214 newCapacity = _array.Length + 1;
215 object[] newContents = new object[newCapacity];
216 CopyTo (newContents, 0);
217 _array = newContents;
219 _tail = _head + _size;
224 private class SyncQueue : Queue {
227 internal SyncQueue (Queue queue) {
231 public override int Count {
239 public override bool IsSynchronized {
245 public override object SyncRoot {
247 return queue.SyncRoot;
251 public override void CopyTo (Array array, int index) {
253 queue.CopyTo (array, index);
257 public override IEnumerator GetEnumerator () {
259 return queue.GetEnumerator ();
263 public override object Clone () {
265 return new SyncQueue((Queue) queue.Clone ());
270 public override bool IsReadOnly {
273 return queue.IsReadOnly;
279 public override void Clear () {
285 public override void TrimToSize () {
291 public override bool Contains (object obj) {
293 return queue.Contains (obj);
297 public override object Dequeue () {
299 return queue.Dequeue ();
303 public override void Enqueue (object obj) {
309 public override object Peek () {
311 return queue.Peek ();
315 public override object[] ToArray () {
317 return queue.ToArray ();
323 private class QueueEnumerator : IEnumerator, ICloneable {
325 private int _version;
328 internal QueueEnumerator (Queue q) {
330 _version = q._version;
331 current = -1; // one element before the _head
334 public object Clone ()
336 QueueEnumerator q = new QueueEnumerator (queue);
337 q._version = _version;
342 public virtual object Current {
344 if (_version != queue._version
346 || current >= queue._size)
347 throw new InvalidOperationException ();
348 return queue._array[(queue._head + current) % queue._array.Length];
352 public virtual bool MoveNext () {
353 if (_version != queue._version) {
354 throw new InvalidOperationException ();
357 if (current >= queue._size - 1) {
358 current = Int32.MaxValue; // to late!
366 public virtual void Reset () {
367 if (_version != queue._version) {
368 throw new InvalidOperationException();