Wed Sep 11 15:26:34 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System / Array.cs
index ae208bc9d20838936fa259d17281d898b33f0517..43e8344bcdcce50d4422743c841ffb7e4205f356 100644 (file)
@@ -1,4 +1,4 @@
-//
+
 // System.Array.cs
 //
 // Authors:
@@ -48,7 +48,7 @@ namespace System
                }
 
                // IList interface
-               public object this [int index] {
+               object IList.this [int index] {
                        get {
                                return GetValueImpl (index);
                        } 
@@ -66,6 +66,9 @@ namespace System
                }
 
                bool IList.Contains (object value) {
+                       if (this.Rank > 1)
+                               throw new RankException ("Only single dimension arrays are supported.");
+
                        int length = this.Length;
                        for (int i = 0; i < length; i++) {
                                if (value.Equals (this.GetValueImpl (i)))
@@ -137,7 +140,7 @@ namespace System
                internal extern static Array CreateInstanceImpl(Type elementType, int[] lengths, int [] bounds);
 
                // Properties
-               public virtual int Count {
+               int ICollection.Count {
                        get {
                                return Length;
                        }
@@ -319,18 +322,52 @@ namespace System
                
                public static int BinarySearch (Array array, object value)
                {
+                       if (array == null)
+                               throw new ArgumentNullException("array");
+
+                       if (array.Rank > 1)
+                               throw new RankException();
+
+                       if (!(value is IComparable))
+                               throw new ArgumentException("value does not support IComparable");
+
                        return BinarySearch (array, array.GetLowerBound (0), array.GetLength (0),
                                             value, null);
                }
 
                public static int BinarySearch (Array array, object value, IComparer comparer)
                {
+                       if (array == null)
+                               throw new ArgumentNullException("array");
+
+                       if (array.Rank > 1)
+                               throw new RankException();
+
+                       if ((comparer == null) && !(value is IComparable))
+                               throw new ArgumentException("comparer is null and value does not support IComparable");
+
                        return BinarySearch (array, array.GetLowerBound (0), array.GetLength (0),
                                             value, comparer);
                }
 
                public static int BinarySearch (Array array, int index, int length, object value)
                {
+                       if (array == null)
+                               throw new ArgumentNullException("array");
+
+                       if (array.Rank > 1)
+                               throw new RankException();
+
+                       if (index < array.GetLowerBound (0))
+                               throw new ArgumentOutOfRangeException("index is less than the lower bound of array.");
+                       if (length < 0)
+                               throw new ArgumentOutOfRangeException("length is less than zero.");
+
+                       if (index + length > array.GetLowerBound (0) + array.GetLength (0))
+                               throw new ArgumentException("index and length do not specify a valid range in array.");
+                       if (!(value is IComparable))
+                               throw new ArgumentException("value does not support IComparable");
+
                        return BinarySearch (array, index, length, value, null);
                }
 
@@ -344,11 +381,16 @@ namespace System
                        if (array.Rank > 1)
                                throw new RankException ();
 
-                       if (index < array.GetLowerBound (0) || length < 0)
-                               throw new ArgumentOutOfRangeException ("index");
+                       if (index < array.GetLowerBound (0))
+                               throw new ArgumentOutOfRangeException("index is less than the lower bound of array.");
+                       if (length < 0)
+                               throw new ArgumentOutOfRangeException("length is less than zero.");
 
-                       if (index + length > array.GetUpperBound (0) + 1)
-                               throw new ArgumentException ("length");
+                       if (index + length > array.GetLowerBound (0) + array.GetLength (0))
+                               throw new ArgumentException("index and length do not specify a valid range in array.");
+
+                       if ((comparer == null) && !(value is IComparable))
+                               throw new ArgumentException("comparer is null and value does not support IComparable");
 
                        // cache this in case we need it
                        IComparable valueCompare = value as IComparable;
@@ -366,7 +408,7 @@ namespace System
                                while (iMin < iMax)
                                {
                                        int iMid = (iMin + iMax) / 2;
-                                       object elt = array.GetValue (iMid);
+                                       object elt = array.GetValueImpl (iMid);
 
                                        // this order is from MSDN
                                        if (comparer != null)
@@ -413,7 +455,7 @@ namespace System
 
                        for (int i = 0; i < length; i++) 
                        {
-                               array.SetValue(null, index + i);
+                               array.SetValueImpl(null, index + i);
                        }
                }
 
@@ -422,6 +464,14 @@ namespace System
 
                public static void Copy (Array source, Array dest, int length)
                {
+                       // need these checks here because we are going to use
+                       // GetLowerBound() on source and dest.
+                       if (source == null)
+                               throw new ArgumentNullException ("null");
+
+                       if (dest == null)
+                               throw new ArgumentNullException ("dest");
+
                        Copy (source, source.GetLowerBound (0), dest, dest.GetLowerBound (0), length);                  
                }
 
@@ -528,7 +578,7 @@ namespace System
 
                        for (int i = 0; i < length; i++)
                        {
-                               if (array.GetValue(index + i).Equals(value))
+                               if (array.GetValueImpl(index + i).Equals(value))
                                        return index + i;
                        }
 
@@ -565,7 +615,7 @@ namespace System
 
                        for (int i = index; i >= index-length+1; i--)
                        {
-                               if (array.GetValue(i).Equals(value))
+                               if (array.GetValueImpl(i).Equals(value))
                                        return i;
                        }
 
@@ -598,9 +648,9 @@ namespace System
                        {
                                object tmp;
 
-                               tmp = array.GetValue (index + i);
-                               array.SetValue(array.GetValue (index + length - i - 1), index + i);
-                               array.SetValue(tmp, index + length - i - 1);
+                               tmp = array.GetValueImpl (index + i);
+                               array.SetValueImpl(array.GetValueImpl (index + length - i - 1), index + i);
+                               array.SetValueImpl(tmp, index + length - i - 1);
                        }
                }               
                
@@ -673,14 +723,14 @@ namespace System
                        int low = low0;
                        int high = high0;
 
-                       object objPivot = keys.GetValue ((low + high) / 2);
+                       object objPivot = keys.GetValueImpl ((low + high) / 2);
 
                        while (low <= high)
                        {
                                // Move the walls in
-                               while (low < high0 && compare (keys.GetValue (low), objPivot, comparer) < 0)
+                               while (low < high0 && compare (keys.GetValueImpl (low), objPivot, comparer) < 0)
                                        ++low;
-                               while (high > low0 && compare (objPivot, keys.GetValue (high), comparer) < 0)
+                               while (high > low0 && compare (objPivot, keys.GetValueImpl (high), comparer) < 0)
                                        --high;
 
                                if (low <= high)
@@ -701,15 +751,15 @@ namespace System
                {
                        object tmp;
 
-                       tmp = keys.GetValue (i);
-                       keys.SetValue (keys.GetValue (j), i);
-                       keys.SetValue (tmp, j);
+                       tmp = keys.GetValueImpl (i);
+                       keys.SetValueImpl (keys.GetValue (j), i);
+                       keys.SetValueImpl (tmp, j);
 
                        if (items != null)
                        {
-                               tmp = items.GetValue (i);
-                               items.SetValue (items.GetValue (j), i);
-                               items.SetValue (tmp, j);
+                               tmp = items.GetValueImpl (i);
+                               items.SetValueImpl (items.GetValueImpl (j), i);
+                               items.SetValueImpl (tmp, j);
                        }
                }