|
| 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 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +namespace System.Buffers |
| 9 | +{ |
| 10 | + /// <summary>Represent a type can be used to index a collection either from the start or the end.</summary> |
| 11 | + /// <remarks> |
| 12 | + /// <code> |
| 13 | + /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ; |
| 14 | + /// int lastElement = someArray[^1]; // lastElement = 5 |
| 15 | + /// </code> |
| 16 | + /// </remarks> |
| 17 | + public readonly struct NIndex : IEquatable<NIndex> |
| 18 | + { |
| 19 | + private readonly nint _value; |
| 20 | + |
| 21 | + /// <summary>Construct an NIndex using a value and indicating if the NIndex is from the start or from the end.</summary> |
| 22 | + /// <param name="value">The index value. it has to be zero or positive number.</param> |
| 23 | + /// <param name="fromEnd">Indicating if the index is from the start or from the end.</param> |
| 24 | + /// <remarks> |
| 25 | + /// If the NIndex constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element. |
| 26 | + /// </remarks> |
| 27 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 28 | + public NIndex(nint value, bool fromEnd = false) |
| 29 | + { |
| 30 | + if (value < 0) |
| 31 | + { |
| 32 | + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 33 | + } |
| 34 | + |
| 35 | + if (fromEnd) |
| 36 | + _value = ~value; |
| 37 | + else |
| 38 | + _value = value; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary>Construct a <see cref="NIndex"/> from a <see cref="Index"/></summary> |
| 42 | + /// <param name="index">The <see cref="Index"/> to create the <see cref="NIndex"/> from.</param> |
| 43 | + /// <remarks> |
| 44 | + /// If the NIndex constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element. |
| 45 | + /// </remarks> |
| 46 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 47 | + public NIndex(Index index) |
| 48 | + { |
| 49 | + if (index.IsFromEnd) |
| 50 | + _value = ~index.Value; |
| 51 | + else |
| 52 | + _value = index.Value; |
| 53 | + } |
| 54 | + |
| 55 | + // The following private constructor exists to skip the checks in the public ctor |
| 56 | + private NIndex(nint value) |
| 57 | + { |
| 58 | + _value = value; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary>Create an NIndex pointing at first element.</summary> |
| 62 | + public static NIndex Start => new NIndex((nint)0); |
| 63 | + |
| 64 | + /// <summary>Create an NIndex pointing at beyond last element.</summary> |
| 65 | + public static NIndex End => new NIndex((nint)~0); |
| 66 | + |
| 67 | + /// <summary>Create an NIndex from the start at the position indicated by the value.</summary> |
| 68 | + /// <param name="value">The index value from the start.</param> |
| 69 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 70 | + public static NIndex FromStart(nint value) |
| 71 | + { |
| 72 | + if (value < 0) |
| 73 | + { |
| 74 | + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 75 | + } |
| 76 | + |
| 77 | + return new NIndex(value); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary>Create an NIndex from the end at the position indicated by the value.</summary> |
| 81 | + /// <param name="value">The index value from the end.</param> |
| 82 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 83 | + public static NIndex FromEnd(nint value) |
| 84 | + { |
| 85 | + if (value < 0) |
| 86 | + { |
| 87 | + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 88 | + } |
| 89 | + |
| 90 | + return new NIndex(~value); |
| 91 | + } |
| 92 | + |
| 93 | + public Index ToIndex() => checked((Index)this); |
| 94 | + public Index ToIndexUnchecked() => (Index)this; |
| 95 | + |
| 96 | + /// <summary>Returns the NIndex value.</summary> |
| 97 | + public nint Value |
| 98 | + { |
| 99 | + get |
| 100 | + { |
| 101 | + if (_value < 0) |
| 102 | + return ~_value; |
| 103 | + else |
| 104 | + return _value; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary>Indicates whether the NIndex is from the start or the end.</summary> |
| 109 | + public bool IsFromEnd => _value < 0; |
| 110 | + |
| 111 | + /// <summary>Calculate the offset from the start using the giving collection length.</summary> |
| 112 | + /// <param name="length">The length of the collection that the NIndex will be used with. length has to be a positive value</param> |
| 113 | + /// <remarks> |
| 114 | + /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values. |
| 115 | + /// we don't validate either the returned offset is greater than the input length. |
| 116 | + /// It is expected NIndex will be used with collections which always have non negative length/count. If the returned offset is negative and |
| 117 | + /// then used to NIndex a collection will get out of range exception which will be same affect as the validation. |
| 118 | + /// </remarks> |
| 119 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 120 | + public nint GetOffset(nint length) |
| 121 | + { |
| 122 | + nint offset = _value; |
| 123 | + if (IsFromEnd) |
| 124 | + { |
| 125 | + // offset = length - (~value) |
| 126 | + // offset = length + (~(~value) + 1) |
| 127 | + // offset = length + value + 1 |
| 128 | + |
| 129 | + offset += length + 1; |
| 130 | + } |
| 131 | + return offset; |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary>Indicates whether the current NIndex object is equal to another object of the same type.</summary> |
| 135 | + /// <param name="value">An object to compare with this object</param> |
| 136 | + public override bool Equals([NotNullWhen(true)] object? value) => value is NIndex other && _value == other._value; |
| 137 | + |
| 138 | + /// <summary>Indicates whether the current NIndex object is equal to another NIndex object.</summary> |
| 139 | + /// <param name="other">An object to compare with this object</param> |
| 140 | + public bool Equals(NIndex other) => _value == other._value; |
| 141 | + |
| 142 | + /// <summary>Returns the hash code for this instance.</summary> |
| 143 | + public override int GetHashCode() => _value.GetHashCode(); |
| 144 | + |
| 145 | + /// <summary>Converts integer number to an NIndex.</summary> |
| 146 | + public static implicit operator NIndex(nint value) => FromStart(value); |
| 147 | + |
| 148 | + /// <summary>Converts native integer number to an NIndex.</summary> |
| 149 | + public static implicit operator NIndex(Index value) => new NIndex(value); |
| 150 | + |
| 151 | + /// <summary>Converts a <see cref="NIndex"/> to an <see cref="Index"/>."/></summary> |
| 152 | + public static explicit operator Index(NIndex value) => new Index((int)value.Value, value.IsFromEnd); |
| 153 | + |
| 154 | + /// <summary>Converts a <see cref="NIndex"/> to an <see cref="Index"/>."/></summary> |
| 155 | + public static explicit operator checked Index(NIndex value) => new Index(checked((int)value.Value), value.IsFromEnd); |
| 156 | + |
| 157 | + /// <summary>Converts the value of the current NIndex object to its equivalent string representation.</summary> |
| 158 | + public override string ToString() |
| 159 | + { |
| 160 | + if (IsFromEnd) |
| 161 | + return ToStringFromEnd(); |
| 162 | + |
| 163 | + return Value.ToString(); |
| 164 | + } |
| 165 | + |
| 166 | + private string ToStringFromEnd() |
| 167 | + { |
| 168 | + Span<char> span = stackalloc char[21]; // 1 for ^ and 20 for longest possible nuint value |
| 169 | + bool formatted = ((uint)Value).TryFormat(span.Slice(1), out int charsWritten); |
| 170 | + Debug.Assert(formatted); |
| 171 | + span[0] = '^'; |
| 172 | + return new string(span.Slice(0, charsWritten + 1)); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments