span 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===---------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_SPAN
  10. #define _LIBCPP_SPAN
  11. /*
  12. span synopsis
  13. namespace std {
  14. // constants
  15. inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
  16. // [views.span], class template span
  17. template <class ElementType, size_t Extent = dynamic_extent>
  18. class span;
  19. template<class ElementType, size_t Extent>
  20. inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;
  21. template<class ElementType, size_t Extent>
  22. inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
  23. // [span.objectrep], views of object representation
  24. template <class ElementType, size_t Extent>
  25. span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
  26. (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
  27. template <class ElementType, size_t Extent>
  28. span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
  29. (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
  30. template <class ElementType, size_t Extent = dynamic_extent>
  31. class span {
  32. public:
  33. // constants and types
  34. using element_type = ElementType;
  35. using value_type = remove_cv_t<ElementType>;
  36. using size_type = size_t;
  37. using difference_type = ptrdiff_t;
  38. using pointer = element_type*;
  39. using const_pointer = const element_type*;
  40. using reference = element_type&;
  41. using const_reference = const element_type&;
  42. using iterator = implementation-defined;
  43. using reverse_iterator = std::reverse_iterator<iterator>;
  44. static constexpr size_type extent = Extent;
  45. // [span.cons], span constructors, copy, assignment, and destructor
  46. constexpr span() noexcept;
  47. template <class It>
  48. constexpr explicit(Extent != dynamic_extent) span(It first, size_type count);
  49. template <class It, class End>
  50. constexpr explicit(Extent != dynamic_extent) span(It first, End last);
  51. template <size_t N>
  52. constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept;
  53. template <size_t N>
  54. constexpr span(array<value_type, N>& arr) noexcept;
  55. template <size_t N>
  56. constexpr span(const array<value_type, N>& arr) noexcept;
  57. template<class R>
  58. constexpr explicit(Extent != dynamic_extent) span(R&& r);
  59. constexpr span(const span& other) noexcept = default;
  60. template <class OtherElementType, size_t OtherExtent>
  61. constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
  62. ~span() noexcept = default;
  63. constexpr span& operator=(const span& other) noexcept = default;
  64. // [span.sub], span subviews
  65. template <size_t Count>
  66. constexpr span<element_type, Count> first() const;
  67. template <size_t Count>
  68. constexpr span<element_type, Count> last() const;
  69. template <size_t Offset, size_t Count = dynamic_extent>
  70. constexpr span<element_type, see below> subspan() const;
  71. constexpr span<element_type, dynamic_extent> first(size_type count) const;
  72. constexpr span<element_type, dynamic_extent> last(size_type count) const;
  73. constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
  74. // [span.obs], span observers
  75. constexpr size_type size() const noexcept;
  76. constexpr size_type size_bytes() const noexcept;
  77. [[nodiscard]] constexpr bool empty() const noexcept;
  78. // [span.elem], span element access
  79. constexpr reference operator[](size_type idx) const;
  80. constexpr reference front() const;
  81. constexpr reference back() const;
  82. constexpr pointer data() const noexcept;
  83. // [span.iterators], span iterator support
  84. constexpr iterator begin() const noexcept;
  85. constexpr iterator end() const noexcept;
  86. constexpr reverse_iterator rbegin() const noexcept;
  87. constexpr reverse_iterator rend() const noexcept;
  88. private:
  89. pointer data_; // exposition only
  90. size_type size_; // exposition only
  91. };
  92. template<class It, class EndOrSize>
  93. span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
  94. template<class T, size_t N>
  95. span(T (&)[N]) -> span<T, N>;
  96. template<class T, size_t N>
  97. span(array<T, N>&) -> span<T, N>;
  98. template<class T, size_t N>
  99. span(const array<T, N>&) -> span<const T, N>;
  100. template<class R>
  101. span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
  102. } // namespace std
  103. */
  104. #include <__assert> // all public C++ headers provide the assertion handler
  105. #include <__config>
  106. #include <__debug>
  107. #include <__fwd/span.h>
  108. #include <__iterator/concepts.h>
  109. #include <__iterator/wrap_iter.h>
  110. #include <__ranges/concepts.h>
  111. #include <__ranges/data.h>
  112. #include <__ranges/enable_borrowed_range.h>
  113. #include <__ranges/enable_view.h>
  114. #include <__ranges/size.h>
  115. #include <array> // for array
  116. #include <cstddef> // for byte
  117. #include <iterator> // for iterators
  118. #include <limits>
  119. #include <type_traits> // for remove_cv, etc
  120. #include <version>
  121. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  122. # pragma GCC system_header
  123. #endif
  124. _LIBCPP_PUSH_MACROS
  125. #include <__undef_macros>
  126. _LIBCPP_BEGIN_NAMESPACE_STD
  127. #if _LIBCPP_STD_VER > 17
  128. template <class _Tp>
  129. struct __is_std_array : false_type {};
  130. template <class _Tp, size_t _Sz>
  131. struct __is_std_array<array<_Tp, _Sz>> : true_type {};
  132. template <class _Tp>
  133. struct __is_std_span : false_type {};
  134. template <class _Tp, size_t _Sz>
  135. struct __is_std_span<span<_Tp, _Sz>> : true_type {};
  136. #if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  137. // This is a temporary workaround until we ship <ranges> -- we've unfortunately been
  138. // shipping <span> before its API was finalized, and we used to provide a constructor
  139. // from container types that had the requirements below. To avoid breaking code that
  140. // has started relying on the range-based constructor until we ship all of <ranges>,
  141. // we emulate the constructor requirements like this.
  142. template <class _Range, class _ElementType>
  143. concept __span_compatible_range =
  144. !__is_std_span<remove_cvref_t<_Range>>::value &&
  145. !__is_std_array<remove_cvref_t<_Range>>::value &&
  146. !is_array_v<remove_cvref_t<_Range>> &&
  147. requires (_Range&& __r) {
  148. data(std::forward<_Range>(__r));
  149. size(std::forward<_Range>(__r));
  150. } &&
  151. is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
  152. #else
  153. template <class _Range, class _ElementType>
  154. concept __span_compatible_range =
  155. ranges::contiguous_range<_Range> &&
  156. ranges::sized_range<_Range> &&
  157. (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
  158. !__is_std_span<remove_cvref_t<_Range>>::value &&
  159. !__is_std_array<remove_cvref_t<_Range>>::value &&
  160. !is_array_v<remove_cvref_t<_Range>> &&
  161. is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
  162. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  163. template <typename _Tp, size_t _Extent>
  164. class _LIBCPP_TEMPLATE_VIS span {
  165. public:
  166. // constants and types
  167. using element_type = _Tp;
  168. using value_type = remove_cv_t<_Tp>;
  169. using size_type = size_t;
  170. using difference_type = ptrdiff_t;
  171. using pointer = _Tp *;
  172. using const_pointer = const _Tp *;
  173. using reference = _Tp &;
  174. using const_reference = const _Tp &;
  175. #if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
  176. using iterator = pointer;
  177. #else
  178. using iterator = __wrap_iter<pointer>;
  179. #endif
  180. using reverse_iterator = _VSTD::reverse_iterator<iterator>;
  181. static constexpr size_type extent = _Extent;
  182. // [span.cons], span constructors, copy, assignment, and destructor
  183. template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
  184. _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
  185. constexpr span (const span&) noexcept = default;
  186. constexpr span& operator=(const span&) noexcept = default;
  187. template <class _It,
  188. enable_if_t<contiguous_iterator<_It> &&
  189. is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>,
  190. nullptr_t> = nullptr>
  191. _LIBCPP_INLINE_VISIBILITY
  192. constexpr explicit span(_It __first, size_type __count)
  193. : __data{_VSTD::to_address(__first)} {
  194. (void)__count;
  195. _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
  196. }
  197. template <
  198. class _It, class _End,
  199. enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
  200. contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
  201. nullptr_t> = nullptr>
  202. _LIBCPP_INLINE_VISIBILITY
  203. constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
  204. (void)__last;
  205. _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)");
  206. _LIBCPP_ASSERT(__last - __first == _Extent,
  207. "invalid range in span's constructor (iterator, sentinel): last - first != extent");
  208. }
  209. _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {}
  210. template <class _OtherElementType,
  211. enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  212. _LIBCPP_INLINE_VISIBILITY
  213. constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
  214. template <class _OtherElementType,
  215. enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  216. _LIBCPP_INLINE_VISIBILITY
  217. constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
  218. #if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  219. template <class _Container>
  220. requires __span_compatible_range<_Container, element_type>
  221. _LIBCPP_INLINE_VISIBILITY
  222. constexpr explicit span(_Container& __c) : __data{std::data(__c)} {
  223. _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
  224. }
  225. template <class _Container>
  226. requires __span_compatible_range<const _Container, element_type>
  227. _LIBCPP_INLINE_VISIBILITY
  228. constexpr explicit span(const _Container& __c) : __data{std::data(__c)} {
  229. _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
  230. }
  231. #else
  232. template <__span_compatible_range<element_type> _Range>
  233. _LIBCPP_INLINE_VISIBILITY
  234. constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} {
  235. _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
  236. }
  237. #endif
  238. template <class _OtherElementType>
  239. _LIBCPP_INLINE_VISIBILITY
  240. constexpr span(const span<_OtherElementType, _Extent>& __other,
  241. enable_if_t<
  242. is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
  243. nullptr_t> = nullptr)
  244. : __data{__other.data()} {}
  245. template <class _OtherElementType>
  246. _LIBCPP_INLINE_VISIBILITY
  247. constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
  248. enable_if_t<
  249. is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
  250. nullptr_t> = nullptr) noexcept
  251. : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
  252. // ~span() noexcept = default;
  253. template <size_t _Count>
  254. _LIBCPP_INLINE_VISIBILITY
  255. constexpr span<element_type, _Count> first() const noexcept
  256. {
  257. static_assert(_Count <= _Extent, "Count out of range in span::first()");
  258. return span<element_type, _Count>{data(), _Count};
  259. }
  260. template <size_t _Count>
  261. _LIBCPP_INLINE_VISIBILITY
  262. constexpr span<element_type, _Count> last() const noexcept
  263. {
  264. static_assert(_Count <= _Extent, "Count out of range in span::last()");
  265. return span<element_type, _Count>{data() + size() - _Count, _Count};
  266. }
  267. _LIBCPP_INLINE_VISIBILITY
  268. constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
  269. {
  270. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
  271. return {data(), __count};
  272. }
  273. _LIBCPP_INLINE_VISIBILITY
  274. constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
  275. {
  276. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
  277. return {data() + size() - __count, __count};
  278. }
  279. template <size_t _Offset, size_t _Count = dynamic_extent>
  280. _LIBCPP_INLINE_VISIBILITY
  281. constexpr auto subspan() const noexcept
  282. -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
  283. {
  284. static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
  285. static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
  286. using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
  287. return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
  288. }
  289. _LIBCPP_INLINE_VISIBILITY
  290. constexpr span<element_type, dynamic_extent>
  291. subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
  292. {
  293. _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
  294. _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
  295. if (__count == dynamic_extent)
  296. return {data() + __offset, size() - __offset};
  297. _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
  298. return {data() + __offset, __count};
  299. }
  300. _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
  301. _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
  302. [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
  303. _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
  304. {
  305. _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
  306. return __data[__idx];
  307. }
  308. _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
  309. {
  310. _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
  311. return __data[0];
  312. }
  313. _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
  314. {
  315. _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
  316. return __data[size()-1];
  317. }
  318. _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
  319. // [span.iter], span iterator support
  320. _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
  321. _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
  322. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
  323. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
  324. _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
  325. { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
  326. _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
  327. { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
  328. private:
  329. pointer __data;
  330. };
  331. template <typename _Tp>
  332. class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
  333. private:
  334. public:
  335. // constants and types
  336. using element_type = _Tp;
  337. using value_type = remove_cv_t<_Tp>;
  338. using size_type = size_t;
  339. using difference_type = ptrdiff_t;
  340. using pointer = _Tp *;
  341. using const_pointer = const _Tp *;
  342. using reference = _Tp &;
  343. using const_reference = const _Tp &;
  344. #if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
  345. using iterator = pointer;
  346. #else
  347. using iterator = __wrap_iter<pointer>;
  348. #endif
  349. using reverse_iterator = _VSTD::reverse_iterator<iterator>;
  350. static constexpr size_type extent = dynamic_extent;
  351. // [span.cons], span constructors, copy, assignment, and destructor
  352. _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
  353. constexpr span (const span&) noexcept = default;
  354. constexpr span& operator=(const span&) noexcept = default;
  355. template <class _It,
  356. enable_if_t<contiguous_iterator<_It> &&
  357. is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>,
  358. nullptr_t> = nullptr>
  359. _LIBCPP_INLINE_VISIBILITY
  360. constexpr span(_It __first, size_type __count)
  361. : __data{_VSTD::to_address(__first)}, __size{__count} {}
  362. template <
  363. class _It, class _End,
  364. enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
  365. contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
  366. nullptr_t> = nullptr>
  367. _LIBCPP_INLINE_VISIBILITY
  368. constexpr span(_It __first, _End __last)
  369. : __data(_VSTD::to_address(__first)), __size(__last - __first) {}
  370. template <size_t _Sz>
  371. _LIBCPP_INLINE_VISIBILITY
  372. constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
  373. template <class _OtherElementType, size_t _Sz,
  374. enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  375. _LIBCPP_INLINE_VISIBILITY
  376. constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
  377. template <class _OtherElementType, size_t _Sz,
  378. enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  379. _LIBCPP_INLINE_VISIBILITY
  380. constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
  381. #if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  382. template <class _Container>
  383. requires __span_compatible_range<_Container, element_type>
  384. _LIBCPP_INLINE_VISIBILITY
  385. constexpr span(_Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
  386. template <class _Container>
  387. requires __span_compatible_range<const _Container, element_type>
  388. _LIBCPP_INLINE_VISIBILITY
  389. constexpr span(const _Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
  390. #else
  391. template <__span_compatible_range<element_type> _Range>
  392. _LIBCPP_INLINE_VISIBILITY
  393. constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
  394. #endif
  395. template <class _OtherElementType, size_t _OtherExtent>
  396. _LIBCPP_INLINE_VISIBILITY
  397. constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
  398. enable_if_t<
  399. is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
  400. nullptr_t> = nullptr) noexcept
  401. : __data{__other.data()}, __size{__other.size()} {}
  402. // ~span() noexcept = default;
  403. template <size_t _Count>
  404. _LIBCPP_INLINE_VISIBILITY
  405. constexpr span<element_type, _Count> first() const noexcept
  406. {
  407. _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
  408. return span<element_type, _Count>{data(), _Count};
  409. }
  410. template <size_t _Count>
  411. _LIBCPP_INLINE_VISIBILITY
  412. constexpr span<element_type, _Count> last() const noexcept
  413. {
  414. _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
  415. return span<element_type, _Count>{data() + size() - _Count, _Count};
  416. }
  417. _LIBCPP_INLINE_VISIBILITY
  418. constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
  419. {
  420. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
  421. return {data(), __count};
  422. }
  423. _LIBCPP_INLINE_VISIBILITY
  424. constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
  425. {
  426. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
  427. return {data() + size() - __count, __count};
  428. }
  429. template <size_t _Offset, size_t _Count = dynamic_extent>
  430. _LIBCPP_INLINE_VISIBILITY
  431. constexpr span<element_type, _Count> subspan() const noexcept
  432. {
  433. _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
  434. _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
  435. return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
  436. }
  437. constexpr span<element_type, dynamic_extent>
  438. _LIBCPP_INLINE_VISIBILITY
  439. subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
  440. {
  441. _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
  442. _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
  443. if (__count == dynamic_extent)
  444. return {data() + __offset, size() - __offset};
  445. _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
  446. return {data() + __offset, __count};
  447. }
  448. _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
  449. _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
  450. [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
  451. _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
  452. {
  453. _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
  454. return __data[__idx];
  455. }
  456. _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
  457. {
  458. _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
  459. return __data[0];
  460. }
  461. _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
  462. {
  463. _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
  464. return __data[size()-1];
  465. }
  466. _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
  467. // [span.iter], span iterator support
  468. _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
  469. _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
  470. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
  471. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
  472. inline _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept;
  473. inline _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept;
  474. private:
  475. pointer __data;
  476. size_type __size;
  477. };
  478. template<typename _Tp>
  479. inline _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> span<_Tp, dynamic_extent>::__as_bytes() const noexcept
  480. { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
  481. template<typename _Tp>
  482. inline _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> span<_Tp, dynamic_extent>::__as_writable_bytes() const noexcept
  483. { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
  484. template <class _Tp, size_t _Extent>
  485. inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
  486. template <class _ElementType, size_t _Extent>
  487. inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
  488. // as_bytes & as_writable_bytes
  489. template <class _Tp, size_t _Extent>
  490. _LIBCPP_INLINE_VISIBILITY
  491. auto as_bytes(span<_Tp, _Extent> __s) noexcept
  492. -> decltype(__s.__as_bytes())
  493. { return __s.__as_bytes(); }
  494. template <class _Tp, size_t _Extent>
  495. _LIBCPP_INLINE_VISIBILITY
  496. auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
  497. -> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
  498. { return __s.__as_writable_bytes(); }
  499. #if _LIBCPP_STD_VER > 17
  500. template<contiguous_iterator _It, class _EndOrSize>
  501. span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
  502. #endif // _LIBCPP_STD_VER > 17
  503. template<class _Tp, size_t _Sz>
  504. span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
  505. template<class _Tp, size_t _Sz>
  506. span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
  507. template<class _Tp, size_t _Sz>
  508. span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
  509. #if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  510. template<class _Container>
  511. span(_Container&) -> span<typename _Container::value_type>;
  512. template<class _Container>
  513. span(const _Container&) -> span<const typename _Container::value_type>;
  514. #else
  515. template<ranges::contiguous_range _Range>
  516. span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
  517. #endif
  518. #endif // _LIBCPP_STD_VER > 17
  519. _LIBCPP_END_NAMESPACE_STD
  520. _LIBCPP_POP_MACROS
  521. #endif // _LIBCPP_SPAN