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