18a0794b7SMarshall Clow// -*- C++ -*- 2eb8650a7SLouis Dionne//===----------------------------------------------------------------------===// 38a0794b7SMarshall Clow// 457b08b09SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 557b08b09SChandler Carruth// See https://llvm.org/LICENSE.txt for license information. 657b08b09SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 78a0794b7SMarshall Clow// 88a0794b7SMarshall Clow//===---------------------------------------------------------------------===// 98a0794b7SMarshall Clow 108a0794b7SMarshall Clow#ifndef _LIBCPP_SPAN 118a0794b7SMarshall Clow#define _LIBCPP_SPAN 128a0794b7SMarshall Clow 138a0794b7SMarshall Clow/* 148a0794b7SMarshall Clow span synopsis 158a0794b7SMarshall Clow 168a0794b7SMarshall Clownamespace std { 178a0794b7SMarshall Clow 188a0794b7SMarshall Clow// constants 197ad06a93SMarshall Clowinline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); 208a0794b7SMarshall Clow 218a0794b7SMarshall Clow// [views.span], class template span 227ad06a93SMarshall Clowtemplate <class ElementType, size_t Extent = dynamic_extent> 238a0794b7SMarshall Clow class span; 248a0794b7SMarshall Clow 2501ace074SMark de Wevertemplate<class ElementType, size_t Extent> 26462f8f06SChristopher Di Bella inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true; 27462f8f06SChristopher Di Bella 28462f8f06SChristopher Di Bellatemplate<class ElementType, size_t Extent> 2901ace074SMark de Wever inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true; 3001ace074SMark de Wever 318a0794b7SMarshall Clow// [span.objectrep], views of object representation 327ad06a93SMarshall Clowtemplate <class ElementType, size_t Extent> 338a0794b7SMarshall Clow span<const byte, ((Extent == dynamic_extent) ? dynamic_extent : 348eda3ad2SMarshall Clow (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept; 358a0794b7SMarshall Clow 367ad06a93SMarshall Clowtemplate <class ElementType, size_t Extent> 378a0794b7SMarshall Clow span< byte, ((Extent == dynamic_extent) ? dynamic_extent : 388eda3ad2SMarshall Clow (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept; 398a0794b7SMarshall Clow 408a0794b7SMarshall Clow 417ad06a93SMarshall Clowtemplate <class ElementType, size_t Extent = dynamic_extent> 428a0794b7SMarshall Clowclass span { 438a0794b7SMarshall Clowpublic: 448a0794b7SMarshall Clow // constants and types 458a0794b7SMarshall Clow using element_type = ElementType; 468a0794b7SMarshall Clow using value_type = remove_cv_t<ElementType>; 471466335cSLouis Dionne using size_type = size_t; 488a0794b7SMarshall Clow using difference_type = ptrdiff_t; 498a0794b7SMarshall Clow using pointer = element_type*; 504fd03954SMarshall Clow using const_pointer = const element_type*; 518a0794b7SMarshall Clow using reference = element_type&; 524fd03954SMarshall Clow using const_reference = const element_type&; 537ad06a93SMarshall Clow using iterator = implementation-defined; 548a0794b7SMarshall Clow using reverse_iterator = std::reverse_iterator<iterator>; 551466335cSLouis Dionne static constexpr size_type extent = Extent; 568a0794b7SMarshall Clow 578a0794b7SMarshall Clow // [span.cons], span constructors, copy, assignment, and destructor 588a0794b7SMarshall Clow constexpr span() noexcept; 593a208c68SJoe Loser template <class It> 603a208c68SJoe Loser constexpr explicit(Extent != dynamic_extent) span(It first, size_type count); 613a208c68SJoe Loser template <class It, class End> 623a208c68SJoe Loser constexpr explicit(Extent != dynamic_extent) span(It first, End last); 638a0794b7SMarshall Clow template <size_t N> 640412c007SArthur O'Dwyer constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept; 658a0794b7SMarshall Clow template <size_t N> 668a0794b7SMarshall Clow constexpr span(array<value_type, N>& arr) noexcept; 678a0794b7SMarshall Clow template <size_t N> 688a0794b7SMarshall Clow constexpr span(const array<value_type, N>& arr) noexcept; 693a208c68SJoe Loser template<class R> 703a208c68SJoe Loser constexpr explicit(Extent != dynamic_extent) span(R&& r); 718a0794b7SMarshall Clow constexpr span(const span& other) noexcept = default; 727ad06a93SMarshall Clow template <class OtherElementType, size_t OtherExtent> 736d2599e4SMichael Schellenberger Costa constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept; 748a0794b7SMarshall Clow ~span() noexcept = default; 758a0794b7SMarshall Clow constexpr span& operator=(const span& other) noexcept = default; 768a0794b7SMarshall Clow 778a0794b7SMarshall Clow // [span.sub], span subviews 787ad06a93SMarshall Clow template <size_t Count> 798a0794b7SMarshall Clow constexpr span<element_type, Count> first() const; 807ad06a93SMarshall Clow template <size_t Count> 818a0794b7SMarshall Clow constexpr span<element_type, Count> last() const; 827ad06a93SMarshall Clow template <size_t Offset, size_t Count = dynamic_extent> 838a0794b7SMarshall Clow constexpr span<element_type, see below> subspan() const; 848a0794b7SMarshall Clow 851466335cSLouis Dionne constexpr span<element_type, dynamic_extent> first(size_type count) const; 861466335cSLouis Dionne constexpr span<element_type, dynamic_extent> last(size_type count) const; 871466335cSLouis Dionne constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const; 888a0794b7SMarshall Clow 898a0794b7SMarshall Clow // [span.obs], span observers 901466335cSLouis Dionne constexpr size_type size() const noexcept; 911466335cSLouis Dionne constexpr size_type size_bytes() const noexcept; 928e92410eSJoe Loser [[nodiscard]] constexpr bool empty() const noexcept; 938a0794b7SMarshall Clow 948a0794b7SMarshall Clow // [span.elem], span element access 951466335cSLouis Dionne constexpr reference operator[](size_type idx) const; 969ab85a69SMarshall Clow constexpr reference front() const; 979ab85a69SMarshall Clow constexpr reference back() const; 988a0794b7SMarshall Clow constexpr pointer data() const noexcept; 998a0794b7SMarshall Clow 1008a0794b7SMarshall Clow // [span.iterators], span iterator support 1018a0794b7SMarshall Clow constexpr iterator begin() const noexcept; 1028a0794b7SMarshall Clow constexpr iterator end() const noexcept; 1038a0794b7SMarshall Clow constexpr reverse_iterator rbegin() const noexcept; 1048a0794b7SMarshall Clow constexpr reverse_iterator rend() const noexcept; 1058a0794b7SMarshall Clow 1068a0794b7SMarshall Clowprivate: 1078a0794b7SMarshall Clow pointer data_; // exposition only 1081466335cSLouis Dionne size_type size_; // exposition only 1098a0794b7SMarshall Clow}; 1108a0794b7SMarshall Clow 1113a208c68SJoe Losertemplate<class It, class EndOrSize> 1123a208c68SJoe Loser span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; 1133a208c68SJoe Loser 1148a0794b7SMarshall Clowtemplate<class T, size_t N> 1158a0794b7SMarshall Clow span(T (&)[N]) -> span<T, N>; 1168a0794b7SMarshall Clow 1178a0794b7SMarshall Clowtemplate<class T, size_t N> 1188a0794b7SMarshall Clow span(array<T, N>&) -> span<T, N>; 1198a0794b7SMarshall Clow 1208a0794b7SMarshall Clowtemplate<class T, size_t N> 1218a0794b7SMarshall Clow span(const array<T, N>&) -> span<const T, N>; 1228a0794b7SMarshall Clow 1233a208c68SJoe Losertemplate<class R> 1243a208c68SJoe Loser span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>; 1258a0794b7SMarshall Clow 1268a0794b7SMarshall Clow} // namespace std 1278a0794b7SMarshall Clow 1288a0794b7SMarshall Clow*/ 1298a0794b7SMarshall Clow 130f87aa19bSLouis Dionne#include <__assert> 13135773f55SMarshall Clow#include <__config> 13206b40e80SArthur O'Dwyer#include <__debug> 1333a208c68SJoe Loser#include <__iterator/concepts.h> 134f32f3db9SLouis Dionne#include <__iterator/wrap_iter.h> 1353a208c68SJoe Loser#include <__ranges/concepts.h> 1363a208c68SJoe Loser#include <__ranges/data.h> 13701ace074SMark de Wever#include <__ranges/enable_borrowed_range.h> 138462f8f06SChristopher Di Bella#include <__ranges/enable_view.h> 1393a208c68SJoe Loser#include <__ranges/size.h> 1408a0794b7SMarshall Clow#include <array> // for array 1418a0794b7SMarshall Clow#include <cstddef> // for byte 142edbaa7fcSLouis Dionne#include <iterator> // for iterators 14369d5a666SChristopher Di Bella#include <limits> 144edbaa7fcSLouis Dionne#include <type_traits> // for remove_cv, etc 145e37bbfe5SChristopher Di Bella#include <version> 1468a0794b7SMarshall Clow 1478a0794b7SMarshall Clow#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1488a0794b7SMarshall Clow# pragma GCC system_header 1498a0794b7SMarshall Clow#endif 1508a0794b7SMarshall Clow 15103ee4612SArthur O'Dwyer_LIBCPP_PUSH_MACROS 15203ee4612SArthur O'Dwyer#include <__undef_macros> 15303ee4612SArthur O'Dwyer 1548a0794b7SMarshall Clow_LIBCPP_BEGIN_NAMESPACE_STD 1558a0794b7SMarshall Clow 1568a0794b7SMarshall Clow#if _LIBCPP_STD_VER > 17 1578a0794b7SMarshall Clow 15803ee4612SArthur O'Dwyerinline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); 1597ad06a93SMarshall Clowtemplate <typename _Tp, size_t _Extent = dynamic_extent> class span; 1608a0794b7SMarshall Clow 1618a0794b7SMarshall Clow 1628a0794b7SMarshall Clowtemplate <class _Tp> 1633a208c68SJoe Loserstruct __is_std_array : false_type {}; 1648a0794b7SMarshall Clow 1658a0794b7SMarshall Clowtemplate <class _Tp, size_t _Sz> 1663a208c68SJoe Loserstruct __is_std_array<array<_Tp, _Sz>> : true_type {}; 1678a0794b7SMarshall Clow 1688a0794b7SMarshall Clowtemplate <class _Tp> 1693a208c68SJoe Loserstruct __is_std_span : false_type {}; 1708a0794b7SMarshall Clow 1713a208c68SJoe Losertemplate <class _Tp, size_t _Sz> 1723a208c68SJoe Loserstruct __is_std_span<span<_Tp, _Sz>> : true_type {}; 1738a0794b7SMarshall Clow 174*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 1753a208c68SJoe Losertemplate <class _Range, class _ElementType> 1763a208c68SJoe Loserconcept __span_compatible_range = 1773a208c68SJoe Loser ranges::contiguous_range<_Range> && 1783a208c68SJoe Loser ranges::sized_range<_Range> && 1793a208c68SJoe Loser (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) && 1803a208c68SJoe Loser !__is_std_span<remove_cvref_t<_Range>>::value && 1813a208c68SJoe Loser !__is_std_array<remove_cvref_t<_Range>>::value && 1823a208c68SJoe Loser !is_array_v<remove_cvref_t<_Range>> && 1833a208c68SJoe Loser is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>; 184*d2baefaeSJoe Loser#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 1858a0794b7SMarshall Clow 1867ad06a93SMarshall Clowtemplate <typename _Tp, size_t _Extent> 1878a0794b7SMarshall Clowclass _LIBCPP_TEMPLATE_VIS span { 1888a0794b7SMarshall Clowpublic: 1898a0794b7SMarshall Clow// constants and types 1908a0794b7SMarshall Clow using element_type = _Tp; 1918a0794b7SMarshall Clow using value_type = remove_cv_t<_Tp>; 1921466335cSLouis Dionne using size_type = size_t; 1938a0794b7SMarshall Clow using difference_type = ptrdiff_t; 1948a0794b7SMarshall Clow using pointer = _Tp *; 1954fd03954SMarshall Clow using const_pointer = const _Tp *; 1968a0794b7SMarshall Clow using reference = _Tp &; 1974fd03954SMarshall Clow using const_reference = const _Tp &; 1986946f0ecSArthur O'Dwyer#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS) 1996946f0ecSArthur O'Dwyer using iterator = pointer; 2006946f0ecSArthur O'Dwyer#else 2018a0794b7SMarshall Clow using iterator = __wrap_iter<pointer>; 2026946f0ecSArthur O'Dwyer#endif 2038a0794b7SMarshall Clow using reverse_iterator = _VSTD::reverse_iterator<iterator>; 2048a0794b7SMarshall Clow 2051466335cSLouis Dionne static constexpr size_type extent = _Extent; 2068a0794b7SMarshall Clow 2078a0794b7SMarshall Clow// [span.cons], span constructors, copy, assignment, and destructor 20879941086SMichael Schellenberger Costa template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr> 20979941086SMichael Schellenberger Costa _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {} 2108a0794b7SMarshall Clow 2118a0794b7SMarshall Clow constexpr span (const span&) noexcept = default; 2128a0794b7SMarshall Clow constexpr span& operator=(const span&) noexcept = default; 2138a0794b7SMarshall Clow 214*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 2153a208c68SJoe Loser template <class _It, 2163a208c68SJoe Loser enable_if_t<contiguous_iterator<_It> && 2173a208c68SJoe Loser is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>, 2183a208c68SJoe Loser nullptr_t> = nullptr> 2193a208c68SJoe Loser _LIBCPP_INLINE_VISIBILITY 2203a208c68SJoe Loser constexpr explicit span(_It __first, size_type __count) 2213a208c68SJoe Loser : __data{_VSTD::to_address(__first)} { 2223a208c68SJoe Loser (void)__count; 2233a208c68SJoe Loser _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)"); 2243a208c68SJoe Loser } 2253a208c68SJoe Loser 2263a208c68SJoe Loser template < 2273a208c68SJoe Loser class _It, class _End, 2283a208c68SJoe Loser enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> && 2293a208c68SJoe Loser contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>, 2303a208c68SJoe Loser nullptr_t> = nullptr> 2313a208c68SJoe Loser _LIBCPP_INLINE_VISIBILITY 2323a208c68SJoe Loser constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} { 2333a208c68SJoe Loser (void)__last; 2343a208c68SJoe Loser _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)"); 2353a208c68SJoe Loser _LIBCPP_ASSERT(__last - __first == _Extent, 2363a208c68SJoe Loser "invalid range in span's constructor (iterator, sentinel): last - first != extent"); 2373a208c68SJoe Loser } 238*d2baefaeSJoe Loser#endif // _LIBCPP_STD_VER > 17 2398a0794b7SMarshall Clow 2400412c007SArthur O'Dwyer _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {} 241ab9f1116SMichael Schellenberger Costa 242ab9f1116SMichael Schellenberger Costa template <class _OtherElementType, 243ab9f1116SMichael Schellenberger Costa enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> 244ab9f1116SMichael Schellenberger Costa _LIBCPP_INLINE_VISIBILITY 245ab9f1116SMichael Schellenberger Costa constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {} 246ab9f1116SMichael Schellenberger Costa 247ab9f1116SMichael Schellenberger Costa template <class _OtherElementType, 248ab9f1116SMichael Schellenberger Costa enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> 249ab9f1116SMichael Schellenberger Costa _LIBCPP_INLINE_VISIBILITY 250ab9f1116SMichael Schellenberger Costa constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {} 2518a0794b7SMarshall Clow 252*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 2533a208c68SJoe Loser template <__span_compatible_range<element_type> _Range> 2546d2599e4SMichael Schellenberger Costa _LIBCPP_INLINE_VISIBILITY 2553a208c68SJoe Loser constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} { 2563a208c68SJoe Loser _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)"); 2576d2599e4SMichael Schellenberger Costa } 258*d2baefaeSJoe Loser#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 2596d2599e4SMichael Schellenberger Costa 2608a0794b7SMarshall Clow template <class _OtherElementType> 2618eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 2628a0794b7SMarshall Clow constexpr span(const span<_OtherElementType, _Extent>& __other, 2638a0794b7SMarshall Clow enable_if_t< 2648a0794b7SMarshall Clow is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, 2658a0794b7SMarshall Clow nullptr_t> = nullptr) 2668a0794b7SMarshall Clow : __data{__other.data()} {} 2678a0794b7SMarshall Clow 2688a0794b7SMarshall Clow template <class _OtherElementType> 2698eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 2706d2599e4SMichael Schellenberger Costa constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other, 2718a0794b7SMarshall Clow enable_if_t< 2728a0794b7SMarshall Clow is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, 2738a0794b7SMarshall Clow nullptr_t> = nullptr) noexcept 2748a0794b7SMarshall Clow : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } 2758a0794b7SMarshall Clow 2768a0794b7SMarshall Clow 2778a0794b7SMarshall Clow// ~span() noexcept = default; 2788a0794b7SMarshall Clow 2797ad06a93SMarshall Clow template <size_t _Count> 2808eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 2818a0794b7SMarshall Clow constexpr span<element_type, _Count> first() const noexcept 2828a0794b7SMarshall Clow { 2838a0794b7SMarshall Clow static_assert(_Count <= _Extent, "Count out of range in span::first()"); 2846d2599e4SMichael Schellenberger Costa return span<element_type, _Count>{data(), _Count}; 2858a0794b7SMarshall Clow } 2868a0794b7SMarshall Clow 2877ad06a93SMarshall Clow template <size_t _Count> 2888eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 2898a0794b7SMarshall Clow constexpr span<element_type, _Count> last() const noexcept 2908a0794b7SMarshall Clow { 2918a0794b7SMarshall Clow static_assert(_Count <= _Extent, "Count out of range in span::last()"); 2926d2599e4SMichael Schellenberger Costa return span<element_type, _Count>{data() + size() - _Count, _Count}; 2938a0794b7SMarshall Clow } 2948a0794b7SMarshall Clow 2958a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY 2961466335cSLouis Dionne constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept 2978a0794b7SMarshall Clow { 2988eda3ad2SMarshall Clow _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); 2998a0794b7SMarshall Clow return {data(), __count}; 3008a0794b7SMarshall Clow } 3018a0794b7SMarshall Clow 3028a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY 3031466335cSLouis Dionne constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept 3048a0794b7SMarshall Clow { 3058eda3ad2SMarshall Clow _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); 3068a0794b7SMarshall Clow return {data() + size() - __count, __count}; 3078a0794b7SMarshall Clow } 3088a0794b7SMarshall Clow 3097ad06a93SMarshall Clow template <size_t _Offset, size_t _Count = dynamic_extent> 3108eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 3118a0794b7SMarshall Clow constexpr auto subspan() const noexcept 3128a0794b7SMarshall Clow -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> 3138a0794b7SMarshall Clow { 3148eda3ad2SMarshall Clow static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()"); 315b5abd50fSLouis Dionne static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()"); 3166d2599e4SMichael Schellenberger Costa 3176d2599e4SMichael Schellenberger Costa using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>; 3186d2599e4SMichael Schellenberger Costa return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; 3198a0794b7SMarshall Clow } 3208a0794b7SMarshall Clow 3218a0794b7SMarshall Clow 3228eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 3238a0794b7SMarshall Clow constexpr span<element_type, dynamic_extent> 3241466335cSLouis Dionne subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept 3258a0794b7SMarshall Clow { 3268eda3ad2SMarshall Clow _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); 3278eda3ad2SMarshall Clow _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)"); 3288a0794b7SMarshall Clow if (__count == dynamic_extent) 3298a0794b7SMarshall Clow return {data() + __offset, size() - __offset}; 330b5abd50fSLouis Dionne _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); 3318a0794b7SMarshall Clow return {data() + __offset, __count}; 3328a0794b7SMarshall Clow } 3338a0794b7SMarshall Clow 3341466335cSLouis Dionne _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; } 3351466335cSLouis Dionne _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } 3368e92410eSJoe Loser [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; } 3378a0794b7SMarshall Clow 3381466335cSLouis Dionne _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept 3398a0794b7SMarshall Clow { 340b4a3e6b6SLouis Dionne _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds"); 3418a0794b7SMarshall Clow return __data[__idx]; 3428a0794b7SMarshall Clow } 3438a0794b7SMarshall Clow 3449ab85a69SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept 3458a0794b7SMarshall Clow { 3460a0e0afaSLouis Dionne _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span"); 3479ab85a69SMarshall Clow return __data[0]; 3489ab85a69SMarshall Clow } 3499ab85a69SMarshall Clow 3509ab85a69SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept 3519ab85a69SMarshall Clow { 3520a0e0afaSLouis Dionne _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span"); 3539ab85a69SMarshall Clow return __data[size()-1]; 3548a0794b7SMarshall Clow } 3558a0794b7SMarshall Clow 3568a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } 3578a0794b7SMarshall Clow 3588a0794b7SMarshall Clow// [span.iter], span iterator support 3598a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } 3608a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } 3618a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } 3628a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } 3638a0794b7SMarshall Clow 3648a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept 3656d2599e4SMichael Schellenberger Costa { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; } 3668a0794b7SMarshall Clow 3677cd0dad8SLouis Dionne _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept 3686d2599e4SMichael Schellenberger Costa { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; } 3698a0794b7SMarshall Clow 3708a0794b7SMarshall Clowprivate: 3718a0794b7SMarshall Clow pointer __data; 3728a0794b7SMarshall Clow 3738a0794b7SMarshall Clow}; 3748a0794b7SMarshall Clow 3758a0794b7SMarshall Clow 3768a0794b7SMarshall Clowtemplate <typename _Tp> 3778a0794b7SMarshall Clowclass _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { 3788a0794b7SMarshall Clowprivate: 3798a0794b7SMarshall Clow 3808a0794b7SMarshall Clowpublic: 3818a0794b7SMarshall Clow// constants and types 3828a0794b7SMarshall Clow using element_type = _Tp; 3838a0794b7SMarshall Clow using value_type = remove_cv_t<_Tp>; 3841466335cSLouis Dionne using size_type = size_t; 3858a0794b7SMarshall Clow using difference_type = ptrdiff_t; 3868a0794b7SMarshall Clow using pointer = _Tp *; 3874fd03954SMarshall Clow using const_pointer = const _Tp *; 3888a0794b7SMarshall Clow using reference = _Tp &; 3894fd03954SMarshall Clow using const_reference = const _Tp &; 3906946f0ecSArthur O'Dwyer#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS) 3916946f0ecSArthur O'Dwyer using iterator = pointer; 3926946f0ecSArthur O'Dwyer#else 3938a0794b7SMarshall Clow using iterator = __wrap_iter<pointer>; 3946946f0ecSArthur O'Dwyer#endif 3958a0794b7SMarshall Clow using reverse_iterator = _VSTD::reverse_iterator<iterator>; 3968a0794b7SMarshall Clow 3971466335cSLouis Dionne static constexpr size_type extent = dynamic_extent; 3988a0794b7SMarshall Clow 3998a0794b7SMarshall Clow// [span.cons], span constructors, copy, assignment, and destructor 4008a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {} 4018a0794b7SMarshall Clow 4028a0794b7SMarshall Clow constexpr span (const span&) noexcept = default; 4038a0794b7SMarshall Clow constexpr span& operator=(const span&) noexcept = default; 4048a0794b7SMarshall Clow 405*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 4063a208c68SJoe Loser template <class _It, 4073a208c68SJoe Loser enable_if_t<contiguous_iterator<_It> && 4083a208c68SJoe Loser is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>, 4093a208c68SJoe Loser nullptr_t> = nullptr> 4103a208c68SJoe Loser _LIBCPP_INLINE_VISIBILITY 4113a208c68SJoe Loser constexpr span(_It __first, size_type __count) 4123a208c68SJoe Loser : __data{_VSTD::to_address(__first)}, __size{__count} {} 4133a208c68SJoe Loser 4143a208c68SJoe Loser template < 4153a208c68SJoe Loser class _It, class _End, 4163a208c68SJoe Loser enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> && 4173a208c68SJoe Loser contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>, 4183a208c68SJoe Loser nullptr_t> = nullptr> 4193a208c68SJoe Loser _LIBCPP_INLINE_VISIBILITY 4203a208c68SJoe Loser constexpr span(_It __first, _End __last) 4213a208c68SJoe Loser : __data(_VSTD::to_address(__first)), __size(__last - __first) {} 422*d2baefaeSJoe Loser#endif // _LIBCPP_STD_VER > 17 4238a0794b7SMarshall Clow 4248a0794b7SMarshall Clow template <size_t _Sz> 4258eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4260412c007SArthur O'Dwyer constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {} 4278a0794b7SMarshall Clow 428ab9f1116SMichael Schellenberger Costa template <class _OtherElementType, size_t _Sz, 429ab9f1116SMichael Schellenberger Costa enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> 4308eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 431ab9f1116SMichael Schellenberger Costa constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} 4328a0794b7SMarshall Clow 433ab9f1116SMichael Schellenberger Costa template <class _OtherElementType, size_t _Sz, 434ab9f1116SMichael Schellenberger Costa enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> 4358eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 436ab9f1116SMichael Schellenberger Costa constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} 4378a0794b7SMarshall Clow 438*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 4393a208c68SJoe Loser template <__span_compatible_range<element_type> _Range> 4408eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4413a208c68SJoe Loser constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {} 442*d2baefaeSJoe Loser#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 4438a0794b7SMarshall Clow 4447ad06a93SMarshall Clow template <class _OtherElementType, size_t _OtherExtent> 4458eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4468a0794b7SMarshall Clow constexpr span(const span<_OtherElementType, _OtherExtent>& __other, 4478a0794b7SMarshall Clow enable_if_t< 4488a0794b7SMarshall Clow is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, 4498a0794b7SMarshall Clow nullptr_t> = nullptr) noexcept 4508a0794b7SMarshall Clow : __data{__other.data()}, __size{__other.size()} {} 4518a0794b7SMarshall Clow 4528a0794b7SMarshall Clow// ~span() noexcept = default; 4538a0794b7SMarshall Clow 4547ad06a93SMarshall Clow template <size_t _Count> 4558eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4568a0794b7SMarshall Clow constexpr span<element_type, _Count> first() const noexcept 4578a0794b7SMarshall Clow { 4588a0794b7SMarshall Clow _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()"); 4596d2599e4SMichael Schellenberger Costa return span<element_type, _Count>{data(), _Count}; 4608a0794b7SMarshall Clow } 4618a0794b7SMarshall Clow 4627ad06a93SMarshall Clow template <size_t _Count> 4638eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4648a0794b7SMarshall Clow constexpr span<element_type, _Count> last() const noexcept 4658a0794b7SMarshall Clow { 4668a0794b7SMarshall Clow _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()"); 4676d2599e4SMichael Schellenberger Costa return span<element_type, _Count>{data() + size() - _Count, _Count}; 4688a0794b7SMarshall Clow } 4698a0794b7SMarshall Clow 4708a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4711466335cSLouis Dionne constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept 4728a0794b7SMarshall Clow { 4738eda3ad2SMarshall Clow _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); 4748a0794b7SMarshall Clow return {data(), __count}; 4758a0794b7SMarshall Clow } 4768a0794b7SMarshall Clow 4778a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4781466335cSLouis Dionne constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept 4798a0794b7SMarshall Clow { 4808eda3ad2SMarshall Clow _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); 4818a0794b7SMarshall Clow return {data() + size() - __count, __count}; 4828a0794b7SMarshall Clow } 4838a0794b7SMarshall Clow 4847ad06a93SMarshall Clow template <size_t _Offset, size_t _Count = dynamic_extent> 4858eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 48692a1f65fSLouis Dionne constexpr span<element_type, _Count> subspan() const noexcept 4878a0794b7SMarshall Clow { 4888eda3ad2SMarshall Clow _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()"); 489b5abd50fSLouis Dionne _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()"); 4906d2599e4SMichael Schellenberger Costa return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; 4918a0794b7SMarshall Clow } 4928a0794b7SMarshall Clow 4938a0794b7SMarshall Clow constexpr span<element_type, dynamic_extent> 4948eda3ad2SMarshall Clow _LIBCPP_INLINE_VISIBILITY 4951466335cSLouis Dionne subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept 4968a0794b7SMarshall Clow { 4978eda3ad2SMarshall Clow _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); 4988eda3ad2SMarshall Clow _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)"); 4998a0794b7SMarshall Clow if (__count == dynamic_extent) 5008a0794b7SMarshall Clow return {data() + __offset, size() - __offset}; 501b5abd50fSLouis Dionne _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); 5028a0794b7SMarshall Clow return {data() + __offset, __count}; 5038a0794b7SMarshall Clow } 5048a0794b7SMarshall Clow 5051466335cSLouis Dionne _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; } 5061466335cSLouis Dionne _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); } 5078e92410eSJoe Loser [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; } 5088a0794b7SMarshall Clow 5091466335cSLouis Dionne _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept 5108a0794b7SMarshall Clow { 511b4a3e6b6SLouis Dionne _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds"); 5128a0794b7SMarshall Clow return __data[__idx]; 5138a0794b7SMarshall Clow } 5148a0794b7SMarshall Clow 5159ab85a69SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept 5168a0794b7SMarshall Clow { 5179ab85a69SMarshall Clow _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span"); 5189ab85a69SMarshall Clow return __data[0]; 5198a0794b7SMarshall Clow } 5208a0794b7SMarshall Clow 5219ab85a69SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept 5229ab85a69SMarshall Clow { 5239ab85a69SMarshall Clow _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span"); 5249ab85a69SMarshall Clow return __data[size()-1]; 5259ab85a69SMarshall Clow } 5269ab85a69SMarshall Clow 5279ab85a69SMarshall Clow 5288a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } 5298a0794b7SMarshall Clow 5308a0794b7SMarshall Clow// [span.iter], span iterator support 5318a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } 5328a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } 5338a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } 5348a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } 5358a0794b7SMarshall Clow 5368a0794b7SMarshall Clow _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept 5378a0794b7SMarshall Clow { return {reinterpret_cast<const byte *>(data()), size_bytes()}; } 5388a0794b7SMarshall Clow 5397cd0dad8SLouis Dionne _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept 5408a0794b7SMarshall Clow { return {reinterpret_cast<byte *>(data()), size_bytes()}; } 5418a0794b7SMarshall Clow 5428a0794b7SMarshall Clowprivate: 5438a0794b7SMarshall Clow pointer __data; 5441466335cSLouis Dionne size_type __size; 5458a0794b7SMarshall Clow}; 5468a0794b7SMarshall Clow 547*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 54801ace074SMark de Wevertemplate <class _Tp, size_t _Extent> 54901ace074SMark de Weverinline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true; 550462f8f06SChristopher Di Bella 551462f8f06SChristopher Di Bellatemplate <class _ElementType, size_t _Extent> 552462f8f06SChristopher Di Bellainline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true; 553*d2baefaeSJoe Loser#endif // _LIBCPP_STD_VER > 17 55401ace074SMark de Wever 5557cd0dad8SLouis Dionne// as_bytes & as_writable_bytes 5567ad06a93SMarshall Clowtemplate <class _Tp, size_t _Extent> 55702e1651cSMarshall Clow_LIBCPP_INLINE_VISIBILITY 5588a0794b7SMarshall Clowauto as_bytes(span<_Tp, _Extent> __s) noexcept 5598a0794b7SMarshall Clow-> decltype(__s.__as_bytes()) 5608a0794b7SMarshall Clow{ return __s.__as_bytes(); } 5618a0794b7SMarshall Clow 5627ad06a93SMarshall Clowtemplate <class _Tp, size_t _Extent> 56302e1651cSMarshall Clow_LIBCPP_INLINE_VISIBILITY 5647cd0dad8SLouis Dionneauto as_writable_bytes(span<_Tp, _Extent> __s) noexcept 5657cd0dad8SLouis Dionne-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())> 5667cd0dad8SLouis Dionne{ return __s.__as_writable_bytes(); } 5678a0794b7SMarshall Clow 568*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 5693a208c68SJoe Losertemplate<contiguous_iterator _It, class _EndOrSize> 5703a208c68SJoe Loser span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; 571*d2baefaeSJoe Loser#endif // _LIBCPP_STD_VER > 17 5723a208c68SJoe Loser 5738a0794b7SMarshall Clowtemplate<class _Tp, size_t _Sz> 5748a0794b7SMarshall Clow span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; 5758a0794b7SMarshall Clow 5768a0794b7SMarshall Clowtemplate<class _Tp, size_t _Sz> 5778a0794b7SMarshall Clow span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; 5788a0794b7SMarshall Clow 5798a0794b7SMarshall Clowtemplate<class _Tp, size_t _Sz> 5808a0794b7SMarshall Clow span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>; 5818a0794b7SMarshall Clow 582*d2baefaeSJoe Loser#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 5833a208c68SJoe Losertemplate<ranges::contiguous_range _Range> 5843a208c68SJoe Loser span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>; 5853a208c68SJoe Loser#endif 5868a0794b7SMarshall Clow 58735773f55SMarshall Clow#endif // _LIBCPP_STD_VER > 17 58835773f55SMarshall Clow 5898a0794b7SMarshall Clow_LIBCPP_END_NAMESPACE_STD 5908a0794b7SMarshall Clow 59103ee4612SArthur O'Dwyer_LIBCPP_POP_MACROS 59203ee4612SArthur O'Dwyer 5938a0794b7SMarshall Clow#endif // _LIBCPP_SPAN 594