Span.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Versioning;
  7. using System.Text;
  8. using EditorBrowsableAttribute = System.ComponentModel.EditorBrowsableAttribute;
  9. using EditorBrowsableState = System.ComponentModel.EditorBrowsableState;
  10. using Internal.Runtime.CompilerServices;
  11. #pragma warning disable 0809 //warning CS0809: Obsolete member 'Span<T>.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
  12. #pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
  13. #if BIT64
  14. using nuint = System.UInt64;
  15. #else
  16. using nuint = System.UInt32;
  17. #endif
  18. namespace System
  19. {
  20. /// <summary>
  21. /// Span represents a contiguous region of arbitrary memory. Unlike arrays, it can point to either managed
  22. /// or native memory, or to memory allocated on the stack. It is type- and memory-safe.
  23. /// </summary>
  24. [DebuggerTypeProxy(typeof(SpanDebugView<>))]
  25. [DebuggerDisplay("{ToString(),raw}")]
  26. [NonVersionable]
  27. public readonly ref struct Span<T>
  28. {
  29. /// <summary>A byref or a native ptr.</summary>
  30. internal readonly ByReference<T> _pointer;
  31. /// <summary>The number of elements this Span contains.</summary>
  32. private readonly int _length;
  33. /// <summary>
  34. /// Creates a new span over the entirety of the target array.
  35. /// </summary>
  36. /// <param name="array">The target array.</param>
  37. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  38. /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public Span(T[]? array)
  41. {
  42. if (array == null)
  43. {
  44. this = default;
  45. return; // returns default
  46. }
  47. if (default(T)! == null && array.GetType() != typeof(T[])) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
  48. ThrowHelper.ThrowArrayTypeMismatchException();
  49. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()));
  50. _length = array.Length;
  51. }
  52. /// <summary>
  53. /// Creates a new span over the portion of the target array beginning
  54. /// at 'start' index and ending at 'end' index (exclusive).
  55. /// </summary>
  56. /// <param name="array">The target array.</param>
  57. /// <param name="start">The index at which to begin the span.</param>
  58. /// <param name="length">The number of items in the span.</param>
  59. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  60. /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
  61. /// <exception cref="System.ArgumentOutOfRangeException">
  62. /// Thrown when the specified <paramref name="start"/> or end index is not in the range (&lt;0 or &gt;Length).
  63. /// </exception>
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public Span(T[]? array, int start, int length)
  66. {
  67. if (array == null)
  68. {
  69. if (start != 0 || length != 0)
  70. ThrowHelper.ThrowArgumentOutOfRangeException();
  71. this = default;
  72. return; // returns default
  73. }
  74. if (default(T)! == null && array.GetType() != typeof(T[])) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
  75. ThrowHelper.ThrowArrayTypeMismatchException();
  76. #if BIT64
  77. // See comment in Span<T>.Slice for how this works.
  78. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
  79. ThrowHelper.ThrowArgumentOutOfRangeException();
  80. #else
  81. if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
  82. ThrowHelper.ThrowArgumentOutOfRangeException();
  83. #endif
  84. _pointer = new ByReference<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start));
  85. _length = length;
  86. }
  87. /// <summary>
  88. /// Creates a new span over the target unmanaged buffer. Clearly this
  89. /// is quite dangerous, because we are creating arbitrarily typed T's
  90. /// out of a void*-typed block of memory. And the length is not checked.
  91. /// But if this creation is correct, then all subsequent uses are correct.
  92. /// </summary>
  93. /// <param name="pointer">An unmanaged pointer to memory.</param>
  94. /// <param name="length">The number of <typeparamref name="T"/> elements the memory contains.</param>
  95. /// <exception cref="System.ArgumentException">
  96. /// Thrown when <typeparamref name="T"/> is reference type or contains pointers and hence cannot be stored in unmanaged memory.
  97. /// </exception>
  98. /// <exception cref="System.ArgumentOutOfRangeException">
  99. /// Thrown when the specified <paramref name="length"/> is negative.
  100. /// </exception>
  101. [CLSCompliant(false)]
  102. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  103. public unsafe Span(void* pointer, int length)
  104. {
  105. if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
  106. ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(T));
  107. if (length < 0)
  108. ThrowHelper.ThrowArgumentOutOfRangeException();
  109. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref *(byte*)pointer));
  110. _length = length;
  111. }
  112. // Constructor for internal use only.
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. internal Span(ref T ptr, int length)
  115. {
  116. Debug.Assert(length >= 0);
  117. _pointer = new ByReference<T>(ref ptr);
  118. _length = length;
  119. }
  120. /// <summary>
  121. /// Returns a reference to specified element of the Span.
  122. /// </summary>
  123. /// <param name="index"></param>
  124. /// <returns></returns>
  125. /// <exception cref="System.IndexOutOfRangeException">
  126. /// Thrown when index less than 0 or index greater than or equal to Length
  127. /// </exception>
  128. public ref T this[int index]
  129. {
  130. [Intrinsic]
  131. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  132. [NonVersionable]
  133. get
  134. {
  135. if ((uint)index >= (uint)_length)
  136. ThrowHelper.ThrowIndexOutOfRangeException();
  137. return ref Unsafe.Add(ref _pointer.Value, index);
  138. }
  139. }
  140. /// <summary>
  141. /// The number of items in the span.
  142. /// </summary>
  143. public int Length
  144. {
  145. [NonVersionable]
  146. get => _length;
  147. }
  148. /// <summary>
  149. /// Returns true if Length is 0.
  150. /// </summary>
  151. public bool IsEmpty
  152. {
  153. [NonVersionable]
  154. get => 0 >= (uint)_length; // Workaround for https://github.com/dotnet/coreclr/issues/19620
  155. }
  156. /// <summary>
  157. /// Returns false if left and right point at the same memory and have the same length. Note that
  158. /// this does *not* check to see if the *contents* are equal.
  159. /// </summary>
  160. public static bool operator !=(Span<T> left, Span<T> right) => !(left == right);
  161. /// <summary>
  162. /// This method is not supported as spans cannot be boxed. To compare two spans, use operator==.
  163. /// <exception cref="System.NotSupportedException">
  164. /// Always thrown by this method.
  165. /// </exception>
  166. /// </summary>
  167. [Obsolete("Equals() on Span will always throw an exception. Use == instead.")]
  168. [EditorBrowsable(EditorBrowsableState.Never)]
  169. public override bool Equals(object? obj) =>
  170. throw new NotSupportedException(SR.NotSupported_CannotCallEqualsOnSpan);
  171. /// <summary>
  172. /// This method is not supported as spans cannot be boxed.
  173. /// <exception cref="System.NotSupportedException">
  174. /// Always thrown by this method.
  175. /// </exception>
  176. /// </summary>
  177. [Obsolete("GetHashCode() on Span will always throw an exception.")]
  178. [EditorBrowsable(EditorBrowsableState.Never)]
  179. public override int GetHashCode() =>
  180. throw new NotSupportedException(SR.NotSupported_CannotCallGetHashCodeOnSpan);
  181. /// <summary>
  182. /// Defines an implicit conversion of an array to a <see cref="Span{T}"/>
  183. /// </summary>
  184. public static implicit operator Span<T>(T[]? array) => new Span<T>(array);
  185. /// <summary>
  186. /// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="Span{T}"/>
  187. /// </summary>
  188. public static implicit operator Span<T>(ArraySegment<T> segment) =>
  189. new Span<T>(segment.Array, segment.Offset, segment.Count);
  190. /// <summary>
  191. /// Returns an empty <see cref="Span{T}"/>
  192. /// </summary>
  193. public static Span<T> Empty => default;
  194. /// <summary>Gets an enumerator for this span.</summary>
  195. public Enumerator GetEnumerator() => new Enumerator(this);
  196. /// <summary>Enumerates the elements of a <see cref="Span{T}"/>.</summary>
  197. public ref struct Enumerator
  198. {
  199. /// <summary>The span being enumerated.</summary>
  200. private readonly Span<T> _span;
  201. /// <summary>The next index to yield.</summary>
  202. private int _index;
  203. /// <summary>Initialize the enumerator.</summary>
  204. /// <param name="span">The span to enumerate.</param>
  205. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  206. internal Enumerator(Span<T> span)
  207. {
  208. _span = span;
  209. _index = -1;
  210. }
  211. /// <summary>Advances the enumerator to the next element of the span.</summary>
  212. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  213. public bool MoveNext()
  214. {
  215. int index = _index + 1;
  216. if (index < _span.Length)
  217. {
  218. _index = index;
  219. return true;
  220. }
  221. return false;
  222. }
  223. /// <summary>Gets the element at the current position of the enumerator.</summary>
  224. public ref T Current
  225. {
  226. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  227. get => ref _span[_index];
  228. }
  229. }
  230. /// <summary>
  231. /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference.
  232. /// It can be used for pinning and is required to support the use of span within a fixed statement.
  233. /// </summary>
  234. [EditorBrowsable(EditorBrowsableState.Never)]
  235. public ref T GetPinnableReference()
  236. {
  237. // Ensure that the native code has just one forward branch that is predicted-not-taken.
  238. ref T ret = ref Unsafe.NullRef<T>();
  239. if (_length != 0) ret = ref _pointer.Value;
  240. return ref ret;
  241. }
  242. /// <summary>
  243. /// Clears the contents of this span.
  244. /// </summary>
  245. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  246. public void Clear()
  247. {
  248. if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
  249. {
  250. SpanHelpers.ClearWithReferences(ref Unsafe.As<T, IntPtr>(ref _pointer.Value), (nuint)_length * (nuint)(Unsafe.SizeOf<T>() / sizeof(nuint)));
  251. }
  252. else
  253. {
  254. SpanHelpers.ClearWithoutReferences(ref Unsafe.As<T, byte>(ref _pointer.Value), (nuint)_length * (nuint)Unsafe.SizeOf<T>());
  255. }
  256. }
  257. /// <summary>
  258. /// Fills the contents of this span with the given value.
  259. /// </summary>
  260. public void Fill(T value)
  261. {
  262. if (Unsafe.SizeOf<T>() == 1)
  263. {
  264. uint length = (uint)_length;
  265. if (length == 0)
  266. return;
  267. T tmp = value; // Avoid taking address of the "value" argument. It would regress performance of the loop below.
  268. Unsafe.InitBlockUnaligned(ref Unsafe.As<T, byte>(ref _pointer.Value), Unsafe.As<T, byte>(ref tmp), length);
  269. }
  270. else
  271. {
  272. // Do all math as nuint to avoid unnecessary 64->32->64 bit integer truncations
  273. nuint length = (uint)_length;
  274. if (length == 0)
  275. return;
  276. ref T r = ref _pointer.Value;
  277. // TODO: Create block fill for value types of power of two sizes e.g. 2,4,8,16
  278. nuint elementSize = (uint)Unsafe.SizeOf<T>();
  279. nuint i = 0;
  280. for (; i < (length & ~(nuint)7); i += 8)
  281. {
  282. Unsafe.AddByteOffset<T>(ref r, (i + 0) * elementSize) = value;
  283. Unsafe.AddByteOffset<T>(ref r, (i + 1) * elementSize) = value;
  284. Unsafe.AddByteOffset<T>(ref r, (i + 2) * elementSize) = value;
  285. Unsafe.AddByteOffset<T>(ref r, (i + 3) * elementSize) = value;
  286. Unsafe.AddByteOffset<T>(ref r, (i + 4) * elementSize) = value;
  287. Unsafe.AddByteOffset<T>(ref r, (i + 5) * elementSize) = value;
  288. Unsafe.AddByteOffset<T>(ref r, (i + 6) * elementSize) = value;
  289. Unsafe.AddByteOffset<T>(ref r, (i + 7) * elementSize) = value;
  290. }
  291. if (i < (length & ~(nuint)3))
  292. {
  293. Unsafe.AddByteOffset<T>(ref r, (i + 0) * elementSize) = value;
  294. Unsafe.AddByteOffset<T>(ref r, (i + 1) * elementSize) = value;
  295. Unsafe.AddByteOffset<T>(ref r, (i + 2) * elementSize) = value;
  296. Unsafe.AddByteOffset<T>(ref r, (i + 3) * elementSize) = value;
  297. i += 4;
  298. }
  299. for (; i < length; i++)
  300. {
  301. Unsafe.AddByteOffset<T>(ref r, i * elementSize) = value;
  302. }
  303. }
  304. }
  305. /// <summary>
  306. /// Copies the contents of this span into destination span. If the source
  307. /// and destinations overlap, this method behaves as if the original values in
  308. /// a temporary location before the destination is overwritten.
  309. /// </summary>
  310. /// <param name="destination">The span to copy items into.</param>
  311. /// <exception cref="System.ArgumentException">
  312. /// Thrown when the destination Span is shorter than the source Span.
  313. /// </exception>
  314. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  315. public void CopyTo(Span<T> destination)
  316. {
  317. // Using "if (!TryCopyTo(...))" results in two branches: one for the length
  318. // check, and one for the result of TryCopyTo. Since these checks are equivalent,
  319. // we can optimize by performing the check once ourselves then calling Memmove directly.
  320. if ((uint)_length <= (uint)destination.Length)
  321. {
  322. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  323. }
  324. else
  325. {
  326. ThrowHelper.ThrowArgumentException_DestinationTooShort();
  327. }
  328. }
  329. /// <summary>
  330. /// Copies the contents of this span into destination span. If the source
  331. /// and destinations overlap, this method behaves as if the original values in
  332. /// a temporary location before the destination is overwritten.
  333. /// </summary>
  334. /// <param name="destination">The span to copy items into.</param>
  335. /// <returns>If the destination span is shorter than the source span, this method
  336. /// return false and no data is written to the destination.</returns>
  337. public bool TryCopyTo(Span<T> destination)
  338. {
  339. bool retVal = false;
  340. if ((uint)_length <= (uint)destination.Length)
  341. {
  342. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  343. retVal = true;
  344. }
  345. return retVal;
  346. }
  347. /// <summary>
  348. /// Returns true if left and right point at the same memory and have the same length. Note that
  349. /// this does *not* check to see if the *contents* are equal.
  350. /// </summary>
  351. public static bool operator ==(Span<T> left, Span<T> right) =>
  352. left._length == right._length &&
  353. Unsafe.AreSame<T>(ref left._pointer.Value, ref right._pointer.Value);
  354. /// <summary>
  355. /// Defines an implicit conversion of a <see cref="Span{T}"/> to a <see cref="ReadOnlySpan{T}"/>
  356. /// </summary>
  357. public static implicit operator ReadOnlySpan<T>(Span<T> span) =>
  358. new ReadOnlySpan<T>(ref span._pointer.Value, span._length);
  359. /// <summary>
  360. /// For <see cref="Span{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
  361. /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements.
  362. /// </summary>
  363. public override string ToString()
  364. {
  365. if (typeof(T) == typeof(char))
  366. {
  367. return new string(new ReadOnlySpan<char>(ref Unsafe.As<T, char>(ref _pointer.Value), _length));
  368. }
  369. #if FEATURE_UTF8STRING
  370. else if (typeof(T) == typeof(Char8))
  371. {
  372. // TODO_UTF8STRING: Call into optimized transcoding routine when it's available.
  373. return Encoding.UTF8.GetString(new ReadOnlySpan<byte>(ref Unsafe.As<T, byte>(ref _pointer.Value), _length));
  374. }
  375. #endif // FEATURE_UTF8STRING
  376. return string.Format("System.Span<{0}>[{1}]", typeof(T).Name, _length);
  377. }
  378. /// <summary>
  379. /// Forms a slice out of the given span, beginning at 'start'.
  380. /// </summary>
  381. /// <param name="start">The index at which to begin this slice.</param>
  382. /// <exception cref="System.ArgumentOutOfRangeException">
  383. /// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;Length).
  384. /// </exception>
  385. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  386. public Span<T> Slice(int start)
  387. {
  388. if ((uint)start > (uint)_length)
  389. ThrowHelper.ThrowArgumentOutOfRangeException();
  390. return new Span<T>(ref Unsafe.Add(ref _pointer.Value, start), _length - start);
  391. }
  392. /// <summary>
  393. /// Forms a slice out of the given span, beginning at 'start', of given length
  394. /// </summary>
  395. /// <param name="start">The index at which to begin this slice.</param>
  396. /// <param name="length">The desired length for the slice (exclusive).</param>
  397. /// <exception cref="System.ArgumentOutOfRangeException">
  398. /// Thrown when the specified <paramref name="start"/> or end index is not in range (&lt;0 or &gt;Length).
  399. /// </exception>
  400. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  401. public Span<T> Slice(int start, int length)
  402. {
  403. #if BIT64
  404. // Since start and length are both 32-bit, their sum can be computed across a 64-bit domain
  405. // without loss of fidelity. The cast to uint before the cast to ulong ensures that the
  406. // extension from 32- to 64-bit is zero-extending rather than sign-extending. The end result
  407. // of this is that if either input is negative or if the input sum overflows past Int32.MaxValue,
  408. // that information is captured correctly in the comparison against the backing _length field.
  409. // We don't use this same mechanism in a 32-bit process due to the overhead of 64-bit arithmetic.
  410. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
  411. ThrowHelper.ThrowArgumentOutOfRangeException();
  412. #else
  413. if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
  414. ThrowHelper.ThrowArgumentOutOfRangeException();
  415. #endif
  416. return new Span<T>(ref Unsafe.Add(ref _pointer.Value, start), length);
  417. }
  418. /// <summary>
  419. /// Copies the contents of this span into a new array. This heap
  420. /// allocates, so should generally be avoided, however it is sometimes
  421. /// necessary to bridge the gap with APIs written in terms of arrays.
  422. /// </summary>
  423. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  424. public T[] ToArray()
  425. {
  426. if (_length == 0)
  427. return Array.Empty<T>();
  428. var destination = new T[_length];
  429. Buffer.Memmove(ref Unsafe.As<byte, T>(ref destination.GetRawSzArrayData()), ref _pointer.Value, (nuint)_length);
  430. return destination;
  431. }
  432. }
  433. }