PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/corlib/System/Array.cs

https://bitbucket.org/cosi2/dotnetanywhere-wb
C# | 458 lines | 365 code | 86 blank | 7 comment | 70 complexity | cb05b3cbacc2d004e060e759ae6c6eed MD5 | raw file
  1. #if !LOCALTEST
  2. using System.Runtime.CompilerServices;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace System {
  6. public abstract class Array : ICloneable, IList, ICollection, IEnumerable {
  7. private class NonGenericEnumerator : IEnumerator {
  8. private Array array;
  9. private int index, length;
  10. public NonGenericEnumerator(Array array) {
  11. this.array = array;
  12. this.index = -1;
  13. this.length = array.length;
  14. }
  15. public object Current {
  16. get {
  17. if (index < 0) {
  18. throw new InvalidOperationException("Enumeration has not started");
  19. }
  20. if (index >= length) {
  21. throw new InvalidOperationException("Enumeration has finished");
  22. }
  23. return array.GetValue(index);
  24. }
  25. }
  26. public bool MoveNext() {
  27. index++;
  28. return (index < length);
  29. }
  30. public void Reset() {
  31. index = -1;
  32. }
  33. }
  34. private struct GenericEnumerator<T> : IEnumerator<T> {
  35. private Array array;
  36. private int index, length;
  37. public GenericEnumerator(Array array) {
  38. this.array = array;
  39. this.index = -1;
  40. this.length = array.length;
  41. }
  42. public T Current {
  43. get {
  44. if (index < 0) {
  45. throw new InvalidOperationException("Enumeration has not started");
  46. }
  47. if (index >= length) {
  48. throw new InvalidOperationException("Enumeration has finished");
  49. }
  50. return (T)array.GetValue(index);
  51. }
  52. }
  53. public void Dispose() {
  54. }
  55. object IEnumerator.Current {
  56. get {
  57. return this.Current;
  58. }
  59. }
  60. public bool MoveNext() {
  61. index++;
  62. return (index < length);
  63. }
  64. public void Reset() {
  65. this.index = -1;
  66. }
  67. }
  68. private Array() {
  69. }
  70. #region Generic interface methods
  71. // The name of these methods are important. They are directly referenced in the interpreter.
  72. private IEnumerator<T> Internal_GetGenericEnumerator<T>() {
  73. return new GenericEnumerator<T>(this);
  74. }
  75. private bool Internal_GenericIsReadOnly() {
  76. return true;
  77. }
  78. private void Internal_GenericAdd<T>(T item) {
  79. throw new NotSupportedException("Collection is read-only");
  80. }
  81. private void Internal_GenericClear() {
  82. Array.Clear(this, 0, this.length);
  83. }
  84. private bool Internal_GenericContains<T>(T item) {
  85. return Array.IndexOf(this, (object)item) >= 0;
  86. }
  87. private void Internal_GenericCopyTo<T>(T[] array, int arrayIndex) {
  88. Array.Copy(this, 0, (Array)array, arrayIndex, this.length);
  89. }
  90. private bool Internal_GenericRemove<T>(T item) {
  91. throw new NotSupportedException("Collection is read-only");
  92. }
  93. private int Internal_GenericIndexOf<T>(T item) {
  94. return IndexOf(this, (object)item);
  95. }
  96. private void Internal_GenericInsert<T>(int index, T item) {
  97. throw new NotSupportedException("List is read-only");
  98. }
  99. private void Internal_GenericRemoveAt(int index) {
  100. throw new NotSupportedException("List is read-only");
  101. }
  102. private T Internal_GenericGetItem<T>(int index) {
  103. return (T)GetValue(index);
  104. }
  105. private void Internal_GenericSetItem<T>(int index, T value) {
  106. SetValue((object)value, index);
  107. }
  108. #endregion
  109. // This must be the only field, as it ties up with the Array definition in InterNet2
  110. private int length;
  111. public int Length {
  112. get {
  113. return this.length;
  114. }
  115. }
  116. [MethodImpl(MethodImplOptions.InternalCall)]
  117. extern private object Internal_GetValue(int index);
  118. /// <summary>
  119. /// Returns true if the value set ok, returns false if the Type was wrong
  120. /// </summary>
  121. [MethodImpl(MethodImplOptions.InternalCall)]
  122. extern public bool Internal_SetValue(object value, int index);
  123. [MethodImpl(MethodImplOptions.InternalCall)]
  124. extern public static void Clear(Array array, int index, int length);
  125. [MethodImpl(MethodImplOptions.InternalCall)]
  126. extern private static bool Internal_Copy(Array src, int srcIndex, Array dst, int dstIndex, int length);
  127. [MethodImpl(MethodImplOptions.InternalCall)]
  128. extern public static void Resize<T>(ref T[] array, int newSize);
  129. [MethodImpl(MethodImplOptions.InternalCall)]
  130. extern public static void Reverse(Array array, int index, int length);
  131. public static void Reverse(Array array) {
  132. Reverse(array, 0, array.length);
  133. }
  134. public static int IndexOf(Array array, object value) {
  135. return IndexOf(array, value, 0, array.length);
  136. }
  137. public static int IndexOf(Array array, object value, int startIndex) {
  138. return IndexOf(array, value, startIndex, array.length - startIndex);
  139. }
  140. public static int IndexOf(Array array, object value, int startIndex, int count) {
  141. if (array == null) {
  142. throw new ArgumentNullException("array");
  143. }
  144. int max = startIndex + count;
  145. if (startIndex < 0 || max > array.length) {
  146. throw new ArgumentOutOfRangeException();
  147. }
  148. for (int i = startIndex; i < max; i++) {
  149. if (object.Equals(value, array.GetValue(i))) {
  150. return i;
  151. }
  152. }
  153. return -1;
  154. }
  155. public static void Copy(Array srcArray, int srcIndex, Array dstArray, int dstIndex, int length) {
  156. if (srcArray == null || dstArray == null) {
  157. throw new ArgumentNullException((srcArray == null) ? "sourceArray" : "destinationArray");
  158. }
  159. if (srcIndex < 0 || dstIndex < 0 || length < 0) {
  160. throw new ArgumentOutOfRangeException();
  161. }
  162. if (srcIndex + length > srcArray.length || dstIndex + length > dstArray.length) {
  163. throw new ArgumentException();
  164. }
  165. if (Internal_Copy(srcArray, srcIndex, dstArray, dstIndex, length)) {
  166. // When src element type can always be cast to dst element type, then can do a really fast copy.
  167. return;
  168. }
  169. int start, inc, end;
  170. // Need to make sure it works even if both arrays are the same
  171. if (dstIndex > srcIndex) {
  172. start = 0;
  173. inc = 1;
  174. end = length;
  175. } else {
  176. start = length - 1;
  177. inc = -1;
  178. end = -1;
  179. }
  180. for (int i = start; i != end; i += inc) {
  181. object item = srcArray.GetValue(srcIndex + i);
  182. dstArray.SetValue(item, dstIndex + i);
  183. }
  184. }
  185. public static void Copy(Array srcArray, Array dstArray, int length) {
  186. Copy(srcArray, 0, dstArray, 0, length);
  187. }
  188. public static int IndexOf<T>(T[] array, T value) {
  189. return IndexOf((Array)array, (object)value);
  190. }
  191. public static int IndexOf<T>(T[] array, T value, int startIndex) {
  192. return IndexOf((Array)array, (object)value, startIndex);
  193. }
  194. public static int IndexOf<T>(T[] array, T value, int startIndex, int count) {
  195. return IndexOf((Array)array, (object)value, startIndex, count);
  196. }
  197. public object GetValue(int index) {
  198. if (index < 0 || index >= this.length) {
  199. throw new IndexOutOfRangeException();
  200. }
  201. return Internal_GetValue(index);
  202. }
  203. public void SetValue(object value, int index) {
  204. if (index < 0 || index >= this.length) {
  205. throw new IndexOutOfRangeException();
  206. }
  207. if (!Internal_SetValue(value, index)) {
  208. throw new InvalidCastException();
  209. }
  210. }
  211. public int Rank {
  212. get {
  213. return 1;
  214. }
  215. }
  216. public int GetLength(int dimension) {
  217. if (dimension != 0) {
  218. throw new IndexOutOfRangeException();
  219. }
  220. return this.length;
  221. }
  222. public int GetLowerBound(int dimension) {
  223. if (dimension != 0) {
  224. throw new IndexOutOfRangeException();
  225. }
  226. return 0;
  227. }
  228. public int GetUpperBound(int dimension) {
  229. if (dimension != 0) {
  230. throw new IndexOutOfRangeException();
  231. }
  232. return this.length - 1;
  233. }
  234. public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter) {
  235. if (array == null) {
  236. throw new ArgumentNullException("array");
  237. }
  238. if (converter == null) {
  239. throw new ArgumentNullException("converter");
  240. }
  241. TOutput[] output = new TOutput[array.Length];
  242. int arrayLen = array.Length;
  243. for (int i = 0; i < arrayLen; i++) {
  244. output[i] = converter(array[i]);
  245. }
  246. return output;
  247. }
  248. #region Interface Members
  249. public object Clone() {
  250. return object.Clone(this);
  251. }
  252. public bool IsFixedSize {
  253. get {
  254. return true;
  255. }
  256. }
  257. public bool IsReadOnly {
  258. get {
  259. return false;
  260. }
  261. }
  262. object IList.this[int index] {
  263. get {
  264. if (index < 0 || index >= this.length) {
  265. throw new ArgumentOutOfRangeException("index");
  266. }
  267. return GetValue(index);
  268. }
  269. set {
  270. if (index < 0 || index >= this.length) {
  271. throw new ArgumentOutOfRangeException("index");
  272. }
  273. SetValue(value, index);
  274. }
  275. }
  276. int IList.Add(object value) {
  277. throw new NotSupportedException("Collection is read-only");
  278. }
  279. void IList.Clear() {
  280. Array.Clear(this, 0, this.length);
  281. }
  282. bool IList.Contains(object value) {
  283. return (IndexOf(this, value) >= 0);
  284. }
  285. int IList.IndexOf(object value) {
  286. return IndexOf(this, value);
  287. }
  288. void IList.Insert(int index, object value) {
  289. throw new NotSupportedException("Collection is read-only");
  290. }
  291. void IList.Remove(object value) {
  292. throw new NotSupportedException("Collection is read-only");
  293. }
  294. void IList.RemoveAt(int index) {
  295. throw new NotSupportedException("Collection is read-only");
  296. }
  297. int ICollection.Count {
  298. get {
  299. return this.length;
  300. }
  301. }
  302. public bool IsSynchronized {
  303. get {
  304. return false;
  305. }
  306. }
  307. public object SyncRoot {
  308. get {
  309. return this;
  310. }
  311. }
  312. public void CopyTo(Array array, int index) {
  313. Copy(this, 0, array, index, this.length);
  314. }
  315. public IEnumerator GetEnumerator() {
  316. return new NonGenericEnumerator(this);
  317. }
  318. IEnumerator IEnumerable.GetEnumerator() {
  319. return GetEnumerator();
  320. }
  321. #endregion
  322. public static void Sort(Array array)
  323. {
  324. if (array == null)
  325. {
  326. throw new ArgumentNullException();
  327. }
  328. Sort(array, null, 0, array.length, null);
  329. }
  330. public static void Sort(Array array, int index, int length, IComparer comparer)
  331. {
  332. Sort(array, null, index, length, comparer);
  333. }
  334. public static void Sort(Array keys, Array items, int index, int length, IComparer comparer)
  335. {
  336. if (keys == null)
  337. {
  338. throw new ArgumentNullException();
  339. }
  340. if ((keys.Rank != 1) || ((items != null) && (items.Rank != 1)))
  341. {
  342. throw new ArgumentException();
  343. }
  344. if ((index < 0) || (length < 0))
  345. {
  346. throw new ArgumentOutOfRangeException();
  347. }
  348. if (((keys.length - index) < length) || ((items != null) && (index > (items.length - length))))
  349. {
  350. throw new ArgumentException();
  351. }
  352. if (length > 1)
  353. {
  354. object[] objArray = keys as object[];
  355. object[] objArray2 = null;
  356. if (objArray != null)
  357. {
  358. objArray2 = items as object[];
  359. }
  360. if ((objArray != null) && ((items == null) || (objArray2 != null)))
  361. {
  362. new SorterObjectArray(objArray, objArray2, comparer).QuickSort(index, (index + length) - 1);
  363. }
  364. else
  365. {
  366. new SorterGenericArray(keys, items, comparer).QuickSort(index, (index + length) - 1);
  367. }
  368. }
  369. }
  370. }
  371. }
  372. #endif