5 // Joe Shaw (joe@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 public abstract class Array : ICloneable {
13 public int lower_bound = 0;
31 public static int BinarySearch (Array array, object value)
33 return BinarySearch (array, this.lower_bound, this.length, value, null);
36 public static int BinarySearch (Array array, object value, IComparer comparer)
38 return BinarySearch (array, this.lower_bound, this.length, value, comparer);
41 public static int BinarySearch (Array array, int index, int length, object value)
43 return BinarySearch (array, index, length, value, null);
46 public static int BinarySearch (Array array, int index, int length, object value, IComparer comparer)
49 throw new ArgumentNullException ();
52 throw new RankException ();
54 if (index < array.lower_bound || length < 0)
55 throw new ArgumentOutOfRangeException ();
57 if (index + length > array.lower_bound + array.Length)
58 throw new ArgumentException ();
60 if (comparer == null && !(value is IComparable))
61 throw new ArgumentException ();
63 // FIXME: Throw an ArgumentException if comparer
64 // is null and value is not of the same type as the
67 for (int i = 0; i < length; i++) {
70 if (comparer == null && !(array[index + i] is IComparable))
71 throw new ArgumentException ();
74 result = comparer.Compare(value, array[index + i]);
76 result = value.CompareTo(array[index + i]);
84 return ~(index + length);
87 public static void Clear (Array array, int index, int length)
90 throw new ArgumentNullException ();
92 if (index < this.lower_bound || length < 0 ||
93 index + length > this.lower_bound + this.length)
94 throw new ArgumentOutOfRangeException ();
96 for (int i = 0; i < length; i++) {
97 if (array[index + i] is bool)
98 array[index + i] = false;
99 else if (array[index + i] is ValueType)
100 array[index + i] = 0;
102 array[index + i] = null;
106 public virtual object Clone ()
110 for (int i = 0; i < this.length; i++) {
111 int index = this.lower_bound + i;
113 a[index] = this[index];
119 public static void Copy (Array source, Array dest, int length)
121 Copy (source, source.lower_bound, dest, dest.lower_bound, length);
124 public static void Copy (Array source, int source_idx, Array dest, int dest_idx, int length)
127 throw new ArgumentOutOfRangeException ();
129 if (source == null || dest == null)
130 throw new ArgumentNullException ();
132 if (source_idx < source.lower_bound || source_idx + length > source.lower_bound + source.Length || dest_idx < dest.lower_bound || dest_idx + length > dest.lower_bound + dest.Length)
133 throw new ArgumentException ();
135 if (source.Rank != dest.Rank)
136 throw new RankException ();
138 for (int i = 0; i < length; i++) {
139 int index = source.lower_bound + i;
141 dest[index] = source[index];
145 public object GetValue (int index)
148 throw new ArgumentException ();
150 if (index < this.lower_bound ||
151 index > this.lower_bound + this.length)
152 throw new ArgumentOutOfRangeException ();
157 public static int IndexOf (Array array, object value)
159 return IndexOf (array, value, 0, array.Length);
162 public static int IndexOf (Array array, object value, int index)
164 return IndexOf (array, value, index, array.Length - index);
167 public static int IndexOf (Array array, object value, int index, int length)
170 throw new ArgumentNullException ();
172 if (length < 0 || index < array.lower_bound || index > array.lower_bound + length)
173 throw new ArgumentOutOfRangeException ();
175 for (int i = 0; i < length; i++) {
176 if (array[index + i] == value)
180 return array.lower_bound - 1;
183 public static int LastIndexOf (Array array, object value)
185 return LastIndexOf (array, value, 0, array.Length);
188 public static int LastIndexOf (Array array, object value, int index)
190 return LastIndexOf (array, value, index, array.Length - index);
193 public static int LastIndexOf (Array array, object value, int index, int length)
195 int occurrance = array.lower_bound - 1;
198 throw new ArgumentNullException ();
200 if (length < 0 || index < array.lower_bound || index > array.lower_bound + length)
201 throw new ArgumentOutOfRangeException ();
203 for (int i = 0; i < length; i++) {
204 if (array[index + i] == value)
205 occurrance = index + i;
211 public static void Reverse (Array array)
213 Reverse (array, array.lower_bound, array.Length);
216 public static void Reverse (Array array, int index, int length)
219 throw new ArgumentNullException ();
222 throw new RankException ();
224 if (index < array.lower_bound || length < 0)
225 throw new ArgumentOutOfRangeException ();
227 if (index + length > array.lower_bound + array.Length)
228 throw new ArgumentException ();
230 for (int i = 0; i < length/2; i++) {
233 tmp = array[index + i];
234 array[index + i] = array[index + length - i - 1];
235 array[index + length - i - 1] = tmp;
239 public static void Sort (Array array)
241 Sort (array, null, array.lower_bound, array.Length, null);
244 public static void Sort (Array keys, Array items)
246 Sort (keys, items, keys.lower_bound, keys.Length, null);
249 public static void Sort (Array array, IComparer comparer)
251 Sort (array, null, array.lower_bound, array.Length, comparer);
254 public static void Sort (Array array, int index, int length)
256 Sort (array, null, index, length, null);
259 public static void Sort (Array keys, Array items, IComparer comparer)
261 Sort (keys, items, keys.lower_bound, keys.Length, comparer);
264 public static void Sort (Array keys, Array items, int index, int length)
266 Sort (keys, items, index, length, null);
269 public static void Sort (Array array, int index, int length, IComparer comparer)
271 Sort (array, null, index, length, comparer);
274 public static void Sort (Array keys, Array items, int index, int length, IComparer comparer)