namespace System.Collections.Generic
{
[ComVisible(false)]
- public class Queue<T> : ICollection<T>, ICollection
+ [Serializable]
+ 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;
public Queue ()
{
+ defaultCapacity = INITIAL_SIZE;
}
public Queue (int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException ("count");
-
+
+ defaultCapacity = count;
data = new T [count];
}
if (collection == null)
throw new ArgumentNullException ("collection");
- foreach (T t in collection)
- Enqueue (t);
+ foreach (T t in collection)
+ Enqueue (t);
+ defaultCapacity = size;
}
public void Clear ()
CopyTo (t, 0);
return t;
}
-
- public void TrimToSize ()
+
+ public void TrimExcess ()
{
- SetCapacity (size);
+ if (data != null && (size < data.Length * 0.9))
+ Array.Resize <T> (ref data, size == 0 ? defaultCapacity : size);
}
void SetCapacity (int new_size)
get { return size; }
}
-
- bool ICollection <T>.IsReadOnly {
- get { return false; }
- }
-
bool ICollection.IsSynchronized {
get { return false; }
}
get { return this; }
}
- void ICollection <T>.Add (T t)
- {
- Enqueue (t);
- }
-
- bool ICollection <T>.Remove (T t)
- {
- throw new InvalidOperationException ("");
- }
-
-
public Enumerator GetEnumerator ()
{
return new Enumerator (this);