You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Range.cs 9.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // https://github.com/dotnet/corefx/blob/1597b894a2e9cac668ce6e484506eca778a85197/src/Common/src/CoreLib/System/Index.cs
  2. // https://github.com/dotnet/corefx/blob/1597b894a2e9cac668ce6e484506eca778a85197/src/Common/src/CoreLib/System/Range.cs
  3. using System.Runtime.CompilerServices;
  4. namespace System
  5. {
  6. /// <summary>Represent a type can be used to index a collection either from the start or the end.</summary>
  7. /// <remarks>
  8. /// Index is used by the C# compiler to support the new index syntax
  9. /// <code>
  10. /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ;
  11. /// int lastElement = someArray[^1]; // lastElement = 5
  12. /// </code>
  13. /// </remarks>
  14. public readonly struct Index : IEquatable<Index>
  15. {
  16. private readonly int _value;
  17. /// <summary>Construct an Index using a value and indicating if the index is from the start or from the end.</summary>
  18. /// <param name="value">The index value. it has to be zero or positive number.</param>
  19. /// <param name="fromEnd">Indicating if the index is from the start or from the end.</param>
  20. /// <remarks>
  21. /// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element.
  22. /// </remarks>
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public Index(int value, bool fromEnd = false)
  25. {
  26. if (value < 0)
  27. {
  28. throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative");
  29. }
  30. if (fromEnd)
  31. _value = ~value;
  32. else
  33. _value = value;
  34. }
  35. // The following private constructors mainly created for perf reason to avoid the checks
  36. private Index(int value)
  37. {
  38. _value = value;
  39. }
  40. /// <summary>Create an Index pointing at first element.</summary>
  41. public static Index Start => new Index(0);
  42. /// <summary>Create an Index pointing at beyond last element.</summary>
  43. public static Index End => new Index(~0);
  44. /// <summary>Create an Index from the start at the position indicated by the value.</summary>
  45. /// <param name="value">The index value from the start.</param>
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static Index FromStart(int value)
  48. {
  49. if (value < 0)
  50. {
  51. throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative");
  52. }
  53. return new Index(value);
  54. }
  55. /// <summary>Create an Index from the end at the position indicated by the value.</summary>
  56. /// <param name="value">The index value from the end.</param>
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static Index FromEnd(int value)
  59. {
  60. if (value < 0)
  61. {
  62. throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative");
  63. }
  64. return new Index(~value);
  65. }
  66. /// <summary>Returns the index value.</summary>
  67. public int Value
  68. {
  69. get
  70. {
  71. if (_value < 0)
  72. {
  73. return ~_value;
  74. }
  75. else
  76. {
  77. return _value;
  78. }
  79. }
  80. }
  81. /// <summary>Indicates whether the index is from the start or the end.</summary>
  82. public bool IsFromEnd => _value < 0;
  83. /// <summary>Calculate the offset from the start using the giving collection length.</summary>
  84. /// <param name="length">The length of the collection that the Index will be used with. length has to be a positive value</param>
  85. /// <remarks>
  86. /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values.
  87. /// we don't validate either the returned offset is greater than the input length.
  88. /// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and
  89. /// then used to index a collection will get out of range exception which will be same affect as the validation.
  90. /// </remarks>
  91. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  92. public int GetOffset(int length)
  93. {
  94. var offset = _value;
  95. if (IsFromEnd)
  96. {
  97. // offset = length - (~value)
  98. // offset = length + (~(~value) + 1)
  99. // offset = length + value + 1
  100. offset += length + 1;
  101. }
  102. return offset;
  103. }
  104. /// <summary>Indicates whether the current Index object is equal to another object of the same type.</summary>
  105. /// <param name="value">An object to compare with this object</param>
  106. public override bool Equals(object? value) => value is Index && _value == ((Index)value)._value;
  107. /// <summary>Indicates whether the current Index object is equal to another Index object.</summary>
  108. /// <param name="other">An object to compare with this object</param>
  109. public bool Equals(Index other) => _value == other._value;
  110. /// <summary>Returns the hash code for this instance.</summary>
  111. public override int GetHashCode() => _value;
  112. /// <summary>Converts integer number to an Index.</summary>
  113. public static implicit operator Index(int value) => FromStart(value);
  114. /// <summary>Converts the value of the current Index object to its equivalent string representation.</summary>
  115. public override string ToString()
  116. {
  117. if (IsFromEnd)
  118. return "^" + ((uint)Value).ToString();
  119. return ((uint)Value).ToString();
  120. }
  121. }
  122. /// <summary>Represent a range has start and end indexes.</summary>
  123. /// <remarks>
  124. /// Range is used by the C# compiler to support the range syntax.
  125. /// <code>
  126. /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 };
  127. /// int[] subArray1 = someArray[0..2]; // { 1, 2 }
  128. /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 }
  129. /// </code>
  130. /// </remarks>
  131. public readonly struct Range : IEquatable<Range>
  132. {
  133. /// <summary>Represent the inclusive start index of the Range.</summary>
  134. public Index Start { get; }
  135. /// <summary>Represent the exclusive end index of the Range.</summary>
  136. public Index End { get; }
  137. /// <summary>Construct a Range object using the start and end indexes.</summary>
  138. /// <param name="start">Represent the inclusive start index of the range.</param>
  139. /// <param name="end">Represent the exclusive end index of the range.</param>
  140. public Range(Index start, Index end)
  141. {
  142. Start = start;
  143. End = end;
  144. }
  145. /// <summary>Indicates whether the current Range object is equal to another object of the same type.</summary>
  146. /// <param name="value">An object to compare with this object</param>
  147. public override bool Equals(object? value) =>
  148. value is Range r &&
  149. r.Start.Equals(Start) &&
  150. r.End.Equals(End);
  151. /// <summary>Indicates whether the current Range object is equal to another Range object.</summary>
  152. /// <param name="other">An object to compare with this object</param>
  153. public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End);
  154. /// <summary>Returns the hash code for this instance.</summary>
  155. public override int GetHashCode()
  156. {
  157. return Start.GetHashCode() * 31 + End.GetHashCode();
  158. }
  159. /// <summary>Converts the value of the current Range object to its equivalent string representation.</summary>
  160. public override string ToString()
  161. {
  162. return Start + ".." + End;
  163. }
  164. /// <summary>Create a Range object starting from start index to the end of the collection.</summary>
  165. public static Range StartAt(Index start) => new Range(start, Index.End);
  166. /// <summary>Create a Range object starting from first element in the collection to the end Index.</summary>
  167. public static Range EndAt(Index end) => new Range(Index.Start, end);
  168. /// <summary>Create a Range object starting from first element to the end.</summary>
  169. public static Range All => new Range(Index.Start, Index.End);
  170. /// <summary>Calculate the start offset and length of range object using a collection length.</summary>
  171. /// <param name="length">The length of the collection that the range will be used with. length has to be a positive value.</param>
  172. /// <remarks>
  173. /// For performance reason, we don't validate the input length parameter against negative values.
  174. /// It is expected Range will be used with collections which always have non negative length/count.
  175. /// We validate the range is inside the length scope though.
  176. /// </remarks>
  177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  178. public (int Offset, int Length) GetOffsetAndLength(int length)
  179. {
  180. int start;
  181. var startIndex = Start;
  182. if (startIndex.IsFromEnd)
  183. start = length - startIndex.Value;
  184. else
  185. start = startIndex.Value;
  186. int end;
  187. var endIndex = End;
  188. if (endIndex.IsFromEnd)
  189. end = length - endIndex.Value;
  190. else
  191. end = endIndex.Value;
  192. if ((uint)end > (uint)length || (uint)start > (uint)end)
  193. {
  194. throw new ArgumentOutOfRangeException(nameof(length));
  195. }
  196. return (start, end - start);
  197. }
  198. }
  199. }