// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-#if NET_2_0
using System;
using System.Runtime.InteropServices;
+using System.Diagnostics;
namespace System.Collections.Generic
{
[ComVisible(false)]
[Serializable]
+ [DebuggerDisplay ("Count={Count}")]
+ [DebuggerTypeProxy (typeof (CollectionDebuggerView))]
public class Queue<T> : IEnumerable <T>, ICollection, IEnumerable
{
- T [] data;
- int head;
- int tail;
- int size;
- int version;
- int defaultCapacity;
-
- private readonly static int INITIAL_SIZE = 16;
+ T [] _array;
+ int _head;
+ int _tail;
+ int _size;
+ int _version;
public Queue ()
{
- defaultCapacity = INITIAL_SIZE;
+ _array = new T [0];
}
- public Queue (int count)
+ public Queue (int capacity)
{
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
+ if (capacity < 0)
+ throw new ArgumentOutOfRangeException ("capacity");
- defaultCapacity = count;
- data = new T [count];
+ _array = new T [capacity];
}
public Queue (IEnumerable <T> collection)
{
if (collection == null)
throw new ArgumentNullException ("collection");
-
+
+ var icoll = collection as ICollection<T>;
+ var size = icoll != null ? icoll.Count : 0;
+
+ _array = new T [size];
+
foreach (T t in collection)
Enqueue (t);
- defaultCapacity = size;
}
public void Clear ()
{
- if (data != null)
- Array.Clear (data, 0, data.Length);
+ Array.Clear (_array, 0, _array.Length);
- head = tail = size = 0;
+ _head = _tail = _size = 0;
+ _version++;
}
public bool Contains (T item)
return false;
}
- public void CopyTo (T [] array, int idx)
+ public void CopyTo (T [] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException ();
-
- if ((uint) idx > (uint) array.Length)
- throw new ArgumentOutOfRangeException ();
-
- if (array.Length - idx < size)
- throw new ArgumentOutOfRangeException ();
-
- if (size == 0)
- return;
-
- int contents_length = data.Length;
- int length_from_head = contents_length - head;
-
- Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
- if (size > length_from_head)
- Array.Copy (data, 0, array,
- idx + length_from_head,
- size - length_from_head);
-
+
+ ((ICollection) this).CopyTo (array, arrayIndex);
}
void ICollection.CopyTo (Array array, int idx)
if (array == null)
throw new ArgumentNullException ();
- if ((uint) idx < (uint) array.Length)
+ if ((uint) idx > (uint) array.Length)
throw new ArgumentOutOfRangeException ();
- if (array.Length - idx < size)
+ if (array.Length - idx < _size)
throw new ArgumentOutOfRangeException ();
- if (size == 0)
+ if (_size == 0)
return;
try {
- int contents_length = data.Length;
- int length_from_head = contents_length - head;
+ int contents_length = _array.Length;
+ int length_from_head = contents_length - _head;
- Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
- if (size > length_from_head)
- Array.Copy (data, 0, array,
+ Array.Copy (_array, _head, array, idx, Math.Min (_size, length_from_head));
+ if (_size > length_from_head)
+ Array.Copy (_array, 0, array,
idx + length_from_head,
- size - length_from_head);
+ _size - length_from_head);
} catch (ArrayTypeMismatchException) {
throw new ArgumentException ();
}
T ret = Peek ();
// clear stuff out to make the GC happy
- data [head] = default (T);
+ _array [_head] = default (T);
- if (++head == data.Length)
- head = 0;
- size --;
- version ++;
+ if (++_head == _array.Length)
+ _head = 0;
+ _size --;
+ _version ++;
return ret;
}
public T Peek ()
{
- if (size == 0)
+ if (_size == 0)
throw new InvalidOperationException ();
- return data [head];
+ return _array [_head];
}
public void Enqueue (T item)
{
- if (data == null || size == data.Length)
- SetCapacity (Math.Max (size * 2, 4));
+ if (_size == _array.Length || _tail == _array.Length)
+ SetCapacity (Math.Max (Math.Max (_size, _tail) * 2, 4));
- data [tail] = item;
+ _array [_tail] = item;
- if (++tail == data.Length)
- tail = 0;
+ if (++_tail == _array.Length)
+ _tail = 0;
- size ++;
- version ++;
+ _size ++;
+ _version ++;
}
public T [] ToArray ()
{
- T [] t = new T [size];
+ T [] t = new T [_size];
CopyTo (t, 0);
return t;
}
public void TrimExcess ()
{
- if (data != null && (size < data.Length * 0.9))
- Array.Resize <T> (ref data, size == 0 ? defaultCapacity : size);
+ if (_size < _array.Length * 0.9)
+ SetCapacity (_size);
}
void SetCapacity (int new_size)
{
- if (data != null && new_size == data.Length)
+ if (new_size == _array.Length)
return;
- if (new_size < size)
+ if (new_size < _size)
throw new InvalidOperationException ("shouldnt happen");
T [] new_data = new T [new_size];
- if (size > 0)
+ if (_size > 0)
CopyTo (new_data, 0);
- data = new_data;
- tail = size;
- head = 0;
- version ++;
+ _array = new_data;
+ _tail = _size;
+ _head = 0;
+ _version ++;
}
public int Count {
- get { return size; }
+ get { return _size; }
}
bool ICollection.IsSynchronized {
return GetEnumerator ();
}
+ [Serializable]
public struct Enumerator : IEnumerator <T>, IEnumerator, IDisposable {
const int NOT_STARTED = -2;
// this MUST be -1, because we depend on it in move next.
- // we just decr the size, so, 0 - 1 == FINISHED
+ // we just decr the _size, so, 0 - 1 == FINISHED
const int FINISHED = -1;
Queue <T> q;
{
this.q = q;
idx = NOT_STARTED;
- ver = q.version;
+ ver = q._version;
}
// for some fucked up reason, MSFT added a useless dispose to this class
public bool MoveNext ()
{
- if (ver != q.version)
+ if (ver != q._version)
throw new InvalidOperationException ();
if (idx == NOT_STARTED)
- idx = q.size;
+ idx = q._size;
return idx != FINISHED && -- idx != FINISHED;
}
if (idx < 0)
throw new InvalidOperationException ();
- return q.data [(q.size - 1 - idx + q.head) % q.data.Length];
+ return q._array [(q._size - 1 - idx + q._head) % q._array.Length];
}
}
void IEnumerator.Reset ()
{
- if (ver != q.version)
+ if (ver != q._version)
throw new InvalidOperationException ();
idx = NOT_STARTED;
}
}
}
-#endif