span 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. constexpr span& operator=(const span& other) noexcept = default;
  63. // [span.sub], span subviews
  64. template <size_t Count>
  65. constexpr span<element_type, Count> first() const;
  66. template <size_t Count>
  67. constexpr span<element_type, Count> last() const;
  68. template <size_t Offset, size_t Count = dynamic_extent>
  69. constexpr span<element_type, see below> subspan() const;
  70. constexpr span<element_type, dynamic_extent> first(size_type count) const;
  71. constexpr span<element_type, dynamic_extent> last(size_type count) const;
  72. constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
  73. // [span.obs], span observers
  74. constexpr size_type size() const noexcept;
  75. constexpr size_type size_bytes() const noexcept;
  76. [[nodiscard]] constexpr bool empty() const noexcept;
  77. // [span.elem], span element access
  78. constexpr reference operator[](size_type idx) const;
  79. constexpr reference front() const;
  80. constexpr reference back() const;
  81. constexpr pointer data() const noexcept;
  82. // [span.iterators], span iterator support
  83. constexpr iterator begin() const noexcept;
  84. constexpr iterator end() const noexcept;
  85. constexpr reverse_iterator rbegin() const noexcept;
  86. constexpr reverse_iterator rend() const noexcept;
  87. private:
  88. pointer data_; // exposition only
  89. size_type size_; // exposition only
  90. };
  91. template<class It, class EndOrSize>
  92. span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
  93. template<class T, size_t N>
  94. span(T (&)[N]) -> span<T, N>;
  95. template<class T, size_t N>
  96. span(array<T, N>&) -> span<T, N>;
  97. template<class T, size_t N>
  98. span(const array<T, N>&) -> span<const T, N>;
  99. template<class R>
  100. span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
  101. } // namespace std
  102. */
  103. #include <__assert> // all public C++ headers provide the assertion handler
  104. #include <__config>
  105. #include <__fwd/span.h>
  106. #include <__iterator/bounded_iter.h>
  107. #include <__iterator/concepts.h>
  108. #include <__iterator/iterator_traits.h>
  109. #include <__iterator/wrap_iter.h>
  110. #include <__memory/pointer_traits.h>
  111. #include <__ranges/concepts.h>
  112. #include <__ranges/data.h>
  113. #include <__ranges/enable_borrowed_range.h>
  114. #include <__ranges/enable_view.h>
  115. #include <__ranges/size.h>
  116. #include <__type_traits/is_convertible.h>
  117. #include <__type_traits/remove_cvref.h>
  118. #include <__type_traits/remove_reference.h>
  119. #include <__type_traits/type_identity.h>
  120. #include <__utility/forward.h>
  121. #include <array> // for array
  122. #include <cstddef> // for byte
  123. #include <limits>
  124. #include <version>
  125. // standard-mandated includes
  126. // [iterator.range]
  127. #include <__iterator/access.h>
  128. #include <__iterator/data.h>
  129. #include <__iterator/empty.h>
  130. #include <__iterator/reverse_access.h>
  131. #include <__iterator/size.h>
  132. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  133. # pragma GCC system_header
  134. #endif
  135. _LIBCPP_PUSH_MACROS
  136. #include <__undef_macros>
  137. _LIBCPP_BEGIN_NAMESPACE_STD
  138. #if _LIBCPP_STD_VER >= 20
  139. template <class _Tp>
  140. struct __is_std_array : false_type {};
  141. template <class _Tp, size_t _Sz>
  142. struct __is_std_array<array<_Tp, _Sz>> : true_type {};
  143. template <class _Tp>
  144. struct __is_std_span : false_type {};
  145. template <class _Tp, size_t _Sz>
  146. struct __is_std_span<span<_Tp, _Sz>> : true_type {};
  147. template <class _Range, class _ElementType>
  148. concept __span_compatible_range =
  149. ranges::contiguous_range<_Range> &&
  150. ranges::sized_range<_Range> &&
  151. (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
  152. !__is_std_span<remove_cvref_t<_Range>>::value &&
  153. !__is_std_array<remove_cvref_t<_Range>>::value &&
  154. !is_array_v<remove_cvref_t<_Range>> &&
  155. is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
  156. template <class _From, class _To>
  157. concept __span_array_convertible = is_convertible_v<_From(*)[], _To(*)[]>;
  158. template <class _It, class _Tp>
  159. concept __span_compatible_iterator = contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>;
  160. template <class _Sentinel, class _It>
  161. concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>;
  162. template <typename _Tp, size_t _Extent>
  163. class _LIBCPP_TEMPLATE_VIS span {
  164. public:
  165. // constants and types
  166. using element_type = _Tp;
  167. using value_type = remove_cv_t<_Tp>;
  168. using size_type = size_t;
  169. using difference_type = ptrdiff_t;
  170. using pointer = _Tp *;
  171. using const_pointer = const _Tp *;
  172. using reference = _Tp &;
  173. using const_reference = const _Tp &;
  174. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  175. using iterator = __bounded_iter<pointer>;
  176. #else
  177. using iterator = __wrap_iter<pointer>;
  178. #endif
  179. using reverse_iterator = _VSTD::reverse_iterator<iterator>;
  180. static constexpr size_type extent = _Extent;
  181. // [span.cons], span constructors, copy, assignment, and destructor
  182. template <size_t _Sz = _Extent> requires(_Sz == 0)
  183. _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data_{nullptr} {}
  184. constexpr span (const span&) noexcept = default;
  185. constexpr span& operator=(const span&) noexcept = default;
  186. template <__span_compatible_iterator<element_type> _It>
  187. _LIBCPP_INLINE_VISIBILITY
  188. constexpr explicit span(_It __first, size_type __count)
  189. : __data_{_VSTD::to_address(__first)} {
  190. (void)__count;
  191. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
  192. }
  193. template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
  194. _LIBCPP_INLINE_VISIBILITY
  195. constexpr explicit span(_It __first, _End __last) : __data_{_VSTD::to_address(__first)} {
  196. // [span.cons]/10
  197. // Throws: When and what last - first throws.
  198. [[maybe_unused]] auto __dist = __last - __first;
  199. _LIBCPP_ASSERT_VALID_INPUT_RANGE(__dist >= 0, "invalid range in span's constructor (iterator, sentinel)");
  200. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  201. __dist == _Extent, "invalid range in span's constructor (iterator, sentinel): last - first != extent");
  202. }
  203. _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data_{__arr} {}
  204. template <__span_array_convertible<element_type> _OtherElementType>
  205. _LIBCPP_INLINE_VISIBILITY
  206. constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {}
  207. template <class _OtherElementType>
  208. requires __span_array_convertible<const _OtherElementType, element_type>
  209. _LIBCPP_INLINE_VISIBILITY
  210. constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {}
  211. template <__span_compatible_range<element_type> _Range>
  212. _LIBCPP_INLINE_VISIBILITY
  213. constexpr explicit span(_Range&& __r) : __data_{ranges::data(__r)} {
  214. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  215. ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
  216. }
  217. template <__span_array_convertible<element_type> _OtherElementType>
  218. _LIBCPP_INLINE_VISIBILITY
  219. constexpr span(const span<_OtherElementType, _Extent>& __other)
  220. : __data_{__other.data()} {}
  221. template <__span_array_convertible<element_type> _OtherElementType>
  222. _LIBCPP_INLINE_VISIBILITY
  223. constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept
  224. : __data_{__other.data()} {
  225. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  226. _Extent == __other.size(), "size mismatch in span's constructor (other span)");
  227. }
  228. template <size_t _Count>
  229. _LIBCPP_INLINE_VISIBILITY
  230. constexpr span<element_type, _Count> first() const noexcept
  231. {
  232. static_assert(_Count <= _Extent, "span<T, N>::first<Count>(): Count out of range");
  233. return span<element_type, _Count>{data(), _Count};
  234. }
  235. template <size_t _Count>
  236. _LIBCPP_INLINE_VISIBILITY
  237. constexpr span<element_type, _Count> last() const noexcept
  238. {
  239. static_assert(_Count <= _Extent, "span<T, N>::last<Count>(): Count out of range");
  240. return span<element_type, _Count>{data() + size() - _Count, _Count};
  241. }
  242. _LIBCPP_INLINE_VISIBILITY
  243. constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
  244. {
  245. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::first(count): count out of range");
  246. return {data(), __count};
  247. }
  248. _LIBCPP_INLINE_VISIBILITY
  249. constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
  250. {
  251. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::last(count): count out of range");
  252. return {data() + size() - __count, __count};
  253. }
  254. template <size_t _Offset, size_t _Count = dynamic_extent>
  255. _LIBCPP_INLINE_VISIBILITY
  256. constexpr auto subspan() const noexcept
  257. -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
  258. {
  259. static_assert(_Offset <= _Extent, "span<T, N>::subspan<Offset, Count>(): Offset out of range");
  260. static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "span<T, N>::subspan<Offset, Count>(): Offset + Count out of range");
  261. using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
  262. return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
  263. }
  264. _LIBCPP_INLINE_VISIBILITY
  265. constexpr span<element_type, dynamic_extent>
  266. subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
  267. {
  268. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  269. __offset <= size(), "span<T, N>::subspan(offset, count): offset out of range");
  270. if (__count == dynamic_extent)
  271. return {data() + __offset, size() - __offset};
  272. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  273. __count <= size() - __offset, "span<T, N>::subspan(offset, count): offset + count out of range");
  274. return {data() + __offset, __count};
  275. }
  276. _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
  277. _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
  278. [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
  279. _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
  280. {
  281. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T, N>::operator[](index): index out of range");
  282. return __data_[__idx];
  283. }
  284. _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
  285. {
  286. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::front() on empty span");
  287. return __data_[0];
  288. }
  289. _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
  290. {
  291. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::back() on empty span");
  292. return __data_[size()-1];
  293. }
  294. _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data_; }
  295. // [span.iter], span iterator support
  296. _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
  297. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  298. return std::__make_bounded_iter(data(), data(), data() + size());
  299. #else
  300. return iterator(data());
  301. #endif
  302. }
  303. _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
  304. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  305. return std::__make_bounded_iter(data() + size(), data(), data() + size());
  306. #else
  307. return iterator(data() + size());
  308. #endif
  309. }
  310. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
  311. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
  312. _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
  313. { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
  314. _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
  315. { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
  316. private:
  317. pointer __data_;
  318. };
  319. template <typename _Tp>
  320. class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
  321. public:
  322. // constants and types
  323. using element_type = _Tp;
  324. using value_type = remove_cv_t<_Tp>;
  325. using size_type = size_t;
  326. using difference_type = ptrdiff_t;
  327. using pointer = _Tp *;
  328. using const_pointer = const _Tp *;
  329. using reference = _Tp &;
  330. using const_reference = const _Tp &;
  331. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  332. using iterator = __bounded_iter<pointer>;
  333. #else
  334. using iterator = __wrap_iter<pointer>;
  335. #endif
  336. using reverse_iterator = _VSTD::reverse_iterator<iterator>;
  337. static constexpr size_type extent = dynamic_extent;
  338. // [span.cons], span constructors, copy, assignment, and destructor
  339. _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data_{nullptr}, __size_{0} {}
  340. constexpr span (const span&) noexcept = default;
  341. constexpr span& operator=(const span&) noexcept = default;
  342. template <__span_compatible_iterator<element_type> _It>
  343. _LIBCPP_INLINE_VISIBILITY
  344. constexpr span(_It __first, size_type __count)
  345. : __data_{_VSTD::to_address(__first)}, __size_{__count} {}
  346. template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
  347. _LIBCPP_INLINE_VISIBILITY constexpr span(_It __first, _End __last)
  348. : __data_(_VSTD::to_address(__first)), __size_(__last - __first) {
  349. _LIBCPP_ASSERT_VALID_INPUT_RANGE(
  350. __last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)");
  351. }
  352. template <size_t _Sz>
  353. _LIBCPP_INLINE_VISIBILITY
  354. constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data_{__arr}, __size_{_Sz} {}
  355. template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz>
  356. _LIBCPP_INLINE_VISIBILITY
  357. constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {}
  358. template <class _OtherElementType, size_t _Sz>
  359. requires __span_array_convertible<const _OtherElementType, element_type>
  360. _LIBCPP_INLINE_VISIBILITY
  361. constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {}
  362. template <__span_compatible_range<element_type> _Range>
  363. _LIBCPP_INLINE_VISIBILITY
  364. constexpr span(_Range&& __r) : __data_(ranges::data(__r)), __size_{ranges::size(__r)} {}
  365. template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent>
  366. _LIBCPP_INLINE_VISIBILITY
  367. constexpr span(const span<_OtherElementType, _OtherExtent>& __other) noexcept
  368. : __data_{__other.data()}, __size_{__other.size()} {}
  369. template <size_t _Count>
  370. _LIBCPP_INLINE_VISIBILITY
  371. constexpr span<element_type, _Count> first() const noexcept
  372. {
  373. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::first<Count>(): Count out of range");
  374. return span<element_type, _Count>{data(), _Count};
  375. }
  376. template <size_t _Count>
  377. _LIBCPP_INLINE_VISIBILITY
  378. constexpr span<element_type, _Count> last() const noexcept
  379. {
  380. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::last<Count>(): Count out of range");
  381. return span<element_type, _Count>{data() + size() - _Count, _Count};
  382. }
  383. _LIBCPP_INLINE_VISIBILITY
  384. constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
  385. {
  386. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::first(count): count out of range");
  387. return {data(), __count};
  388. }
  389. _LIBCPP_INLINE_VISIBILITY
  390. constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
  391. {
  392. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::last(count): count out of range");
  393. return {data() + size() - __count, __count};
  394. }
  395. template <size_t _Offset, size_t _Count = dynamic_extent>
  396. _LIBCPP_INLINE_VISIBILITY
  397. constexpr span<element_type, _Count> subspan() const noexcept
  398. {
  399. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  400. _Offset <= size(), "span<T>::subspan<Offset, Count>(): Offset out of range");
  401. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count == dynamic_extent || _Count <= size() - _Offset,
  402. "span<T>::subspan<Offset, Count>(): Offset + Count out of range");
  403. return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
  404. }
  405. constexpr span<element_type, dynamic_extent>
  406. _LIBCPP_INLINE_VISIBILITY
  407. subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
  408. {
  409. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span<T>::subspan(offset, count): offset out of range");
  410. if (__count == dynamic_extent)
  411. return {data() + __offset, size() - __offset};
  412. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  413. __count <= size() - __offset, "span<T>::subspan(offset, count): offset + count out of range");
  414. return {data() + __offset, __count};
  415. }
  416. _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size_; }
  417. _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size_ * sizeof(element_type); }
  418. [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size_ == 0; }
  419. _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
  420. {
  421. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T>::operator[](index): index out of range");
  422. return __data_[__idx];
  423. }
  424. _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
  425. {
  426. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::front() on empty span");
  427. return __data_[0];
  428. }
  429. _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
  430. {
  431. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::back() on empty span");
  432. return __data_[size()-1];
  433. }
  434. _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data_; }
  435. // [span.iter], span iterator support
  436. _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
  437. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  438. return std::__make_bounded_iter(data(), data(), data() + size());
  439. #else
  440. return iterator(data());
  441. #endif
  442. }
  443. _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
  444. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  445. return std::__make_bounded_iter(data() + size(), data(), data() + size());
  446. #else
  447. return iterator(data() + size());
  448. #endif
  449. }
  450. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
  451. _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
  452. _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
  453. { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
  454. _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
  455. { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
  456. private:
  457. pointer __data_;
  458. size_type __size_;
  459. };
  460. template <class _Tp, size_t _Extent>
  461. inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
  462. template <class _ElementType, size_t _Extent>
  463. inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
  464. // as_bytes & as_writable_bytes
  465. template <class _Tp, size_t _Extent>
  466. _LIBCPP_INLINE_VISIBILITY
  467. auto as_bytes(span<_Tp, _Extent> __s) noexcept
  468. { return __s.__as_bytes(); }
  469. template <class _Tp, size_t _Extent> requires(!is_const_v<_Tp>)
  470. _LIBCPP_INLINE_VISIBILITY
  471. auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
  472. { return __s.__as_writable_bytes(); }
  473. #if _LIBCPP_STD_VER >= 20
  474. template<contiguous_iterator _It, class _EndOrSize>
  475. span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
  476. #endif // _LIBCPP_STD_VER >= 20
  477. template<class _Tp, size_t _Sz>
  478. span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
  479. template<class _Tp, size_t _Sz>
  480. span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
  481. template<class _Tp, size_t _Sz>
  482. span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
  483. template<ranges::contiguous_range _Range>
  484. span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
  485. #endif // _LIBCPP_STD_VER >= 20
  486. _LIBCPP_END_NAMESPACE_STD
  487. _LIBCPP_POP_MACROS
  488. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  489. # include <concepts>
  490. # include <functional>
  491. # include <iterator>
  492. # include <type_traits>
  493. #endif
  494. #endif // _LIBCPP_SPAN