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