Array.cs 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Diagnostics;
  8. using System.Runtime.CompilerServices;
  9. using Internal.Runtime.CompilerServices;
  10. namespace System
  11. {
  12. [Serializable]
  13. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  14. public abstract partial class Array : ICloneable, IList, IStructuralComparable, IStructuralEquatable
  15. {
  16. // We impose limits on maximum array lenght in each dimension to allow efficient
  17. // implementation of advanced range check elimination in future.
  18. // Keep in sync with vm\gcscan.cpp and HashHelpers.MaxPrimeArrayLength.
  19. // The constants are defined in this method: inline SIZE_T MaxArrayLength(SIZE_T componentSize) from gcscan
  20. // We have different max sizes for arrays with elements of size 1 for backwards compatibility
  21. internal const int MaxArrayLength = 0X7FEFFFFF;
  22. internal const int MaxByteArrayLength = 0x7FFFFFC7;
  23. // This ctor exists solely to prevent C# from generating a protected .ctor that violates the surface area. I really want this to be a
  24. // "protected-and-internal" rather than "internal" but C# has no keyword for the former.
  25. internal Array() { }
  26. public static ReadOnlyCollection<T> AsReadOnly<T>(T[] array)
  27. {
  28. if (array == null)
  29. {
  30. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  31. }
  32. // T[] implements IList<T>.
  33. return new ReadOnlyCollection<T>(array);
  34. }
  35. public static void Resize<T>(ref T[] array, int newSize)
  36. {
  37. if (newSize < 0)
  38. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.newSize, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  39. T[] larray = array;
  40. if (larray == null)
  41. {
  42. array = new T[newSize];
  43. return;
  44. }
  45. if (larray.Length != newSize)
  46. {
  47. T[] newArray = new T[newSize];
  48. Copy(larray, 0, newArray, 0, larray.Length > newSize ? newSize : larray.Length);
  49. array = newArray;
  50. }
  51. }
  52. public static Array CreateInstance(Type elementType, params long[] lengths)
  53. {
  54. if (lengths == null)
  55. {
  56. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.lengths);
  57. }
  58. if (lengths.Length == 0)
  59. ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NeedAtLeast1Rank);
  60. int[] intLengths = new int[lengths.Length];
  61. for (int i = 0; i < lengths.Length; ++i)
  62. {
  63. long len = lengths[i];
  64. int ilen = (int)len;
  65. if (len != ilen)
  66. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.len, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  67. intLengths[i] = ilen;
  68. }
  69. return Array.CreateInstance(elementType, intLengths);
  70. }
  71. public static void Copy(Array sourceArray, Array destinationArray, long length)
  72. {
  73. int ilength = (int)length;
  74. if (length != ilength)
  75. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  76. Copy(sourceArray, destinationArray, ilength);
  77. }
  78. public static void Copy(Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length)
  79. {
  80. int isourceIndex = (int)sourceIndex;
  81. int idestinationIndex = (int)destinationIndex;
  82. int ilength = (int)length;
  83. if (sourceIndex != isourceIndex)
  84. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.sourceIndex, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  85. if (destinationIndex != idestinationIndex)
  86. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destinationIndex, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  87. if (length != ilength)
  88. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  89. Copy(sourceArray, isourceIndex, destinationArray, idestinationIndex, ilength);
  90. }
  91. public object GetValue(long index)
  92. {
  93. int iindex = (int)index;
  94. if (index != iindex)
  95. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  96. return this.GetValue(iindex);
  97. }
  98. public object GetValue(long index1, long index2)
  99. {
  100. int iindex1 = (int)index1;
  101. int iindex2 = (int)index2;
  102. if (index1 != iindex1)
  103. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  104. if (index2 != iindex2)
  105. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  106. return this.GetValue(iindex1, iindex2);
  107. }
  108. public object GetValue(long index1, long index2, long index3)
  109. {
  110. int iindex1 = (int)index1;
  111. int iindex2 = (int)index2;
  112. int iindex3 = (int)index3;
  113. if (index1 != iindex1)
  114. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  115. if (index2 != iindex2)
  116. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  117. if (index3 != iindex3)
  118. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index3, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  119. return this.GetValue(iindex1, iindex2, iindex3);
  120. }
  121. public object GetValue(params long[] indices)
  122. {
  123. if (indices == null)
  124. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.indices);
  125. if (Rank != indices.Length)
  126. ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices);
  127. int[] intIndices = new int[indices.Length];
  128. for (int i = 0; i < indices.Length; ++i)
  129. {
  130. long index = indices[i];
  131. int iindex = (int)index;
  132. if (index != iindex)
  133. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  134. intIndices[i] = iindex;
  135. }
  136. return this.GetValue(intIndices);
  137. }
  138. public void SetValue(object value, long index)
  139. {
  140. int iindex = (int)index;
  141. if (index != iindex)
  142. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  143. this.SetValue(value, iindex);
  144. }
  145. public void SetValue(object value, long index1, long index2)
  146. {
  147. int iindex1 = (int)index1;
  148. int iindex2 = (int)index2;
  149. if (index1 != iindex1)
  150. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  151. if (index2 != iindex2)
  152. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  153. this.SetValue(value, iindex1, iindex2);
  154. }
  155. public void SetValue(object value, long index1, long index2, long index3)
  156. {
  157. int iindex1 = (int)index1;
  158. int iindex2 = (int)index2;
  159. int iindex3 = (int)index3;
  160. if (index1 != iindex1)
  161. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  162. if (index2 != iindex2)
  163. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  164. if (index3 != iindex3)
  165. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index3, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  166. this.SetValue(value, iindex1, iindex2, iindex3);
  167. }
  168. public void SetValue(object value, params long[] indices)
  169. {
  170. if (indices == null)
  171. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.indices);
  172. if (Rank != indices.Length)
  173. ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices);
  174. int[] intIndices = new int[indices.Length];
  175. for (int i = 0; i < indices.Length; ++i)
  176. {
  177. long index = indices[i];
  178. int iindex = (int)index;
  179. if (index != iindex)
  180. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  181. intIndices[i] = iindex;
  182. }
  183. this.SetValue(value, intIndices);
  184. }
  185. private static int GetMedian(int low, int hi)
  186. {
  187. // Note both may be negative, if we are dealing with arrays w/ negative lower bounds.
  188. Debug.Assert(low <= hi);
  189. Debug.Assert(hi - low >= 0, "Length overflow!");
  190. return low + ((hi - low) >> 1);
  191. }
  192. public long GetLongLength(int dimension)
  193. {
  194. //This method should throw an IndexOufOfRangeException for compat if dimension < 0 or >= Rank
  195. return GetLength(dimension);
  196. }
  197. // Number of elements in the Array.
  198. int ICollection.Count
  199. { get { return Length; } }
  200. // Returns an object appropriate for synchronizing access to this
  201. // Array.
  202. public object SyncRoot
  203. { get { return this; } }
  204. // Is this Array read-only?
  205. public bool IsReadOnly
  206. { get { return false; } }
  207. public bool IsFixedSize
  208. {
  209. get { return true; }
  210. }
  211. // Is this Array synchronized (i.e., thread-safe)? If you want a synchronized
  212. // collection, you can use SyncRoot as an object to synchronize your
  213. // collection with. You could also call GetSynchronized()
  214. // to get a synchronized wrapper around the Array.
  215. public bool IsSynchronized
  216. { get { return false; } }
  217. object IList.this[int index]
  218. {
  219. get { return GetValue(index); }
  220. set { SetValue(value, index); }
  221. }
  222. int IList.Add(object value)
  223. {
  224. ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection);
  225. return default;
  226. }
  227. bool IList.Contains(object value)
  228. {
  229. return Array.IndexOf(this, value) >= this.GetLowerBound(0);
  230. }
  231. void IList.Clear()
  232. {
  233. Array.Clear(this, this.GetLowerBound(0), this.Length);
  234. }
  235. int IList.IndexOf(object value)
  236. {
  237. return Array.IndexOf(this, value);
  238. }
  239. void IList.Insert(int index, object value)
  240. {
  241. ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection);
  242. }
  243. void IList.Remove(object value)
  244. {
  245. ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection);
  246. }
  247. void IList.RemoveAt(int index)
  248. {
  249. ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection);
  250. }
  251. // Make a new array which is a shallow copy of the original array.
  252. //
  253. public object Clone()
  254. {
  255. return MemberwiseClone();
  256. }
  257. int IStructuralComparable.CompareTo(object other, IComparer comparer)
  258. {
  259. if (other == null)
  260. {
  261. return 1;
  262. }
  263. Array o = other as Array;
  264. if (o == null || this.Length != o.Length)
  265. {
  266. ThrowHelper.ThrowArgumentException(ExceptionResource.ArgumentException_OtherNotArrayOfCorrectLength, ExceptionArgument.other);
  267. }
  268. int i = 0;
  269. int c = 0;
  270. while (i < o.Length && c == 0)
  271. {
  272. object left = GetValue(i);
  273. object right = o.GetValue(i);
  274. c = comparer.Compare(left, right);
  275. i++;
  276. }
  277. return c;
  278. }
  279. bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer)
  280. {
  281. if (other == null)
  282. {
  283. return false;
  284. }
  285. if (object.ReferenceEquals(this, other))
  286. {
  287. return true;
  288. }
  289. if (!(other is Array o) || o.Length != this.Length)
  290. {
  291. return false;
  292. }
  293. int i = 0;
  294. while (i < o.Length)
  295. {
  296. object left = GetValue(i);
  297. object right = o.GetValue(i);
  298. if (!comparer.Equals(left, right))
  299. {
  300. return false;
  301. }
  302. i++;
  303. }
  304. return true;
  305. }
  306. private static int CombineHashCodes(int h1, int h2)
  307. {
  308. return (((h1 << 5) + h1) ^ h2);
  309. }
  310. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  311. {
  312. if (comparer == null)
  313. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
  314. int ret = 0;
  315. for (int i = (this.Length >= 8 ? this.Length - 8 : 0); i < this.Length; i++)
  316. {
  317. ret = CombineHashCodes(ret, comparer.GetHashCode(GetValue(i)));
  318. }
  319. return ret;
  320. }
  321. // Searches an array for a given element using a binary search algorithm.
  322. // Elements of the array are compared to the search value using the
  323. // IComparable interface, which must be implemented by all elements
  324. // of the array and the given search value. This method assumes that the
  325. // array is already sorted according to the IComparable interface;
  326. // if this is not the case, the result will be incorrect.
  327. //
  328. // The method returns the index of the given value in the array. If the
  329. // array does not contain the given value, the method returns a negative
  330. // integer. The bitwise complement operator (~) can be applied to a
  331. // negative result to produce the index of the first element (if any) that
  332. // is larger than the given search value.
  333. //
  334. public static int BinarySearch(Array array, object value)
  335. {
  336. if (array == null)
  337. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  338. return BinarySearch(array, array.GetLowerBound(0), array.Length, value, null);
  339. }
  340. // Searches a section of an array for a given element using a binary search
  341. // algorithm. Elements of the array are compared to the search value using
  342. // the IComparable interface, which must be implemented by all
  343. // elements of the array and the given search value. This method assumes
  344. // that the array is already sorted according to the IComparable
  345. // interface; if this is not the case, the result will be incorrect.
  346. //
  347. // The method returns the index of the given value in the array. If the
  348. // array does not contain the given value, the method returns a negative
  349. // integer. The bitwise complement operator (~) can be applied to a
  350. // negative result to produce the index of the first element (if any) that
  351. // is larger than the given search value.
  352. //
  353. public static int BinarySearch(Array array, int index, int length, object value)
  354. {
  355. return BinarySearch(array, index, length, value, null);
  356. }
  357. // Searches an array for a given element using a binary search algorithm.
  358. // Elements of the array are compared to the search value using the given
  359. // IComparer interface. If comparer is null, elements of the
  360. // array are compared to the search value using the IComparable
  361. // interface, which in that case must be implemented by all elements of the
  362. // array and the given search value. This method assumes that the array is
  363. // already sorted; if this is not the case, the result will be incorrect.
  364. //
  365. // The method returns the index of the given value in the array. If the
  366. // array does not contain the given value, the method returns a negative
  367. // integer. The bitwise complement operator (~) can be applied to a
  368. // negative result to produce the index of the first element (if any) that
  369. // is larger than the given search value.
  370. //
  371. public static int BinarySearch(Array array, object value, IComparer comparer)
  372. {
  373. if (array == null)
  374. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  375. return BinarySearch(array, array.GetLowerBound(0), array.Length, value, comparer);
  376. }
  377. // Searches a section of an array for a given element using a binary search
  378. // algorithm. Elements of the array are compared to the search value using
  379. // the given IComparer interface. If comparer is null,
  380. // elements of the array are compared to the search value using the
  381. // IComparable interface, which in that case must be implemented by
  382. // all elements of the array and the given search value. This method
  383. // assumes that the array is already sorted; if this is not the case, the
  384. // result will be incorrect.
  385. //
  386. // The method returns the index of the given value in the array. If the
  387. // array does not contain the given value, the method returns a negative
  388. // integer. The bitwise complement operator (~) can be applied to a
  389. // negative result to produce the index of the first element (if any) that
  390. // is larger than the given search value.
  391. //
  392. public static int BinarySearch(Array array, int index, int length, object value, IComparer comparer)
  393. {
  394. if (array == null)
  395. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  396. int lb = array.GetLowerBound(0);
  397. if (index < lb)
  398. ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
  399. if (length < 0)
  400. ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
  401. if (array.Length - (index - lb) < length)
  402. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  403. if (array.Rank != 1)
  404. ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported);
  405. if (comparer == null) comparer = Comparer.Default;
  406. #if CORECLR
  407. if (comparer == Comparer.Default)
  408. {
  409. int retval;
  410. bool r = TrySZBinarySearch(array, index, length, value, out retval);
  411. if (r)
  412. return retval;
  413. }
  414. #endif
  415. int lo = index;
  416. int hi = index + length - 1;
  417. if (array is object[] objArray)
  418. {
  419. while (lo <= hi)
  420. {
  421. // i might overflow if lo and hi are both large positive numbers.
  422. int i = GetMedian(lo, hi);
  423. int c;
  424. try
  425. {
  426. c = comparer.Compare(objArray[i], value);
  427. }
  428. catch (Exception e)
  429. {
  430. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
  431. return default;
  432. }
  433. if (c == 0) return i;
  434. if (c < 0)
  435. {
  436. lo = i + 1;
  437. }
  438. else
  439. {
  440. hi = i - 1;
  441. }
  442. }
  443. }
  444. else
  445. {
  446. while (lo <= hi)
  447. {
  448. int i = GetMedian(lo, hi);
  449. int c;
  450. try
  451. {
  452. c = comparer.Compare(array.GetValue(i), value);
  453. }
  454. catch (Exception e)
  455. {
  456. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
  457. return default;
  458. }
  459. if (c == 0) return i;
  460. if (c < 0)
  461. {
  462. lo = i + 1;
  463. }
  464. else
  465. {
  466. hi = i - 1;
  467. }
  468. }
  469. }
  470. return ~lo;
  471. }
  472. public static int BinarySearch<T>(T[] array, T value)
  473. {
  474. if (array == null)
  475. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  476. return BinarySearch<T>(array, 0, array.Length, value, null);
  477. }
  478. public static int BinarySearch<T>(T[] array, T value, System.Collections.Generic.IComparer<T> comparer)
  479. {
  480. if (array == null)
  481. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  482. return BinarySearch<T>(array, 0, array.Length, value, comparer);
  483. }
  484. public static int BinarySearch<T>(T[] array, int index, int length, T value)
  485. {
  486. return BinarySearch<T>(array, index, length, value, null);
  487. }
  488. public static int BinarySearch<T>(T[] array, int index, int length, T value, System.Collections.Generic.IComparer<T> comparer)
  489. {
  490. if (array == null)
  491. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  492. if (index < 0)
  493. ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
  494. if (length < 0)
  495. ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
  496. if (array.Length - index < length)
  497. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  498. return ArraySortHelper<T>.Default.BinarySearch(array, index, length, value, comparer);
  499. }
  500. public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter)
  501. {
  502. if (array == null)
  503. {
  504. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  505. }
  506. if (converter == null)
  507. {
  508. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
  509. }
  510. TOutput[] newArray = new TOutput[array.Length];
  511. for (int i = 0; i < array.Length; i++)
  512. {
  513. newArray[i] = converter(array[i]);
  514. }
  515. return newArray;
  516. }
  517. // CopyTo copies a collection into an Array, starting at a particular
  518. // index into the array.
  519. //
  520. // This method is to support the ICollection interface, and calls
  521. // Array.Copy internally. If you aren't using ICollection explicitly,
  522. // call Array.Copy to avoid an extra indirection.
  523. //
  524. public void CopyTo(Array array, int index)
  525. {
  526. if (array != null && array.Rank != 1)
  527. ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
  528. // Note: Array.Copy throws a RankException and we want a consistent ArgumentException for all the IList CopyTo methods.
  529. Array.Copy(this, GetLowerBound(0), array, index, Length);
  530. }
  531. public void CopyTo(Array array, long index)
  532. {
  533. int iindex = (int)index;
  534. if (index != iindex)
  535. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported);
  536. this.CopyTo(array, iindex);
  537. }
  538. private static class EmptyArray<T>
  539. {
  540. internal static readonly T[] Value = new T[0];
  541. }
  542. public static T[] Empty<T>()
  543. {
  544. return EmptyArray<T>.Value;
  545. }
  546. public static bool Exists<T>(T[] array, Predicate<T> match)
  547. {
  548. return Array.FindIndex(array, match) != -1;
  549. }
  550. public static void Fill<T>(T[] array, T value)
  551. {
  552. if (array == null)
  553. {
  554. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  555. }
  556. for (int i = 0; i < array.Length; i++)
  557. {
  558. array[i] = value;
  559. }
  560. }
  561. public static void Fill<T>(T[] array, T value, int startIndex, int count)
  562. {
  563. if (array == null)
  564. {
  565. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  566. }
  567. if (startIndex < 0 || startIndex > array.Length)
  568. {
  569. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  570. }
  571. if (count < 0 || startIndex > array.Length - count)
  572. {
  573. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  574. }
  575. for (int i = startIndex; i < startIndex + count; i++)
  576. {
  577. array[i] = value;
  578. }
  579. }
  580. public static T Find<T>(T[] array, Predicate<T> match)
  581. {
  582. if (array == null)
  583. {
  584. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  585. }
  586. if (match == null)
  587. {
  588. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  589. }
  590. for (int i = 0; i < array.Length; i++)
  591. {
  592. if (match(array[i]))
  593. {
  594. return array[i];
  595. }
  596. }
  597. return default;
  598. }
  599. public static T[] FindAll<T>(T[] array, Predicate<T> match)
  600. {
  601. if (array == null)
  602. {
  603. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  604. }
  605. if (match == null)
  606. {
  607. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  608. }
  609. List<T> list = new List<T>();
  610. for (int i = 0; i < array.Length; i++)
  611. {
  612. if (match(array[i]))
  613. {
  614. list.Add(array[i]);
  615. }
  616. }
  617. return list.ToArray();
  618. }
  619. public static int FindIndex<T>(T[] array, Predicate<T> match)
  620. {
  621. if (array == null)
  622. {
  623. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  624. }
  625. return FindIndex(array, 0, array.Length, match);
  626. }
  627. public static int FindIndex<T>(T[] array, int startIndex, Predicate<T> match)
  628. {
  629. if (array == null)
  630. {
  631. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  632. }
  633. return FindIndex(array, startIndex, array.Length - startIndex, match);
  634. }
  635. public static int FindIndex<T>(T[] array, int startIndex, int count, Predicate<T> match)
  636. {
  637. if (array == null)
  638. {
  639. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  640. }
  641. if (startIndex < 0 || startIndex > array.Length)
  642. {
  643. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  644. }
  645. if (count < 0 || startIndex > array.Length - count)
  646. {
  647. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  648. }
  649. if (match == null)
  650. {
  651. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  652. }
  653. int endIndex = startIndex + count;
  654. for (int i = startIndex; i < endIndex; i++)
  655. {
  656. if (match(array[i]))
  657. return i;
  658. }
  659. return -1;
  660. }
  661. public static T FindLast<T>(T[] array, Predicate<T> match)
  662. {
  663. if (array == null)
  664. {
  665. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  666. }
  667. if (match == null)
  668. {
  669. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  670. }
  671. for (int i = array.Length - 1; i >= 0; i--)
  672. {
  673. if (match(array[i]))
  674. {
  675. return array[i];
  676. }
  677. }
  678. return default;
  679. }
  680. public static int FindLastIndex<T>(T[] array, Predicate<T> match)
  681. {
  682. if (array == null)
  683. {
  684. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  685. }
  686. return FindLastIndex(array, array.Length - 1, array.Length, match);
  687. }
  688. public static int FindLastIndex<T>(T[] array, int startIndex, Predicate<T> match)
  689. {
  690. if (array == null)
  691. {
  692. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  693. }
  694. return FindLastIndex(array, startIndex, startIndex + 1, match);
  695. }
  696. public static int FindLastIndex<T>(T[] array, int startIndex, int count, Predicate<T> match)
  697. {
  698. if (array == null)
  699. {
  700. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  701. }
  702. if (match == null)
  703. {
  704. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  705. }
  706. if (array.Length == 0)
  707. {
  708. // Special case for 0 length List
  709. if (startIndex != -1)
  710. {
  711. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  712. }
  713. }
  714. else
  715. {
  716. // Make sure we're not out of range
  717. if (startIndex < 0 || startIndex >= array.Length)
  718. {
  719. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  720. }
  721. }
  722. // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
  723. if (count < 0 || startIndex - count + 1 < 0)
  724. {
  725. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  726. }
  727. int endIndex = startIndex - count;
  728. for (int i = startIndex; i > endIndex; i--)
  729. {
  730. if (match(array[i]))
  731. {
  732. return i;
  733. }
  734. }
  735. return -1;
  736. }
  737. public static void ForEach<T>(T[] array, Action<T> action)
  738. {
  739. if (array == null)
  740. {
  741. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  742. }
  743. if (action == null)
  744. {
  745. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action);
  746. }
  747. for (int i = 0; i < array.Length; i++)
  748. {
  749. action(array[i]);
  750. }
  751. }
  752. // Returns the index of the first occurrence of a given value in an array.
  753. // The array is searched forwards, and the elements of the array are
  754. // compared to the given value using the Object.Equals method.
  755. //
  756. public static int IndexOf(Array array, object value)
  757. {
  758. if (array == null)
  759. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  760. return IndexOf(array, value, array.GetLowerBound(0), array.Length);
  761. }
  762. // Returns the index of the first occurrence of a given value in a range of
  763. // an array. The array is searched forwards, starting at index
  764. // startIndex and ending at the last element of the array. The
  765. // elements of the array are compared to the given value using the
  766. // Object.Equals method.
  767. //
  768. public static int IndexOf(Array array, object value, int startIndex)
  769. {
  770. if (array == null)
  771. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  772. int lb = array.GetLowerBound(0);
  773. return IndexOf(array, value, startIndex, array.Length - startIndex + lb);
  774. }
  775. // Returns the index of the first occurrence of a given value in a range of
  776. // an array. The array is searched forwards, starting at index
  777. // startIndex and upto count elements. The
  778. // elements of the array are compared to the given value using the
  779. // Object.Equals method.
  780. //
  781. public static int IndexOf(Array array, object value, int startIndex, int count)
  782. {
  783. if (array == null)
  784. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  785. if (array.Rank != 1)
  786. ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported);
  787. int lb = array.GetLowerBound(0);
  788. if (startIndex < lb || startIndex > array.Length + lb)
  789. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  790. if (count < 0 || count > array.Length - startIndex + lb)
  791. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  792. #if CORECLR
  793. // Try calling a quick native method to handle primitive types.
  794. int retVal;
  795. bool r = TrySZIndexOf(array, startIndex, count, value, out retVal);
  796. if (r)
  797. return retVal;
  798. #endif
  799. object[] objArray = array as object[];
  800. int endIndex = startIndex + count;
  801. if (objArray != null)
  802. {
  803. if (value == null)
  804. {
  805. for (int i = startIndex; i < endIndex; i++)
  806. {
  807. if (objArray[i] == null)
  808. return i;
  809. }
  810. }
  811. else
  812. {
  813. for (int i = startIndex; i < endIndex; i++)
  814. {
  815. object obj = objArray[i];
  816. if (obj != null && obj.Equals(value))
  817. return i;
  818. }
  819. }
  820. }
  821. else
  822. {
  823. for (int i = startIndex; i < endIndex; i++)
  824. {
  825. object obj = array.GetValue(i);
  826. if (obj == null)
  827. {
  828. if (value == null)
  829. return i;
  830. }
  831. else
  832. {
  833. if (obj.Equals(value))
  834. return i;
  835. }
  836. }
  837. }
  838. // Return one less than the lower bound of the array. This way,
  839. // for arrays with a lower bound of -1 we will not return -1 when the
  840. // item was not found. And for SZArrays (the vast majority), -1 still
  841. // works for them.
  842. return lb - 1;
  843. }
  844. public static int IndexOf<T>(T[] array, T value)
  845. {
  846. if (array == null)
  847. {
  848. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  849. }
  850. return IndexOf(array, value, 0, array.Length);
  851. }
  852. public static int IndexOf<T>(T[] array, T value, int startIndex)
  853. {
  854. if (array == null)
  855. {
  856. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  857. }
  858. return IndexOf(array, value, startIndex, array.Length - startIndex);
  859. }
  860. public static int IndexOf<T>(T[] array, T value, int startIndex, int count)
  861. {
  862. if (array == null)
  863. {
  864. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  865. }
  866. if ((uint)startIndex > (uint)array.Length)
  867. {
  868. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  869. }
  870. if ((uint)count > (uint)(array.Length - startIndex))
  871. {
  872. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  873. }
  874. // Hits a code generation bug on ProjectN
  875. #if !PROJECTN
  876. if (typeof(T) == typeof(byte))
  877. {
  878. int result = SpanHelpers.IndexOf(
  879. ref Unsafe.Add(ref array.GetRawSzArrayData(), startIndex),
  880. Unsafe.As<T, byte>(ref value),
  881. count);
  882. return (result >= 0 ? startIndex : 0) + result;
  883. }
  884. if (typeof(T) == typeof(char))
  885. {
  886. int result = SpanHelpers.IndexOf(
  887. ref Unsafe.Add(ref Unsafe.As<byte, char>(ref array.GetRawSzArrayData()), startIndex),
  888. Unsafe.As<T, char>(ref value),
  889. count);
  890. return (result >= 0 ? startIndex : 0) + result;
  891. }
  892. #endif
  893. #if CORECLR
  894. return EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count);
  895. #else
  896. return IndexOfImpl(array, value, startIndex, count);
  897. #endif
  898. }
  899. // Returns the index of the last occurrence of a given value in an array.
  900. // The array is searched backwards, and the elements of the array are
  901. // compared to the given value using the Object.Equals method.
  902. //
  903. public static int LastIndexOf(Array array, object value)
  904. {
  905. if (array == null)
  906. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  907. int lb = array.GetLowerBound(0);
  908. return LastIndexOf(array, value, array.Length - 1 + lb, array.Length);
  909. }
  910. // Returns the index of the last occurrence of a given value in a range of
  911. // an array. The array is searched backwards, starting at index
  912. // startIndex and ending at index 0. The elements of the array are
  913. // compared to the given value using the Object.Equals method.
  914. //
  915. public static int LastIndexOf(Array array, object value, int startIndex)
  916. {
  917. if (array == null)
  918. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  919. int lb = array.GetLowerBound(0);
  920. return LastIndexOf(array, value, startIndex, startIndex + 1 - lb);
  921. }
  922. // Returns the index of the last occurrence of a given value in a range of
  923. // an array. The array is searched backwards, starting at index
  924. // startIndex and counting uptocount elements. The elements of
  925. // the array are compared to the given value using the Object.Equals
  926. // method.
  927. //
  928. public static int LastIndexOf(Array array, object value, int startIndex, int count)
  929. {
  930. if (array == null)
  931. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  932. int lb = array.GetLowerBound(0);
  933. if (array.Length == 0)
  934. {
  935. return lb - 1;
  936. }
  937. if (startIndex < lb || startIndex >= array.Length + lb)
  938. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  939. if (count < 0)
  940. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  941. if (count > startIndex - lb + 1)
  942. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.endIndex, ExceptionResource.ArgumentOutOfRange_EndIndexStartIndex);
  943. if (array.Rank != 1)
  944. ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported);
  945. #if CORECLR
  946. // Try calling a quick native method to handle primitive types.
  947. int retVal;
  948. bool r = TrySZLastIndexOf(array, startIndex, count, value, out retVal);
  949. if (r)
  950. return retVal;
  951. #endif
  952. object[] objArray = array as object[];
  953. int endIndex = startIndex - count + 1;
  954. if (objArray != null)
  955. {
  956. if (value == null)
  957. {
  958. for (int i = startIndex; i >= endIndex; i--)
  959. {
  960. if (objArray[i] == null)
  961. return i;
  962. }
  963. }
  964. else
  965. {
  966. for (int i = startIndex; i >= endIndex; i--)
  967. {
  968. object obj = objArray[i];
  969. if (obj != null && obj.Equals(value))
  970. return i;
  971. }
  972. }
  973. }
  974. else
  975. {
  976. for (int i = startIndex; i >= endIndex; i--)
  977. {
  978. object obj = array.GetValue(i);
  979. if (obj == null)
  980. {
  981. if (value == null)
  982. return i;
  983. }
  984. else
  985. {
  986. if (obj.Equals(value))
  987. return i;
  988. }
  989. }
  990. }
  991. return lb - 1; // Return lb-1 for arrays with negative lower bounds.
  992. }
  993. public static int LastIndexOf<T>(T[] array, T value)
  994. {
  995. if (array == null)
  996. {
  997. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  998. }
  999. return LastIndexOf(array, value, array.Length - 1, array.Length);
  1000. }
  1001. public static int LastIndexOf<T>(T[] array, T value, int startIndex)
  1002. {
  1003. if (array == null)
  1004. {
  1005. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1006. }
  1007. // if array is empty and startIndex is 0, we need to pass 0 as count
  1008. return LastIndexOf(array, value, startIndex, (array.Length == 0) ? 0 : (startIndex + 1));
  1009. }
  1010. public static int LastIndexOf<T>(T[] array, T value, int startIndex, int count)
  1011. {
  1012. if (array == null)
  1013. {
  1014. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1015. }
  1016. if (array.Length == 0)
  1017. {
  1018. //
  1019. // Special case for 0 length List
  1020. // accept -1 and 0 as valid startIndex for compablility reason.
  1021. //
  1022. if (startIndex != -1 && startIndex != 0)
  1023. {
  1024. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  1025. }
  1026. // only 0 is a valid value for count if array is empty
  1027. if (count != 0)
  1028. {
  1029. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  1030. }
  1031. return -1;
  1032. }
  1033. // Make sure we're not out of range
  1034. if ((uint)startIndex >= (uint)array.Length)
  1035. {
  1036. ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
  1037. }
  1038. // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
  1039. if (count < 0 || startIndex - count + 1 < 0)
  1040. {
  1041. ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
  1042. }
  1043. // Hits a code generation bug on ProjectN
  1044. #if !PROJECTN
  1045. if (typeof(T) == typeof(byte))
  1046. {
  1047. int endIndex = startIndex - count + 1;
  1048. int result = SpanHelpers.LastIndexOf(
  1049. ref Unsafe.Add(ref array.GetRawSzArrayData(), endIndex),
  1050. Unsafe.As<T, byte>(ref value),
  1051. count);
  1052. return (result >= 0 ? endIndex : 0) + result;
  1053. }
  1054. if (typeof(T) == typeof(char))
  1055. {
  1056. int endIndex = startIndex - count + 1;
  1057. int result = SpanHelpers.LastIndexOf(
  1058. ref Unsafe.Add(ref Unsafe.As<byte, char>(ref array.GetRawSzArrayData()), endIndex),
  1059. Unsafe.As<T, char>(ref value),
  1060. count);
  1061. return (result >= 0 ? endIndex : 0) + result;
  1062. }
  1063. #endif
  1064. #if CORECLR
  1065. return EqualityComparer<T>.Default.LastIndexOf(array, value, startIndex, count);
  1066. #else
  1067. return LastIndexOfImpl(array, value, startIndex, count);
  1068. #endif
  1069. }
  1070. // Reverses all elements of the given array. Following a call to this
  1071. // method, an element previously located at index i will now be
  1072. // located at index length - i - 1, where length is the
  1073. // length of the array.
  1074. //
  1075. public static void Reverse(Array array)
  1076. {
  1077. if (array == null)
  1078. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1079. Reverse(array, array.GetLowerBound(0), array.Length);
  1080. }
  1081. // Reverses the elements in a range of an array. Following a call to this
  1082. // method, an element in the range given by index and count
  1083. // which was previously located at index i will now be located at
  1084. // index index + (index + count - i - 1).
  1085. // Reliability note: This may fail because it may have to box objects.
  1086. //
  1087. public static void Reverse(Array array, int index, int length)
  1088. {
  1089. if (array == null)
  1090. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1091. int lowerBound = array.GetLowerBound(0);
  1092. if (index < lowerBound)
  1093. ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
  1094. if (length < 0)
  1095. ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
  1096. if (array.Length - (index - lowerBound) < length)
  1097. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  1098. if (array.Rank != 1)
  1099. ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported);
  1100. if (length <= 1)
  1101. return;
  1102. #if CORECLR
  1103. bool r = TrySZReverse(array, index, length);
  1104. if (r)
  1105. return;
  1106. #endif
  1107. if (array is object[] objArray)
  1108. {
  1109. Array.Reverse<object>(objArray, index, length);
  1110. }
  1111. else
  1112. {
  1113. int i = index;
  1114. int j = index + length - 1;
  1115. while (i < j)
  1116. {
  1117. object temp = array.GetValue(i);
  1118. array.SetValue(array.GetValue(j), i);
  1119. array.SetValue(temp, j);
  1120. i++;
  1121. j--;
  1122. }
  1123. }
  1124. }
  1125. public static void Reverse<T>(T[] array)
  1126. {
  1127. if (array == null)
  1128. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1129. Reverse(array, 0, array.Length);
  1130. }
  1131. public static void Reverse<T>(T[] array, int index, int length)
  1132. {
  1133. if (array == null)
  1134. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1135. if (index < 0)
  1136. ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
  1137. if (length < 0)
  1138. ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
  1139. if (array.Length - index < length)
  1140. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  1141. if (length <= 1)
  1142. return;
  1143. ref T first = ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), index);
  1144. ref T last = ref Unsafe.Add(ref Unsafe.Add(ref first, length), -1);
  1145. do
  1146. {
  1147. T temp = first;
  1148. first = last;
  1149. last = temp;
  1150. first = ref Unsafe.Add(ref first, 1);
  1151. last = ref Unsafe.Add(ref last, -1);
  1152. } while (Unsafe.IsAddressLessThan(ref first, ref last));
  1153. }
  1154. // Sorts the elements of an array. The sort compares the elements to each
  1155. // other using the IComparable interface, which must be implemented
  1156. // by all elements of the array.
  1157. //
  1158. public static void Sort(Array array)
  1159. {
  1160. if (array == null)
  1161. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1162. Sort(array, null, array.GetLowerBound(0), array.Length, null);
  1163. }
  1164. // Sorts the elements of two arrays based on the keys in the first array.
  1165. // Elements in the keys array specify the sort keys for
  1166. // corresponding elements in the items array. The sort compares the
  1167. // keys to each other using the IComparable interface, which must be
  1168. // implemented by all elements of the keys array.
  1169. //
  1170. public static void Sort(Array keys, Array items)
  1171. {
  1172. if (keys == null)
  1173. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys);
  1174. Sort(keys, items, keys.GetLowerBound(0), keys.Length, null);
  1175. }
  1176. // Sorts the elements in a section of an array. The sort compares the
  1177. // elements to each other using the IComparable interface, which
  1178. // must be implemented by all elements in the given section of the array.
  1179. //
  1180. public static void Sort(Array array, int index, int length)
  1181. {
  1182. Sort(array, null, index, length, null);
  1183. }
  1184. // Sorts the elements in a section of two arrays based on the keys in the
  1185. // first array. Elements in the keys array specify the sort keys for
  1186. // corresponding elements in the items array. The sort compares the
  1187. // keys to each other using the IComparable interface, which must be
  1188. // implemented by all elements of the keys array.
  1189. //
  1190. public static void Sort(Array keys, Array items, int index, int length)
  1191. {
  1192. Sort(keys, items, index, length, null);
  1193. }
  1194. // Sorts the elements of an array. The sort compares the elements to each
  1195. // other using the given IComparer interface. If comparer is
  1196. // null, the elements are compared to each other using the
  1197. // IComparable interface, which in that case must be implemented by
  1198. // all elements of the array.
  1199. //
  1200. public static void Sort(Array array, IComparer comparer)
  1201. {
  1202. if (array == null)
  1203. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1204. Sort(array, null, array.GetLowerBound(0), array.Length, comparer);
  1205. }
  1206. // Sorts the elements of two arrays based on the keys in the first array.
  1207. // Elements in the keys array specify the sort keys for
  1208. // corresponding elements in the items array. The sort compares the
  1209. // keys to each other using the given IComparer interface. If
  1210. // comparer is null, the elements are compared to each other using
  1211. // the IComparable interface, which in that case must be implemented
  1212. // by all elements of the keys array.
  1213. //
  1214. public static void Sort(Array keys, Array items, IComparer comparer)
  1215. {
  1216. if (keys == null)
  1217. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys);
  1218. Sort(keys, items, keys.GetLowerBound(0), keys.Length, comparer);
  1219. }
  1220. // Sorts the elements in a section of an array. The sort compares the
  1221. // elements to each other using the given IComparer interface. If
  1222. // comparer is null, the elements are compared to each other using
  1223. // the IComparable interface, which in that case must be implemented
  1224. // by all elements in the given section of the array.
  1225. //
  1226. public static void Sort(Array array, int index, int length, IComparer comparer)
  1227. {
  1228. Sort(array, null, index, length, comparer);
  1229. }
  1230. // Sorts the elements in a section of two arrays based on the keys in the
  1231. // first array. Elements in the keys array specify the sort keys for
  1232. // corresponding elements in the items array. The sort compares the
  1233. // keys to each other using the given IComparer interface. If
  1234. // comparer is null, the elements are compared to each other using
  1235. // the IComparable interface, which in that case must be implemented
  1236. // by all elements of the given section of the keys array.
  1237. //
  1238. public static void Sort(Array keys, Array items, int index, int length, IComparer comparer)
  1239. {
  1240. if (keys == null)
  1241. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys);
  1242. if (keys.Rank != 1 || (items != null && items.Rank != 1))
  1243. ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported);
  1244. int keysLowerBound = keys.GetLowerBound(0);
  1245. if (items != null && keysLowerBound != items.GetLowerBound(0))
  1246. ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_LowerBoundsMustMatch);
  1247. if (index < keysLowerBound)
  1248. ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
  1249. if (length < 0)
  1250. ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
  1251. if (keys.Length - (index - keysLowerBound) < length || (items != null && (index - keysLowerBound) > items.Length - length))
  1252. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  1253. if (length > 1)
  1254. {
  1255. SortImpl(keys, items, index, length, comparer ?? Comparer.Default);
  1256. }
  1257. }
  1258. public static void Sort<T>(T[] array)
  1259. {
  1260. if (array == null)
  1261. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1262. Sort<T>(array, 0, array.Length, null);
  1263. }
  1264. public static void Sort<TKey, TValue>(TKey[] keys, TValue[] items)
  1265. {
  1266. if (keys == null)
  1267. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys);
  1268. Sort<TKey, TValue>(keys, items, 0, keys.Length, null);
  1269. }
  1270. public static void Sort<T>(T[] array, int index, int length)
  1271. {
  1272. Sort<T>(array, index, length, null);
  1273. }
  1274. public static void Sort<TKey, TValue>(TKey[] keys, TValue[] items, int index, int length)
  1275. {
  1276. Sort<TKey, TValue>(keys, items, index, length, null);
  1277. }
  1278. public static void Sort<T>(T[] array, System.Collections.Generic.IComparer<T> comparer)
  1279. {
  1280. if (array == null)
  1281. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1282. Sort<T>(array, 0, array.Length, comparer);
  1283. }
  1284. public static void Sort<TKey, TValue>(TKey[] keys, TValue[] items, System.Collections.Generic.IComparer<TKey> comparer)
  1285. {
  1286. if (keys == null)
  1287. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys);
  1288. Sort<TKey, TValue>(keys, items, 0, keys.Length, comparer);
  1289. }
  1290. public static void Sort<T>(T[] array, int index, int length, System.Collections.Generic.IComparer<T> comparer)
  1291. {
  1292. if (array == null)
  1293. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1294. if (index < 0)
  1295. ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
  1296. if (length < 0)
  1297. ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
  1298. if (array.Length - index < length)
  1299. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  1300. if (length > 1)
  1301. {
  1302. #if CORECLR
  1303. if (comparer == null || comparer == Comparer<T>.Default)
  1304. {
  1305. if (TrySZSort(array, null, index, index + length - 1))
  1306. {
  1307. return;
  1308. }
  1309. }
  1310. #endif
  1311. ArraySortHelper<T>.Default.Sort(array, index, length, comparer);
  1312. }
  1313. }
  1314. public static void Sort<TKey, TValue>(TKey[] keys, TValue[] items, int index, int length, System.Collections.Generic.IComparer<TKey> comparer)
  1315. {
  1316. if (keys == null)
  1317. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys);
  1318. if (index < 0)
  1319. ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
  1320. if (length < 0)
  1321. ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
  1322. if (keys.Length - index < length || (items != null && index > items.Length - length))
  1323. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  1324. if (length > 1)
  1325. {
  1326. #if CORECLR
  1327. if (comparer == null || comparer == Comparer<TKey>.Default)
  1328. {
  1329. if (TrySZSort(keys, items, index, index + length - 1))
  1330. {
  1331. return;
  1332. }
  1333. }
  1334. #endif
  1335. if (items == null)
  1336. {
  1337. Sort<TKey>(keys, index, length, comparer);
  1338. return;
  1339. }
  1340. ArraySortHelper<TKey, TValue>.Default.Sort(keys, items, index, length, comparer);
  1341. }
  1342. }
  1343. public static void Sort<T>(T[] array, Comparison<T> comparison)
  1344. {
  1345. if (array == null)
  1346. {
  1347. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1348. }
  1349. if (comparison == null)
  1350. {
  1351. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison);
  1352. }
  1353. ArraySortHelper<T>.Sort(array, 0, array.Length, comparison);
  1354. }
  1355. public static bool TrueForAll<T>(T[] array, Predicate<T> match)
  1356. {
  1357. if (array == null)
  1358. {
  1359. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
  1360. }
  1361. if (match == null)
  1362. {
  1363. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  1364. }
  1365. for (int i = 0; i < array.Length; i++)
  1366. {
  1367. if (!match(array[i]))
  1368. {
  1369. return false;
  1370. }
  1371. }
  1372. return true;
  1373. }
  1374. #if !CORERT
  1375. // Private value type used by the Sort methods.
  1376. private readonly struct SorterObjectArray
  1377. {
  1378. private readonly object[] keys;
  1379. private readonly object[] items;
  1380. private readonly IComparer comparer;
  1381. internal SorterObjectArray(object[] keys, object[] items, IComparer comparer)
  1382. {
  1383. this.keys = keys;
  1384. this.items = items;
  1385. this.comparer = comparer;
  1386. }
  1387. internal void SwapIfGreaterWithItems(int a, int b)
  1388. {
  1389. if (a != b)
  1390. {
  1391. if (comparer.Compare(keys[a], keys[b]) > 0)
  1392. {
  1393. object temp = keys[a];
  1394. keys[a] = keys[b];
  1395. keys[b] = temp;
  1396. if (items != null)
  1397. {
  1398. object item = items[a];
  1399. items[a] = items[b];
  1400. items[b] = item;
  1401. }
  1402. }
  1403. }
  1404. }
  1405. private void Swap(int i, int j)
  1406. {
  1407. object t = keys[i];
  1408. keys[i] = keys[j];
  1409. keys[j] = t;
  1410. if (items != null)
  1411. {
  1412. object item = items[i];
  1413. items[i] = items[j];
  1414. items[j] = item;
  1415. }
  1416. }
  1417. internal void Sort(int left, int length)
  1418. {
  1419. IntrospectiveSort(left, length);
  1420. }
  1421. private void IntrospectiveSort(int left, int length)
  1422. {
  1423. if (length < 2)
  1424. return;
  1425. try
  1426. {
  1427. IntroSort(left, length + left - 1, 2 * IntrospectiveSortUtilities.FloorLog2PlusOne(length));
  1428. }
  1429. catch (IndexOutOfRangeException)
  1430. {
  1431. IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer(comparer);
  1432. }
  1433. catch (Exception e)
  1434. {
  1435. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
  1436. }
  1437. }
  1438. private void IntroSort(int lo, int hi, int depthLimit)
  1439. {
  1440. while (hi > lo)
  1441. {
  1442. int partitionSize = hi - lo + 1;
  1443. if (partitionSize <= IntrospectiveSortUtilities.IntrosortSizeThreshold)
  1444. {
  1445. if (partitionSize == 1)
  1446. {
  1447. return;
  1448. }
  1449. if (partitionSize == 2)
  1450. {
  1451. SwapIfGreaterWithItems(lo, hi);
  1452. return;
  1453. }
  1454. if (partitionSize == 3)
  1455. {
  1456. SwapIfGreaterWithItems(lo, hi - 1);
  1457. SwapIfGreaterWithItems(lo, hi);
  1458. SwapIfGreaterWithItems(hi - 1, hi);
  1459. return;
  1460. }
  1461. InsertionSort(lo, hi);
  1462. return;
  1463. }
  1464. if (depthLimit == 0)
  1465. {
  1466. Heapsort(lo, hi);
  1467. return;
  1468. }
  1469. depthLimit--;
  1470. int p = PickPivotAndPartition(lo, hi);
  1471. IntroSort(p + 1, hi, depthLimit);
  1472. hi = p - 1;
  1473. }
  1474. }
  1475. private int PickPivotAndPartition(int lo, int hi)
  1476. {
  1477. // Compute median-of-three. But also partition them, since we've done the comparison.
  1478. int mid = lo + (hi - lo) / 2;
  1479. // Sort lo, mid and hi appropriately, then pick mid as the pivot.
  1480. SwapIfGreaterWithItems(lo, mid);
  1481. SwapIfGreaterWithItems(lo, hi);
  1482. SwapIfGreaterWithItems(mid, hi);
  1483. object pivot = keys[mid];
  1484. Swap(mid, hi - 1);
  1485. int left = lo, right = hi - 1; // We already partitioned lo and hi and put the pivot in hi - 1. And we pre-increment & decrement below.
  1486. while (left < right)
  1487. {
  1488. while (comparer.Compare(keys[++left], pivot) < 0) ;
  1489. while (comparer.Compare(pivot, keys[--right]) < 0) ;
  1490. if (left >= right)
  1491. break;
  1492. Swap(left, right);
  1493. }
  1494. // Put pivot in the right location.
  1495. Swap(left, (hi - 1));
  1496. return left;
  1497. }
  1498. private void Heapsort(int lo, int hi)
  1499. {
  1500. int n = hi - lo + 1;
  1501. for (int i = n / 2; i >= 1; i = i - 1)
  1502. {
  1503. DownHeap(i, n, lo);
  1504. }
  1505. for (int i = n; i > 1; i = i - 1)
  1506. {
  1507. Swap(lo, lo + i - 1);
  1508. DownHeap(1, i - 1, lo);
  1509. }
  1510. }
  1511. private void DownHeap(int i, int n, int lo)
  1512. {
  1513. object d = keys[lo + i - 1];
  1514. object dt = (items != null) ? items[lo + i - 1] : null;
  1515. int child;
  1516. while (i <= n / 2)
  1517. {
  1518. child = 2 * i;
  1519. if (child < n && comparer.Compare(keys[lo + child - 1], keys[lo + child]) < 0)
  1520. {
  1521. child++;
  1522. }
  1523. if (!(comparer.Compare(d, keys[lo + child - 1]) < 0))
  1524. break;
  1525. keys[lo + i - 1] = keys[lo + child - 1];
  1526. if (items != null)
  1527. items[lo + i - 1] = items[lo + child - 1];
  1528. i = child;
  1529. }
  1530. keys[lo + i - 1] = d;
  1531. if (items != null)
  1532. items[lo + i - 1] = dt;
  1533. }
  1534. private void InsertionSort(int lo, int hi)
  1535. {
  1536. int i, j;
  1537. object t, ti;
  1538. for (i = lo; i < hi; i++)
  1539. {
  1540. j = i;
  1541. t = keys[i + 1];
  1542. ti = (items != null) ? items[i + 1] : null;
  1543. while (j >= lo && comparer.Compare(t, keys[j]) < 0)
  1544. {
  1545. keys[j + 1] = keys[j];
  1546. if (items != null)
  1547. items[j + 1] = items[j];
  1548. j--;
  1549. }
  1550. keys[j + 1] = t;
  1551. if (items != null)
  1552. items[j + 1] = ti;
  1553. }
  1554. }
  1555. }
  1556. // Private value used by the Sort methods for instances of Array.
  1557. // This is slower than the one for Object[], since we can't use the JIT helpers
  1558. // to access the elements. We must use GetValue & SetValue.
  1559. private readonly struct SorterGenericArray
  1560. {
  1561. private readonly Array keys;
  1562. private readonly Array items;
  1563. private readonly IComparer comparer;
  1564. internal SorterGenericArray(Array keys, Array items, IComparer comparer)
  1565. {
  1566. this.keys = keys;
  1567. this.items = items;
  1568. this.comparer = comparer;
  1569. }
  1570. internal void SwapIfGreaterWithItems(int a, int b)
  1571. {
  1572. if (a != b)
  1573. {
  1574. if (comparer.Compare(keys.GetValue(a), keys.GetValue(b)) > 0)
  1575. {
  1576. object key = keys.GetValue(a);
  1577. keys.SetValue(keys.GetValue(b), a);
  1578. keys.SetValue(key, b);
  1579. if (items != null)
  1580. {
  1581. object item = items.GetValue(a);
  1582. items.SetValue(items.GetValue(b), a);
  1583. items.SetValue(item, b);
  1584. }
  1585. }
  1586. }
  1587. }
  1588. private void Swap(int i, int j)
  1589. {
  1590. object t1 = keys.GetValue(i);
  1591. keys.SetValue(keys.GetValue(j), i);
  1592. keys.SetValue(t1, j);
  1593. if (items != null)
  1594. {
  1595. object t2 = items.GetValue(i);
  1596. items.SetValue(items.GetValue(j), i);
  1597. items.SetValue(t2, j);
  1598. }
  1599. }
  1600. internal void Sort(int left, int length)
  1601. {
  1602. IntrospectiveSort(left, length);
  1603. }
  1604. private void IntrospectiveSort(int left, int length)
  1605. {
  1606. if (length < 2)
  1607. return;
  1608. try
  1609. {
  1610. IntroSort(left, length + left - 1, 2 * IntrospectiveSortUtilities.FloorLog2PlusOne(length));
  1611. }
  1612. catch (IndexOutOfRangeException)
  1613. {
  1614. IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer(comparer);
  1615. }
  1616. catch (Exception e)
  1617. {
  1618. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
  1619. }
  1620. }
  1621. private void IntroSort(int lo, int hi, int depthLimit)
  1622. {
  1623. while (hi > lo)
  1624. {
  1625. int partitionSize = hi - lo + 1;
  1626. if (partitionSize <= IntrospectiveSortUtilities.IntrosortSizeThreshold)
  1627. {
  1628. if (partitionSize == 1)
  1629. {
  1630. return;
  1631. }
  1632. if (partitionSize == 2)
  1633. {
  1634. SwapIfGreaterWithItems(lo, hi);
  1635. return;
  1636. }
  1637. if (partitionSize == 3)
  1638. {
  1639. SwapIfGreaterWithItems(lo, hi - 1);
  1640. SwapIfGreaterWithItems(lo, hi);
  1641. SwapIfGreaterWithItems(hi - 1, hi);
  1642. return;
  1643. }
  1644. InsertionSort(lo, hi);
  1645. return;
  1646. }
  1647. if (depthLimit == 0)
  1648. {
  1649. Heapsort(lo, hi);
  1650. return;
  1651. }
  1652. depthLimit--;
  1653. int p = PickPivotAndPartition(lo, hi);
  1654. IntroSort(p + 1, hi, depthLimit);
  1655. hi = p - 1;
  1656. }
  1657. }
  1658. private int PickPivotAndPartition(int lo, int hi)
  1659. {
  1660. // Compute median-of-three. But also partition them, since we've done the comparison.
  1661. int mid = lo + (hi - lo) / 2;
  1662. SwapIfGreaterWithItems(lo, mid);
  1663. SwapIfGreaterWithItems(lo, hi);
  1664. SwapIfGreaterWithItems(mid, hi);
  1665. object pivot = keys.GetValue(mid);
  1666. Swap(mid, hi - 1);
  1667. int left = lo, right = hi - 1; // We already partitioned lo and hi and put the pivot in hi - 1. And we pre-increment & decrement below.
  1668. while (left < right)
  1669. {
  1670. while (comparer.Compare(keys.GetValue(++left), pivot) < 0) ;
  1671. while (comparer.Compare(pivot, keys.GetValue(--right)) < 0) ;
  1672. if (left >= right)
  1673. break;
  1674. Swap(left, right);
  1675. }
  1676. // Put pivot in the right location.
  1677. Swap(left, (hi - 1));
  1678. return left;
  1679. }
  1680. private void Heapsort(int lo, int hi)
  1681. {
  1682. int n = hi - lo + 1;
  1683. for (int i = n / 2; i >= 1; i = i - 1)
  1684. {
  1685. DownHeap(i, n, lo);
  1686. }
  1687. for (int i = n; i > 1; i = i - 1)
  1688. {
  1689. Swap(lo, lo + i - 1);
  1690. DownHeap(1, i - 1, lo);
  1691. }
  1692. }
  1693. private void DownHeap(int i, int n, int lo)
  1694. {
  1695. object d = keys.GetValue(lo + i - 1);
  1696. object dt = (items != null) ? items.GetValue(lo + i - 1) : null;
  1697. int child;
  1698. while (i <= n / 2)
  1699. {
  1700. child = 2 * i;
  1701. if (child < n && comparer.Compare(keys.GetValue(lo + child - 1), keys.GetValue(lo + child)) < 0)
  1702. {
  1703. child++;
  1704. }
  1705. if (!(comparer.Compare(d, keys.GetValue(lo + child - 1)) < 0))
  1706. break;
  1707. keys.SetValue(keys.GetValue(lo + child - 1), lo + i - 1);
  1708. if (items != null)
  1709. items.SetValue(items.GetValue(lo + child - 1), lo + i - 1);
  1710. i = child;
  1711. }
  1712. keys.SetValue(d, lo + i - 1);
  1713. if (items != null)
  1714. items.SetValue(dt, lo + i - 1);
  1715. }
  1716. private void InsertionSort(int lo, int hi)
  1717. {
  1718. int i, j;
  1719. object t, dt;
  1720. for (i = lo; i < hi; i++)
  1721. {
  1722. j = i;
  1723. t = keys.GetValue(i + 1);
  1724. dt = (items != null) ? items.GetValue(i + 1) : null;
  1725. while (j >= lo && comparer.Compare(t, keys.GetValue(j)) < 0)
  1726. {
  1727. keys.SetValue(keys.GetValue(j), j + 1);
  1728. if (items != null)
  1729. items.SetValue(items.GetValue(j), j + 1);
  1730. j--;
  1731. }
  1732. keys.SetValue(t, j + 1);
  1733. if (items != null)
  1734. items.SetValue(dt, j + 1);
  1735. }
  1736. }
  1737. }
  1738. public IEnumerator GetEnumerator()
  1739. {
  1740. int lowerBound = GetLowerBound(0);
  1741. if (Rank == 1 && lowerBound == 0)
  1742. return new SZArrayEnumerator(this);
  1743. else
  1744. return new ArrayEnumerator(this, lowerBound, Length);
  1745. }
  1746. #endif // !CORERT
  1747. }
  1748. }