span 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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>
  105. #include <__config>
  106. #include <__debug>
  107. #include <__iterator/concepts.h>
  108. #include <__iterator/wrap_iter.h>
  109. #include <__ranges/concepts.h>
  110. #include <__ranges/data.h>
  111. #include <__ranges/enable_borrowed_range.h>
  112. #include <__ranges/enable_view.h>
  113. #include <__ranges/size.h>
  114. #include <array> // for array
  115. #include <cstddef> // for byte
  116. #include <iterator> // for iterators
  117. #include <limits>
  118. #include <type_traits> // for remove_cv, etc
  119. #include <version>
  120. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  121. # pragma GCC system_header
  122. #endif
  123. _LIBCPP_PUSH_MACROS
  124. #include <__undef_macros>
  125. _LIBCPP_BEGIN_NAMESPACE_STD
  126. #if _LIBCPP_STD_VER > 17
  127. inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
  128. template <typename _Tp, size_t _Extent = dynamic_extent> class span;
  129. template <class _Tp>
  130. struct __is_std_array : false_type {};
  131. template <class _Tp, size_t _Sz>
  132. struct __is_std_array<array<_Tp, _Sz>> : true_type {};
  133. template <class _Tp>
  134. struct __is_std_span : false_type {};
  135. template <class _Tp, size_t _Sz>
  136. struct __is_std_span<span<_Tp, _Sz>> : true_type {};
  137. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  138. template <class _Range, class _ElementType>
  139. concept __span_compatible_range =
  140. ranges::contiguous_range<_Range> &&
  141. ranges::sized_range<_Range> &&
  142. (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
  143. !__is_std_span<remove_cvref_t<_Range>>::value &&
  144. !__is_std_array<remove_cvref_t<_Range>>::value &&
  145. !is_array_v<remove_cvref_t<_Range>> &&
  146. is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
  147. #else
  148. template <class _Tp, class _ElementType, class = void>
  149. struct __is_span_compatible_container : public false_type {};
  150. template <class _Tp, class _ElementType>
  151. struct __is_span_compatible_container<_Tp, _ElementType,
  152. void_t<
  153. // is not a specialization of span
  154. enable_if_t<!__is_std_span<_Tp>::value, nullptr_t>,
  155. // is not a specialization of array
  156. enable_if_t<!__is_std_array<_Tp>::value, nullptr_t>,
  157. // is_array_v<Container> is false,
  158. enable_if_t<!is_array_v<_Tp>, nullptr_t>,
  159. // data(cont) and size(cont) are well formed
  160. decltype(data(declval<_Tp>())),
  161. decltype(size(declval<_Tp>())),
  162. // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
  163. enable_if_t<
  164. is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
  165. _ElementType(*)[]>,
  166. nullptr_t>
  167. >>
  168. : public true_type {};
  169. #endif
  170. template <typename _Tp, size_t _Extent>
  171. class _LIBCPP_TEMPLATE_VIS span {
  172. public:
  173. // constants and types
  174. using element_type = _Tp;
  175. using value_type = remove_cv_t<_Tp>;
  176. using size_type = size_t;
  177. using difference_type = ptrdiff_t;
  178. using pointer = _Tp *;
  179. using const_pointer = const _Tp *;
  180. using reference = _Tp &;
  181. using const_reference = const _Tp &;
  182. #if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
  183. using iterator = pointer;
  184. #else
  185. using iterator = __wrap_iter<pointer>;
  186. #endif
  187. using reverse_iterator = _VSTD::reverse_iterator<iterator>;
  188. static constexpr size_type extent = _Extent;
  189. // [span.cons], span constructors, copy, assignment, and destructor
  190. template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
  191. _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
  192. constexpr span (const span&) noexcept = default;
  193. constexpr span& operator=(const span&) noexcept = default;
  194. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  195. template <class _It,
  196. enable_if_t<contiguous_iterator<_It> &&
  197. is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>,
  198. nullptr_t> = nullptr>
  199. _LIBCPP_INLINE_VISIBILITY
  200. constexpr explicit span(_It __first, size_type __count)
  201. : __data{_VSTD::to_address(__first)} {
  202. (void)__count;
  203. _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
  204. }
  205. template <
  206. class _It, class _End,
  207. enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
  208. contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
  209. nullptr_t> = nullptr>
  210. _LIBCPP_INLINE_VISIBILITY
  211. constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
  212. (void)__last;
  213. _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)");
  214. _LIBCPP_ASSERT(__last - __first == _Extent,
  215. "invalid range in span's constructor (iterator, sentinel): last - first != extent");
  216. }
  217. #else
  218. _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __ptr, size_type __count) : __data{__ptr}
  219. { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
  220. _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __f, pointer __l) : __data{__f}
  221. { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
  222. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  223. _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {}
  224. template <class _OtherElementType,
  225. enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  226. _LIBCPP_INLINE_VISIBILITY
  227. constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
  228. template <class _OtherElementType,
  229. enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  230. _LIBCPP_INLINE_VISIBILITY
  231. constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
  232. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  233. template <__span_compatible_range<element_type> _Range>
  234. _LIBCPP_INLINE_VISIBILITY
  235. constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} {
  236. _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
  237. }
  238. #else
  239. template <class _Container>
  240. _LIBCPP_INLINE_VISIBILITY
  241. constexpr explicit span( _Container& __c,
  242. enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
  243. : __data{_VSTD::data(__c)} {
  244. _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
  245. }
  246. template <class _Container>
  247. _LIBCPP_INLINE_VISIBILITY
  248. constexpr explicit span(const _Container& __c,
  249. enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
  250. : __data{_VSTD::data(__c)} {
  251. _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
  252. }
  253. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  254. template <class _OtherElementType>
  255. _LIBCPP_INLINE_VISIBILITY
  256. constexpr span(const span<_OtherElementType, _Extent>& __other,
  257. enable_if_t<
  258. is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
  259. nullptr_t> = nullptr)
  260. : __data{__other.data()} {}
  261. template <class _OtherElementType>
  262. _LIBCPP_INLINE_VISIBILITY
  263. constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
  264. enable_if_t<
  265. is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
  266. nullptr_t> = nullptr) noexcept
  267. : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
  268. // ~span() noexcept = default;
  269. template <size_t _Count>
  270. _LIBCPP_INLINE_VISIBILITY
  271. constexpr span<element_type, _Count> first() const noexcept
  272. {
  273. static_assert(_Count <= _Extent, "Count out of range in span::first()");
  274. return span<element_type, _Count>{data(), _Count};
  275. }
  276. template <size_t _Count>
  277. _LIBCPP_INLINE_VISIBILITY
  278. constexpr span<element_type, _Count> last() const noexcept
  279. {
  280. static_assert(_Count <= _Extent, "Count out of range in span::last()");
  281. return span<element_type, _Count>{data() + size() - _Count, _Count};
  282. }
  283. _LIBCPP_INLINE_VISIBILITY
  284. constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
  285. {
  286. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
  287. return {data(), __count};
  288. }
  289. _LIBCPP_INLINE_VISIBILITY
  290. constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
  291. {
  292. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
  293. return {data() + size() - __count, __count};
  294. }
  295. template <size_t _Offset, size_t _Count = dynamic_extent>
  296. _LIBCPP_INLINE_VISIBILITY
  297. constexpr auto subspan() const noexcept
  298. -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
  299. {
  300. static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
  301. static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
  302. using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
  303. return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
  304. }
  305. _LIBCPP_INLINE_VISIBILITY
  306. constexpr span<element_type, dynamic_extent>
  307. subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
  308. {
  309. _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
  310. _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
  311. if (__count == dynamic_extent)
  312. return {data() + __offset, size() - __offset};
  313. _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
  314. return {data() + __offset, __count};
  315. }
  316. _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
  317. _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
  318. [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
  319. _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
  320. {
  321. _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
  322. return __data[__idx];
  323. }
  324. _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
  325. {
  326. _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
  327. return __data[0];
  328. }
  329. _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
  330. {
  331. _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
  332. return __data[size()-1];
  333. }
  334. _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
  335. // [span.iter], span iterator support
  336. _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
  337. _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
  338. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
  339. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
  340. _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
  341. { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
  342. _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
  343. { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
  344. private:
  345. pointer __data;
  346. };
  347. template <typename _Tp>
  348. class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
  349. private:
  350. public:
  351. // constants and types
  352. using element_type = _Tp;
  353. using value_type = remove_cv_t<_Tp>;
  354. using size_type = size_t;
  355. using difference_type = ptrdiff_t;
  356. using pointer = _Tp *;
  357. using const_pointer = const _Tp *;
  358. using reference = _Tp &;
  359. using const_reference = const _Tp &;
  360. #if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
  361. using iterator = pointer;
  362. #else
  363. using iterator = __wrap_iter<pointer>;
  364. #endif
  365. using reverse_iterator = _VSTD::reverse_iterator<iterator>;
  366. static constexpr size_type extent = dynamic_extent;
  367. // [span.cons], span constructors, copy, assignment, and destructor
  368. _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
  369. constexpr span (const span&) noexcept = default;
  370. constexpr span& operator=(const span&) noexcept = default;
  371. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  372. template <class _It,
  373. enable_if_t<contiguous_iterator<_It> &&
  374. is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>,
  375. nullptr_t> = nullptr>
  376. _LIBCPP_INLINE_VISIBILITY
  377. constexpr span(_It __first, size_type __count)
  378. : __data{_VSTD::to_address(__first)}, __size{__count} {}
  379. template <
  380. class _It, class _End,
  381. enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
  382. contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
  383. nullptr_t> = nullptr>
  384. _LIBCPP_INLINE_VISIBILITY
  385. constexpr span(_It __first, _End __last)
  386. : __data(_VSTD::to_address(__first)), __size(__last - __first) {}
  387. #else
  388. _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}, __size{__count} {}
  389. _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{static_cast<size_t>(distance(__f, __l))} {}
  390. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  391. template <size_t _Sz>
  392. _LIBCPP_INLINE_VISIBILITY
  393. constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
  394. template <class _OtherElementType, size_t _Sz,
  395. enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  396. _LIBCPP_INLINE_VISIBILITY
  397. constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
  398. template <class _OtherElementType, size_t _Sz,
  399. enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
  400. _LIBCPP_INLINE_VISIBILITY
  401. constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
  402. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  403. template <__span_compatible_range<element_type> _Range>
  404. _LIBCPP_INLINE_VISIBILITY
  405. constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
  406. #else
  407. template <class _Container>
  408. _LIBCPP_INLINE_VISIBILITY
  409. constexpr span( _Container& __c,
  410. enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
  411. : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
  412. template <class _Container>
  413. _LIBCPP_INLINE_VISIBILITY
  414. constexpr span(const _Container& __c,
  415. enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
  416. : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
  417. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  418. template <class _OtherElementType, size_t _OtherExtent>
  419. _LIBCPP_INLINE_VISIBILITY
  420. constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
  421. enable_if_t<
  422. is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
  423. nullptr_t> = nullptr) noexcept
  424. : __data{__other.data()}, __size{__other.size()} {}
  425. // ~span() noexcept = default;
  426. template <size_t _Count>
  427. _LIBCPP_INLINE_VISIBILITY
  428. constexpr span<element_type, _Count> first() const noexcept
  429. {
  430. _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
  431. return span<element_type, _Count>{data(), _Count};
  432. }
  433. template <size_t _Count>
  434. _LIBCPP_INLINE_VISIBILITY
  435. constexpr span<element_type, _Count> last() const noexcept
  436. {
  437. _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
  438. return span<element_type, _Count>{data() + size() - _Count, _Count};
  439. }
  440. _LIBCPP_INLINE_VISIBILITY
  441. constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
  442. {
  443. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
  444. return {data(), __count};
  445. }
  446. _LIBCPP_INLINE_VISIBILITY
  447. constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
  448. {
  449. _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
  450. return {data() + size() - __count, __count};
  451. }
  452. template <size_t _Offset, size_t _Count = dynamic_extent>
  453. _LIBCPP_INLINE_VISIBILITY
  454. constexpr span<element_type, _Count> subspan() const noexcept
  455. {
  456. _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
  457. _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
  458. return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
  459. }
  460. constexpr span<element_type, dynamic_extent>
  461. _LIBCPP_INLINE_VISIBILITY
  462. subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
  463. {
  464. _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
  465. _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
  466. if (__count == dynamic_extent)
  467. return {data() + __offset, size() - __offset};
  468. _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
  469. return {data() + __offset, __count};
  470. }
  471. _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
  472. _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
  473. [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
  474. _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
  475. {
  476. _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
  477. return __data[__idx];
  478. }
  479. _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
  480. {
  481. _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
  482. return __data[0];
  483. }
  484. _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
  485. {
  486. _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
  487. return __data[size()-1];
  488. }
  489. _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
  490. // [span.iter], span iterator support
  491. _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
  492. _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
  493. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
  494. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
  495. inline _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept;
  496. inline _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept;
  497. private:
  498. pointer __data;
  499. size_type __size;
  500. };
  501. template<typename _Tp>
  502. inline _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> span<_Tp, dynamic_extent>::__as_bytes() const noexcept
  503. { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
  504. template<typename _Tp>
  505. inline _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> span<_Tp, dynamic_extent>::__as_writable_bytes() const noexcept
  506. { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
  507. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  508. template <class _Tp, size_t _Extent>
  509. inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
  510. template <class _ElementType, size_t _Extent>
  511. inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
  512. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  513. // as_bytes & as_writable_bytes
  514. template <class _Tp, size_t _Extent>
  515. _LIBCPP_INLINE_VISIBILITY
  516. auto as_bytes(span<_Tp, _Extent> __s) noexcept
  517. -> decltype(__s.__as_bytes())
  518. { return __s.__as_bytes(); }
  519. template <class _Tp, size_t _Extent>
  520. _LIBCPP_INLINE_VISIBILITY
  521. auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
  522. -> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
  523. { return __s.__as_writable_bytes(); }
  524. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  525. template<contiguous_iterator _It, class _EndOrSize>
  526. span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
  527. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  528. template<class _Tp, size_t _Sz>
  529. span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
  530. template<class _Tp, size_t _Sz>
  531. span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
  532. template<class _Tp, size_t _Sz>
  533. span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
  534. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  535. template<ranges::contiguous_range _Range>
  536. span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
  537. #endif
  538. #endif // _LIBCPP_STD_VER > 17
  539. _LIBCPP_END_NAMESPACE_STD
  540. _LIBCPP_POP_MACROS
  541. #endif // _LIBCPP_SPAN