dotnetanywhere-wb /corlib/System/Array.cs

Language C# Lines 459
MD5 Hash cb05b3cbacc2d004e060e759ae6c6eed Estimated Cost $9,377 (why?)
Repository https://bitbucket.org/cosi2/dotnetanywhere-wb.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#if !LOCALTEST

using System.Runtime.CompilerServices;
using System.Collections;
using System.Collections.Generic;

namespace System {

	public abstract class Array : ICloneable, IList, ICollection, IEnumerable {

		private class NonGenericEnumerator : IEnumerator {

			private Array array;
			private int index, length;

			public NonGenericEnumerator(Array array) {
				this.array = array;
				this.index = -1;
				this.length = array.length;
			}

			public object Current {
				get {
					if (index < 0) {
						throw new InvalidOperationException("Enumeration has not started");
					}
					if (index >= length) {
						throw new InvalidOperationException("Enumeration has finished");
					}
					return array.GetValue(index);
				}
			}

			public bool MoveNext() {
				index++;
				return (index < length);
			}

			public void Reset() {
				index = -1;
			}

		}

		private struct GenericEnumerator<T> : IEnumerator<T> {

			private Array array;
			private int index, length;

			public GenericEnumerator(Array array) {
				this.array = array;
				this.index = -1;
				this.length = array.length;
			}

			public T Current {
				get {
					if (index < 0) {
						throw new InvalidOperationException("Enumeration has not started");
					}
					if (index >= length) {
						throw new InvalidOperationException("Enumeration has finished");
					}
					return (T)array.GetValue(index);
				}
			}

			public void Dispose() {
			}

			object IEnumerator.Current {
				get {
					return this.Current;
				}
			}

			public bool MoveNext() {
				index++;
				return (index < length);
			}

			public void Reset() {
				this.index = -1;
			}
		}

		private Array() {
		}

		#region Generic interface methods

		// The name of these methods are important. They are directly referenced in the interpreter.
		private IEnumerator<T> Internal_GetGenericEnumerator<T>() {
			return new GenericEnumerator<T>(this);
		}

		private bool Internal_GenericIsReadOnly() {
			return true;
		}

		private void Internal_GenericAdd<T>(T item) {
			throw new NotSupportedException("Collection is read-only");
		}

		private void Internal_GenericClear() {
			Array.Clear(this, 0, this.length);
		}

		private bool Internal_GenericContains<T>(T item) {
			return Array.IndexOf(this, (object)item) >= 0;
		}

		private void Internal_GenericCopyTo<T>(T[] array, int arrayIndex) {
			Array.Copy(this, 0, (Array)array, arrayIndex, this.length);
		}

		private bool Internal_GenericRemove<T>(T item) {
			throw new NotSupportedException("Collection is read-only");
		}

		private int Internal_GenericIndexOf<T>(T item) {
			return IndexOf(this, (object)item);
		}

		private void Internal_GenericInsert<T>(int index, T item) {
			throw new NotSupportedException("List is read-only");
		}

		private void Internal_GenericRemoveAt(int index) {
			throw new NotSupportedException("List is read-only");
		}

		private T Internal_GenericGetItem<T>(int index) {
			return (T)GetValue(index);
		}

		private void Internal_GenericSetItem<T>(int index, T value) {
			SetValue((object)value, index);
		}

		#endregion

		// This must be the only field, as it ties up with the Array definition in InterNet2
		private int length;

		public int Length {
			get {
				return this.length;
			}
		}

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private object Internal_GetValue(int index);

		/// <summary>
		/// Returns true if the value set ok, returns false if the Type was wrong
		/// </summary>
		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public bool Internal_SetValue(object value, int index);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public static void Clear(Array array, int index, int length);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private static bool Internal_Copy(Array src, int srcIndex, Array dst, int dstIndex, int length);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public static void Resize<T>(ref T[] array, int newSize);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public static void Reverse(Array array, int index, int length);

		public static void Reverse(Array array) {
			Reverse(array, 0, array.length);
		}

		public static int IndexOf(Array array, object value) {
			return IndexOf(array, value, 0, array.length);
		}

		public static int IndexOf(Array array, object value, int startIndex) {
			return IndexOf(array, value, startIndex, array.length - startIndex);
		}

		public static int IndexOf(Array array, object value, int startIndex, int count) {
			if (array == null) {
				throw new ArgumentNullException("array");
			}
			int max = startIndex + count;
			if (startIndex < 0 || max > array.length) {
				throw new ArgumentOutOfRangeException();
			}
			for (int i = startIndex; i < max; i++) {
				if (object.Equals(value, array.GetValue(i))) {
					return i;
				}
			}
			return -1;
		}

		public static void Copy(Array srcArray, int srcIndex, Array dstArray, int dstIndex, int length) {
			if (srcArray == null || dstArray == null) {
				throw new ArgumentNullException((srcArray == null) ? "sourceArray" : "destinationArray");
			}
			if (srcIndex < 0 || dstIndex < 0 || length < 0) {
				throw new ArgumentOutOfRangeException();
			}
			if (srcIndex + length > srcArray.length || dstIndex + length > dstArray.length) {
				throw new ArgumentException();
			}
			if (Internal_Copy(srcArray, srcIndex, dstArray, dstIndex, length)) {
				// When src element type can always be cast to dst element type, then can do a really fast copy.
				return;
			}

			int start, inc, end;
			// Need to make sure it works even if both arrays are the same
			if (dstIndex > srcIndex) {
				start = 0;
				inc = 1;
				end = length;
			} else {
				start = length - 1;
				inc = -1;
				end = -1;
			}
			for (int i = start; i != end; i += inc) {
				object item = srcArray.GetValue(srcIndex + i);
				dstArray.SetValue(item, dstIndex + i);
			}
		}

		public static void Copy(Array srcArray, Array dstArray, int length) {
			Copy(srcArray, 0, dstArray, 0, length);
		}

		public static int IndexOf<T>(T[] array, T value) {
			return IndexOf((Array)array, (object)value);
		}

		public static int IndexOf<T>(T[] array, T value, int startIndex) {
			return IndexOf((Array)array, (object)value, startIndex);
		}

		public static int IndexOf<T>(T[] array, T value, int startIndex, int count) {
			return IndexOf((Array)array, (object)value, startIndex, count);
		}

		public object GetValue(int index) {
			if (index < 0 || index >= this.length) {
				throw new IndexOutOfRangeException();
			}
			return Internal_GetValue(index);
		}

		public void SetValue(object value, int index) {
			if (index < 0 || index >= this.length) {
				throw new IndexOutOfRangeException();
			}
			if (!Internal_SetValue(value, index)) {
				throw new InvalidCastException();
			}
		}

		public int Rank {
			get {
				return 1;
			}
		}

		public int GetLength(int dimension) {
			if (dimension != 0) {
				throw new IndexOutOfRangeException();
			}
			return this.length;
		}

		public int GetLowerBound(int dimension) {
			if (dimension != 0) {
				throw new IndexOutOfRangeException();
			}
			return 0;
		}

		public int GetUpperBound(int dimension) {
			if (dimension != 0) {
				throw new IndexOutOfRangeException();
			}
			return this.length - 1;
		}

		public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter) {
			if (array == null) {
				throw new ArgumentNullException("array");
			}
			if (converter == null) {
				throw new ArgumentNullException("converter");
			}

			TOutput[] output = new TOutput[array.Length];
			int arrayLen = array.Length;
			for (int i = 0; i < arrayLen; i++) {
				output[i] = converter(array[i]);
			}

			return output;
		}


		#region Interface Members

		public object Clone() {
			return object.Clone(this);
		}

		public bool IsFixedSize {
			get {
				return true;
			}
		}

		public bool IsReadOnly {
			get {
				return false;
			}
		}

		object IList.this[int index] {
			get {
				if (index < 0 || index >= this.length) {
					throw new ArgumentOutOfRangeException("index");
				}
				return GetValue(index);
			}
			set {
				if (index < 0 || index >= this.length) {
					throw new ArgumentOutOfRangeException("index");
				}
				SetValue(value, index);
			}
		}

		int IList.Add(object value) {
			throw new NotSupportedException("Collection is read-only");
		}

		void IList.Clear() {
			Array.Clear(this, 0, this.length);
		}

		bool IList.Contains(object value) {
			return (IndexOf(this, value) >= 0);
		}

		int IList.IndexOf(object value) {
			return IndexOf(this, value);
		}

		void IList.Insert(int index, object value) {
			throw new NotSupportedException("Collection is read-only");
		}

		void IList.Remove(object value) {
			throw new NotSupportedException("Collection is read-only");
		}

		void IList.RemoveAt(int index) {
			throw new NotSupportedException("Collection is read-only");
		}

		int ICollection.Count {
			get {
				return this.length;
			}
		}

		public bool IsSynchronized {
			get {
				return false;
			}
		}

		public object SyncRoot {
			get {
				return this;
			}
		}

		public void CopyTo(Array array, int index) {
			Copy(this, 0, array, index, this.length);
		}

		public IEnumerator GetEnumerator() {
			return new NonGenericEnumerator(this);
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return GetEnumerator();
		}

		#endregion


        public static void Sort(Array array)
        {
            if (array == null)
            {
                throw new ArgumentNullException();
            }
            Sort(array, null, 0, array.length, null);
        }

        public static void Sort(Array array, int index, int length, IComparer comparer)
        {
            Sort(array, null, index, length, comparer);
        }

        public static void Sort(Array keys, Array items, int index, int length, IComparer comparer)
        {
            if (keys == null)
            {
                throw new ArgumentNullException();
            }
            if ((keys.Rank != 1) || ((items != null) && (items.Rank != 1)))
            {
                throw new ArgumentException();
            }
            if ((index < 0) || (length < 0))
            {
                throw new ArgumentOutOfRangeException();
            }
            if (((keys.length - index) < length) || ((items != null) && (index > (items.length - length))))
            {
                throw new ArgumentException();
            }
            if (length > 1)
            {
                object[] objArray = keys as object[];
                object[] objArray2 = null;
                if (objArray != null)
                {
                    objArray2 = items as object[];
                }
                if ((objArray != null) && ((items == null) || (objArray2 != null)))
                {
                    new SorterObjectArray(objArray, objArray2, comparer).QuickSort(index, (index + length) - 1);
                }
                else
                {
                    new SorterGenericArray(keys, items, comparer).QuickSort(index, (index + length) - 1);
                }
            }
        }

	}

}
#endif
Back to Top