Tuple.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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.Diagnostics;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. namespace System
  10. {
  11. /// <summary>
  12. /// Helper so we can call some tuple methods recursively without knowing the underlying types.
  13. /// </summary>
  14. internal interface ITupleInternal : ITuple
  15. {
  16. string ToString(StringBuilder sb);
  17. int GetHashCode(IEqualityComparer comparer);
  18. }
  19. public static class Tuple
  20. {
  21. public static Tuple<T1> Create<T1>(T1 item1)
  22. {
  23. return new Tuple<T1>(item1);
  24. }
  25. public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
  26. {
  27. return new Tuple<T1, T2>(item1, item2);
  28. }
  29. public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
  30. {
  31. return new Tuple<T1, T2, T3>(item1, item2, item3);
  32. }
  33. public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4)
  34. {
  35. return new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4);
  36. }
  37. public static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
  38. {
  39. return new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
  40. }
  41. public static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
  42. {
  43. return new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
  44. }
  45. public static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
  46. {
  47. return new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
  48. }
  49. public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>> Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, T8 item8)
  50. {
  51. return new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>(item1, item2, item3, item4, item5, item6, item7, new Tuple<T8>(item8));
  52. }
  53. // Note: F# compiler depends on the exact tuple hashing algorithm. Do not ever change it.
  54. // From System.Web.Util.HashCodeCombiner
  55. internal static int CombineHashCodes(int h1, int h2)
  56. {
  57. return ((h1 << 5) + h1) ^ h2;
  58. }
  59. internal static int CombineHashCodes(int h1, int h2, int h3)
  60. {
  61. return CombineHashCodes(CombineHashCodes(h1, h2), h3);
  62. }
  63. internal static int CombineHashCodes(int h1, int h2, int h3, int h4)
  64. {
  65. return CombineHashCodes(CombineHashCodes(h1, h2), CombineHashCodes(h3, h4));
  66. }
  67. internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5)
  68. {
  69. return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), h5);
  70. }
  71. internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6)
  72. {
  73. return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6));
  74. }
  75. internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7)
  76. {
  77. return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7));
  78. }
  79. internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8)
  80. {
  81. return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7, h8));
  82. }
  83. }
  84. [Serializable]
  85. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  86. public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple
  87. {
  88. private readonly T1 m_Item1; // Do not rename (binary serialization)
  89. public T1 Item1 => m_Item1;
  90. public Tuple(T1 item1)
  91. {
  92. m_Item1 = item1;
  93. }
  94. public override bool Equals(object? obj)
  95. {
  96. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  97. }
  98. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  99. {
  100. if (other == null) return false;
  101. if (!(other is Tuple<T1> objTuple))
  102. {
  103. return false;
  104. }
  105. return comparer.Equals(m_Item1, objTuple.m_Item1);
  106. }
  107. int IComparable.CompareTo(object? obj)
  108. {
  109. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  110. }
  111. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  112. {
  113. if (other == null) return 1;
  114. if (!(other is Tuple<T1> objTuple))
  115. {
  116. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  117. }
  118. return comparer.Compare(m_Item1, objTuple.m_Item1);
  119. }
  120. public override int GetHashCode()
  121. {
  122. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  123. }
  124. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  125. {
  126. return comparer.GetHashCode(m_Item1!);
  127. }
  128. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  129. {
  130. return ((IStructuralEquatable)this).GetHashCode(comparer);
  131. }
  132. public override string ToString()
  133. {
  134. StringBuilder sb = new StringBuilder();
  135. sb.Append('(');
  136. return ((ITupleInternal)this).ToString(sb);
  137. }
  138. string ITupleInternal.ToString(StringBuilder sb)
  139. {
  140. sb.Append(m_Item1);
  141. sb.Append(')');
  142. return sb.ToString();
  143. }
  144. /// <summary>
  145. /// The number of positions in this data structure.
  146. /// </summary>
  147. int ITuple.Length => 1;
  148. /// <summary>
  149. /// Get the element at position <param name="index"/>.
  150. /// </summary>
  151. object? ITuple.this[int index]
  152. {
  153. get
  154. {
  155. if (index != 0)
  156. {
  157. throw new IndexOutOfRangeException();
  158. }
  159. return Item1;
  160. }
  161. }
  162. }
  163. [Serializable]
  164. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  165. public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple
  166. {
  167. private readonly T1 m_Item1; // Do not rename (binary serialization)
  168. private readonly T2 m_Item2; // Do not rename (binary serialization)
  169. public T1 Item1 => m_Item1;
  170. public T2 Item2 => m_Item2;
  171. public Tuple(T1 item1, T2 item2)
  172. {
  173. m_Item1 = item1;
  174. m_Item2 = item2;
  175. }
  176. public override bool Equals(object? obj)
  177. {
  178. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  179. }
  180. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  181. {
  182. if (other == null) return false;
  183. if (!(other is Tuple<T1, T2> objTuple))
  184. {
  185. return false;
  186. }
  187. return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2);
  188. }
  189. int IComparable.CompareTo(object? obj)
  190. {
  191. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  192. }
  193. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  194. {
  195. if (other == null) return 1;
  196. if (!(other is Tuple<T1, T2> objTuple))
  197. {
  198. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  199. }
  200. int c = comparer.Compare(m_Item1, objTuple.m_Item1);
  201. if (c != 0) return c;
  202. return comparer.Compare(m_Item2, objTuple.m_Item2);
  203. }
  204. public override int GetHashCode()
  205. {
  206. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  207. }
  208. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  209. {
  210. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!));
  211. }
  212. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  213. {
  214. return ((IStructuralEquatable)this).GetHashCode(comparer);
  215. }
  216. public override string ToString()
  217. {
  218. StringBuilder sb = new StringBuilder();
  219. sb.Append('(');
  220. return ((ITupleInternal)this).ToString(sb);
  221. }
  222. string ITupleInternal.ToString(StringBuilder sb)
  223. {
  224. sb.Append(m_Item1);
  225. sb.Append(", ");
  226. sb.Append(m_Item2);
  227. sb.Append(')');
  228. return sb.ToString();
  229. }
  230. /// <summary>
  231. /// The number of positions in this data structure.
  232. /// </summary>
  233. int ITuple.Length => 2;
  234. /// <summary>
  235. /// Get the element at position <param name="index"/>.
  236. /// </summary>
  237. object? ITuple.this[int index] =>
  238. index switch
  239. {
  240. 0 => Item1,
  241. 1 => Item2,
  242. _ => throw new IndexOutOfRangeException(),
  243. };
  244. }
  245. [Serializable]
  246. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  247. public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple
  248. {
  249. private readonly T1 m_Item1; // Do not rename (binary serialization)
  250. private readonly T2 m_Item2; // Do not rename (binary serialization)
  251. private readonly T3 m_Item3; // Do not rename (binary serialization)
  252. public T1 Item1 => m_Item1;
  253. public T2 Item2 => m_Item2;
  254. public T3 Item3 => m_Item3;
  255. public Tuple(T1 item1, T2 item2, T3 item3)
  256. {
  257. m_Item1 = item1;
  258. m_Item2 = item2;
  259. m_Item3 = item3;
  260. }
  261. public override bool Equals(object? obj)
  262. {
  263. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  264. }
  265. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  266. {
  267. if (other == null) return false;
  268. if (!(other is Tuple<T1, T2, T3> objTuple))
  269. {
  270. return false;
  271. }
  272. return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2) && comparer.Equals(m_Item3, objTuple.m_Item3);
  273. }
  274. int IComparable.CompareTo(object? obj)
  275. {
  276. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  277. }
  278. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  279. {
  280. if (other == null) return 1;
  281. if (!(other is Tuple<T1, T2, T3> objTuple))
  282. {
  283. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  284. }
  285. int c = comparer.Compare(m_Item1, objTuple.m_Item1);
  286. if (c != 0) return c;
  287. c = comparer.Compare(m_Item2, objTuple.m_Item2);
  288. if (c != 0) return c;
  289. return comparer.Compare(m_Item3, objTuple.m_Item3);
  290. }
  291. public override int GetHashCode()
  292. {
  293. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  294. }
  295. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  296. {
  297. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!));
  298. }
  299. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  300. {
  301. return ((IStructuralEquatable)this).GetHashCode(comparer);
  302. }
  303. public override string ToString()
  304. {
  305. StringBuilder sb = new StringBuilder();
  306. sb.Append('(');
  307. return ((ITupleInternal)this).ToString(sb);
  308. }
  309. string ITupleInternal.ToString(StringBuilder sb)
  310. {
  311. sb.Append(m_Item1);
  312. sb.Append(", ");
  313. sb.Append(m_Item2);
  314. sb.Append(", ");
  315. sb.Append(m_Item3);
  316. sb.Append(')');
  317. return sb.ToString();
  318. }
  319. /// <summary>
  320. /// The number of positions in this data structure.
  321. /// </summary>
  322. int ITuple.Length => 3;
  323. /// <summary>
  324. /// Get the element at position <param name="index"/>.
  325. /// </summary>
  326. object? ITuple.this[int index] =>
  327. index switch
  328. {
  329. 0 => Item1,
  330. 1 => Item2,
  331. 2 => Item3,
  332. _ => throw new IndexOutOfRangeException(),
  333. };
  334. }
  335. [Serializable]
  336. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  337. public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple
  338. {
  339. private readonly T1 m_Item1; // Do not rename (binary serialization)
  340. private readonly T2 m_Item2; // Do not rename (binary serialization)
  341. private readonly T3 m_Item3; // Do not rename (binary serialization)
  342. private readonly T4 m_Item4; // Do not rename (binary serialization)
  343. public T1 Item1 => m_Item1;
  344. public T2 Item2 => m_Item2;
  345. public T3 Item3 => m_Item3;
  346. public T4 Item4 => m_Item4;
  347. public Tuple(T1 item1, T2 item2, T3 item3, T4 item4)
  348. {
  349. m_Item1 = item1;
  350. m_Item2 = item2;
  351. m_Item3 = item3;
  352. m_Item4 = item4;
  353. }
  354. public override bool Equals(object? obj)
  355. {
  356. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  357. }
  358. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  359. {
  360. if (other == null) return false;
  361. if (!(other is Tuple<T1, T2, T3, T4> objTuple))
  362. {
  363. return false;
  364. }
  365. return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2) && comparer.Equals(m_Item3, objTuple.m_Item3) && comparer.Equals(m_Item4, objTuple.m_Item4);
  366. }
  367. int IComparable.CompareTo(object? obj)
  368. {
  369. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  370. }
  371. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  372. {
  373. if (other == null) return 1;
  374. if (!(other is Tuple<T1, T2, T3, T4> objTuple))
  375. {
  376. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  377. }
  378. int c = comparer.Compare(m_Item1, objTuple.m_Item1);
  379. if (c != 0) return c;
  380. c = comparer.Compare(m_Item2, objTuple.m_Item2);
  381. if (c != 0) return c;
  382. c = comparer.Compare(m_Item3, objTuple.m_Item3);
  383. if (c != 0) return c;
  384. return comparer.Compare(m_Item4, objTuple.m_Item4);
  385. }
  386. public override int GetHashCode()
  387. {
  388. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  389. }
  390. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  391. {
  392. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!));
  393. }
  394. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  395. {
  396. return ((IStructuralEquatable)this).GetHashCode(comparer);
  397. }
  398. public override string ToString()
  399. {
  400. StringBuilder sb = new StringBuilder();
  401. sb.Append('(');
  402. return ((ITupleInternal)this).ToString(sb);
  403. }
  404. string ITupleInternal.ToString(StringBuilder sb)
  405. {
  406. sb.Append(m_Item1);
  407. sb.Append(", ");
  408. sb.Append(m_Item2);
  409. sb.Append(", ");
  410. sb.Append(m_Item3);
  411. sb.Append(", ");
  412. sb.Append(m_Item4);
  413. sb.Append(')');
  414. return sb.ToString();
  415. }
  416. /// <summary>
  417. /// The number of positions in this data structure.
  418. /// </summary>
  419. int ITuple.Length => 4;
  420. /// <summary>
  421. /// Get the element at position <param name="index"/>.
  422. /// </summary>
  423. object? ITuple.this[int index] =>
  424. index switch
  425. {
  426. 0 => Item1,
  427. 1 => Item2,
  428. 2 => Item3,
  429. 3 => Item4,
  430. _ => throw new IndexOutOfRangeException(),
  431. };
  432. }
  433. [Serializable]
  434. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  435. public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple
  436. {
  437. private readonly T1 m_Item1; // Do not rename (binary serialization)
  438. private readonly T2 m_Item2; // Do not rename (binary serialization)
  439. private readonly T3 m_Item3; // Do not rename (binary serialization)
  440. private readonly T4 m_Item4; // Do not rename (binary serialization)
  441. private readonly T5 m_Item5; // Do not rename (binary serialization)
  442. public T1 Item1 => m_Item1;
  443. public T2 Item2 => m_Item2;
  444. public T3 Item3 => m_Item3;
  445. public T4 Item4 => m_Item4;
  446. public T5 Item5 => m_Item5;
  447. public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
  448. {
  449. m_Item1 = item1;
  450. m_Item2 = item2;
  451. m_Item3 = item3;
  452. m_Item4 = item4;
  453. m_Item5 = item5;
  454. }
  455. public override bool Equals(object? obj)
  456. {
  457. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  458. }
  459. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  460. {
  461. if (other == null) return false;
  462. if (!(other is Tuple<T1, T2, T3, T4, T5> objTuple))
  463. {
  464. return false;
  465. }
  466. return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2) && comparer.Equals(m_Item3, objTuple.m_Item3) && comparer.Equals(m_Item4, objTuple.m_Item4) && comparer.Equals(m_Item5, objTuple.m_Item5);
  467. }
  468. int IComparable.CompareTo(object? obj)
  469. {
  470. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  471. }
  472. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  473. {
  474. if (other == null) return 1;
  475. if (!(other is Tuple<T1, T2, T3, T4, T5> objTuple))
  476. {
  477. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  478. }
  479. int c = comparer.Compare(m_Item1, objTuple.m_Item1);
  480. if (c != 0) return c;
  481. c = comparer.Compare(m_Item2, objTuple.m_Item2);
  482. if (c != 0) return c;
  483. c = comparer.Compare(m_Item3, objTuple.m_Item3);
  484. if (c != 0) return c;
  485. c = comparer.Compare(m_Item4, objTuple.m_Item4);
  486. if (c != 0) return c;
  487. return comparer.Compare(m_Item5, objTuple.m_Item5);
  488. }
  489. public override int GetHashCode()
  490. {
  491. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  492. }
  493. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  494. {
  495. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!));
  496. }
  497. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  498. {
  499. return ((IStructuralEquatable)this).GetHashCode(comparer);
  500. }
  501. public override string ToString()
  502. {
  503. StringBuilder sb = new StringBuilder();
  504. sb.Append('(');
  505. return ((ITupleInternal)this).ToString(sb);
  506. }
  507. string ITupleInternal.ToString(StringBuilder sb)
  508. {
  509. sb.Append(m_Item1);
  510. sb.Append(", ");
  511. sb.Append(m_Item2);
  512. sb.Append(", ");
  513. sb.Append(m_Item3);
  514. sb.Append(", ");
  515. sb.Append(m_Item4);
  516. sb.Append(", ");
  517. sb.Append(m_Item5);
  518. sb.Append(')');
  519. return sb.ToString();
  520. }
  521. /// <summary>
  522. /// The number of positions in this data structure.
  523. /// </summary>
  524. int ITuple.Length => 5;
  525. /// <summary>
  526. /// Get the element at position <param name="index"/>.
  527. /// </summary>
  528. object? ITuple.this[int index] =>
  529. index switch
  530. {
  531. 0 => Item1,
  532. 1 => Item2,
  533. 2 => Item3,
  534. 3 => Item4,
  535. 4 => Item5,
  536. _ => throw new IndexOutOfRangeException(),
  537. };
  538. }
  539. [Serializable]
  540. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  541. public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple
  542. {
  543. private readonly T1 m_Item1; // Do not rename (binary serialization)
  544. private readonly T2 m_Item2; // Do not rename (binary serialization)
  545. private readonly T3 m_Item3; // Do not rename (binary serialization)
  546. private readonly T4 m_Item4; // Do not rename (binary serialization)
  547. private readonly T5 m_Item5; // Do not rename (binary serialization)
  548. private readonly T6 m_Item6; // Do not rename (binary serialization)
  549. public T1 Item1 => m_Item1;
  550. public T2 Item2 => m_Item2;
  551. public T3 Item3 => m_Item3;
  552. public T4 Item4 => m_Item4;
  553. public T5 Item5 => m_Item5;
  554. public T6 Item6 => m_Item6;
  555. public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
  556. {
  557. m_Item1 = item1;
  558. m_Item2 = item2;
  559. m_Item3 = item3;
  560. m_Item4 = item4;
  561. m_Item5 = item5;
  562. m_Item6 = item6;
  563. }
  564. public override bool Equals(object? obj)
  565. {
  566. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  567. }
  568. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  569. {
  570. if (other == null) return false;
  571. if (!(other is Tuple<T1, T2, T3, T4, T5, T6> objTuple))
  572. {
  573. return false;
  574. }
  575. return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2) && comparer.Equals(m_Item3, objTuple.m_Item3) && comparer.Equals(m_Item4, objTuple.m_Item4) && comparer.Equals(m_Item5, objTuple.m_Item5) && comparer.Equals(m_Item6, objTuple.m_Item6);
  576. }
  577. int IComparable.CompareTo(object? obj)
  578. {
  579. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  580. }
  581. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  582. {
  583. if (other == null) return 1;
  584. if (!(other is Tuple<T1, T2, T3, T4, T5, T6> objTuple))
  585. {
  586. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  587. }
  588. int c = comparer.Compare(m_Item1, objTuple.m_Item1);
  589. if (c != 0) return c;
  590. c = comparer.Compare(m_Item2, objTuple.m_Item2);
  591. if (c != 0) return c;
  592. c = comparer.Compare(m_Item3, objTuple.m_Item3);
  593. if (c != 0) return c;
  594. c = comparer.Compare(m_Item4, objTuple.m_Item4);
  595. if (c != 0) return c;
  596. c = comparer.Compare(m_Item5, objTuple.m_Item5);
  597. if (c != 0) return c;
  598. return comparer.Compare(m_Item6, objTuple.m_Item6);
  599. }
  600. public override int GetHashCode()
  601. {
  602. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  603. }
  604. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  605. {
  606. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!));
  607. }
  608. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  609. {
  610. return ((IStructuralEquatable)this).GetHashCode(comparer);
  611. }
  612. public override string ToString()
  613. {
  614. StringBuilder sb = new StringBuilder();
  615. sb.Append('(');
  616. return ((ITupleInternal)this).ToString(sb);
  617. }
  618. string ITupleInternal.ToString(StringBuilder sb)
  619. {
  620. sb.Append(m_Item1);
  621. sb.Append(", ");
  622. sb.Append(m_Item2);
  623. sb.Append(", ");
  624. sb.Append(m_Item3);
  625. sb.Append(", ");
  626. sb.Append(m_Item4);
  627. sb.Append(", ");
  628. sb.Append(m_Item5);
  629. sb.Append(", ");
  630. sb.Append(m_Item6);
  631. sb.Append(')');
  632. return sb.ToString();
  633. }
  634. /// <summary>
  635. /// The number of positions in this data structure.
  636. /// </summary>
  637. int ITuple.Length => 6;
  638. /// <summary>
  639. /// Get the element at position <param name="index"/>.
  640. /// </summary>
  641. object? ITuple.this[int index] =>
  642. index switch
  643. {
  644. 0 => Item1,
  645. 1 => Item2,
  646. 2 => Item3,
  647. 3 => Item4,
  648. 4 => Item5,
  649. 5 => Item6,
  650. _ => throw new IndexOutOfRangeException(),
  651. };
  652. }
  653. [Serializable]
  654. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  655. public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple
  656. {
  657. private readonly T1 m_Item1; // Do not rename (binary serialization)
  658. private readonly T2 m_Item2; // Do not rename (binary serialization)
  659. private readonly T3 m_Item3; // Do not rename (binary serialization)
  660. private readonly T4 m_Item4; // Do not rename (binary serialization)
  661. private readonly T5 m_Item5; // Do not rename (binary serialization)
  662. private readonly T6 m_Item6; // Do not rename (binary serialization)
  663. private readonly T7 m_Item7; // Do not rename (binary serialization)
  664. public T1 Item1 => m_Item1;
  665. public T2 Item2 => m_Item2;
  666. public T3 Item3 => m_Item3;
  667. public T4 Item4 => m_Item4;
  668. public T5 Item5 => m_Item5;
  669. public T6 Item6 => m_Item6;
  670. public T7 Item7 => m_Item7;
  671. public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
  672. {
  673. m_Item1 = item1;
  674. m_Item2 = item2;
  675. m_Item3 = item3;
  676. m_Item4 = item4;
  677. m_Item5 = item5;
  678. m_Item6 = item6;
  679. m_Item7 = item7;
  680. }
  681. public override bool Equals(object? obj)
  682. {
  683. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  684. }
  685. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  686. {
  687. if (other == null) return false;
  688. if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7> objTuple))
  689. {
  690. return false;
  691. }
  692. return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2) && comparer.Equals(m_Item3, objTuple.m_Item3) && comparer.Equals(m_Item4, objTuple.m_Item4) && comparer.Equals(m_Item5, objTuple.m_Item5) && comparer.Equals(m_Item6, objTuple.m_Item6) && comparer.Equals(m_Item7, objTuple.m_Item7);
  693. }
  694. int IComparable.CompareTo(object? obj)
  695. {
  696. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  697. }
  698. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  699. {
  700. if (other == null) return 1;
  701. if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7> objTuple))
  702. {
  703. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  704. }
  705. int c = comparer.Compare(m_Item1, objTuple.m_Item1);
  706. if (c != 0) return c;
  707. c = comparer.Compare(m_Item2, objTuple.m_Item2);
  708. if (c != 0) return c;
  709. c = comparer.Compare(m_Item3, objTuple.m_Item3);
  710. if (c != 0) return c;
  711. c = comparer.Compare(m_Item4, objTuple.m_Item4);
  712. if (c != 0) return c;
  713. c = comparer.Compare(m_Item5, objTuple.m_Item5);
  714. if (c != 0) return c;
  715. c = comparer.Compare(m_Item6, objTuple.m_Item6);
  716. if (c != 0) return c;
  717. return comparer.Compare(m_Item7, objTuple.m_Item7);
  718. }
  719. public override int GetHashCode()
  720. {
  721. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  722. }
  723. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  724. {
  725. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!));
  726. }
  727. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  728. {
  729. return ((IStructuralEquatable)this).GetHashCode(comparer);
  730. }
  731. public override string ToString()
  732. {
  733. StringBuilder sb = new StringBuilder();
  734. sb.Append('(');
  735. return ((ITupleInternal)this).ToString(sb);
  736. }
  737. string ITupleInternal.ToString(StringBuilder sb)
  738. {
  739. sb.Append(m_Item1);
  740. sb.Append(", ");
  741. sb.Append(m_Item2);
  742. sb.Append(", ");
  743. sb.Append(m_Item3);
  744. sb.Append(", ");
  745. sb.Append(m_Item4);
  746. sb.Append(", ");
  747. sb.Append(m_Item5);
  748. sb.Append(", ");
  749. sb.Append(m_Item6);
  750. sb.Append(", ");
  751. sb.Append(m_Item7);
  752. sb.Append(')');
  753. return sb.ToString();
  754. }
  755. /// <summary>
  756. /// The number of positions in this data structure.
  757. /// </summary>
  758. int ITuple.Length => 7;
  759. /// <summary>
  760. /// Get the element at position <param name="index"/>.
  761. /// </summary>
  762. object? ITuple.this[int index] =>
  763. index switch
  764. {
  765. 0 => Item1,
  766. 1 => Item2,
  767. 2 => Item3,
  768. 3 => Item4,
  769. 4 => Item5,
  770. 5 => Item6,
  771. 6 => Item7,
  772. _ => throw new IndexOutOfRangeException(),
  773. };
  774. }
  775. [Serializable]
  776. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  777. public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable, ITupleInternal, ITuple where TRest : notnull
  778. {
  779. private readonly T1 m_Item1; // Do not rename (binary serialization)
  780. private readonly T2 m_Item2; // Do not rename (binary serialization)
  781. private readonly T3 m_Item3; // Do not rename (binary serialization)
  782. private readonly T4 m_Item4; // Do not rename (binary serialization)
  783. private readonly T5 m_Item5; // Do not rename (binary serialization)
  784. private readonly T6 m_Item6; // Do not rename (binary serialization)
  785. private readonly T7 m_Item7; // Do not rename (binary serialization)
  786. private readonly TRest m_Rest; // Do not rename (binary serialization)
  787. public T1 Item1 => m_Item1;
  788. public T2 Item2 => m_Item2;
  789. public T3 Item3 => m_Item3;
  790. public T4 Item4 => m_Item4;
  791. public T5 Item5 => m_Item5;
  792. public T6 Item6 => m_Item6;
  793. public T7 Item7 => m_Item7;
  794. public TRest Rest => m_Rest;
  795. public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest)
  796. {
  797. if (!(rest is ITupleInternal))
  798. {
  799. throw new ArgumentException(SR.ArgumentException_TupleLastArgumentNotATuple);
  800. }
  801. m_Item1 = item1;
  802. m_Item2 = item2;
  803. m_Item3 = item3;
  804. m_Item4 = item4;
  805. m_Item5 = item5;
  806. m_Item6 = item6;
  807. m_Item7 = item7;
  808. m_Rest = rest;
  809. }
  810. public override bool Equals(object? obj)
  811. {
  812. return ((IStructuralEquatable)this).Equals(obj, EqualityComparer<object>.Default);
  813. }
  814. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  815. {
  816. if (other == null) return false;
  817. if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple))
  818. {
  819. return false;
  820. }
  821. return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2) && comparer.Equals(m_Item3, objTuple.m_Item3) && comparer.Equals(m_Item4, objTuple.m_Item4) && comparer.Equals(m_Item5, objTuple.m_Item5) && comparer.Equals(m_Item6, objTuple.m_Item6) && comparer.Equals(m_Item7, objTuple.m_Item7) && comparer.Equals(m_Rest, objTuple.m_Rest);
  822. }
  823. int IComparable.CompareTo(object? obj)
  824. {
  825. return ((IStructuralComparable)this).CompareTo(obj, Comparer<object>.Default);
  826. }
  827. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  828. {
  829. if (other == null) return 1;
  830. if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple))
  831. {
  832. throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, GetType()), nameof(other));
  833. }
  834. int c = comparer.Compare(m_Item1, objTuple.m_Item1);
  835. if (c != 0) return c;
  836. c = comparer.Compare(m_Item2, objTuple.m_Item2);
  837. if (c != 0) return c;
  838. c = comparer.Compare(m_Item3, objTuple.m_Item3);
  839. if (c != 0) return c;
  840. c = comparer.Compare(m_Item4, objTuple.m_Item4);
  841. if (c != 0) return c;
  842. c = comparer.Compare(m_Item5, objTuple.m_Item5);
  843. if (c != 0) return c;
  844. c = comparer.Compare(m_Item6, objTuple.m_Item6);
  845. if (c != 0) return c;
  846. c = comparer.Compare(m_Item7, objTuple.m_Item7);
  847. if (c != 0) return c;
  848. return comparer.Compare(m_Rest, objTuple.m_Rest);
  849. }
  850. public override int GetHashCode()
  851. {
  852. return ((IStructuralEquatable)this).GetHashCode(EqualityComparer<object>.Default);
  853. }
  854. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  855. {
  856. // We want to have a limited hash in this case. We'll use the last 8 elements of the tuple
  857. ITupleInternal t = (ITupleInternal)m_Rest;
  858. if (t.Length >= 8) { return t.GetHashCode(comparer); }
  859. // In this case, the m_Rest member has fewer than 8 elements so we need to combine our elements with the elements in m_Rest.
  860. int k = 8 - t.Length;
  861. switch (k)
  862. {
  863. case 1:
  864. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item7!), t.GetHashCode(comparer));
  865. case 2:
  866. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!), t.GetHashCode(comparer));
  867. case 3:
  868. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!), t.GetHashCode(comparer));
  869. case 4:
  870. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!), t.GetHashCode(comparer));
  871. case 5:
  872. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!), t.GetHashCode(comparer));
  873. case 6:
  874. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!), t.GetHashCode(comparer));
  875. case 7:
  876. return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!), t.GetHashCode(comparer));
  877. }
  878. Debug.Fail("Missed all cases for computing Tuple hash code");
  879. return -1;
  880. }
  881. int ITupleInternal.GetHashCode(IEqualityComparer comparer)
  882. {
  883. return ((IStructuralEquatable)this).GetHashCode(comparer);
  884. }
  885. public override string ToString()
  886. {
  887. StringBuilder sb = new StringBuilder();
  888. sb.Append('(');
  889. return ((ITupleInternal)this).ToString(sb);
  890. }
  891. string ITupleInternal.ToString(StringBuilder sb)
  892. {
  893. sb.Append(m_Item1);
  894. sb.Append(", ");
  895. sb.Append(m_Item2);
  896. sb.Append(", ");
  897. sb.Append(m_Item3);
  898. sb.Append(", ");
  899. sb.Append(m_Item4);
  900. sb.Append(", ");
  901. sb.Append(m_Item5);
  902. sb.Append(", ");
  903. sb.Append(m_Item6);
  904. sb.Append(", ");
  905. sb.Append(m_Item7);
  906. sb.Append(", ");
  907. return ((ITupleInternal)m_Rest).ToString(sb);
  908. }
  909. /// <summary>
  910. /// The number of positions in this data structure.
  911. /// </summary>
  912. int ITuple.Length => 7 + ((ITupleInternal)Rest).Length;
  913. /// <summary>
  914. /// Get the element at position <param name="index"/>.
  915. /// </summary>
  916. object? ITuple.this[int index] =>
  917. index switch
  918. {
  919. 0 => Item1,
  920. 1 => Item2,
  921. 2 => Item3,
  922. 3 => Item4,
  923. 4 => Item5,
  924. 5 => Item6,
  925. 6 => Item7,
  926. _ => ((ITupleInternal)Rest)[index - 7],
  927. };
  928. }
  929. }