Array.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //
  2. // System.Array.cs
  3. //
  4. // Author:
  5. // Joe Shaw ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.Collections;
  10. using System.Runtime.CompilerServices;
  11. namespace System
  12. {
  13. public abstract class Array : ICloneable
  14. {
  15. // Constructor
  16. protected Array ()
  17. {
  18. /* empty */
  19. }
  20. // Properties
  21. public int Length
  22. {
  23. get
  24. {
  25. int length = this.GetLength (0);
  26. for (int i = 1; i < this.Rank; i++) {
  27. length *= this.GetLength (i);
  28. }
  29. return length;
  30. }
  31. }
  32. public int Rank
  33. {
  34. get
  35. {
  36. return this.GetRank ();
  37. }
  38. }
  39. // InternalCall Methods
  40. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  41. public extern int GetRank ();
  42. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  43. public extern int GetLength (int dimension);
  44. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  45. public extern int GetLowerBound (int dimension);
  46. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  47. public extern object GetValue (int[] idxs);
  48. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  49. public extern void SetValue (object value, int[] idxs);
  50. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  51. public extern static Array CreateInstance(Type elementType, int[] lengths, int [] bounds);
  52. // Methods Implementations
  53. public int GetUpperBound (int dimension)
  54. {
  55. return GetLowerBound (dimension) +
  56. GetLength (dimension);
  57. }
  58. public object GetValue (int idx)
  59. {
  60. int[] ind = new int [1];
  61. ind [0] = idx;
  62. return GetValue (ind);
  63. }
  64. public object GetValue (int idx1, int idx2)
  65. {
  66. int[] ind = new int [2];
  67. ind [0] = idx1;
  68. ind [1] = idx2;
  69. return GetValue (ind);
  70. }
  71. public object GetValue (int idx1, int idx2, int idx3)
  72. {
  73. int[] ind = new int [3];
  74. ind [0] = idx1;
  75. ind [1] = idx2;
  76. ind [2] = idx3;
  77. return GetValue (ind);
  78. }
  79. public void SetValue (object value, int idx)
  80. {
  81. int[] ind = new int [1];
  82. ind [0] = idx;
  83. SetValue (value, ind);
  84. }
  85. public void SetValue (object value, int idx1, int idx2)
  86. {
  87. int[] ind = new int [2];
  88. ind [0] = idx1;
  89. ind [1] = idx2;
  90. SetValue (value, ind);
  91. }
  92. public void SetValue (object value, int idx1, int idx2, int idx3)
  93. {
  94. int[] ind = new int [3];
  95. ind [0] = idx1;
  96. ind [1] = idx2;
  97. ind [2] = idx3;
  98. SetValue (value, ind);
  99. }
  100. public static Array CreateInstance(Type elementType, int length)
  101. {
  102. int[] lengths = new int [1];
  103. int[] bounds = null;
  104. lengths [0] = length;
  105. return CreateInstance (elementType, lengths, bounds);
  106. }
  107. public static Array CreateInstance(Type elementType, int l1, int l2)
  108. {
  109. int[] lengths = new int [2];
  110. int[] bounds = null;
  111. lengths [0] = l1;
  112. lengths [1] = l2;
  113. return CreateInstance (elementType, lengths, bounds);
  114. }
  115. public static Array CreateInstance(Type elementType, int l1, int l2, int l3)
  116. {
  117. int[] lengths = new int [3];
  118. int[] bounds = null;
  119. lengths [0] = l1;
  120. lengths [1] = l2;
  121. lengths [2] = l3;
  122. return CreateInstance (elementType, lengths, bounds);
  123. }
  124. public static Array CreateInstance(Type elementType, int[] lengths)
  125. {
  126. int[] bounds = null;
  127. return CreateInstance (elementType, lengths, bounds);
  128. }
  129. public static int BinarySearch (Array array, object value)
  130. {
  131. return BinarySearch (array, array.GetLowerBound (0), array.GetLength (0),
  132. value, null);
  133. }
  134. public static int BinarySearch (Array array, object value, IComparer comparer)
  135. {
  136. return BinarySearch (array, array.GetLowerBound (0), array.GetLength (0),
  137. value, comparer);
  138. }
  139. public static int BinarySearch (Array array, int index, int length, object value)
  140. {
  141. return BinarySearch (array, index, length, value, null);
  142. }
  143. public static int BinarySearch (Array array, int index,
  144. int length, object value,
  145. IComparer comparer)
  146. {
  147. if (array == null)
  148. throw new ArgumentNullException ();
  149. if (array.Rank > 1)
  150. throw new RankException ();
  151. if (index < array.GetLowerBound (0) || length < 0)
  152. throw new ArgumentOutOfRangeException ();
  153. if (index + length > array.GetUpperBound (0))
  154. throw new ArgumentException ();
  155. if (comparer == null && !(value is IComparable))
  156. throw new ArgumentException ();
  157. // FIXME: Throw an ArgumentException if comparer
  158. // is null and value is not of the same type as the
  159. // elements of array.
  160. for (int i = 0; i < length; i++)
  161. {
  162. int result;
  163. if (comparer == null && !(array.GetValue(index + i) is IComparable))
  164. throw new ArgumentException ();
  165. if (comparer == null)
  166. result = (value as IComparable).CompareTo(array.GetValue(index + i));
  167. else
  168. result = comparer.Compare(value, array.GetValue(index + i));
  169. if (result == 0)
  170. return index + i;
  171. else if (result < 0)
  172. return ~(index + i);
  173. }
  174. return ~(index + length);
  175. }
  176. public static void Clear (Array array, int index, int length)
  177. {
  178. if (array == null)
  179. throw new ArgumentNullException ();
  180. if (array.Rank > 1)
  181. throw new RankException ();
  182. if (index < array.GetLowerBound (0) || length < 0 ||
  183. index + length > array.GetUpperBound (0))
  184. throw new ArgumentOutOfRangeException ();
  185. for (int i = 0; i < length; i++)
  186. {
  187. if (array.GetValue(index + i) is bool)
  188. array.SetValue(false, index + i);
  189. else if (array.GetValue(index + i) is ValueType)
  190. array.SetValue(0, index + i);
  191. else
  192. array.SetValue(null, index + i);
  193. }
  194. }
  195. public virtual object Clone ()
  196. {
  197. // Array is abstract -- Array a = new Array();
  198. Array a = (Array)this.Clone();
  199. // I don't know how to handle this ?
  200. if (this.Rank > 1)
  201. throw new RankException ();
  202. for (int i = 0; i < this.GetLength (0); i++)
  203. {
  204. int index = this.GetLowerBound (0) + i;
  205. a.SetValue(this.GetValue(index), index);
  206. }
  207. return a;
  208. }
  209. public static void Copy (Array source, Array dest, int length)
  210. {
  211. // I don't know how to handle this ?
  212. if (source.Rank > 1 || dest.Rank > 1)
  213. throw new RankException ();
  214. Copy (source, source.GetLowerBound (0), dest, dest.GetLowerBound (0), length);
  215. }
  216. public static void Copy (Array source, int source_idx, Array dest, int dest_idx, int length)
  217. {
  218. // I don't know how to handle this ?
  219. if (source.Rank > 1 || dest.Rank > 1)
  220. throw new RankException ();
  221. if (length < 0)
  222. throw new ArgumentOutOfRangeException ();
  223. if (source == null || dest == null)
  224. throw new ArgumentNullException ();
  225. if (source_idx < source.GetLowerBound (0) ||
  226. source_idx + length > source.GetUpperBound (0) ||
  227. dest_idx < dest.GetLowerBound (0) || dest_idx + length > dest.GetUpperBound (0))
  228. throw new ArgumentException ();
  229. if (source.Rank != dest.Rank)
  230. throw new RankException ();
  231. for (int i = 0; i < length; i++)
  232. {
  233. int index = source.GetLowerBound (0) + i;
  234. dest.SetValue(source.GetValue(index), index);
  235. }
  236. }
  237. public static int IndexOf (Array array, object value)
  238. {
  239. return IndexOf (array, value, 0, array.Length);
  240. }
  241. public static int IndexOf (Array array, object value, int index)
  242. {
  243. return IndexOf (array, value, index, array.Length - index);
  244. }
  245. public static int IndexOf (Array array, object value, int index, int length)
  246. {
  247. if (array == null)
  248. throw new ArgumentNullException ();
  249. if (length < 0 || index < array.GetLowerBound (0) ||
  250. index > array.GetUpperBound (0))
  251. throw new ArgumentOutOfRangeException ();
  252. for (int i = 0; i < length; i++)
  253. {
  254. if (array.GetValue(index + i) == value)
  255. return index + i;
  256. }
  257. return array.GetLowerBound (0) - 1;
  258. }
  259. public static int LastIndexOf (Array array, object value)
  260. {
  261. return LastIndexOf (array, value, 0, array.Length);
  262. }
  263. public static int LastIndexOf (Array array, object value, int index)
  264. {
  265. return LastIndexOf (array, value, index, array.Length - index);
  266. }
  267. public static int LastIndexOf (Array array, object value, int index, int length)
  268. {
  269. if (array == null)
  270. throw new ArgumentNullException ();
  271. if (length < 0 || index < array.GetLowerBound (0) ||
  272. index > array.GetUpperBound (0))
  273. throw new ArgumentOutOfRangeException ();
  274. for (int i = length - 1; i >= 0; i--)
  275. {
  276. if (array.GetValue(index + i) == value)
  277. return index + i;
  278. }
  279. return array.GetLowerBound (0) - 1;
  280. }
  281. public static void Reverse (Array array)
  282. {
  283. Reverse (array, array.GetLowerBound (0), array.GetLength (0));
  284. }
  285. public static void Reverse (Array array, int index, int length)
  286. {
  287. if (array == null)
  288. throw new ArgumentNullException ();
  289. if (array.Rank > 1)
  290. throw new RankException ();
  291. if (index < array.GetLowerBound (0) || length < 0)
  292. throw new ArgumentOutOfRangeException ();
  293. if (index + length > array.GetUpperBound (0))
  294. throw new ArgumentException ();
  295. for (int i = 0; i < length/2; i++)
  296. {
  297. object tmp;
  298. tmp = array.GetValue (index + i);
  299. array.SetValue(array.GetValue (index + length - i - 1), index + i);
  300. array.SetValue(tmp, index + length - i - 1);
  301. }
  302. }
  303. public static void Sort (Array array)
  304. {
  305. Sort (array, null, array.GetLowerBound (0), array.GetLength (0), null);
  306. }
  307. public static void Sort (Array keys, Array items)
  308. {
  309. Sort (keys, items, keys.GetLowerBound (0), keys.GetLength (0), null);
  310. }
  311. public static void Sort (Array array, IComparer comparer)
  312. {
  313. Sort (array, null, array.GetLowerBound (0), array.GetLength (0), comparer);
  314. }
  315. public static void Sort (Array array, int index, int length)
  316. {
  317. Sort (array, null, index, length, null);
  318. }
  319. public static void Sort (Array keys, Array items, IComparer comparer)
  320. {
  321. Sort (keys, items, keys.GetLowerBound (0), keys.GetLength (0), comparer);
  322. }
  323. public static void Sort (Array keys, Array items, int index, int length)
  324. {
  325. Sort (keys, items, index, length, null);
  326. }
  327. public static void Sort (Array array, int index, int length, IComparer comparer)
  328. {
  329. Sort (array, null, index, length, comparer);
  330. }
  331. public static void Sort (Array keys, Array items, int index, int length, IComparer comparer)
  332. {
  333. int low0 = index;
  334. int high0 = index + length - 1;
  335. qsort (keys, items, index, index + length - 1, comparer);
  336. }
  337. private static void qsort (Array keys, Array items, int low0, int high0, IComparer comparer)
  338. {
  339. int pivot;
  340. int low = low0;
  341. int high = high0;
  342. if (keys.Rank > 1 || items.Rank > 1)
  343. throw new RankException ();
  344. if (low >= high)
  345. return;
  346. pivot = (low + high) / 2;
  347. if (compare (keys.GetValue (low), keys.GetValue (pivot), comparer) > 0)
  348. swap (keys, items, low, pivot);
  349. if (compare (keys.GetValue (pivot), keys.GetValue (high), comparer) > 0)
  350. swap (keys, items, pivot, high);
  351. while (low < high)
  352. {
  353. // Move the walls in
  354. while (low < high && compare (keys.GetValue (low), keys.GetValue (pivot), comparer) < 0)
  355. low++;
  356. while (low < high && compare (keys.GetValue (pivot), keys.GetValue (high), comparer) < 0)
  357. high--;
  358. if (low < high)
  359. {
  360. swap (keys, items, low, high);
  361. low++;
  362. high--;
  363. }
  364. }
  365. qsort (keys, items, low0, low - 1, comparer);
  366. qsort (keys, items, high + 1, high0, comparer);
  367. }
  368. private static void swap (Array keys, Array items, int i, int j)
  369. {
  370. object tmp;
  371. tmp = keys.GetValue (i);
  372. keys.SetValue (keys.GetValue (j), i);
  373. keys.SetValue (tmp, j);
  374. if (items != null)
  375. {
  376. tmp = items.GetValue (i);
  377. items.SetValue (items.GetValue (j), i);
  378. items.SetValue (tmp, j);
  379. }
  380. }
  381. private static int compare (object value1, object value2, IComparer comparer)
  382. {
  383. if (comparer == null)
  384. return ((IComparable) value1).CompareTo(value2);
  385. else
  386. return comparer.Compare(value1, value2);
  387. }
  388. }
  389. }