System/PCL: Implement HttpWebRequest.SupportsCookieContainer, WebRequest.CreateHttp...
[mono.git] / mcs / class / System / System.Collections.Generic / Queue.cs
index 6d0aa3d5dec48ab75c2953b320d285bfe147c994..2c3aff6a76f35a0eb7be487770f45e1c61c15759 100644 (file)
 // 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)
@@ -97,29 +99,12 @@ namespace System.Collections.Generic
                        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)
@@ -127,24 +112,24 @@ namespace System.Collections.Generic
                        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 ();
                        }
@@ -155,71 +140,71 @@ namespace System.Collections.Generic
                        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 {
@@ -250,7 +235,7 @@ namespace System.Collections.Generic
                        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;
@@ -261,7 +246,7 @@ namespace System.Collections.Generic
                        {
                                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
@@ -274,11 +259,11 @@ namespace System.Collections.Generic
                        
                        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;
                        }
@@ -288,13 +273,13 @@ namespace System.Collections.Generic
                                        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;
@@ -307,4 +292,3 @@ namespace System.Collections.Generic
                }
        }
 }
-#endif