Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------------ span ---------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_SPAN |
| 11 | #define _LIBCPP_SPAN |
| 12 | |
| 13 | /* |
| 14 | span synopsis |
| 15 | |
| 16 | namespace std { |
| 17 | |
| 18 | // constants |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 19 | inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 20 | |
| 21 | // [views.span], class template span |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 22 | template <class ElementType, size_t Extent = dynamic_extent> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 23 | class span; |
| 24 | |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 25 | template<class ElementType, size_t Extent> |
Christopher Di Bella | 92072c2 | 2021-05-14 06:20:07 +0000 | [diff] [blame] | 26 | inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true; |
| 27 | |
| 28 | template<class ElementType, size_t Extent> |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 29 | inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true; |
| 30 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 31 | // [span.objectrep], views of object representation |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 32 | template <class ElementType, size_t Extent> |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 33 | span<const byte, ((Extent == dynamic_extent) ? dynamic_extent : |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 34 | (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 35 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 36 | template <class ElementType, size_t Extent> |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 37 | span< byte, ((Extent == dynamic_extent) ? dynamic_extent : |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 38 | (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 39 | |
| 40 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 41 | template <class ElementType, size_t Extent = dynamic_extent> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 42 | class span { |
| 43 | public: |
| 44 | // constants and types |
| 45 | using element_type = ElementType; |
| 46 | using value_type = remove_cv_t<ElementType>; |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 47 | using size_type = size_t; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 48 | using difference_type = ptrdiff_t; |
| 49 | using pointer = element_type*; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 50 | using const_pointer = const element_type*; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 51 | using reference = element_type&; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 52 | using const_reference = const element_type&; |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 53 | using iterator = implementation-defined; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 54 | using reverse_iterator = std::reverse_iterator<iterator>; |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 55 | static constexpr size_type extent = Extent; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 56 | |
| 57 | // [span.cons], span constructors, copy, assignment, and destructor |
| 58 | constexpr span() noexcept; |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 59 | template <class It> |
| 60 | constexpr explicit(Extent != dynamic_extent) span(It first, size_type count); |
| 61 | template <class It, class End> |
| 62 | constexpr explicit(Extent != dynamic_extent) span(It first, End last); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 63 | template <size_t N> |
| 64 | constexpr span(element_type (&arr)[N]) noexcept; |
| 65 | template <size_t N> |
| 66 | constexpr span(array<value_type, N>& arr) noexcept; |
| 67 | template <size_t N> |
| 68 | constexpr span(const array<value_type, N>& arr) noexcept; |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 69 | template<class R> |
| 70 | constexpr explicit(Extent != dynamic_extent) span(R&& r); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 71 | constexpr span(const span& other) noexcept = default; |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 72 | template <class OtherElementType, size_t OtherExtent> |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 73 | constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 74 | ~span() noexcept = default; |
| 75 | constexpr span& operator=(const span& other) noexcept = default; |
| 76 | |
| 77 | // [span.sub], span subviews |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 78 | template <size_t Count> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 79 | constexpr span<element_type, Count> first() const; |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 80 | template <size_t Count> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 81 | constexpr span<element_type, Count> last() const; |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 82 | template <size_t Offset, size_t Count = dynamic_extent> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 83 | constexpr span<element_type, see below> subspan() const; |
| 84 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 85 | constexpr span<element_type, dynamic_extent> first(size_type count) const; |
| 86 | constexpr span<element_type, dynamic_extent> last(size_type count) const; |
| 87 | constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 88 | |
| 89 | // [span.obs], span observers |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 90 | constexpr size_type size() const noexcept; |
| 91 | constexpr size_type size_bytes() const noexcept; |
Joe Loser | f4a2a1e | 2021-10-12 22:30:58 -0400 | [diff] [blame] | 92 | [[nodiscard]] constexpr bool empty() const noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 93 | |
| 94 | // [span.elem], span element access |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 95 | constexpr reference operator[](size_type idx) const; |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 96 | constexpr reference front() const; |
| 97 | constexpr reference back() const; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 98 | constexpr pointer data() const noexcept; |
| 99 | |
| 100 | // [span.iterators], span iterator support |
| 101 | constexpr iterator begin() const noexcept; |
| 102 | constexpr iterator end() const noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 103 | constexpr reverse_iterator rbegin() const noexcept; |
| 104 | constexpr reverse_iterator rend() const noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 107 | pointer data_; // exposition only |
| 108 | size_type size_; // exposition only |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 111 | template<class It, class EndOrSize> |
| 112 | span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; |
| 113 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 114 | template<class T, size_t N> |
| 115 | span(T (&)[N]) -> span<T, N>; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 116 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 117 | template<class T, size_t N> |
| 118 | span(array<T, N>&) -> span<T, N>; |
| 119 | |
| 120 | template<class T, size_t N> |
| 121 | span(const array<T, N>&) -> span<const T, N>; |
| 122 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 123 | template<class R> |
| 124 | span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 125 | |
| 126 | } // namespace std |
| 127 | |
| 128 | */ |
| 129 | |
Marshall Clow | 8dc3ea1 | 2018-07-24 03:56:38 +0000 | [diff] [blame] | 130 | #include <__config> |
Arthur O'Dwyer | 597cac4 | 2021-05-12 23:04:03 -0400 | [diff] [blame] | 131 | #include <__debug> |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 132 | #include <__iterator/concepts.h> |
Louis Dionne | 7724952 | 2021-06-11 09:55:11 -0400 | [diff] [blame] | 133 | #include <__iterator/wrap_iter.h> |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 134 | #include <__ranges/concepts.h> |
| 135 | #include <__ranges/data.h> |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 136 | #include <__ranges/enable_borrowed_range.h> |
Christopher Di Bella | 92072c2 | 2021-05-14 06:20:07 +0000 | [diff] [blame] | 137 | #include <__ranges/enable_view.h> |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 138 | #include <__ranges/size.h> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 139 | #include <array> // for array |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 140 | #include <cstddef> // for byte |
Louis Dionne | 533a14e | 2020-02-11 11:16:40 +0100 | [diff] [blame] | 141 | #include <iterator> // for iterators |
Christopher Di Bella | 599a6c6 | 2021-06-09 23:10:17 +0000 | [diff] [blame] | 142 | #include <limits> |
Louis Dionne | 533a14e | 2020-02-11 11:16:40 +0100 | [diff] [blame] | 143 | #include <type_traits> // for remove_cv, etc |
Christopher Di Bella | 60fc660 | 2021-07-13 19:44:07 +0000 | [diff] [blame] | 144 | #include <version> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 145 | |
| 146 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 147 | #pragma GCC system_header |
| 148 | #endif |
| 149 | |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 150 | _LIBCPP_PUSH_MACROS |
| 151 | #include <__undef_macros> |
| 152 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 153 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 154 | |
| 155 | #if _LIBCPP_STD_VER > 17 |
| 156 | |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 157 | inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 158 | template <typename _Tp, size_t _Extent = dynamic_extent> class span; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 159 | |
| 160 | |
| 161 | template <class _Tp> |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 162 | struct __is_std_array : false_type {}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 163 | |
| 164 | template <class _Tp, size_t _Sz> |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 165 | struct __is_std_array<array<_Tp, _Sz>> : true_type {}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 166 | |
| 167 | template <class _Tp> |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 168 | struct __is_std_span : false_type {}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 169 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 170 | template <class _Tp, size_t _Sz> |
| 171 | struct __is_std_span<span<_Tp, _Sz>> : true_type {}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 172 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 173 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
| 174 | template <class _Range, class _ElementType> |
| 175 | concept __span_compatible_range = |
| 176 | ranges::contiguous_range<_Range> && |
| 177 | ranges::sized_range<_Range> && |
| 178 | (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) && |
| 179 | !__is_std_span<remove_cvref_t<_Range>>::value && |
| 180 | !__is_std_array<remove_cvref_t<_Range>>::value && |
| 181 | !is_array_v<remove_cvref_t<_Range>> && |
| 182 | is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>; |
| 183 | #endif |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 184 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 185 | template <typename _Tp, size_t _Extent> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 186 | class _LIBCPP_TEMPLATE_VIS span { |
| 187 | public: |
| 188 | // constants and types |
| 189 | using element_type = _Tp; |
| 190 | using value_type = remove_cv_t<_Tp>; |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 191 | using size_type = size_t; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 192 | using difference_type = ptrdiff_t; |
| 193 | using pointer = _Tp *; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 194 | using const_pointer = const _Tp *; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 195 | using reference = _Tp &; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 196 | using const_reference = const _Tp &; |
Arthur O'Dwyer | 0a5557f | 2021-04-20 22:08:00 -0400 | [diff] [blame] | 197 | #if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS) |
| 198 | using iterator = pointer; |
| 199 | #else |
| 200 | using iterator = __wrap_iter<pointer>; |
| 201 | #endif |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 202 | using reverse_iterator = _VSTD::reverse_iterator<iterator>; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 203 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 204 | static constexpr size_type extent = _Extent; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 205 | |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 206 | // [span.cons], span constructors, copy, assignment, and destructor |
Michael Schellenberger Costa | 1180735 | 2020-05-14 09:28:27 -0400 | [diff] [blame] | 207 | template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr> |
| 208 | _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 209 | |
| 210 | constexpr span (const span&) noexcept = default; |
| 211 | constexpr span& operator=(const span&) noexcept = default; |
| 212 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 213 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
| 214 | template <class _It, |
| 215 | enable_if_t<contiguous_iterator<_It> && |
| 216 | is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>, |
| 217 | nullptr_t> = nullptr> |
| 218 | _LIBCPP_INLINE_VISIBILITY |
| 219 | constexpr explicit span(_It __first, size_type __count) |
| 220 | : __data{_VSTD::to_address(__first)} { |
| 221 | (void)__count; |
| 222 | _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)"); |
| 223 | } |
| 224 | |
| 225 | template < |
| 226 | class _It, class _End, |
| 227 | enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> && |
| 228 | contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>, |
| 229 | nullptr_t> = nullptr> |
| 230 | _LIBCPP_INLINE_VISIBILITY |
| 231 | constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} { |
| 232 | (void)__last; |
| 233 | _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)"); |
| 234 | _LIBCPP_ASSERT(__last - __first == _Extent, |
| 235 | "invalid range in span's constructor (iterator, sentinel): last - first != extent"); |
| 236 | } |
| 237 | #endif |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 238 | |
| 239 | _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {} |
Michael Schellenberger Costa | b0444f3 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 240 | |
| 241 | template <class _OtherElementType, |
| 242 | enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> |
| 243 | _LIBCPP_INLINE_VISIBILITY |
| 244 | constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {} |
| 245 | |
| 246 | template <class _OtherElementType, |
| 247 | enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> |
| 248 | _LIBCPP_INLINE_VISIBILITY |
| 249 | constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 250 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 251 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
| 252 | template <__span_compatible_range<element_type> _Range> |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 253 | _LIBCPP_INLINE_VISIBILITY |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 254 | constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} { |
| 255 | _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)"); |
| 256 | } |
| 257 | #endif |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 258 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 259 | template <class _OtherElementType> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 260 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 261 | constexpr span(const span<_OtherElementType, _Extent>& __other, |
| 262 | enable_if_t< |
| 263 | is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, |
| 264 | nullptr_t> = nullptr) |
| 265 | : __data{__other.data()} {} |
| 266 | |
| 267 | template <class _OtherElementType> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 268 | _LIBCPP_INLINE_VISIBILITY |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 269 | constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other, |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 270 | enable_if_t< |
| 271 | is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, |
| 272 | nullptr_t> = nullptr) noexcept |
| 273 | : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } |
| 274 | |
| 275 | |
| 276 | // ~span() noexcept = default; |
| 277 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 278 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 279 | _LIBCPP_INLINE_VISIBILITY |
| 280 | constexpr span<element_type, _Count> first() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 281 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 282 | static_assert(_Count <= _Extent, "Count out of range in span::first()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 283 | return span<element_type, _Count>{data(), _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 286 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 287 | _LIBCPP_INLINE_VISIBILITY |
| 288 | constexpr span<element_type, _Count> last() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 289 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 290 | static_assert(_Count <= _Extent, "Count out of range in span::last()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 291 | return span<element_type, _Count>{data() + size() - _Count, _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 292 | } |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 293 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 294 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 295 | constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 296 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 297 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 298 | return {data(), __count}; |
| 299 | } |
| 300 | |
| 301 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 302 | constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 303 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 304 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 305 | return {data() + size() - __count, __count}; |
| 306 | } |
| 307 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 308 | template <size_t _Offset, size_t _Count = dynamic_extent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 309 | _LIBCPP_INLINE_VISIBILITY |
| 310 | constexpr auto subspan() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 311 | -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> |
| 312 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 313 | static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()"); |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 314 | static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 315 | |
| 316 | using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>; |
| 317 | return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 321 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 322 | constexpr span<element_type, dynamic_extent> |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 323 | subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 324 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 325 | _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); |
| 326 | _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 327 | if (__count == dynamic_extent) |
| 328 | return {data() + __offset, size() - __offset}; |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 329 | _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 330 | return {data() + __offset, __count}; |
| 331 | } |
| 332 | |
Joe Loser | f4a2a1e | 2021-10-12 22:30:58 -0400 | [diff] [blame] | 333 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; } |
| 334 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } |
| 335 | [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 336 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 337 | _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 338 | { |
Louis Dionne | 956eca8 | 2020-02-11 11:38:00 +0100 | [diff] [blame] | 339 | _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds"); |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 340 | return __data[__idx]; |
| 341 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 342 | |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 343 | _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 344 | { |
Louis Dionne | 0c12d93 | 2020-02-14 14:31:15 +0100 | [diff] [blame] | 345 | _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span"); |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 346 | return __data[0]; |
| 347 | } |
| 348 | |
| 349 | _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept |
| 350 | { |
Louis Dionne | 0c12d93 | 2020-02-14 14:31:15 +0100 | [diff] [blame] | 351 | _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span"); |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 352 | return __data[size()-1]; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 353 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 354 | |
| 355 | _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } |
| 356 | |
| 357 | // [span.iter], span iterator support |
| 358 | _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } |
| 359 | _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 360 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } |
| 361 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 362 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 363 | _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 364 | { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 365 | |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 366 | _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 367 | { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 368 | |
| 369 | private: |
| 370 | pointer __data; |
| 371 | |
| 372 | }; |
| 373 | |
| 374 | |
| 375 | template <typename _Tp> |
| 376 | class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { |
| 377 | private: |
| 378 | |
| 379 | public: |
| 380 | // constants and types |
| 381 | using element_type = _Tp; |
| 382 | using value_type = remove_cv_t<_Tp>; |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 383 | using size_type = size_t; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 384 | using difference_type = ptrdiff_t; |
| 385 | using pointer = _Tp *; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 386 | using const_pointer = const _Tp *; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 387 | using reference = _Tp &; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 388 | using const_reference = const _Tp &; |
Arthur O'Dwyer | 0a5557f | 2021-04-20 22:08:00 -0400 | [diff] [blame] | 389 | #if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS) |
| 390 | using iterator = pointer; |
| 391 | #else |
| 392 | using iterator = __wrap_iter<pointer>; |
| 393 | #endif |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 394 | using reverse_iterator = _VSTD::reverse_iterator<iterator>; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 395 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 396 | static constexpr size_type extent = dynamic_extent; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 397 | |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 398 | // [span.cons], span constructors, copy, assignment, and destructor |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 399 | _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {} |
| 400 | |
| 401 | constexpr span (const span&) noexcept = default; |
| 402 | constexpr span& operator=(const span&) noexcept = default; |
| 403 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 404 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
| 405 | template <class _It, |
| 406 | enable_if_t<contiguous_iterator<_It> && |
| 407 | is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>, |
| 408 | nullptr_t> = nullptr> |
| 409 | _LIBCPP_INLINE_VISIBILITY |
| 410 | constexpr span(_It __first, size_type __count) |
| 411 | : __data{_VSTD::to_address(__first)}, __size{__count} {} |
| 412 | |
| 413 | template < |
| 414 | class _It, class _End, |
| 415 | enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> && |
| 416 | contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>, |
| 417 | nullptr_t> = nullptr> |
| 418 | _LIBCPP_INLINE_VISIBILITY |
| 419 | constexpr span(_It __first, _End __last) |
| 420 | : __data(_VSTD::to_address(__first)), __size(__last - __first) {} |
| 421 | #endif |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 422 | |
| 423 | template <size_t _Sz> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 424 | _LIBCPP_INLINE_VISIBILITY |
| 425 | constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 426 | |
Michael Schellenberger Costa | b0444f3 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 427 | template <class _OtherElementType, size_t _Sz, |
| 428 | enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 429 | _LIBCPP_INLINE_VISIBILITY |
Michael Schellenberger Costa | b0444f3 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 430 | constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 431 | |
Michael Schellenberger Costa | b0444f3 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 432 | template <class _OtherElementType, size_t _Sz, |
| 433 | enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 434 | _LIBCPP_INLINE_VISIBILITY |
Michael Schellenberger Costa | b0444f3 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 435 | constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 436 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 437 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
| 438 | template <__span_compatible_range<element_type> _Range> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 439 | _LIBCPP_INLINE_VISIBILITY |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 440 | constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {} |
| 441 | # endif |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 442 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 443 | template <class _OtherElementType, size_t _OtherExtent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 444 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 445 | constexpr span(const span<_OtherElementType, _OtherExtent>& __other, |
| 446 | enable_if_t< |
| 447 | is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, |
| 448 | nullptr_t> = nullptr) noexcept |
| 449 | : __data{__other.data()}, __size{__other.size()} {} |
| 450 | |
| 451 | // ~span() noexcept = default; |
| 452 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 453 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 454 | _LIBCPP_INLINE_VISIBILITY |
| 455 | constexpr span<element_type, _Count> first() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 456 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 457 | _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 458 | return span<element_type, _Count>{data(), _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 461 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 462 | _LIBCPP_INLINE_VISIBILITY |
| 463 | constexpr span<element_type, _Count> last() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 464 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 465 | _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 466 | return span<element_type, _Count>{data() + size() - _Count, _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 470 | constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 471 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 472 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 473 | return {data(), __count}; |
| 474 | } |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 475 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 476 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 477 | constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 478 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 479 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 480 | return {data() + size() - __count, __count}; |
| 481 | } |
| 482 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 483 | template <size_t _Offset, size_t _Count = dynamic_extent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 484 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 22c735e | 2020-02-11 11:57:35 +0100 | [diff] [blame] | 485 | constexpr span<element_type, _Count> subspan() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 486 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 487 | _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()"); |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 488 | _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame] | 489 | return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | constexpr span<element_type, dynamic_extent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 493 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 494 | subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 495 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 496 | _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); |
| 497 | _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 498 | if (__count == dynamic_extent) |
| 499 | return {data() + __offset, size() - __offset}; |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 500 | _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 501 | return {data() + __offset, __count}; |
| 502 | } |
| 503 | |
Joe Loser | f4a2a1e | 2021-10-12 22:30:58 -0400 | [diff] [blame] | 504 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; } |
| 505 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); } |
| 506 | [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 507 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 508 | _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 509 | { |
Louis Dionne | 956eca8 | 2020-02-11 11:38:00 +0100 | [diff] [blame] | 510 | _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds"); |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 511 | return __data[__idx]; |
| 512 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 513 | |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 514 | _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 515 | { |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 516 | _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span"); |
| 517 | return __data[0]; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 518 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 519 | |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 520 | _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept |
| 521 | { |
| 522 | _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span"); |
| 523 | return __data[size()-1]; |
| 524 | } |
| 525 | |
| 526 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 527 | _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } |
| 528 | |
| 529 | // [span.iter], span iterator support |
| 530 | _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } |
| 531 | _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 532 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } |
| 533 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 534 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 535 | _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept |
| 536 | { return {reinterpret_cast<const byte *>(data()), size_bytes()}; } |
| 537 | |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 538 | _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 539 | { return {reinterpret_cast<byte *>(data()), size_bytes()}; } |
| 540 | |
| 541 | private: |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 542 | pointer __data; |
| 543 | size_type __size; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 544 | }; |
| 545 | |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 546 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
| 547 | template <class _Tp, size_t _Extent> |
| 548 | inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true; |
Christopher Di Bella | 92072c2 | 2021-05-14 06:20:07 +0000 | [diff] [blame] | 549 | |
| 550 | template <class _ElementType, size_t _Extent> |
| 551 | inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true; |
Mark de Wever | af59b2d | 2020-11-24 18:08:02 +0100 | [diff] [blame] | 552 | #endif // !defined(_LIBCPP_HAS_NO_RANGES) |
| 553 | |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 554 | // as_bytes & as_writable_bytes |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 555 | template <class _Tp, size_t _Extent> |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 556 | _LIBCPP_INLINE_VISIBILITY |
| 557 | auto as_bytes(span<_Tp, _Extent> __s) noexcept |
| 558 | -> decltype(__s.__as_bytes()) |
| 559 | { return __s.__as_bytes(); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 560 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 561 | template <class _Tp, size_t _Extent> |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 562 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 563 | auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept |
| 564 | -> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())> |
| 565 | { return __s.__as_writable_bytes(); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 566 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 567 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 568 | // Deduction guides |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 569 | template<contiguous_iterator _It, class _EndOrSize> |
| 570 | span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; |
| 571 | #endif |
| 572 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 573 | template<class _Tp, size_t _Sz> |
| 574 | span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 575 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 576 | template<class _Tp, size_t _Sz> |
| 577 | span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; |
| 578 | |
| 579 | template<class _Tp, size_t _Sz> |
| 580 | span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>; |
| 581 | |
Joe Loser | daf1bdf | 2021-10-08 16:57:44 -0400 | [diff] [blame] | 582 | #if !defined(_LIBCPP_HAS_NO_RANGES) |
| 583 | template<ranges::contiguous_range _Range> |
| 584 | span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>; |
| 585 | #endif |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 586 | |
Marshall Clow | 8dc3ea1 | 2018-07-24 03:56:38 +0000 | [diff] [blame] | 587 | #endif // _LIBCPP_STD_VER > 17 |
| 588 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 589 | _LIBCPP_END_NAMESPACE_STD |
| 590 | |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 591 | _LIBCPP_POP_MACROS |
| 592 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 593 | #endif // _LIBCPP_SPAN |