2007-10-27 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / class / corlib / System / Array.cs
index 577d9794bf793c64032a42774410961135e4cdc8..b0653b0169a5da70c7b59b05bbeca7238b9ac337 100644 (file)
@@ -58,32 +58,32 @@ namespace System
 #if NET_2_0
                // FIXME: they should not be exposed, but there are some
                // dependent code in the runtime.
-               public int InternalArray__ICollection_get_Count<T> ()
+               protected int InternalArray__ICollection_get_Count<T> ()
                {
                        return Length;
                }
 
-               public IEnumerator<T> InternalArray__IEnumerable_GetEnumerator<T> ()
+               protected IEnumerator<T> InternalArray__IEnumerable_GetEnumerator<T> ()
                {
                        return new InternalEnumerator<T> (this);
                }
 
-               public void InternalArray__ICollection_Clear<T> ()
+               protected void InternalArray__ICollection_Clear<T> ()
                {
                        throw new NotSupportedException ("Collection is read-only");
                }
 
-               public void InternalArray__ICollection_Add<T> (T item)
+               protected void InternalArray__ICollection_Add<T> (T item)
                {
                        throw new NotSupportedException ("Collection is read-only");
                }
 
-               public bool InternalArray__ICollection_Remove<T> (T item)
+               protected bool InternalArray__ICollection_Remove<T> (T item)
                {
                        throw new NotSupportedException ("Collection is read-only");
                }
 
-               public bool InternalArray__ICollection_Contains<T> (T item)
+               protected bool InternalArray__ICollection_Contains<T> (T item)
                {
                        if (this.Rank > 1)
                                throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
@@ -106,7 +106,7 @@ namespace System
                        return false;
                }
 
-               public void InternalArray__ICollection_CopyTo<T> (T[] array, int index)
+               protected void InternalArray__ICollection_CopyTo<T> (T[] array, int index)
                {
                        if (array == null)
                                throw new ArgumentNullException ("array");
@@ -116,7 +116,9 @@ namespace System
                        if (this.Rank > 1)
                                throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
                        if (index + this.GetLength (0) > array.GetLowerBound (0) + array.GetLength (0))
-                               throw new ArgumentException ();
+                               throw new ArgumentException ("Destination array was not long " +
+                                       "enough. Check destIndex and length, and the array's " +
+                                       "lower bounds.");
                        if (array.Rank > 1)
                                throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
                        if (index < 0)
@@ -126,17 +128,17 @@ namespace System
                        Copy (this, this.GetLowerBound (0), array, index, this.GetLength (0));
                }
 
-               public void InternalArray__Insert<T> (int index, T item)
+               protected void InternalArray__Insert<T> (int index, T item)
                {
                        throw new NotSupportedException ("Collection is read-only");
                }
 
-               public void InternalArray__RemoveAt<T> (int index)
+               protected void InternalArray__RemoveAt<T> (int index)
                {
                        throw new NotSupportedException ("Collection is read-only");
                }
 
-               public int InternalArray__IndexOf<T> (T item)
+               protected int InternalArray__IndexOf<T> (T item)
                {
                        if (this.Rank > 1)
                                throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
@@ -169,7 +171,7 @@ namespace System
                        return retVal;
                }
 
-               public T InternalArray__get_Item<T> (int index)
+               protected T InternalArray__get_Item<T> (int index)
                {
                        if (unchecked ((uint) index) >= unchecked ((uint) Length))
                                throw new ArgumentOutOfRangeException ("index");
@@ -179,15 +181,22 @@ namespace System
                        return value;
                }
 
-               public void InternalArray__set_Item<T> (int index, T item)
+               protected void InternalArray__set_Item<T> (int index, T item)
                {
-                       throw new NotSupportedException ("Collection is read-only");
+                       if (unchecked ((uint) index) >= unchecked ((uint) Length))
+                               throw new ArgumentOutOfRangeException ("index");
+
+                       SetGenericValueImpl (index, ref item);
                }
 
                // CAUTION! No bounds checking!
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                internal extern void GetGenericValueImpl<T> (int pos, out T value);
 
+               // CAUTION! No bounds checking!
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               internal extern void SetGenericValueImpl<T> (int pos, ref T value);
+
                internal struct InternalEnumerator<T> : IEnumerator<T>
                {
                        const int NOT_STARTED = -2;
@@ -831,7 +840,9 @@ namespace System
                        int iCmp = 0;
                        try {
                                while (iMin <= iMax) {
-                                       int iMid = (iMin + iMax) / 2;
+                                       // Be careful with overflow
+                                       // http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
+                                       int iMid = iMin + ((iMax - iMin) / 2);
                                        object elt = array.GetValueImpl (iMid);
 
                                        iCmp = comparer.Compare (elt, value);
@@ -930,9 +941,19 @@ namespace System
                        int dest_pos = destinationIndex - destinationArray.GetLowerBound (0);
 
                        // re-ordered to avoid possible integer overflow
-                       if (source_pos > sourceArray.Length - length || dest_pos > destinationArray.Length - length)
+                       if (source_pos > sourceArray.Length - length)
                                throw new ArgumentException ("length");
 
+                       if (dest_pos > destinationArray.Length - length) {
+                               string msg = "Destination array was not long enough. Check " +
+                                       "destIndex and length, and the array's lower bounds";
+#if NET_2_0
+                               throw new ArgumentException (msg, string.Empty);
+#else
+                               throw new ArgumentException (msg);
+#endif
+                       }
+
                        if (sourceArray.Rank != destinationArray.Rank)
                                throw new RankException (Locale.GetText ("Arrays must be of same size."));
 
@@ -1137,7 +1158,9 @@ namespace System
                        if (array is double[])
                                return new Swapper (array.double_swapper);
 
-                       return new Swapper (array.obj_swapper);
+                       // gmcs refuses to compile this
+                       //return new Swapper (array.generic_swapper<T>);
+                       return new Swapper (array.slow_swapper);
                }
 #endif
 
@@ -1366,6 +1389,15 @@ namespace System
                        array [j] = val;
                }
 
+#if NET_2_0
+               void generic_swapper<T> (int i, int j) {
+                       T[] array = this as T[];
+                       T val = array [i];
+                       array [i] = array [j];
+                       array [j] = val;
+               }
+#endif
+
                static int new_gap (int gap)
                {
                        gap = (gap * 10) / 13;
@@ -1456,7 +1488,9 @@ namespace System
                        int low = low0;
                        int high = high0;
 
-                       object objPivot = keys.GetValueImpl ((low + high) / 2);
+                       // Be careful with overflows
+                       int mid = low + ((high - low) / 2);
+                       object objPivot = keys.GetValueImpl (mid);
 
                        while (low <= high) {
                                // Move the walls in
@@ -1654,7 +1688,9 @@ namespace System
                        int low = low0;
                        int high = high0;
 
-                       K keyPivot = keys [(low + high) / 2];
+                       // Be careful with overflows
+                       int mid = low + ((high - low) / 2);
+                       K keyPivot = keys [mid];
 
                        while (low <= high) {
                                // Move the walls in
@@ -1703,7 +1739,9 @@ namespace System
                        int low = low0;
                        int high = high0;
 
-                       T keyPivot = array [(low + high) / 2];
+                       // Be careful with overflows
+                       int mid = low + ((high - low) / 2);
+                       T keyPivot = array [mid];
 
                        while (low <= high) {
                                // Move the walls in
@@ -1763,7 +1801,9 @@ namespace System
                        if (this.Rank > 1)
                                throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
                        if (index + this.GetLength (0) > array.GetLowerBound (0) + array.GetLength (0))
-                               throw new ArgumentException ();
+                               throw new ArgumentException ("Destination array was not long " +
+                                       "enough. Check destIndex and length, and the array's " +
+                                       "lower bounds.");
                        if (array.Rank > 1)
                                throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
                        if (index < 0)
@@ -2018,7 +2058,8 @@ namespace System
                        int iCmp = 0;
                        try {
                                while (iMin <= iMax) {
-                                       int iMid = (iMin + iMax) / 2;
+                                       // Be careful with overflows
+                                       int iMid = iMin + ((iMax - iMin) / 2);
                                        iCmp = comparer.Compare (value, array [iMid]);
 
                                        if (iCmp == 0)