5 // Joe Shaw (joe@ximian.com)
6 // Martin Baulig (martin@gnome.org)
7 // Dietmar Maurer (dietmar@ximian.com)
8 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
10 // (C) 2001-2003 Ximian, Inc. http://www.ximian.com
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
40 using System.Collections.Generic;
45 [MonoTODO ("We are doing way to many double/triple exception checks for the overloaded functions")]
46 [MonoTODO ("Sort overloads parameter checks are VERY inconsistent")]
47 public abstract class Array : ICloneable, ICollection, IList, IEnumerable
57 int length = this.GetLength (0);
59 for (int i = 1; i < this.Rank; i++) {
60 length *= this.GetLength (i);
68 public long LongLength {
69 get { return Length; }
75 return this.GetRank ();
80 object IList.this [int index] {
82 if (unchecked ((uint) index) >= unchecked ((uint) Length))
83 throw new ArgumentOutOfRangeException ("index");
84 return GetValueImpl (index);
87 if (unchecked ((uint) index) >= unchecked ((uint) Length))
88 throw new ArgumentOutOfRangeException ("index");
89 SetValueImpl (value, index);
93 int IList.Add (object value)
95 throw new NotSupportedException ();
100 Array.Clear (this, this.GetLowerBound (0), this.Length);
103 bool IList.Contains (object value)
106 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
108 int length = this.Length;
109 for (int i = 0; i < length; i++) {
110 if (Object.Equals (value, this.GetValueImpl (i)))
116 int IList.IndexOf (object value)
119 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
121 int length = this.Length;
122 for (int i = 0; i < length; i++) {
123 if (Object.Equals (value, this.GetValueImpl (i)))
124 // array index may not be zero-based.
126 return i + this.GetLowerBound (0);
131 // lower bound may be MinValue
132 retVal = this.GetLowerBound (0) - 1;
138 void IList.Insert (int index, object value)
140 throw new NotSupportedException ();
143 void IList.Remove (object value)
145 throw new NotSupportedException ();
148 void IList.RemoveAt (int index)
150 throw new NotSupportedException ();
153 // InternalCall Methods
154 [MethodImplAttribute (MethodImplOptions.InternalCall)]
155 private extern int GetRank ();
157 [MethodImplAttribute (MethodImplOptions.InternalCall)]
158 public extern int GetLength (int dimension);
162 public long GetLongLength (int dimension)
164 return GetLength (dimension);
168 [MethodImplAttribute (MethodImplOptions.InternalCall)]
169 public extern int GetLowerBound (int dimension);
171 [MethodImplAttribute (MethodImplOptions.InternalCall)]
172 public extern object GetValue (int[] indices);
174 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175 public extern void SetValue (object value, int[] indices);
177 // CAUTION! No bounds checking!
178 [MethodImplAttribute (MethodImplOptions.InternalCall)]
179 internal extern object GetValueImpl (int pos);
181 // CAUTION! No bounds checking!
182 [MethodImplAttribute (MethodImplOptions.InternalCall)]
183 internal extern void SetValueImpl (object value, int pos);
185 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186 internal extern static bool FastCopy (Array source, int source_idx, Array dest, int dest_idx, int length);
188 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189 internal extern static Array CreateInstanceImpl (Type elementType, int[] lengths, int[] bounds);
192 int ICollection.Count {
198 public virtual bool IsSynchronized {
204 public virtual object SyncRoot {
210 public virtual bool IsFixedSize {
216 public virtual bool IsReadOnly {
222 public virtual IEnumerator GetEnumerator ()
224 return new SimpleEnumerator (this);
227 public int GetUpperBound (int dimension)
229 return GetLowerBound (dimension) + GetLength (dimension) - 1;
232 public object GetValue (int index)
235 throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
236 if (index < GetLowerBound (0) || index > GetUpperBound (0))
237 throw new IndexOutOfRangeException (Locale.GetText (
238 "Index has to be between upper and lower bound of the array."));
240 return GetValueImpl (index - GetLowerBound (0));
243 public object GetValue (int index1, int index2)
245 int[] ind = {index1, index2};
246 return GetValue (ind);
249 public object GetValue (int index1, int index2, int index3)
251 int[] ind = {index1, index2, index3};
252 return GetValue (ind);
257 public object GetValue (long index)
259 if (index < 0 || index > Int32.MaxValue)
260 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
261 "Value must be >= 0 and <= Int32.MaxValue."));
263 return GetValue ((int) index);
267 public object GetValue (long index1, long index2)
269 if (index1 < 0 || index1 > Int32.MaxValue)
270 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
271 "Value must be >= 0 and <= Int32.MaxValue."));
273 if (index2 < 0 || index2 > Int32.MaxValue)
274 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
275 "Value must be >= 0 and <= Int32.MaxValue."));
277 return GetValue ((int) index1, (int) index2);
281 public object GetValue (long index1, long index2, long index3)
283 if (index1 < 0 || index1 > Int32.MaxValue)
284 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
285 "Value must be >= 0 and <= Int32.MaxValue."));
287 if (index2 < 0 || index2 > Int32.MaxValue)
288 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
289 "Value must be >= 0 and <= Int32.MaxValue."));
291 if (index3 < 0 || index3 > Int32.MaxValue)
292 throw new ArgumentOutOfRangeException ("index3", Locale.GetText (
293 "Value must be >= 0 and <= Int32.MaxValue."));
295 return GetValue ((int) index1, (int) index2, (int) index3);
299 public void SetValue (object value, long index)
301 if (index < 0 || index > Int32.MaxValue)
302 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
303 "Value must be >= 0 and <= Int32.MaxValue."));
305 SetValue (value, (int) index);
309 public void SetValue (object value, long index1, long index2)
311 if (index1 < 0 || index1 > Int32.MaxValue)
312 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
313 "Value must be >= 0 and <= Int32.MaxValue."));
315 if (index2 < 0 || index2 > Int32.MaxValue)
316 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
317 "Value must be >= 0 and <= Int32.MaxValue."));
319 int[] ind = {(int) index1, (int) index2};
320 SetValue (value, ind);
324 public void SetValue (object value, long index1, long index2, long index3)
326 if (index1 < 0 || index1 > Int32.MaxValue)
327 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
328 "Value must be >= 0 and <= Int32.MaxValue."));
330 if (index2 < 0 || index2 > Int32.MaxValue)
331 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
332 "Value must be >= 0 and <= Int32.MaxValue."));
334 if (index3 < 0 || index3 > Int32.MaxValue)
335 throw new ArgumentOutOfRangeException ("index3", Locale.GetText (
336 "Value must be >= 0 and <= Int32.MaxValue."));
338 int[] ind = {(int) index1, (int) index2, (int) index3};
339 SetValue (value, ind);
343 // // This function is currently unused, but just in case we need it later on ... */
344 // internal int IndexToPos (int[] idxs)
347 // throw new ArgumentNullException ("idxs");
349 // if ((idxs.Rank != 1) || (idxs.Length != Rank))
350 // throw new ArgumentException ();
352 // if ((idxs [0] < GetLowerBound (0)) || (idxs [0] > GetUpperBound (0)))
353 // throw new IndexOutOfRangeException();
355 // int pos = idxs [0] - GetLowerBound (0);
356 // for (int i = 1; i < Rank; i++) {
357 // if ((idxs [i] < GetLowerBound (i)) || (idxs [i] > GetUpperBound (i)))
358 // throw new IndexOutOfRangeException();
360 // pos *= GetLength (i);
361 // pos += idxs [i] - GetLowerBound (i);
367 public void SetValue (object value, int index)
370 throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
371 if (index < GetLowerBound (0) || index > GetUpperBound (0))
372 throw new IndexOutOfRangeException (Locale.GetText (
373 "Index has to be >= lower bound and <= upper bound of the array."));
375 SetValueImpl (value, index - GetLowerBound (0));
378 public void SetValue (object value, int index1, int index2)
380 int[] ind = {index1, index2};
381 SetValue (value, ind);
384 public void SetValue (object value, int index1, int index2, int index3)
386 int[] ind = {index1, index2, index3};
387 SetValue (value, ind);
390 public static Array CreateInstance (Type elementType, int length)
392 int[] lengths = {length};
395 return CreateInstanceImpl (elementType, lengths, bounds);
398 public static Array CreateInstance (Type elementType, int length1, int length2)
400 int[] lengths = {length1, length2};
403 return CreateInstanceImpl (elementType, lengths, bounds);
406 public static Array CreateInstance (Type elementType, int length1, int length2, int length3)
408 int[] lengths = {length1, length2, length3};
411 return CreateInstanceImpl (elementType, lengths, bounds);
414 public static Array CreateInstance (Type elementType, int[] lengths)
416 if (elementType == null)
417 throw new ArgumentNullException ("elementType");
419 throw new ArgumentNullException ("lengths");
421 if (lengths.Length > 255)
422 throw new TypeLoadException ();
426 return CreateInstanceImpl (elementType, lengths, bounds);
429 public static Array CreateInstance (Type elementType, int[] lengths, int [] bounds)
431 if (elementType == null)
432 throw new ArgumentNullException ("elementType");
434 throw new ArgumentNullException ("lengths");
436 throw new ArgumentNullException ("bounds");
438 if (lengths.Length < 1)
439 throw new ArgumentException (Locale.GetText ("Arrays must contain >= 1 elements."));
441 if (lengths.Length != bounds.Length)
442 throw new ArgumentException (Locale.GetText ("Arrays must be of same size."));
444 for (int j = 0; j < bounds.Length; j ++) {
446 throw new ArgumentOutOfRangeException ("lengths", Locale.GetText (
447 "Each value has to be >= 0."));
448 if ((long)bounds [j] + (long)lengths [j] > (long)Int32.MaxValue)
449 throw new ArgumentOutOfRangeException ("lengths", Locale.GetText (
450 "Length + bound must not exceed Int32.MaxValue."));
453 if (lengths.Length > 255)
454 throw new TypeLoadException ();
456 return CreateInstanceImpl (elementType, lengths, bounds);
460 static int [] GetIntArray (long [] values)
462 int len = values.Length;
463 int [] ints = new int [len];
464 for (int i = 0; i < len; i++) {
465 long current = values [i];
466 if (current < 0 || current > (long) Int32.MaxValue)
467 throw new ArgumentOutOfRangeException ("values", Locale.GetText (
468 "Each value has to be >= 0 and <= Int32.MaxValue."));
470 ints [i] = (int) current;
475 public static Array CreateInstance (Type elementType, params long [] lengths)
477 if (lengths == null) {
478 // LAMESPEC: Docs say we should throw a ArgumentNull, but .NET
479 // 1.1 actually throws a NullReference.
480 throw new NullReferenceException (Locale.GetText ("'lengths' cannot be null."));
482 return CreateInstance (elementType, GetIntArray (lengths));
486 public object GetValue (long [] indices)
488 if (indices == null) {
489 // LAMESPEC: Docs say we should throw a ArgumentNull, but .NET
490 // 1.1 actually throws a NullReference.
491 throw new NullReferenceException (Locale.GetText ("'indices' cannot be null."));
493 return GetValue (GetIntArray (indices));
497 public void SetValue (object value, long [] indices)
499 if (indices == null) {
500 // LAMESPEC: Docs say we should throw a ArgumentNull, but .NET
501 // 1.1 actually throws a NullReference.
502 throw new NullReferenceException (Locale.GetText ("'indices' cannot be null."));
504 SetValue (value, GetIntArray (indices));
508 public static int BinarySearch (Array array, object value)
511 throw new ArgumentNullException ("array");
517 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
519 if (!(value is IComparable))
520 throw new ArgumentException (Locale.GetText ("value does not support IComparable."));
522 return DoBinarySearch (array, array.GetLowerBound (0), array.GetLength (0), value, null);
525 public static int BinarySearch (Array array, object value, IComparer comparer)
528 throw new ArgumentNullException ("array");
531 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
533 if ((comparer == null) && (value != null) && !(value is IComparable))
534 throw new ArgumentException (Locale.GetText (
535 "comparer is null and value does not support IComparable."));
537 return DoBinarySearch (array, array.GetLowerBound (0), array.GetLength (0), value, comparer);
540 public static int BinarySearch (Array array, int index, int length, object value)
543 throw new ArgumentNullException ("array");
546 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
548 if (index < array.GetLowerBound (0))
549 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
550 "index is less than the lower bound of array."));
552 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
553 "Value has to be >= 0."));
554 // re-ordered to avoid possible integer overflow
555 if (index > array.GetLowerBound (0) + array.GetLength (0) - length)
556 throw new ArgumentException (Locale.GetText (
557 "index and length do not specify a valid range in array."));
558 if ((value != null) && (!(value is IComparable)))
559 throw new ArgumentException (Locale.GetText (
560 "value does not support IComparable"));
562 return DoBinarySearch (array, index, length, value, null);
565 public static int BinarySearch (Array array, int index, int length, object value, IComparer comparer)
568 throw new ArgumentNullException ("array");
571 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
573 if (index < array.GetLowerBound (0))
574 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
575 "index is less than the lower bound of array."));
577 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
578 "Value has to be >= 0."));
579 // re-ordered to avoid possible integer overflow
580 if (index > array.GetLowerBound (0) + array.GetLength (0) - length)
581 throw new ArgumentException (Locale.GetText (
582 "index and length do not specify a valid range in array."));
584 if ((comparer == null) && (value != null) && !(value is IComparable))
585 throw new ArgumentException (Locale.GetText (
586 "comparer is null and value does not support IComparable."));
588 return DoBinarySearch (array, index, length, value, comparer);
591 static int DoBinarySearch (Array array, int index, int length, object value, IComparer comparer)
593 // cache this in case we need it
594 if (comparer == null)
595 comparer = Comparer.Default;
598 // Comment from Tum (tum@veridicus.com):
599 // *Must* start at index + length - 1 to pass rotor test co2460binarysearch_iioi
600 int iMax = index + length - 1;
603 while (iMin <= iMax) {
604 int iMid = (iMin + iMax) / 2;
605 object elt = array.GetValueImpl (iMid);
607 iCmp = comparer.Compare (value, elt);
614 iMin = iMid + 1; // compensate for the rounding down
617 catch (Exception e) {
618 throw new InvalidOperationException (Locale.GetText ("Comparer threw an exception."), e);
624 public static void Clear (Array array, int index, int length)
627 throw new ArgumentNullException ("array");
629 throw new ArgumentOutOfRangeException ("length < 0");
631 int low = array.GetLowerBound (0);
633 throw new IndexOutOfRangeException ("index < lower bound");
636 // re-ordered to avoid possible integer overflow
637 if (index > array.Length - length)
638 throw new IndexOutOfRangeException ("index + length > size");
640 ClearInternal (array, index, length);
643 [MethodImplAttribute (MethodImplOptions.InternalCall)]
644 static extern void ClearInternal (Array a, int index, int count);
646 [MethodImplAttribute (MethodImplOptions.InternalCall)]
647 public virtual extern object Clone ();
649 public static void Copy (Array sourceArray, Array destinationArray, int length)
651 // need these checks here because we are going to use
652 // GetLowerBound() on source and dest.
653 if (sourceArray == null)
654 throw new ArgumentNullException ("sourceArray");
656 if (destinationArray == null)
657 throw new ArgumentNullException ("destinationArray");
659 Copy (sourceArray, sourceArray.GetLowerBound (0), destinationArray,
660 destinationArray.GetLowerBound (0), length);
663 public static void Copy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
665 if (sourceArray == null)
666 throw new ArgumentNullException ("sourceArray");
668 if (destinationArray == null)
669 throw new ArgumentNullException ("destinationArray");
672 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
673 "Value has to be >= 0."));;
676 throw new ArgumentOutOfRangeException ("sourceIndex", Locale.GetText (
677 "Value has to be >= 0."));;
679 if (destinationIndex < 0)
680 throw new ArgumentOutOfRangeException ("destinationIndex", Locale.GetText (
681 "Value has to be >= 0."));;
683 if (FastCopy (sourceArray, sourceIndex, destinationArray, destinationIndex, length))
686 int source_pos = sourceIndex - sourceArray.GetLowerBound (0);
687 int dest_pos = destinationIndex - destinationArray.GetLowerBound (0);
689 // re-ordered to avoid possible integer overflow
690 if (source_pos > sourceArray.Length - length || dest_pos > destinationArray.Length - length)
691 throw new ArgumentException ("length");
693 if (sourceArray.Rank != destinationArray.Rank)
694 throw new RankException (Locale.GetText ("Arrays must be of same size."));
696 Type src_type = sourceArray.GetType ().GetElementType ();
697 Type dst_type = destinationArray.GetType ().GetElementType ();
699 if (!Object.ReferenceEquals (sourceArray, destinationArray) || source_pos > dest_pos) {
700 for (int i = 0; i < length; i++) {
701 Object srcval = sourceArray.GetValueImpl (source_pos + i);
704 destinationArray.SetValueImpl (srcval, dest_pos + i);
706 if ((dst_type.IsValueType || dst_type.Equals (typeof (String))) &&
707 (src_type.Equals (typeof (Object))))
708 throw new InvalidCastException ();
710 throw new ArrayTypeMismatchException (String.Format (Locale.GetText (
711 "(Types: source={0}; target={1})"), src_type.FullName, dst_type.FullName));
716 for (int i = length - 1; i >= 0; i--) {
717 Object srcval = sourceArray.GetValueImpl (source_pos + i);
720 destinationArray.SetValueImpl (srcval, dest_pos + i);
722 if ((dst_type.IsValueType || dst_type.Equals (typeof (String))) &&
723 (src_type.Equals (typeof (Object))))
724 throw new InvalidCastException ();
726 throw new ArrayTypeMismatchException (String.Format (Locale.GetText (
727 "(Types: source={0}; target={1})"), src_type.FullName, dst_type.FullName));
734 public static void Copy (Array sourceArray, long sourceIndex, Array destinationArray,
735 long destinationIndex, long length)
737 if (sourceArray == null)
738 throw new ArgumentNullException ("sourceArray");
740 if (destinationArray == null)
741 throw new ArgumentNullException ("destinationArray");
743 if (sourceIndex < Int32.MinValue || sourceIndex > Int32.MaxValue)
744 throw new ArgumentOutOfRangeException ("sourceIndex",
745 Locale.GetText ("Must be in the Int32 range."));
747 if (destinationIndex < Int32.MinValue || destinationIndex > Int32.MaxValue)
748 throw new ArgumentOutOfRangeException ("destinationIndex",
749 Locale.GetText ("Must be in the Int32 range."));
751 if (length < 0 || length > Int32.MaxValue)
752 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
753 "Value must be >= 0 and <= Int32.MaxValue."));
755 Copy (sourceArray, (int) sourceIndex, destinationArray, (int) destinationIndex, (int) length);
758 public static void Copy (Array sourceArray, Array destinationArray, long length)
760 if (length < 0 || length > Int32.MaxValue)
761 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
762 "Value must be >= 0 and <= Int32.MaxValue."));
764 Copy (sourceArray, destinationArray, (int) length);
768 public static int IndexOf (Array array, object value)
771 throw new ArgumentNullException ("array");
773 return IndexOf (array, value, 0, array.Length);
776 public static int IndexOf (Array array, object value, int startIndex)
779 throw new ArgumentNullException ("array");
781 return IndexOf (array, value, startIndex, array.Length - startIndex);
784 public static int IndexOf (Array array, object value, int startIndex, int count)
787 throw new ArgumentNullException ("array");
790 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
792 // re-ordered to avoid possible integer overflow
793 if (count < 0 || startIndex < array.GetLowerBound (0) || startIndex - 1 > array.GetUpperBound (0) - count)
794 throw new ArgumentOutOfRangeException ();
796 int max = startIndex + count;
797 for (int i = startIndex; i < max; i++) {
798 if (Object.Equals (array.GetValueImpl (i), value))
802 return array.GetLowerBound (0) - 1;
806 public void Initialize()
808 //FIXME: We would like to find a compiler that uses
809 // this method. It looks like this method do nothing
810 // in C# so no exception is trown by the moment.
813 public static int LastIndexOf (Array array, object value)
816 throw new ArgumentNullException ("array");
818 return LastIndexOf (array, value, array.Length - 1);
821 public static int LastIndexOf (Array array, object value, int startIndex)
824 throw new ArgumentNullException ("array");
826 return LastIndexOf (array, value, startIndex, startIndex - array.GetLowerBound (0) + 1);
829 public static int LastIndexOf (Array array, object value, int startIndex, int count)
832 throw new ArgumentNullException ("array");
835 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
837 if (count < 0 || startIndex < array.GetLowerBound (0) ||
838 startIndex > array.GetUpperBound (0) || startIndex - count + 1 < array.GetLowerBound (0))
839 throw new ArgumentOutOfRangeException ();
841 for (int i = startIndex; i >= startIndex - count + 1; i--) {
842 if (Object.Equals (array.GetValueImpl (i), value))
846 return array.GetLowerBound (0) - 1;
849 public static void Reverse (Array array)
852 throw new ArgumentNullException ("array");
854 Reverse (array, array.GetLowerBound (0), array.GetLength (0));
857 public static void Reverse (Array array, int index, int length)
860 throw new ArgumentNullException ("array");
863 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
865 if (index < array.GetLowerBound (0) || length < 0)
866 throw new ArgumentOutOfRangeException ();
868 // re-ordered to avoid possible integer overflow
869 if (index > array.GetUpperBound (0) + 1 - length)
870 throw new ArgumentException ();
872 for (int i = 0; i < length / 2; i++)
876 tmp = array.GetValueImpl (index + i);
877 array.SetValueImpl (array.GetValueImpl (index + length - i - 1), index + i);
878 array.SetValueImpl (tmp, index + length - i - 1);
882 public static void Sort (Array array)
885 throw new ArgumentNullException ("array");
887 Sort (array, null, array.GetLowerBound (0), array.GetLength (0), null);
890 public static void Sort (Array keys, Array items)
893 throw new ArgumentNullException ("keys");
895 Sort (keys, items, keys.GetLowerBound (0), keys.GetLength (0), null);
898 public static void Sort (Array array, IComparer comparer)
901 throw new ArgumentNullException ("array");
903 Sort (array, null, array.GetLowerBound (0), array.GetLength (0), comparer);
906 public static void Sort (Array array, int index, int length)
908 Sort (array, null, index, length, null);
911 public static void Sort (Array keys, Array items, IComparer comparer)
914 throw new ArgumentNullException ("keys");
916 Sort (keys, items, keys.GetLowerBound (0), keys.GetLength (0), comparer);
919 public static void Sort (Array keys, Array items, int index, int length)
921 Sort (keys, items, index, length, null);
924 public static void Sort (Array array, int index, int length, IComparer comparer)
926 Sort (array, null, index, length, comparer);
929 public static void Sort (Array keys, Array items, int index, int length, IComparer comparer)
932 throw new ArgumentNullException ("keys");
934 if (keys.Rank > 1 || (items != null && items.Rank > 1))
935 throw new RankException ();
937 if (items != null && keys.GetLowerBound (0) != items.GetLowerBound (0))
938 throw new ArgumentException ();
940 if (index < keys.GetLowerBound (0))
941 throw new ArgumentOutOfRangeException ("index");
944 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
945 "Value has to be >= 0."));
947 if (keys.Length - (index + keys.GetLowerBound (0)) < length
948 || (items != null && index > items.Length - length))
949 throw new ArgumentException ();
953 int high0 = index + length - 1;
954 qsort (keys, items, low0, high0, comparer);
956 catch (Exception e) {
957 throw new InvalidOperationException (Locale.GetText ("The comparer threw an exception."), e);
961 private static void qsort (Array keys, Array items, int low0, int high0, IComparer comparer)
969 object objPivot = keys.GetValueImpl ((low + high) / 2);
971 while (low <= high) {
973 while (low < high0 && compare (keys.GetValueImpl (low), objPivot, comparer) < 0)
975 while (high > low0 && compare (objPivot, keys.GetValueImpl (high), comparer) < 0)
979 swap (keys, items, low, high);
986 qsort (keys, items, low0, high, comparer);
988 qsort (keys, items, low, high0, comparer);
991 private static void swap (Array keys, Array items, int i, int j)
995 tmp = keys.GetValueImpl (i);
996 keys.SetValueImpl (keys.GetValue (j), i);
997 keys.SetValueImpl (tmp, j);
1000 tmp = items.GetValueImpl (i);
1001 items.SetValueImpl (items.GetValueImpl (j), i);
1002 items.SetValueImpl (tmp, j);
1006 private static int compare (object value1, object value2, IComparer comparer)
1009 return value2 == null ? 0 : -1;
1010 else if (value2 == null)
1012 else if (comparer == null)
1013 return ((IComparable) value1).CompareTo (value2);
1015 return comparer.Compare (value1, value2);
1018 public virtual void CopyTo (Array array, int index)
1021 throw new ArgumentNullException ("array");
1023 // The order of these exception checks may look strange,
1024 // but that's how the microsoft runtime does it.
1026 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
1027 if (index + this.GetLength (0) > array.GetLowerBound (0) + array.GetLength (0))
1028 throw new ArgumentException ();
1030 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
1032 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
1033 "Value has to be >= 0."));
1035 Copy (this, this.GetLowerBound (0), array, index, this.GetLength (0));
1039 [ComVisible (false)]
1040 public virtual void CopyTo (Array array, long index)
1042 if (index < 0 || index > Int32.MaxValue)
1043 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
1044 "Value must be >= 0 and <= Int32.MaxValue."));
1046 CopyTo (array, (int) index);
1050 internal class SimpleEnumerator : IEnumerator, ICloneable
1056 public SimpleEnumerator (Array arrayToEnumerate)
1058 this.enumeratee = arrayToEnumerate;
1059 this.currentpos = -1;
1060 this.length = arrayToEnumerate.Length;
1063 public object Current {
1065 // Exception messages based on MS implementation
1066 if (currentpos < 0 )
1067 throw new InvalidOperationException (Locale.GetText (
1068 "Enumeration has not started."));
1069 if (currentpos >= length)
1070 throw new InvalidOperationException (Locale.GetText (
1071 "Enumeration has already ended"));
1072 // Current should not increase the position. So no ++ over here.
1073 return enumeratee.GetValueImpl (currentpos);
1077 public bool MoveNext()
1079 //The docs say Current should throw an exception if last
1080 //call to MoveNext returned false. This means currentpos
1081 //should be set to length when returning false.
1082 if (currentpos < length)
1084 if(currentpos < length)
1095 public object Clone ()
1097 return MemberwiseClone ();
1101 [CLSCompliant (false)]
1102 public static void Resize <T> (ref T [] arr, int sz) {
1104 throw new ArgumentOutOfRangeException ();
1111 if (arr.Length == sz)
1114 T [] a = new T [sz];
1115 Array.Copy (arr, a, Math.Min (sz, arr.Length));
1119 [CLSCompliant (false)]
1120 public static bool TrueForAll <T> (T [] array, Predicate <T> p)
1122 if (array == null || p == null)
1123 throw new ArgumentNullException ();
1125 foreach (T t in array)
1131 [CLSCompliant (false)]
1132 public static void ForEach <T> (T [] array, Action <T> a)
1134 if (array == null || a == null)
1135 throw new ArgumentNullException ();
1137 foreach (T t in array)
1141 [CLSCompliant (false)]
1142 public static U [] ConvertAll <T, U> (T [] s, Converter <T, U> c)
1144 if (s == null || c == null)
1145 throw new ArgumentNullException ();
1147 U [] r = new U [s.Length];
1149 for (int i = 0; i < s.Length; i ++)
1155 [CLSCompliant (false)]
1156 public static int FindLastIndex <T> (T [] a, Predicate <T> c)
1159 throw new ArgumentNullException ();
1161 return FindLastIndex <T> (a, 0, a.Length, c);
1164 [CLSCompliant (false)]
1165 public static int FindLastIndex <T> (T [] a, int idx, Predicate <T> c)
1168 throw new ArgumentNullException ();
1170 return FindLastIndex <T> (a, idx, a.Length - idx, c);
1173 [CLSCompliant (false)]
1174 public static int FindLastIndex <T> (T [] a, int idx, int cnt, Predicate <T> c)
1176 if (a == null || c == null)
1177 throw new ArgumentNullException ();
1179 if (idx > a.Length || idx + cnt > a.Length)
1180 throw new ArgumentOutOfRangeException ();
1182 for (int i = idx + cnt - 1; i >= idx; i --)
1189 [CLSCompliant (false)]
1190 public static int FindIndex <T> (T [] a, Predicate <T> c)
1193 throw new ArgumentNullException ();
1195 return FindIndex <T> (a, 0, a.Length, c);
1198 [CLSCompliant (false)]
1199 public static int FindIndex <T> (T [] a, int idx, Predicate <T> c)
1202 throw new ArgumentNullException ();
1204 return FindIndex <T> (a, idx, a.Length - idx, c);
1207 [CLSCompliant (false)]
1208 public static int FindIndex <T> (T [] a, int idx, int cnt, Predicate <T> c)
1210 if (a == null || c == null)
1211 throw new ArgumentNullException ();
1213 if (idx > a.Length || idx + cnt > a.Length)
1214 throw new ArgumentOutOfRangeException ();
1216 for (int i = idx; i < idx + cnt; i ++)
1223 [CLSCompliant (false)]
1224 public static int BinarySearch <T> (T [] a, T x)
1227 throw new ArgumentNullException ("a");
1229 return BinarySearch <T> (a, 0, a.Length, x, null);
1232 [CLSCompliant (false)]
1233 public static int BinarySearch <T> (T [] a, T x, IComparer <T> c)
1236 throw new ArgumentNullException ("a");
1238 return BinarySearch <T> (a, 0, a.Length, x, c);
1241 [CLSCompliant (false)]
1242 public static int BinarySearch <T> (T [] a, int offset, int length, T x)
1244 return BinarySearch <T> (a, offset, length, x, null);
1247 [CLSCompliant (false)]
1248 public static int BinarySearch <T> (T [] array, int index, int length, T x, IComparer <T> c)
1251 throw new ArgumentNullException ("array");
1253 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
1254 "index is less than the lower bound of array."));
1256 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
1257 "Value has to be >= 0."));
1258 // re-ordered to avoid possible integer overflow
1259 if (index > array.Length - length)
1260 throw new ArgumentException (Locale.GetText (
1261 "index and length do not specify a valid range in array."));
1263 c = Comparer <T>.Default;
1266 int iMax = index + length - 1;
1269 while (iMin <= iMax) {
1270 int iMid = (iMin + iMax) / 2;
1271 iCmp = c.Compare (x, array [iMid]);
1278 iMin = iMid + 1; // compensate for the rounding down
1281 catch (Exception e) {
1282 throw new InvalidOperationException (Locale.GetText ("Comparer threw an exception."), e);
1288 [CLSCompliant (false)]
1289 public static int IndexOf <T> (T [] array, T value)
1292 throw new ArgumentNullException ("array");
1294 return IndexOf (array, value, 0, array.Length);
1297 [CLSCompliant (false)]
1298 public static int IndexOf <T> (T [] array, T value, int startIndex)
1301 throw new ArgumentNullException ("array");
1303 return IndexOf (array, value, startIndex, array.Length - startIndex);
1306 [CLSCompliant (false)]
1307 public static int IndexOf <T> (T [] array, T value, int startIndex, int count)
1310 throw new ArgumentNullException ("array");
1312 // re-ordered to avoid possible integer overflow
1313 if (count < 0 || startIndex < array.GetLowerBound (0) || startIndex - 1 > array.GetUpperBound (0) - count)
1314 throw new ArgumentOutOfRangeException ();
1316 int max = startIndex + count;
1317 for (int i = startIndex; i < max; i++) {
1318 if (Object.Equals (array [i], value))
1319 if (Object.Equals (array [i], value))
1326 [CLSCompliant (false)]
1327 public static int LastIndexOf <T> (T [] array, T value)
1330 throw new ArgumentNullException ("array");
1332 return LastIndexOf (array, value, array.Length - 1);
1335 [CLSCompliant (false)]
1336 public static int LastIndexOf <T> (T [] array, T value, int startIndex)
1339 throw new ArgumentNullException ("array");
1341 return LastIndexOf (array, value, startIndex, startIndex + 1);
1344 [CLSCompliant (false)]
1345 public static int LastIndexOf <T> (T [] array, T value, int startIndex, int count)
1348 throw new ArgumentNullException ("array");
1350 if (count < 0 || startIndex > array.Length || startIndex - count + 1 < 0)
1351 throw new ArgumentOutOfRangeException ();
1353 for (int i = startIndex; i >= startIndex - count + 1; i--) {
1354 if (Object.Equals (array [i], value))
1361 [CLSCompliant (false)]
1362 public static T [] FindAll <T> (T [] a, Predicate <T> p)
1364 if (a == null || p == null)
1365 throw new ArgumentNullException ();
1368 T [] d = new T [a.Length];
1373 Resize <T> (ref a, pos);
1377 [CLSCompliant (false)]
1378 public static bool Exists <T> (T [] a, Predicate <T> p)
1380 if (a == null || p == null)
1381 throw new ArgumentNullException ();
1389 [CLSCompliant (false)]
1391 public static IList<T> AsReadOnly<T> (T[] array)
1394 throw new ArgumentNullException ("array");
1395 return new ReadOnlyArray <T> (array);
1399 [CLSCompliant (false)]
1400 public static Nullable <T> Find <T> (T [] a, Predicate <T> p)
1402 if (a == null || p == null)
1403 throw new ArgumentNullException ();
1407 return new Nullable <T> (t);
1409 return default (Nullable <T>);
1412 [CLSCompliant (false)]
1413 public static Nullable <T> FindLast <T> (T [] a, Predicate <T> p)
1415 if (a == null || p == null)
1416 throw new ArgumentNullException ();
1418 for (int i = a.Length - 1; i >= 0; i--)
1420 return new Nullable <T> (a [i]);
1422 return default (Nullable <T>);
1426 // Fixme: wtf is constrained about this
1427 public static void ConstrainedCopy (Array s, int s_i, Array d, int d_i, int c)
1429 Copy (s, s_i, d, d_i, c);
1437 internal struct ReadOnlyArrayEnumerator <T> : IEnumerator <T> {
1438 const int NOT_STARTED = -2;
1440 // this MUST be -1, because we depend on it in move next.
1441 // we just decr the size, so, 0 - 1 == FINISHED
1442 const int FINISHED = -1;
1444 ReadOnlyArray <T> array;
1447 internal ReadOnlyArrayEnumerator (ReadOnlyArray<T> array)
1453 public void Dispose ()
1458 public bool MoveNext ()
1460 if (idx == NOT_STARTED)
1463 return idx != FINISHED && -- idx != FINISHED;
1469 throw new InvalidOperationException ();
1471 return array [array.Count - 1 - idx];
1476 internal class ReadOnlyArray <T> : ICollection <T>, IList <T>, IEnumerable <T>
1480 internal ReadOnlyArray (T[] array) {
1484 // ICollection<T> interface
1491 public bool IsReadOnly {
1497 public void Add (T item) {
1498 throw new NotSupportedException ("Collection is read-only");
1501 public bool Remove (T item) {
1502 throw new NotSupportedException ("Collection is read-only");
1505 public void Clear () {
1506 throw new NotSupportedException ("Collection is read-only");
1509 public void CopyTo (T[] array, int index) {
1510 arr.CopyTo (array, index);
1513 public bool Contains (T item) {
1514 int length = arr.Length;
1515 for (int i = 0; i < length; i++) {
1516 if (arr [i] == item)
1522 // IList<T> interface
1523 public T this [int index] {
1525 if (unchecked ((uint) index) >= unchecked ((uint) arr.Length))
1526 throw new ArgumentOutOfRangeException ("index");
1530 if (unchecked ((uint) index) >= unchecked ((uint) arr.Length))
1531 throw new ArgumentOutOfRangeException ("index");
1532 arr [index] = value;
1536 public void Insert (int index, T item) {
1537 throw new NotSupportedException ("Collection is read-only");
1540 public void RemoveAt (int index) {
1541 throw new NotSupportedException ("Collection is read-only");
1544 public int IndexOf (T item) {
1545 int length = arr.Length;
1546 for (int i = 0; i < length; i++) {
1547 if (arr [i] == item)
1553 // IEnumerable<T> interface
1554 public IEnumerator<T> GetEnumerator () {
1555 return new ReadOnlyArrayEnumerator <T> (this);