* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / System.Collections.Generic / Queue.cs
index d2457651576f8b96d4ff52a9ab6f9d41d4c8e440..dee0b5f2469b59f9d88c83a8b6e7e0582706a48f 100644 (file)
@@ -38,6 +38,7 @@ using System.Runtime.InteropServices;
 namespace System.Collections.Generic
 {
        [ComVisible(false)]
+       [Serializable]
        public class Queue<T> : IEnumerable <T>, ICollection, IEnumerable
        {
                T [] data;
@@ -45,16 +46,21 @@ namespace System.Collections.Generic
                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];
                }
                
@@ -63,8 +69,9 @@ namespace System.Collections.Generic
                        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 ()
@@ -186,10 +193,11 @@ namespace System.Collections.Generic
                        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)