|
| 1 | +// <auto-generated> |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | + |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +namespace System |
| 9 | +{ |
| 10 | + /// <summary>Represent a type can be used to index a collection either from the start or the end.</summary> |
| 11 | + /// <remarks> |
| 12 | + /// Index is used by the C# compiler to support the new index syntax |
| 13 | + /// <code> |
| 14 | + /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ; |
| 15 | + /// int lastElement = someArray[^1]; // lastElement = 5 |
| 16 | + /// </code> |
| 17 | + /// </remarks> |
| 18 | + internal readonly struct Index : IEquatable<Index> |
| 19 | + { |
| 20 | + private readonly int _value; |
| 21 | + |
| 22 | + /// <summary>Construct an Index using a value and indicating if the index is from the start or from the end.</summary> |
| 23 | + /// <param name="value">The index value. it has to be zero or positive number.</param> |
| 24 | + /// <param name="fromEnd">Indicating if the index is from the start or from the end.</param> |
| 25 | + /// <remarks> |
| 26 | + /// 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. |
| 27 | + /// </remarks> |
| 28 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 29 | + public Index(int value, bool fromEnd = false) |
| 30 | + { |
| 31 | + if (value < 0) |
| 32 | + { |
| 33 | + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 34 | + } |
| 35 | + |
| 36 | + if (fromEnd) |
| 37 | + _value = ~value; |
| 38 | + else |
| 39 | + _value = value; |
| 40 | + } |
| 41 | + |
| 42 | + // The following private constructors mainly created for perf reason to avoid the checks |
| 43 | + private Index(int value) => _value = value; |
| 44 | + |
| 45 | + /// <summary>Create an Index pointing at first element.</summary> |
| 46 | + public static Index Start => new Index(0); |
| 47 | + |
| 48 | + /// <summary>Create an Index pointing at beyond last element.</summary> |
| 49 | + public static Index End => new Index(~0); |
| 50 | + |
| 51 | + /// <summary>Create an Index from the start at the position indicated by the value.</summary> |
| 52 | + /// <param name="value">The index value from the start.</param> |
| 53 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 54 | + public static Index FromStart(int value) |
| 55 | + { |
| 56 | + if (value < 0) |
| 57 | + { |
| 58 | + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 59 | + } |
| 60 | + |
| 61 | + return new Index(value); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary>Create an Index from the end at the position indicated by the value.</summary> |
| 65 | + /// <param name="value">The index value from the end.</param> |
| 66 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 67 | + public static Index FromEnd(int value) |
| 68 | + { |
| 69 | + if (value < 0) |
| 70 | + { |
| 71 | + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 72 | + } |
| 73 | + |
| 74 | + return new Index(~value); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary>Returns the index value.</summary> |
| 78 | + public int Value |
| 79 | + { |
| 80 | + get |
| 81 | + { |
| 82 | + if (_value < 0) |
| 83 | + return ~_value; |
| 84 | + else |
| 85 | + return _value; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary>Indicates whether the index is from the start or the end.</summary> |
| 90 | + public bool IsFromEnd => _value < 0; |
| 91 | + |
| 92 | + /// <summary>Calculate the offset from the start using the giving collection length.</summary> |
| 93 | + /// <param name="length">The length of the collection that the Index will be used with. length has to be a positive value</param> |
| 94 | + /// <remarks> |
| 95 | + /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values. |
| 96 | + /// we don't validate either the returned offset is greater than the input length. |
| 97 | + /// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and |
| 98 | + /// then used to index a collection will get out of range exception which will be same affect as the validation. |
| 99 | + /// </remarks> |
| 100 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 101 | + public int GetOffset(int length) |
| 102 | + { |
| 103 | + int offset = _value; |
| 104 | + if (IsFromEnd) |
| 105 | + { |
| 106 | + // offset = length - (~value) |
| 107 | + // offset = length + (~(~value) + 1) |
| 108 | + // offset = length + value + 1 |
| 109 | + |
| 110 | + offset += length + 1; |
| 111 | + } |
| 112 | + return offset; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary>Indicates whether the current Index object is equal to another object of the same type.</summary> |
| 116 | + /// <param name="value">An object to compare with this object</param> |
| 117 | + public override bool Equals([NotNullWhen(true)] object? value) => value is Index && _value == ((Index)value)._value; |
| 118 | + |
| 119 | + /// <summary>Indicates whether the current Index object is equal to another Index object.</summary> |
| 120 | + /// <param name="other">An object to compare with this object</param> |
| 121 | + public bool Equals(Index other) => _value == other._value; |
| 122 | + |
| 123 | + /// <summary>Returns the hash code for this instance.</summary> |
| 124 | + public override int GetHashCode() => _value; |
| 125 | + |
| 126 | + /// <summary>Converts integer number to an Index.</summary> |
| 127 | + public static implicit operator Index(int value) => FromStart(value); |
| 128 | + |
| 129 | + /// <summary>Converts the value of the current Index object to its equivalent string representation.</summary> |
| 130 | + public override string ToString() |
| 131 | + { |
| 132 | + if (IsFromEnd) |
| 133 | + return ToStringFromEnd(); |
| 134 | + |
| 135 | + return ((uint)Value).ToString(); |
| 136 | + } |
| 137 | + |
| 138 | + private string ToStringFromEnd() |
| 139 | + { |
| 140 | + return '^' + Value.ToString(); |
| 141 | + } |
| 142 | + |
| 143 | + internal static class ThrowHelper |
| 144 | + { |
| 145 | + [DoesNotReturn, MethodImpl(MethodImplOptions.NoInlining)] |
| 146 | + public static void ThrowValueArgumentOutOfRange_NeedNonNegNumException() |
| 147 | + { |
| 148 | + throw new ArgumentOutOfRangeException( |
| 149 | + "Non-negative number required. (Parameter 'value')", |
| 150 | + "value"); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments