/// <summary> /// Forms a slice out of the given span, beginning at 'start', and /// ending at 'end' (exclusive). /// </summary> /// <param name="start">The index at which to begin this slice.</param> /// <param name="end">The index at which to end this slice (exclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when the specified start or end index is not in range (<0 or >&eq;_length). /// </exception> public ReadOnlySpan <T> Slice(int start, int length) { if ((uint)start >= (uint)_length || (uint)length > (uint)(_length - start)) { ThrowHelper.ThrowArgumentOutOfRangeException(); } return(new ReadOnlySpan <T>(ref JitHelpers.AddByRef(ref JitHelpers.GetByRef <T>(ref _rawPointer), start), length)); }
/// <summary> /// Creates a new span over the portion of the target array beginning /// at 'start' index and ending at 'end' index (exclusive). /// </summary> /// <param name="array">The target array.</param> /// <param name="start">The index at which to begin the span.</param> /// <param name="length">The number of items in the span.</param> public ReadOnlySpan(T[] array, int start, int length) { if ((uint)start >= (uint)array.Length || (uint)length > (uint)(array.Length - start)) { ThrowHelper.ThrowArgumentOutOfRangeException(); } JitHelpers.SetByRef(out _rawPointer, ref JitHelpers.AddByRef(ref JitHelpers.GetArrayData(array), start)); _length = length; }
/// <summary> /// Fetches the element at the specified index. /// </summary> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when the specified index is not in range (<0 or >&eq;_length). /// </exception> public T this[int index] { get { if ((uint)index >= (uint)_length) { throw new IndexOutOfRangeException(); } return(JitHelpers.AddByRef(ref JitHelpers.GetByRef <T>(ref _rawPointer), index)); } }
/// <summary> /// Copies the contents of this span into a new array. This heap /// allocates, so should generally be avoided, however is sometimes /// necessary to bridge the gap with APIs written in terms of arrays. /// </summary> public T[] CreateArray() { var src = JitHelpers.GetByRef <T>(ref _rawPointer); var dest = new T[_length]; // TODO: Specialize to use a fast memcpy for (int i = 0; i < dest.Length; i++) { dest[i] = JitHelpers.AddByRef(ref src, i); } return(dest); }