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 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 25 | // [span.objectrep], views of object representation |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 26 | template <class ElementType, size_t Extent> |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 27 | span<const byte, ((Extent == dynamic_extent) ? dynamic_extent : |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 28 | (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 29 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 30 | template <class ElementType, size_t Extent> |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 31 | span< byte, ((Extent == dynamic_extent) ? dynamic_extent : |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 32 | (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | namespace std { |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 36 | template <class ElementType, size_t Extent = dynamic_extent> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 37 | class span { |
| 38 | public: |
| 39 | // constants and types |
| 40 | using element_type = ElementType; |
| 41 | using value_type = remove_cv_t<ElementType>; |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 42 | using size_type = size_t; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 43 | using difference_type = ptrdiff_t; |
| 44 | using pointer = element_type*; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 45 | using const_pointer = const element_type*; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 46 | using reference = element_type&; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 47 | using const_reference = const element_type&; |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 48 | using iterator = implementation-defined; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 49 | using const_iterator = implementation-defined; |
| 50 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 51 | using const_reverse_iterator = std::reverse_iterator<const_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; |
| 100 | constexpr const_iterator cbegin() const noexcept; |
| 101 | constexpr const_iterator cend() const noexcept; |
| 102 | constexpr reverse_iterator rbegin() const noexcept; |
| 103 | constexpr reverse_iterator rend() const noexcept; |
| 104 | constexpr const_reverse_iterator crbegin() const noexcept; |
| 105 | constexpr const_reverse_iterator crend() const noexcept; |
| 106 | |
| 107 | private: |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 108 | pointer data_; // exposition only |
| 109 | size_type size_; // exposition only |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | template<class T, size_t N> |
| 113 | span(T (&)[N]) -> span<T, N>; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 114 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 115 | template<class T, size_t N> |
| 116 | span(array<T, N>&) -> span<T, N>; |
| 117 | |
| 118 | template<class T, size_t N> |
| 119 | span(const array<T, N>&) -> span<const T, N>; |
| 120 | |
| 121 | template<class Container> |
| 122 | span(Container&) -> span<typename Container::value_type>; |
| 123 | |
| 124 | template<class Container> |
| 125 | span(const Container&) -> span<const typename Container::value_type>; |
| 126 | |
| 127 | } // namespace std |
| 128 | |
| 129 | */ |
| 130 | |
Marshall Clow | 8dc3ea1 | 2018-07-24 03:56:38 +0000 | [diff] [blame] | 131 | #include <__config> |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 132 | #include <array> // for array |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 133 | #include <cstddef> // for byte |
Louis Dionne | 533a14e | 2020-02-11 11:16:40 +0100 | [diff] [blame] | 134 | #include <iterator> // for iterators |
| 135 | #include <type_traits> // for remove_cv, etc |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 136 | |
| 137 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 138 | #pragma GCC system_header |
| 139 | #endif |
| 140 | |
| 141 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 142 | |
| 143 | #if _LIBCPP_STD_VER > 17 |
| 144 | |
Louis Dionne | cba47f7 | 2020-02-10 13:38:04 +0100 | [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 &; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 203 | using iterator = __wrap_iter<pointer>; |
| 204 | using const_iterator = __wrap_iter<const_pointer>; |
| 205 | using reverse_iterator = _VSTD::reverse_iterator<iterator>; |
| 206 | using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>; |
| 207 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 208 | static constexpr size_type extent = _Extent; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 209 | |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 210 | // [span.cons], span constructors, copy, assignment, and destructor |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 211 | _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} |
| 212 | { static_assert(_Extent == 0, "Can't default construct a statically sized span with size > 0"); } |
| 213 | |
| 214 | constexpr span (const span&) noexcept = default; |
| 215 | constexpr span& operator=(const span&) noexcept = default; |
| 216 | |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame^] | 217 | _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] | 218 | { (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^] | 219 | _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __f, pointer __l) : __data{__f} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 220 | { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); } |
| 221 | |
| 222 | _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {} |
| 223 | _LIBCPP_INLINE_VISIBILITY constexpr span( array<value_type, _Extent>& __arr) noexcept : __data{__arr.data()} {} |
| 224 | _LIBCPP_INLINE_VISIBILITY constexpr span(const array<value_type, _Extent>& __arr) noexcept : __data{__arr.data()} {} |
| 225 | |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame^] | 226 | template <class _Container> |
| 227 | _LIBCPP_INLINE_VISIBILITY |
| 228 | constexpr explicit span( _Container& __c, |
| 229 | enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr) |
| 230 | : __data{_VSTD::data(__c)} { |
| 231 | _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)"); |
| 232 | } |
| 233 | |
| 234 | template <class _Container> |
| 235 | _LIBCPP_INLINE_VISIBILITY |
| 236 | constexpr explicit span(const _Container& __c, |
| 237 | enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr) |
| 238 | : __data{_VSTD::data(__c)} { |
| 239 | _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)"); |
| 240 | } |
| 241 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 242 | template <class _OtherElementType> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 243 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 244 | constexpr span(const span<_OtherElementType, _Extent>& __other, |
| 245 | enable_if_t< |
| 246 | is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, |
| 247 | nullptr_t> = nullptr) |
| 248 | : __data{__other.data()} {} |
| 249 | |
| 250 | template <class _OtherElementType> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 251 | _LIBCPP_INLINE_VISIBILITY |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame^] | 252 | constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other, |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 253 | enable_if_t< |
| 254 | is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, |
| 255 | nullptr_t> = nullptr) noexcept |
| 256 | : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } |
| 257 | |
| 258 | |
| 259 | // ~span() noexcept = default; |
| 260 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 261 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 262 | _LIBCPP_INLINE_VISIBILITY |
| 263 | constexpr span<element_type, _Count> first() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 264 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 265 | static_assert(_Count <= _Extent, "Count out of range in span::first()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame^] | 266 | return span<element_type, _Count>{data(), _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 269 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 270 | _LIBCPP_INLINE_VISIBILITY |
| 271 | constexpr span<element_type, _Count> last() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 272 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 273 | static_assert(_Count <= _Extent, "Count out of range in span::last()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame^] | 274 | return span<element_type, _Count>{data() + size() - _Count, _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 275 | } |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 276 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 277 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 278 | constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 279 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 280 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 281 | return {data(), __count}; |
| 282 | } |
| 283 | |
| 284 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 285 | constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 286 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 287 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 288 | return {data() + size() - __count, __count}; |
| 289 | } |
| 290 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 291 | template <size_t _Offset, size_t _Count = dynamic_extent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 292 | _LIBCPP_INLINE_VISIBILITY |
| 293 | constexpr auto subspan() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 294 | -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> |
| 295 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 296 | static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()"); |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 297 | 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^] | 298 | |
| 299 | using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>; |
| 300 | return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 304 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 305 | constexpr span<element_type, dynamic_extent> |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 306 | subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 307 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 308 | _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); |
| 309 | _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] | 310 | if (__count == dynamic_extent) |
| 311 | return {data() + __offset, size() - __offset}; |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 312 | _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] | 313 | return {data() + __offset, __count}; |
| 314 | } |
| 315 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 316 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; } |
| 317 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } |
| 318 | _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 319 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 320 | _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 321 | { |
Louis Dionne | 956eca8 | 2020-02-11 11:38:00 +0100 | [diff] [blame] | 322 | _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds"); |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 323 | return __data[__idx]; |
| 324 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 325 | |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 326 | _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 327 | { |
Louis Dionne | 0c12d93 | 2020-02-14 14:31:15 +0100 | [diff] [blame] | 328 | _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span"); |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 329 | return __data[0]; |
| 330 | } |
| 331 | |
| 332 | _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept |
| 333 | { |
Louis Dionne | 0c12d93 | 2020-02-14 14:31:15 +0100 | [diff] [blame] | 334 | _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span"); |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 335 | return __data[size()-1]; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 336 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 337 | |
| 338 | _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } |
| 339 | |
| 340 | // [span.iter], span iterator support |
| 341 | _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } |
| 342 | _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } |
| 343 | _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); } |
| 344 | _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); } |
| 345 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } |
| 346 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } |
| 347 | _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); } |
| 348 | _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); } |
| 349 | |
| 350 | _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept |
| 351 | { |
| 352 | pointer __p = __data; |
| 353 | __data = __other.__data; |
| 354 | __other.__data = __p; |
| 355 | } |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 356 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 357 | _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^] | 358 | { 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] | 359 | |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 360 | _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^] | 361 | { 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] | 362 | |
| 363 | private: |
| 364 | pointer __data; |
| 365 | |
| 366 | }; |
| 367 | |
| 368 | |
| 369 | template <typename _Tp> |
| 370 | class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { |
| 371 | private: |
| 372 | |
| 373 | public: |
| 374 | // constants and types |
| 375 | using element_type = _Tp; |
| 376 | using value_type = remove_cv_t<_Tp>; |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 377 | using size_type = size_t; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 378 | using difference_type = ptrdiff_t; |
| 379 | using pointer = _Tp *; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 380 | using const_pointer = const _Tp *; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 381 | using reference = _Tp &; |
Marshall Clow | 4b7f2c3 | 2019-02-25 17:58:03 +0000 | [diff] [blame] | 382 | using const_reference = const _Tp &; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 383 | using iterator = __wrap_iter<pointer>; |
| 384 | using const_iterator = __wrap_iter<const_pointer>; |
| 385 | using reverse_iterator = _VSTD::reverse_iterator<iterator>; |
| 386 | using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>; |
| 387 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 388 | static constexpr size_type extent = dynamic_extent; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 389 | |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 390 | // [span.cons], span constructors, copy, assignment, and destructor |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 391 | _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {} |
| 392 | |
| 393 | constexpr span (const span&) noexcept = default; |
| 394 | constexpr span& operator=(const span&) noexcept = default; |
| 395 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 396 | _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] | 397 | _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] | 398 | |
| 399 | template <size_t _Sz> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 400 | _LIBCPP_INLINE_VISIBILITY |
| 401 | constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 402 | |
| 403 | template <size_t _Sz> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 404 | _LIBCPP_INLINE_VISIBILITY |
| 405 | constexpr span(array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 406 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 407 | template <size_t _Sz> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 408 | _LIBCPP_INLINE_VISIBILITY |
| 409 | constexpr span(const array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 410 | |
| 411 | template <class _Container> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 412 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 413 | constexpr span( _Container& __c, |
| 414 | 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] | 415 | : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 416 | |
| 417 | template <class _Container> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 418 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 419 | constexpr span(const _Container& __c, |
| 420 | 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] | 421 | : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {} |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 422 | |
| 423 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 424 | template <class _OtherElementType, size_t _OtherExtent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 425 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 426 | constexpr span(const span<_OtherElementType, _OtherExtent>& __other, |
| 427 | enable_if_t< |
| 428 | is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, |
| 429 | nullptr_t> = nullptr) noexcept |
| 430 | : __data{__other.data()}, __size{__other.size()} {} |
| 431 | |
| 432 | // ~span() noexcept = default; |
| 433 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 434 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 435 | _LIBCPP_INLINE_VISIBILITY |
| 436 | constexpr span<element_type, _Count> first() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 437 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 438 | _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame^] | 439 | return span<element_type, _Count>{data(), _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 442 | template <size_t _Count> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 443 | _LIBCPP_INLINE_VISIBILITY |
| 444 | constexpr span<element_type, _Count> last() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 445 | { |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 446 | _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()"); |
Michael Schellenberger Costa | ea23428 | 2020-05-13 09:50:06 -0400 | [diff] [blame^] | 447 | return span<element_type, _Count>{data() + size() - _Count, _Count}; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 451 | constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 452 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 453 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 454 | return {data(), __count}; |
| 455 | } |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 456 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 457 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 458 | constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 459 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 460 | _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 461 | return {data() + size() - __count, __count}; |
| 462 | } |
| 463 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 464 | template <size_t _Offset, size_t _Count = dynamic_extent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 465 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 22c735e | 2020-02-11 11:57:35 +0100 | [diff] [blame] | 466 | constexpr span<element_type, _Count> subspan() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 467 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 468 | _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()"); |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 469 | _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^] | 470 | 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] | 471 | } |
| 472 | |
| 473 | constexpr span<element_type, dynamic_extent> |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 474 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 475 | subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 476 | { |
Marshall Clow | e246c9f | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 477 | _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); |
| 478 | _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] | 479 | if (__count == dynamic_extent) |
| 480 | return {data() + __offset, size() - __offset}; |
Louis Dionne | 45931e9 | 2020-02-12 16:20:09 +0100 | [diff] [blame] | 481 | _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] | 482 | return {data() + __offset, __count}; |
| 483 | } |
| 484 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 485 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; } |
| 486 | _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); } |
| 487 | _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 488 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 489 | _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 490 | { |
Louis Dionne | 956eca8 | 2020-02-11 11:38:00 +0100 | [diff] [blame] | 491 | _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds"); |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 492 | return __data[__idx]; |
| 493 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 494 | |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 495 | _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 496 | { |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 497 | _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span"); |
| 498 | return __data[0]; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 499 | } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 500 | |
Marshall Clow | 8940880 | 2019-02-25 17:54:08 +0000 | [diff] [blame] | 501 | _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept |
| 502 | { |
| 503 | _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span"); |
| 504 | return __data[size()-1]; |
| 505 | } |
| 506 | |
| 507 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 508 | _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } |
| 509 | |
| 510 | // [span.iter], span iterator support |
| 511 | _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } |
| 512 | _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } |
| 513 | _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); } |
| 514 | _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); } |
| 515 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } |
| 516 | _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } |
| 517 | _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); } |
| 518 | _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); } |
| 519 | |
| 520 | _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept |
| 521 | { |
| 522 | pointer __p = __data; |
| 523 | __data = __other.__data; |
| 524 | __other.__data = __p; |
| 525 | |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 526 | size_type __sz = __size; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 527 | __size = __other.__size; |
| 528 | __other.__size = __sz; |
| 529 | } |
| 530 | |
| 531 | _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept |
| 532 | { return {reinterpret_cast<const byte *>(data()), size_bytes()}; } |
| 533 | |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 534 | _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 535 | { return {reinterpret_cast<byte *>(data()), size_bytes()}; } |
| 536 | |
| 537 | private: |
Louis Dionne | 7b89ab0 | 2019-11-14 09:07:05 -0500 | [diff] [blame] | 538 | pointer __data; |
| 539 | size_type __size; |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 542 | // tuple interface |
| 543 | template <class _Tp, size_t _Size> |
| 544 | struct _LIBCPP_TEMPLATE_VIS tuple_size<span<_Tp, _Size>> |
| 545 | : public integral_constant<size_t, _Size> {}; |
| 546 | |
| 547 | template <class _Tp> |
| 548 | struct _LIBCPP_TEMPLATE_VIS tuple_size<span<_Tp, dynamic_extent>>; // declared but not defined |
| 549 | |
| 550 | |
| 551 | template <size_t _Ip, class _Tp, size_t _Size> |
Louis Dionne | e684aa6 | 2019-04-01 16:39:34 +0000 | [diff] [blame] | 552 | struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, span<_Tp, _Size>> |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 553 | { |
| 554 | static_assert( dynamic_extent != _Size, "std::tuple_element<> not supported for std::span<T, dynamic_extent>"); |
| 555 | static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::span)"); |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 556 | typedef _Tp type; |
| 557 | }; |
| 558 | |
| 559 | template <size_t _Ip, class _Tp, size_t _Size> |
| 560 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 561 | _Tp& |
| 562 | get(span<_Tp, _Size> __s) noexcept |
| 563 | { |
| 564 | static_assert( dynamic_extent != _Size, "std::get<> not supported for std::span<T, dynamic_extent>"); |
| 565 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::span)"); |
| 566 | return __s[_Ip]; |
| 567 | } |
| 568 | |
| 569 | |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 570 | // as_bytes & as_writable_bytes |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 571 | template <class _Tp, size_t _Extent> |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 572 | _LIBCPP_INLINE_VISIBILITY |
| 573 | auto as_bytes(span<_Tp, _Extent> __s) noexcept |
| 574 | -> decltype(__s.__as_bytes()) |
| 575 | { return __s.__as_bytes(); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 576 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 577 | template <class _Tp, size_t _Extent> |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 578 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | ac9f0c6 | 2019-03-28 01:27:52 +0000 | [diff] [blame] | 579 | auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept |
| 580 | -> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())> |
| 581 | { return __s.__as_writable_bytes(); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 582 | |
Marshall Clow | cd6d880 | 2019-02-27 00:32:16 +0000 | [diff] [blame] | 583 | template <class _Tp, size_t _Extent> |
Marshall Clow | e4859a0 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 584 | _LIBCPP_INLINE_VISIBILITY |
| 585 | constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept |
| 586 | { __lhs.swap(__rhs); } |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 587 | |
| 588 | |
| 589 | // Deduction guides |
| 590 | template<class _Tp, size_t _Sz> |
| 591 | span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 592 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 593 | template<class _Tp, size_t _Sz> |
| 594 | span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; |
| 595 | |
| 596 | template<class _Tp, size_t _Sz> |
| 597 | span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>; |
| 598 | |
| 599 | template<class _Container> |
| 600 | span(_Container&) -> span<typename _Container::value_type>; |
| 601 | |
| 602 | template<class _Container> |
| 603 | span(const _Container&) -> span<const typename _Container::value_type>; |
| 604 | |
Marshall Clow | 8dc3ea1 | 2018-07-24 03:56:38 +0000 | [diff] [blame] | 605 | #endif // _LIBCPP_STD_VER > 17 |
| 606 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 607 | _LIBCPP_END_NAMESPACE_STD |
| 608 | |
Marshall Clow | d2ad87e | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 609 | #endif // _LIBCPP_SPAN |