span 23 KB

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