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
11 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
38 using System.Collections.Generic;
39 using System.Collections.ObjectModel;
40 using System.Runtime.ConstrainedExecution;
47 [MonoTODO ("We are doing way to many double/triple exception checks for the overloaded functions")]
48 [MonoTODO ("Sort overloads parameter checks are VERY inconsistent")]
49 public abstract class Array : ICloneable, ICollection, IList, IEnumerable
59 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
62 int length = this.GetLength (0);
64 for (int i = 1; i < this.Rank; i++) {
65 length *= this.GetLength (i);
73 public long LongLength {
75 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
77 get { return Length; }
83 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
86 return this.GetRank ();
91 object IList.this [int index] {
93 if (unchecked ((uint) index) >= unchecked ((uint) Length))
94 throw new ArgumentOutOfRangeException ("index");
95 return GetValueImpl (index);
98 if (unchecked ((uint) index) >= unchecked ((uint) Length))
99 throw new ArgumentOutOfRangeException ("index");
100 SetValueImpl (value, index);
104 int IList.Add (object value)
106 throw new NotSupportedException ();
111 Array.Clear (this, this.GetLowerBound (0), this.Length);
114 bool IList.Contains (object value)
117 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
119 int length = this.Length;
120 for (int i = 0; i < length; i++) {
121 if (Object.Equals (value, this.GetValueImpl (i)))
128 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
130 int IList.IndexOf (object value)
133 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
135 int length = this.Length;
136 for (int i = 0; i < length; i++) {
137 if (Object.Equals (value, this.GetValueImpl (i)))
138 // array index may not be zero-based.
140 return i + this.GetLowerBound (0);
145 // lower bound may be MinValue
146 retVal = this.GetLowerBound (0) - 1;
152 void IList.Insert (int index, object value)
154 throw new NotSupportedException ();
157 void IList.Remove (object value)
159 throw new NotSupportedException ();
162 void IList.RemoveAt (int index)
164 throw new NotSupportedException ();
167 // InternalCall Methods
168 [MethodImplAttribute (MethodImplOptions.InternalCall)]
169 private extern int GetRank ();
171 [MethodImplAttribute (MethodImplOptions.InternalCall)]
172 public extern int GetLength (int dimension);
176 public long GetLongLength (int dimension)
178 return GetLength (dimension);
183 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
185 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186 public extern int GetLowerBound (int dimension);
188 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189 public extern object GetValue (params int[] indices);
191 [MethodImplAttribute (MethodImplOptions.InternalCall)]
192 public extern void SetValue (object value, params int[] indices);
194 // CAUTION! No bounds checking!
195 [MethodImplAttribute (MethodImplOptions.InternalCall)]
196 internal extern object GetValueImpl (int pos);
198 // CAUTION! No bounds checking!
199 [MethodImplAttribute (MethodImplOptions.InternalCall)]
200 internal extern void SetValueImpl (object value, int pos);
202 [MethodImplAttribute (MethodImplOptions.InternalCall)]
203 internal extern static bool FastCopy (Array source, int source_idx, Array dest, int dest_idx, int length);
205 [MethodImplAttribute (MethodImplOptions.InternalCall)]
206 internal extern static Array CreateInstanceImpl (Type elementType, int[] lengths, int[] bounds);
209 int ICollection.Count {
219 bool IsSynchronized {
259 IEnumerator GetEnumerator ()
261 return new SimpleEnumerator (this);
265 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
267 public int GetUpperBound (int dimension)
269 return GetLowerBound (dimension) + GetLength (dimension) - 1;
272 public object GetValue (int index)
275 throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
276 if (index < GetLowerBound (0) || index > GetUpperBound (0))
277 throw new IndexOutOfRangeException (Locale.GetText (
278 "Index has to be between upper and lower bound of the array."));
280 return GetValueImpl (index - GetLowerBound (0));
283 public object GetValue (int index1, int index2)
285 int[] ind = {index1, index2};
286 return GetValue (ind);
289 public object GetValue (int index1, int index2, int index3)
291 int[] ind = {index1, index2, index3};
292 return GetValue (ind);
297 public object GetValue (long index)
299 if (index < 0 || index > Int32.MaxValue)
300 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
301 "Value must be >= 0 and <= Int32.MaxValue."));
303 return GetValue ((int) index);
307 public object GetValue (long index1, long index2)
309 if (index1 < 0 || index1 > Int32.MaxValue)
310 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
311 "Value must be >= 0 and <= Int32.MaxValue."));
313 if (index2 < 0 || index2 > Int32.MaxValue)
314 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
315 "Value must be >= 0 and <= Int32.MaxValue."));
317 return GetValue ((int) index1, (int) index2);
321 public object GetValue (long index1, long index2, long index3)
323 if (index1 < 0 || index1 > Int32.MaxValue)
324 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
325 "Value must be >= 0 and <= Int32.MaxValue."));
327 if (index2 < 0 || index2 > Int32.MaxValue)
328 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
329 "Value must be >= 0 and <= Int32.MaxValue."));
331 if (index3 < 0 || index3 > Int32.MaxValue)
332 throw new ArgumentOutOfRangeException ("index3", Locale.GetText (
333 "Value must be >= 0 and <= Int32.MaxValue."));
335 return GetValue ((int) index1, (int) index2, (int) index3);
339 public void SetValue (object value, long index)
341 if (index < 0 || index > Int32.MaxValue)
342 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
343 "Value must be >= 0 and <= Int32.MaxValue."));
345 SetValue (value, (int) index);
349 public void SetValue (object value, long index1, long index2)
351 if (index1 < 0 || index1 > Int32.MaxValue)
352 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
353 "Value must be >= 0 and <= Int32.MaxValue."));
355 if (index2 < 0 || index2 > Int32.MaxValue)
356 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
357 "Value must be >= 0 and <= Int32.MaxValue."));
359 int[] ind = {(int) index1, (int) index2};
360 SetValue (value, ind);
364 public void SetValue (object value, long index1, long index2, long index3)
366 if (index1 < 0 || index1 > Int32.MaxValue)
367 throw new ArgumentOutOfRangeException ("index1", Locale.GetText (
368 "Value must be >= 0 and <= Int32.MaxValue."));
370 if (index2 < 0 || index2 > Int32.MaxValue)
371 throw new ArgumentOutOfRangeException ("index2", Locale.GetText (
372 "Value must be >= 0 and <= Int32.MaxValue."));
374 if (index3 < 0 || index3 > Int32.MaxValue)
375 throw new ArgumentOutOfRangeException ("index3", Locale.GetText (
376 "Value must be >= 0 and <= Int32.MaxValue."));
378 int[] ind = {(int) index1, (int) index2, (int) index3};
379 SetValue (value, ind);
383 public void SetValue (object value, int index)
386 throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
387 if (index < GetLowerBound (0) || index > GetUpperBound (0))
388 throw new IndexOutOfRangeException (Locale.GetText (
389 "Index has to be >= lower bound and <= upper bound of the array."));
391 SetValueImpl (value, index - GetLowerBound (0));
394 public void SetValue (object value, int index1, int index2)
396 int[] ind = {index1, index2};
397 SetValue (value, ind);
400 public void SetValue (object value, int index1, int index2, int index3)
402 int[] ind = {index1, index2, index3};
403 SetValue (value, ind);
406 public static Array CreateInstance (Type elementType, int length)
408 int[] lengths = {length};
410 return CreateInstance (elementType, lengths);
413 public static Array CreateInstance (Type elementType, int length1, int length2)
415 int[] lengths = {length1, length2};
417 return CreateInstance (elementType, lengths);
420 public static Array CreateInstance (Type elementType, int length1, int length2, int length3)
422 int[] lengths = {length1, length2, length3};
424 return CreateInstance (elementType, lengths);
427 public static Array CreateInstance (Type elementType, int[] lengths)
429 if (elementType == null)
430 throw new ArgumentNullException ("elementType");
432 throw new ArgumentNullException ("lengths");
434 if (lengths.Length > 255)
435 throw new TypeLoadException ();
439 elementType = elementType.UnderlyingSystemType;
440 if (!elementType.IsSystemType)
441 throw new ArgumentException ("Type must be a type provided by the runtime.", "elementType");
443 return CreateInstanceImpl (elementType, lengths, bounds);
446 public static Array CreateInstance (Type elementType, int[] lengths, int [] bounds)
448 if (elementType == null)
449 throw new ArgumentNullException ("elementType");
451 throw new ArgumentNullException ("lengths");
453 throw new ArgumentNullException ("bounds");
455 elementType = elementType.UnderlyingSystemType;
456 if (!elementType.IsSystemType)
457 throw new ArgumentException ("Type must be a type provided by the runtime.", "elementType");
459 if (lengths.Length < 1)
460 throw new ArgumentException (Locale.GetText ("Arrays must contain >= 1 elements."));
462 if (lengths.Length != bounds.Length)
463 throw new ArgumentException (Locale.GetText ("Arrays must be of same size."));
465 for (int j = 0; j < bounds.Length; j ++) {
467 throw new ArgumentOutOfRangeException ("lengths", Locale.GetText (
468 "Each value has to be >= 0."));
469 if ((long)bounds [j] + (long)lengths [j] > (long)Int32.MaxValue)
470 throw new ArgumentOutOfRangeException ("lengths", Locale.GetText (
471 "Length + bound must not exceed Int32.MaxValue."));
474 if (lengths.Length > 255)
475 throw new TypeLoadException ();
477 return CreateInstanceImpl (elementType, lengths, bounds);
481 static int [] GetIntArray (long [] values)
483 int len = values.Length;
484 int [] ints = new int [len];
485 for (int i = 0; i < len; i++) {
486 long current = values [i];
487 if (current < 0 || current > (long) Int32.MaxValue)
488 throw new ArgumentOutOfRangeException ("values", Locale.GetText (
489 "Each value has to be >= 0 and <= Int32.MaxValue."));
491 ints [i] = (int) current;
496 public static Array CreateInstance (Type elementType, params long [] lengths)
500 throw new ArgumentNullException ("lengths");
502 return CreateInstance (elementType, GetIntArray (lengths));
506 public object GetValue (params long [] indices)
510 throw new ArgumentNullException ("indices");
512 return GetValue (GetIntArray (indices));
516 public void SetValue (object value, params long [] indices)
520 throw new ArgumentNullException ("indices");
522 SetValue (value, GetIntArray (indices));
527 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
529 public static int BinarySearch (Array array, object value)
532 throw new ArgumentNullException ("array");
538 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
540 if (array.Length == 0)
543 if (!(value is IComparable))
544 throw new ArgumentException (Locale.GetText ("value does not support IComparable."));
546 return DoBinarySearch (array, array.GetLowerBound (0), array.GetLength (0), value, null);
550 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
552 public static int BinarySearch (Array array, object value, IComparer comparer)
555 throw new ArgumentNullException ("array");
558 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
560 if (array.Length == 0)
563 if ((comparer == null) && (value != null) && !(value is IComparable))
564 throw new ArgumentException (Locale.GetText (
565 "comparer is null and value does not support IComparable."));
567 return DoBinarySearch (array, array.GetLowerBound (0), array.GetLength (0), value, comparer);
571 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
573 public static int BinarySearch (Array array, int index, int length, object value)
576 throw new ArgumentNullException ("array");
579 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
581 if (index < array.GetLowerBound (0))
582 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
583 "index is less than the lower bound of array."));
585 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
586 "Value has to be >= 0."));
587 // re-ordered to avoid possible integer overflow
588 if (index > array.GetLowerBound (0) + array.GetLength (0) - length)
589 throw new ArgumentException (Locale.GetText (
590 "index and length do not specify a valid range in array."));
592 if (array.Length == 0)
595 if ((value != null) && (!(value is IComparable)))
596 throw new ArgumentException (Locale.GetText (
597 "value does not support IComparable"));
599 return DoBinarySearch (array, index, length, value, null);
603 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
605 public static int BinarySearch (Array array, int index, int length, object value, IComparer comparer)
608 throw new ArgumentNullException ("array");
611 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
613 if (index < array.GetLowerBound (0))
614 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
615 "index is less than the lower bound of array."));
617 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
618 "Value has to be >= 0."));
619 // re-ordered to avoid possible integer overflow
620 if (index > array.GetLowerBound (0) + array.GetLength (0) - length)
621 throw new ArgumentException (Locale.GetText (
622 "index and length do not specify a valid range in array."));
624 if (array.Length == 0)
627 if ((comparer == null) && (value != null) && !(value is IComparable))
628 throw new ArgumentException (Locale.GetText (
629 "comparer is null and value does not support IComparable."));
631 return DoBinarySearch (array, index, length, value, comparer);
634 static int DoBinarySearch (Array array, int index, int length, object value, IComparer comparer)
636 // cache this in case we need it
637 if (comparer == null)
638 comparer = Comparer.Default;
641 // Comment from Tum (tum@veridicus.com):
642 // *Must* start at index + length - 1 to pass rotor test co2460binarysearch_iioi
643 int iMax = index + length - 1;
646 while (iMin <= iMax) {
647 int iMid = (iMin + iMax) / 2;
648 object elt = array.GetValueImpl (iMid);
650 iCmp = comparer.Compare (elt, value);
657 iMin = iMid + 1; // compensate for the rounding down
660 catch (Exception e) {
661 throw new InvalidOperationException (Locale.GetText ("Comparer threw an exception."), e);
668 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
670 public static void Clear (Array array, int index, int length)
673 throw new ArgumentNullException ("array");
675 throw new ArgumentOutOfRangeException ("length < 0");
677 int low = array.GetLowerBound (0);
679 throw new IndexOutOfRangeException ("index < lower bound");
682 // re-ordered to avoid possible integer overflow
683 if (index > array.Length - length)
684 throw new IndexOutOfRangeException ("index + length > size");
686 ClearInternal (array, index, length);
689 [MethodImplAttribute (MethodImplOptions.InternalCall)]
690 static extern void ClearInternal (Array a, int index, int count);
692 [MethodImplAttribute (MethodImplOptions.InternalCall)]
697 extern object Clone ();
700 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
702 public static void Copy (Array sourceArray, Array destinationArray, int length)
704 // need these checks here because we are going to use
705 // GetLowerBound() on source and dest.
706 if (sourceArray == null)
707 throw new ArgumentNullException ("sourceArray");
709 if (destinationArray == null)
710 throw new ArgumentNullException ("destinationArray");
712 Copy (sourceArray, sourceArray.GetLowerBound (0), destinationArray,
713 destinationArray.GetLowerBound (0), length);
717 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
719 public static void Copy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
721 if (sourceArray == null)
722 throw new ArgumentNullException ("sourceArray");
724 if (destinationArray == null)
725 throw new ArgumentNullException ("destinationArray");
728 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
729 "Value has to be >= 0."));;
732 throw new ArgumentOutOfRangeException ("sourceIndex", Locale.GetText (
733 "Value has to be >= 0."));;
735 if (destinationIndex < 0)
736 throw new ArgumentOutOfRangeException ("destinationIndex", Locale.GetText (
737 "Value has to be >= 0."));;
739 if (FastCopy (sourceArray, sourceIndex, destinationArray, destinationIndex, length))
742 int source_pos = sourceIndex - sourceArray.GetLowerBound (0);
743 int dest_pos = destinationIndex - destinationArray.GetLowerBound (0);
745 // re-ordered to avoid possible integer overflow
746 if (source_pos > sourceArray.Length - length || dest_pos > destinationArray.Length - length)
747 throw new ArgumentException ("length");
749 if (sourceArray.Rank != destinationArray.Rank)
750 throw new RankException (Locale.GetText ("Arrays must be of same size."));
752 Type src_type = sourceArray.GetType ().GetElementType ();
753 Type dst_type = destinationArray.GetType ().GetElementType ();
755 if (!Object.ReferenceEquals (sourceArray, destinationArray) || source_pos > dest_pos) {
756 for (int i = 0; i < length; i++) {
757 Object srcval = sourceArray.GetValueImpl (source_pos + i);
760 destinationArray.SetValueImpl (srcval, dest_pos + i);
762 if ((dst_type.IsValueType || dst_type.Equals (typeof (String))) &&
763 (src_type.Equals (typeof (Object))))
764 throw new InvalidCastException ();
766 throw new ArrayTypeMismatchException (String.Format (Locale.GetText (
767 "(Types: source={0}; target={1})"), src_type.FullName, dst_type.FullName));
772 for (int i = length - 1; i >= 0; i--) {
773 Object srcval = sourceArray.GetValueImpl (source_pos + i);
776 destinationArray.SetValueImpl (srcval, dest_pos + i);
778 if ((dst_type.IsValueType || dst_type.Equals (typeof (String))) &&
779 (src_type.Equals (typeof (Object))))
780 throw new InvalidCastException ();
782 throw new ArrayTypeMismatchException (String.Format (Locale.GetText (
783 "(Types: source={0}; target={1})"), src_type.FullName, dst_type.FullName));
791 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
793 public static void Copy (Array sourceArray, long sourceIndex, Array destinationArray,
794 long destinationIndex, long length)
796 if (sourceArray == null)
797 throw new ArgumentNullException ("sourceArray");
799 if (destinationArray == null)
800 throw new ArgumentNullException ("destinationArray");
802 if (sourceIndex < Int32.MinValue || sourceIndex > Int32.MaxValue)
803 throw new ArgumentOutOfRangeException ("sourceIndex",
804 Locale.GetText ("Must be in the Int32 range."));
806 if (destinationIndex < Int32.MinValue || destinationIndex > Int32.MaxValue)
807 throw new ArgumentOutOfRangeException ("destinationIndex",
808 Locale.GetText ("Must be in the Int32 range."));
810 if (length < 0 || length > Int32.MaxValue)
811 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
812 "Value must be >= 0 and <= Int32.MaxValue."));
814 Copy (sourceArray, (int) sourceIndex, destinationArray, (int) destinationIndex, (int) length);
818 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
820 public static void Copy (Array sourceArray, Array destinationArray, long length)
822 if (length < 0 || length > Int32.MaxValue)
823 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
824 "Value must be >= 0 and <= Int32.MaxValue."));
826 Copy (sourceArray, destinationArray, (int) length);
831 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
833 public static int IndexOf (Array array, object value)
836 throw new ArgumentNullException ("array");
838 return IndexOf (array, value, 0, array.Length);
842 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
844 public static int IndexOf (Array array, object value, int startIndex)
847 throw new ArgumentNullException ("array");
849 return IndexOf (array, value, startIndex, array.Length - startIndex);
853 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
855 public static int IndexOf (Array array, object value, int startIndex, int count)
858 throw new ArgumentNullException ("array");
861 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
863 // re-ordered to avoid possible integer overflow
864 if (count < 0 || startIndex < array.GetLowerBound (0) || startIndex - 1 > array.GetUpperBound (0) - count)
865 throw new ArgumentOutOfRangeException ();
867 int max = startIndex + count;
868 for (int i = startIndex; i < max; i++) {
869 if (Object.Equals (value, array.GetValueImpl (i)))
873 return array.GetLowerBound (0) - 1;
877 public void Initialize()
879 //FIXME: We would like to find a compiler that uses
880 // this method. It looks like this method do nothing
881 // in C# so no exception is trown by the moment.
885 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
887 public static int LastIndexOf (Array array, object value)
890 throw new ArgumentNullException ("array");
892 return LastIndexOf (array, value, array.Length - 1);
896 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
898 public static int LastIndexOf (Array array, object value, int startIndex)
901 throw new ArgumentNullException ("array");
903 return LastIndexOf (array, value, startIndex, startIndex - array.GetLowerBound (0) + 1);
907 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
909 public static int LastIndexOf (Array array, object value, int startIndex, int count)
912 throw new ArgumentNullException ("array");
915 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
917 if (count < 0 || startIndex < array.GetLowerBound (0) ||
918 startIndex > array.GetUpperBound (0) || startIndex - count + 1 < array.GetLowerBound (0))
919 throw new ArgumentOutOfRangeException ();
921 for (int i = startIndex; i >= startIndex - count + 1; i--) {
922 if (Object.Equals (value, array.GetValueImpl (i)))
926 return array.GetLowerBound (0) - 1;
929 #if !BOOTSTRAP_WITH_OLDLIB
930 /* delegate used to swap array elements */
931 delegate void Swapper (int i, int j);
934 static Swapper get_swapper (Array array)
937 return new Swapper (array.int_swapper);
938 if (array is double[])
939 return new Swapper (array.double_swapper);
940 if (array is object[]) {
941 return new Swapper (array.obj_swapper);
943 return new Swapper (array.slow_swapper);
947 static Swapper get_swapper<T> (T [] array)
950 return new Swapper (array.int_swapper);
951 if (array is double[])
952 return new Swapper (array.double_swapper);
954 return new Swapper (array.obj_swapper);
959 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
961 public static void Reverse (Array array)
964 throw new ArgumentNullException ("array");
966 Reverse (array, array.GetLowerBound (0), array.GetLength (0));
970 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
972 public static void Reverse (Array array, int index, int length)
975 throw new ArgumentNullException ("array");
978 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
980 if (index < array.GetLowerBound (0) || length < 0)
981 throw new ArgumentOutOfRangeException ();
983 // re-ordered to avoid possible integer overflow
984 if (index > array.GetUpperBound (0) + 1 - length)
985 throw new ArgumentException ();
987 int end = index + length - 1;
988 object[] oarray = array as object[];
989 if (oarray != null) {
990 while (index < end) {
991 object tmp = oarray [index];
992 oarray [index] = oarray [end];
999 int[] iarray = array as int[];
1000 if (iarray != null) {
1001 while (index < end) {
1002 int tmp = iarray [index];
1003 iarray [index] = iarray [end];
1010 double[] darray = array as double[];
1011 if (darray != null) {
1012 while (index < end) {
1013 double tmp = darray [index];
1014 darray [index] = darray [end];
1022 Swapper swapper = get_swapper (array);
1023 while (index < end) {
1024 swapper (index, end);
1031 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1033 public static void Sort (Array array)
1036 throw new ArgumentNullException ("array");
1038 Sort (array, null, array.GetLowerBound (0), array.GetLength (0), null);
1042 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1044 public static void Sort (Array keys, Array items)
1047 throw new ArgumentNullException ("keys");
1049 Sort (keys, items, keys.GetLowerBound (0), keys.GetLength (0), null);
1053 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1055 public static void Sort (Array array, IComparer comparer)
1058 throw new ArgumentNullException ("array");
1060 Sort (array, null, array.GetLowerBound (0), array.GetLength (0), comparer);
1064 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1066 public static void Sort (Array array, int index, int length)
1068 Sort (array, null, index, length, null);
1072 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1074 public static void Sort (Array keys, Array items, IComparer comparer)
1077 throw new ArgumentNullException ("keys");
1079 Sort (keys, items, keys.GetLowerBound (0), keys.GetLength (0), comparer);
1083 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1085 public static void Sort (Array keys, Array items, int index, int length)
1087 Sort (keys, items, index, length, null);
1091 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1093 public static void Sort (Array array, int index, int length, IComparer comparer)
1095 Sort (array, null, index, length, comparer);
1099 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
1102 public static void Sort (Array keys, Array items, int index, int length, IComparer comparer)
1105 throw new ArgumentNullException ("keys");
1107 if (keys.Rank > 1 || (items != null && items.Rank > 1))
1108 throw new RankException ();
1110 if (items != null && keys.GetLowerBound (0) != items.GetLowerBound (0))
1111 throw new ArgumentException ();
1113 if (index < keys.GetLowerBound (0))
1114 throw new ArgumentOutOfRangeException ("index");
1117 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
1118 "Value has to be >= 0."));
1120 if (keys.Length - (index + keys.GetLowerBound (0)) < length || (items != null && index > items.Length - length))
1121 throw new ArgumentException ();
1126 if (comparer == null) {
1131 iswapper = get_swapper (items);
1132 if (keys is double[]) {
1133 combsort (keys as double[], index, length, iswapper);
1136 if (keys is int[]) {
1137 combsort (keys as int[], index, length, iswapper);
1140 if (keys is char[]) {
1141 combsort (keys as char[], index, length, iswapper);
1147 int high0 = index + length - 1;
1148 qsort (keys, items, low0, high0, comparer);
1150 catch (Exception e) {
1151 throw new InvalidOperationException (Locale.GetText ("The comparer threw an exception."), e);
1155 /* note, these are instance methods */
1156 void int_swapper (int i, int j) {
1157 int[] array = this as int[];
1158 int val = array [i];
1159 array [i] = array [j];
1163 void obj_swapper (int i, int j) {
1164 object[] array = this as object[];
1165 object val = array [i];
1166 array [i] = array [j];
1170 void slow_swapper (int i, int j) {
1171 object val = GetValueImpl (i);
1172 SetValueImpl (GetValue (j), i);
1173 SetValueImpl (val, j);
1176 void double_swapper (int i, int j) {
1177 double[] array = this as double[];
1178 double val = array [i];
1179 array [i] = array [j];
1183 static int new_gap (int gap)
1185 gap = (gap * 10) / 13;
1186 if (gap == 9 || gap == 10)
1193 /* we use combsort because it's fast enough and very small, since we have
1194 * several specialized versions here.
1196 static void combsort (double[] array, int start, int size, Swapper swap_items)
1200 gap = new_gap (gap);
1201 bool swapped = false;
1202 int end = start + size - gap;
1203 for (int i = start; i < end; i++) {
1205 if (array [i] > array [j]) {
1206 double val = array [i];
1207 array [i] = array [j];
1210 if (swap_items != null)
1214 if (gap == 1 && !swapped)
1219 static void combsort (int[] array, int start, int size, Swapper swap_items)
1223 gap = new_gap (gap);
1224 bool swapped = false;
1225 int end = start + size - gap;
1226 for (int i = start; i < end; i++) {
1228 if (array [i] > array [j]) {
1229 int val = array [i];
1230 array [i] = array [j];
1233 if (swap_items != null)
1237 if (gap == 1 && !swapped)
1242 static void combsort (char[] array, int start, int size, Swapper swap_items)
1246 gap = new_gap (gap);
1247 bool swapped = false;
1248 int end = start + size - gap;
1249 for (int i = start; i < end; i++) {
1251 if (array [i] > array [j]) {
1252 char val = array [i];
1253 array [i] = array [j];
1256 if (swap_items != null)
1260 if (gap == 1 && !swapped)
1265 private static void qsort (Array keys, Array items, int low0, int high0, IComparer comparer)
1273 object objPivot = keys.GetValueImpl ((low + high) / 2);
1275 while (low <= high) {
1276 // Move the walls in
1277 while (low < high0 && compare (keys.GetValueImpl (low), objPivot, comparer) < 0)
1279 while (high > low0 && compare (objPivot, keys.GetValueImpl (high), comparer) < 0)
1283 swap (keys, items, low, high);
1290 qsort (keys, items, low0, high, comparer);
1292 qsort (keys, items, low, high0, comparer);
1295 private static void swap (Array keys, Array items, int i, int j)
1299 tmp = keys.GetValueImpl (i);
1300 keys.SetValueImpl (keys.GetValue (j), i);
1301 keys.SetValueImpl (tmp, j);
1303 if (items != null) {
1304 tmp = items.GetValueImpl (i);
1305 items.SetValueImpl (items.GetValueImpl (j), i);
1306 items.SetValueImpl (tmp, j);
1310 private static int compare (object value1, object value2, IComparer comparer)
1313 return value2 == null ? 0 : -1;
1314 else if (value2 == null)
1316 else if (comparer == null)
1317 return ((IComparable) value1).CompareTo (value2);
1319 return comparer.Compare (value1, value2);
1323 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1324 public static void Sort<T> (T [] array)
1327 throw new ArgumentNullException ("array");
1329 Sort<T, T> (array, null, 0, array.Length, null);
1332 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1333 public static void Sort<TKey, TValue> (TKey [] keys, TValue [] items)
1336 throw new ArgumentNullException ("keys");
1338 Sort<TKey, TValue> (keys, items, 0, keys.Length, null);
1341 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1342 public static void Sort<T> (T [] array, IComparer<T> comparer)
1345 throw new ArgumentNullException ("array");
1347 Sort<T, T> (array, null, 0, array.Length, comparer);
1350 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1351 public static void Sort<TKey, TValue> (TKey [] keys, TValue [] items, IComparer<TKey> comparer)
1354 throw new ArgumentNullException ("keys");
1356 Sort<TKey, TValue> (keys, items, 0, keys.Length, comparer);
1359 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1360 public static void Sort<T> (T [] array, int index, int length)
1363 throw new ArgumentNullException ("array");
1365 Sort<T, T> (array, null, index, length, null);
1368 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1369 public static void Sort<TKey, TValue> (TKey [] keys, TValue [] items, int index, int length)
1371 Sort<TKey, TValue> (keys, items, index, length, null);
1374 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1375 public static void Sort<T> (T [] array, int index, int length, IComparer<T> comparer)
1378 throw new ArgumentNullException ("array");
1380 Sort<T, T> (array, null, index, length, comparer);
1383 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1384 public static void Sort<TKey, TValue> (TKey [] keys, TValue [] items, int index, int length, IComparer<TKey> comparer)
1387 throw new ArgumentNullException ("keys");
1390 throw new ArgumentOutOfRangeException ("index");
1393 throw new ArgumentOutOfRangeException ("length");
1395 if (keys.Length - index < length
1396 || (items != null && index > items.Length - length))
1397 throw new ArgumentException ();
1403 // Check for value types which can be sorted without Compare () method
1405 if (comparer == null) {
1410 iswapper = get_swapper<TValue> (items);
1411 if (keys is double[]) {
1412 combsort (keys as double[], index, length, iswapper);
1415 if (keys is int[]) {
1416 combsort (keys as int[], index, length, iswapper);
1419 if (keys is char[]) {
1420 combsort (keys as char[], index, length, iswapper);
1424 // Use Comparer<T>.Default instead
1425 // comparer = Comparer<K>.Default;
1430 int high0 = index + length - 1;
1431 qsort<TKey, TValue> (keys, items, low0, high0, comparer);
1433 catch (Exception e) {
1434 throw new InvalidOperationException (Locale.GetText ("The comparer threw an exception."), e);
1438 public static void Sort<T> (T [] array, Comparison<T> comparison)
1441 throw new ArgumentNullException ("array");
1442 if (comparison == null)
1443 throw new ArgumentNullException ("comparison");
1445 if (array.Length <= 1)
1450 int high0 = array.Length - 1;
1451 qsort<T> (array, low0, high0, comparison);
1453 catch (Exception e) {
1454 throw new InvalidOperationException (Locale.GetText ("Comparison threw an exception."), e);
1458 private static void qsort<K, V> (K [] keys, V [] items, int low0, int high0, IComparer<K> comparer)
1466 K keyPivot = keys [(low + high) / 2];
1468 while (low <= high) {
1469 // Move the walls in
1470 //while (low < high0 && comparer.Compare (keys [low], keyPivot) < 0)
1471 while (low < high0 && compare (keys [low], keyPivot, comparer) < 0)
1473 //while (high > low0 && comparer.Compare (keyPivot, keys [high]) < 0)
1474 while (high > low0 && compare (keyPivot, keys [high], comparer) < 0)
1478 swap<K, V> (keys, items, low, high);
1485 qsort<K, V> (keys, items, low0, high, comparer);
1487 qsort<K, V> (keys, items, low, high0, comparer);
1490 private static int compare<T> (T value1, T value2, IComparer<T> comparer)
1493 return value2 == null ? 0 : -1;
1494 else if (value2 == null)
1496 else if (value1 is IComparable<T>)
1497 return ((IComparable<T>) value1).CompareTo (value2);
1498 else if (value1 is IComparable)
1499 return ((IComparable) value1).CompareTo (value2);
1500 else if (comparer != null)
1501 return comparer.Compare (value1, value2);
1503 string msg = Locale.GetText ("No IComparable or IComparable<T> interface found for type '{0}'.");
1504 throw new InvalidOperationException (String.Format (msg, typeof (T)));
1507 private static void qsort<T> (T [] array, int low0, int high0, Comparison<T> comparison)
1515 T keyPivot = array [(low + high) / 2];
1517 while (low <= high) {
1518 // Move the walls in
1519 while (low < high0 && comparison (array [low], keyPivot) < 0)
1521 while (high > low0 && comparison (keyPivot, array [high]) < 0)
1525 swap<T> (array, low, high);
1532 qsort<T> (array, low0, high, comparison);
1534 qsort<T> (array, low, high0, comparison);
1537 private static void swap<K, V> (K [] keys, V [] items, int i, int j)
1542 keys [i] = keys [j];
1545 if (items != null) {
1548 items [i] = items [j];
1553 private static void swap<T> (T [] array, int i, int j)
1556 array [i] = array [j];
1565 void CopyTo (Array array, int index)
1568 throw new ArgumentNullException ("array");
1570 // The order of these exception checks may look strange,
1571 // but that's how the microsoft runtime does it.
1573 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
1574 if (index + this.GetLength (0) > array.GetLowerBound (0) + array.GetLength (0))
1575 throw new ArgumentException ();
1577 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
1579 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
1580 "Value has to be >= 0."));
1582 Copy (this, this.GetLowerBound (0), array, index, this.GetLength (0));
1586 [ComVisible (false)]
1591 void CopyTo (Array array, long index)
1593 if (index < 0 || index > Int32.MaxValue)
1594 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
1595 "Value must be >= 0 and <= Int32.MaxValue."));
1597 CopyTo (array, (int) index);
1601 internal class SimpleEnumerator : IEnumerator, ICloneable
1607 public SimpleEnumerator (Array arrayToEnumerate)
1609 this.enumeratee = arrayToEnumerate;
1610 this.currentpos = -1;
1611 this.length = arrayToEnumerate.Length;
1614 public object Current {
1616 // Exception messages based on MS implementation
1617 if (currentpos < 0 )
1618 throw new InvalidOperationException (Locale.GetText (
1619 "Enumeration has not started."));
1620 if (currentpos >= length)
1621 throw new InvalidOperationException (Locale.GetText (
1622 "Enumeration has already ended"));
1623 // Current should not increase the position. So no ++ over here.
1624 return enumeratee.GetValueImpl (currentpos);
1628 public bool MoveNext()
1630 //The docs say Current should throw an exception if last
1631 //call to MoveNext returned false. This means currentpos
1632 //should be set to length when returning false.
1633 if (currentpos < length)
1635 if(currentpos < length)
1646 public object Clone ()
1648 return MemberwiseClone ();
1653 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1654 public static void Resize<T> (ref T [] array, int newSize)
1656 Resize<T> (ref array, array == null ? 0 : array.Length, newSize);
1659 internal static void Resize<T> (ref T[] array, int length, int newSize)
1662 throw new ArgumentOutOfRangeException ();
1664 if (array == null) {
1665 array = new T [newSize];
1669 if (array.Length == newSize)
1672 T [] a = new T [newSize];
1673 Array.Copy (array, a, Math.Min (newSize, length));
1677 public static bool TrueForAll <T> (T [] array, Predicate <T> match)
1680 throw new ArgumentNullException ("array");
1682 throw new ArgumentNullException ("match");
1684 foreach (T t in array)
1691 public static void ForEach<T> (T [] array, Action <T> action)
1694 throw new ArgumentNullException ("array");
1696 throw new ArgumentNullException ("action");
1698 foreach (T t in array)
1702 public static TOutput[] ConvertAll<TInput, TOutput> (TInput [] array, Converter<TInput, TOutput> converter)
1705 throw new ArgumentNullException ("array");
1706 if (converter == null)
1707 throw new ArgumentNullException ("converter");
1709 TOutput [] output = new TOutput [array.Length];
1710 for (int i = 0; i < array.Length; i ++)
1711 output [i] = converter (array [i]);
1716 public static int FindLastIndex<T> (T [] array, Predicate <T> match)
1719 throw new ArgumentNullException ("array");
1721 return FindLastIndex<T> (array, 0, array.Length, match);
1724 public static int FindLastIndex<T> (T [] array, int startIndex, Predicate<T> match)
1727 throw new ArgumentNullException ();
1729 return FindLastIndex<T> (array, startIndex, array.Length - startIndex, match);
1732 public static int FindLastIndex<T> (T [] array, int startIndex, int count, Predicate<T> match)
1735 throw new ArgumentNullException ("array");
1737 throw new ArgumentNullException ("match");
1739 if (startIndex > array.Length || startIndex + count > array.Length)
1740 throw new ArgumentOutOfRangeException ();
1742 for (int i = startIndex + count - 1; i >= startIndex; i--)
1743 if (match (array [i]))
1749 public static int FindIndex<T> (T [] array, Predicate<T> match)
1752 throw new ArgumentNullException ("array");
1754 return FindIndex<T> (array, 0, array.Length, match);
1757 public static int FindIndex<T> (T [] array, int startIndex, Predicate<T> match)
1760 throw new ArgumentNullException ("array");
1762 return FindIndex<T> (array, startIndex, array.Length - startIndex, match);
1765 public static int FindIndex<T> (T [] array, int startIndex, int count, Predicate<T> match)
1768 throw new ArgumentNullException ("array");
1771 throw new ArgumentNullException ("match");
1773 if (startIndex > array.Length || startIndex + count > array.Length)
1774 throw new ArgumentOutOfRangeException ();
1776 for (int i = startIndex; i < startIndex + count; i ++)
1777 if (match (array [i]))
1783 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1784 public static int BinarySearch<T> (T [] array, T value)
1787 throw new ArgumentNullException ("array");
1789 return BinarySearch<T> (array, 0, array.Length, value, null);
1792 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1793 public static int BinarySearch<T> (T [] array, T value, IComparer<T> comparer)
1796 throw new ArgumentNullException ("array");
1798 return BinarySearch<T> (array, 0, array.Length, value, comparer);
1801 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1802 public static int BinarySearch<T> (T [] array, int offset, int length, T value)
1804 return BinarySearch<T> (array, offset, length, value, null);
1807 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1808 public static int BinarySearch<T> (T [] array, int index, int length, T value, IComparer<T> comparer)
1811 throw new ArgumentNullException ("array");
1813 throw new ArgumentOutOfRangeException ("index", Locale.GetText (
1814 "index is less than the lower bound of array."));
1816 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
1817 "Value has to be >= 0."));
1818 // re-ordered to avoid possible integer overflow
1819 if (index > array.Length - length)
1820 throw new ArgumentException (Locale.GetText (
1821 "index and length do not specify a valid range in array."));
1822 if (comparer == null)
1823 comparer = Comparer <T>.Default;
1826 int iMax = index + length - 1;
1829 while (iMin <= iMax) {
1830 int iMid = (iMin + iMax) / 2;
1831 iCmp = comparer.Compare (value, array [iMid]);
1838 iMin = iMid + 1; // compensate for the rounding down
1840 } catch (Exception e) {
1841 throw new InvalidOperationException (Locale.GetText ("Comparer threw an exception."), e);
1847 public static int IndexOf<T> (T [] array, T value)
1850 throw new ArgumentNullException ("array");
1852 return IndexOf<T> (array, value, 0, array.Length);
1855 public static int IndexOf<T> (T [] array, T value, int startIndex)
1858 throw new ArgumentNullException ("array");
1860 return IndexOf<T> (array, value, startIndex, array.Length - startIndex);
1863 public static int IndexOf<T> (T [] array, T value, int startIndex, int count)
1866 throw new ArgumentNullException ("array");
1868 // re-ordered to avoid possible integer overflow
1869 if (count < 0 || startIndex < array.GetLowerBound (0) || startIndex - 1 > array.GetUpperBound (0) - count)
1870 throw new ArgumentOutOfRangeException ();
1872 int max = startIndex + count;
1873 EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
1874 for (int i = startIndex; i < max; i++) {
1875 if (equalityComparer.Equals (value, array [i]))
1882 public static int LastIndexOf<T> (T [] array, T value)
1885 throw new ArgumentNullException ("array");
1887 return LastIndexOf<T> (array, value, array.Length - 1);
1890 public static int LastIndexOf<T> (T [] array, T value, int startIndex)
1893 throw new ArgumentNullException ("array");
1895 return LastIndexOf<T> (array, value, startIndex, startIndex + 1);
1898 public static int LastIndexOf<T> (T [] array, T value, int startIndex, int count)
1901 throw new ArgumentNullException ("array");
1903 if (count < 0 || startIndex > array.Length || startIndex - count + 1 < 0)
1904 throw new ArgumentOutOfRangeException ();
1906 EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
1907 for (int i = startIndex; i >= startIndex - count + 1; i--) {
1908 if (equalityComparer.Equals (value, array [i]))
1915 public static T [] FindAll<T> (T [] array, Predicate <T> match)
1918 throw new ArgumentNullException ("array");
1921 throw new ArgumentNullException ("match");
1924 T [] d = new T [array.Length];
1925 foreach (T t in array)
1929 Resize <T> (ref d, pos);
1933 public static bool Exists<T> (T [] array, Predicate <T> match)
1936 throw new ArgumentNullException ("array");
1939 throw new ArgumentNullException ("match");
1941 foreach (T t in array)
1947 public static ReadOnlyCollection<T> AsReadOnly<T> (T[] array)
1950 throw new ArgumentNullException ("array");
1951 return new ReadOnlyCollection<T> (new ArrayReadOnlyList<T> (array));
1954 public static T Find<T> (T [] array, Predicate<T> match)
1957 throw new ArgumentNullException ("array");
1960 throw new ArgumentNullException ("match");
1962 foreach (T t in array)
1969 public static T FindLast<T> (T [] array, Predicate <T> match)
1972 throw new ArgumentNullException ("array");
1975 throw new ArgumentNullException ("match");
1977 for (int i = array.Length - 1; i >= 0; i--)
1978 if (match (array [i]))
1984 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
1986 // The constrained copy should guarantee that if there is an exception thrown
1987 // during the copy, the destination array remains unchanged.
1988 // This is related to System.Runtime.Reliability.CER
1989 public static void ConstrainedCopy (Array s, int s_i, Array d, int d_i, int c)
1991 Copy (s, s_i, d, d_i, c);
1997 // This is used internally by the runtime to represent arrays; see #74953.
1999 // Note that you normally can't derive a class from System.Array (CS0644),
2000 // but GMCS makes an exception here for all classes which are nested inside
2003 internal class InternalArray<T> : Array, IList<T>
2005 new public IEnumerator<T> GetEnumerator ()
2007 return new InternalEnumerator (this);
2016 bool ICollection<T>.IsReadOnly {
2022 void ICollection<T>.Clear ()
2024 throw new NotSupportedException ("Collection is read-only");
2027 void ICollection<T>.Add (T item)
2029 throw new NotSupportedException ("Collection is read-only");
2032 bool ICollection<T>.Remove (T item)
2034 throw new NotSupportedException ("Collection is read-only");
2037 public bool Contains (T item)
2040 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
2042 int length = this.Length;
2043 for (int i = 0; i < length; i++) {
2045 GetGenericValueImpl (i, out value);
2046 if (item.Equals (value))
2053 public void CopyTo (T[] array, int index)
2056 throw new ArgumentNullException ("array");
2058 // The order of these exception checks may look strange,
2059 // but that's how the microsoft runtime does it.
2061 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
2062 if (index + this.GetLength (0) > array.GetLowerBound (0) + array.GetLength (0))
2063 throw new ArgumentException ();
2065 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
2067 throw new ArgumentOutOfRangeException (
2068 "index", Locale.GetText ("Value has to be >= 0."));
2070 Copy (this, this.GetLowerBound (0), array, index, this.GetLength (0));
2073 new public T this [int index] {
2075 if (unchecked ((uint) index) >= unchecked ((uint) Length))
2076 throw new ArgumentOutOfRangeException ("index");
2079 GetGenericValueImpl (index, out value);
2084 throw new NotSupportedException ("Collection is read-only");
2088 void IList<T>.Insert (int index, T item)
2090 throw new NotSupportedException ("Collection is read-only");
2093 void IList<T>.RemoveAt (int index)
2095 throw new NotSupportedException ("Collection is read-only");
2098 public int IndexOf (T item)
2101 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
2103 int length = this.Length;
2104 for (int i = 0; i < length; i++) {
2106 GetGenericValueImpl (i, out value);
2107 if (item.Equals (value))
2108 // array index may not be zero-based.
2110 return i + this.GetLowerBound (0);
2115 // lower bound may be MinValue
2116 retVal = this.GetLowerBound (0) - 1;
2122 // CAUTION! No bounds checking!
2123 [MethodImplAttribute (MethodImplOptions.InternalCall)]
2124 protected extern void GetGenericValueImpl (int pos, out T value);
2126 internal struct InternalEnumerator : IEnumerator<T>
2128 const int NOT_STARTED = -2;
2130 // this MUST be -1, because we depend on it in move next.
2131 // we just decr the size, so, 0 - 1 == FINISHED
2132 const int FINISHED = -1;
2134 InternalArray<T> array;
2137 internal InternalEnumerator (InternalArray<T> array)
2143 public void Dispose ()
2148 public bool MoveNext ()
2150 if (idx == NOT_STARTED)
2153 return idx != FINISHED && -- idx != FINISHED;
2159 throw new InvalidOperationException ();
2161 return array [array.Length - 1 - idx];
2165 void IEnumerator.Reset ()
2167 throw new NotImplementedException ();
2170 object IEnumerator.Current {
2178 class ArrayReadOnlyList<T> : IList<T>
2183 public ArrayReadOnlyList (T [] array)
2186 is_value_type = typeof (T).IsValueType;
2189 public T this [int index] {
2191 if (unchecked ((uint) index) >= unchecked ((uint) array.Length))
2192 throw new ArgumentOutOfRangeException ("index");
2193 return array [index];
2195 set { throw ReadOnlyError (); }
2199 get { return array.Length; }
2202 public bool IsReadOnly {
2203 get { return true; }
2206 public void Add (T item)
2208 throw ReadOnlyError ();
2211 public void Clear ()
2213 throw ReadOnlyError ();
2216 public bool Contains (T item)
2218 return Array.IndexOf<T> (array, item) >= 0;
2221 public void CopyTo (T [] array, int index)
2223 array.CopyTo (array, index);
2226 IEnumerator IEnumerable.GetEnumerator ()
2228 return GetEnumerator ();
2231 public IEnumerator<T> GetEnumerator ()
2233 for (int i = 0; i < array.Length; i++)
2234 yield return array [i];
2237 public int IndexOf (T item)
2239 return Array.IndexOf<T> (array, item);
2242 public void Insert (int index, T item)
2244 throw ReadOnlyError ();
2247 public bool Remove (T item)
2249 throw ReadOnlyError ();
2252 public void RemoveAt (int index)
2254 throw ReadOnlyError ();
2257 Exception ReadOnlyError ()
2259 return new NotSupportedException ("This collection is read-only.");
2265 #if BOOTSTRAP_WITH_OLDLIB
2266 /* delegate used to swap array elements, keep defined outside Array */
2267 delegate void Swapper (int i, int j);