span 25 KB

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