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