// -*- C++ -*- //===------------------------------ span ---------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===---------------------------------------------------------------------===// #ifndef _LIBCPP_SPAN #define _LIBCPP_SPAN /* span synopsis namespace std { // constants inline constexpr ptrdiff_t dynamic_extent = -1; // [views.span], class template span template class span; // [span.comparison], span comparison operators template constexpr bool operator==(span l, span r); template constexpr bool operator!=(span l, span r); template constexpr bool operator<(span l, span r); template constexpr bool operator<=(span l, span r); template constexpr bool operator>(span l, span r); template constexpr bool operator>=(span l, span r); // [span.objectrep], views of object representation template span(sizeof(ElementType)) * Extent))> as_bytes(span s) noexcept; template span< byte, ((Extent == dynamic_extent) ? dynamic_extent : (static_cast(sizeof(ElementType)) * Extent))> as_writable_bytes(span s) noexcept; namespace std { template class span { public: // constants and types using element_type = ElementType; using value_type = remove_cv_t; using index_type = ptrdiff_t; using difference_type = ptrdiff_t; using pointer = element_type*; using reference = element_type&; using iterator = implementation-defined; using const_iterator = implementation-defined; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; static constexpr index_type extent = Extent; // [span.cons], span constructors, copy, assignment, and destructor constexpr span() noexcept; constexpr span(pointer ptr, index_type count); constexpr span(pointer firstElem, pointer lastElem); template constexpr span(element_type (&arr)[N]) noexcept; template constexpr span(array& arr) noexcept; template constexpr span(const array& arr) noexcept; template constexpr span(Container& cont); template constexpr span(const Container& cont); constexpr span(const span& other) noexcept = default; template constexpr span(const span& s) noexcept; ~span() noexcept = default; constexpr span& operator=(const span& other) noexcept = default; // [span.sub], span subviews template constexpr span first() const; template constexpr span last() const; template constexpr span subspan() const; constexpr span first(index_type count) const; constexpr span last(index_type count) const; constexpr span subspan(index_type offset, index_type count = dynamic_extent) const; // [span.obs], span observers constexpr index_type size() const noexcept; constexpr index_type size_bytes() const noexcept; constexpr bool empty() const noexcept; // [span.elem], span element access constexpr reference operator[](index_type idx) const; constexpr reference operator()(index_type idx) const; constexpr pointer data() const noexcept; // [span.iterators], span iterator support constexpr iterator begin() const noexcept; constexpr iterator end() const noexcept; constexpr const_iterator cbegin() const noexcept; constexpr const_iterator cend() const noexcept; constexpr reverse_iterator rbegin() const noexcept; constexpr reverse_iterator rend() const noexcept; constexpr const_reverse_iterator crbegin() const noexcept; constexpr const_reverse_iterator crend() const noexcept; private: pointer data_; // exposition only index_type size_; // exposition only }; template span(T (&)[N]) -> span; template span(array&) -> span; template span(const array&) -> span; template span(Container&) -> span; template span(const Container&) -> span; } // namespace std */ #include <__config> #include // for ptrdiff_t #include // for iterators #include // for array #include // for remove_cv, etc #include // for byte #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header #endif _LIBCPP_BEGIN_NAMESPACE_STD #if _LIBCPP_STD_VER > 17 inline constexpr ptrdiff_t dynamic_extent = -1; template class span; template struct __is_span_impl : public false_type {}; template struct __is_span_impl> : public true_type {}; template struct __is_span : public __is_span_impl> {}; template struct __is_std_array_impl : public false_type {}; template struct __is_std_array_impl> : public true_type {}; template struct __is_std_array : public __is_std_array_impl> {}; template struct __is_span_compatible_container : public false_type {}; template struct __is_span_compatible_container<_Tp, _ElementType, void_t< // is not a specialization of span typename enable_if::value, nullptr_t>::type, // is not a specialization of array typename enable_if::value, nullptr_t>::type, // is_array_v is false, typename enable_if, nullptr_t>::type, // data(cont) and size(cont) are well formed decltype(data(declval<_Tp>())), decltype(size(declval<_Tp>())), // remove_pointer_t(*)[] is convertible to ElementType(*)[] typename enable_if< is_convertible_v()))>(*)[], _ElementType(*)[]>, nullptr_t>::type >> : public true_type {}; template class _LIBCPP_TEMPLATE_VIS span { public: // constants and types using element_type = _Tp; using value_type = remove_cv_t<_Tp>; using index_type = ptrdiff_t; using difference_type = ptrdiff_t; using pointer = _Tp *; using const_pointer = const _Tp *; // not in standard using reference = _Tp &; using const_reference = const _Tp &; // not in standard using iterator = __wrap_iter; using const_iterator = __wrap_iter; using reverse_iterator = _VSTD::reverse_iterator; using const_reverse_iterator = _VSTD::reverse_iterator; static constexpr index_type extent = _Extent; static_assert (_Extent >= 0, "Can't have a span with an extent < 0"); // [span.cons], span constructors, copy, assignment, and destructor _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} { static_assert(_Extent == 0, "Can't default construct a statically sized span with size > 0"); } constexpr span (const span&) noexcept = default; constexpr span& operator=(const span&) noexcept = default; _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr} { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); } _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f} { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); } _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {} _LIBCPP_INLINE_VISIBILITY constexpr span( array& __arr) noexcept : __data{__arr.data()} {} _LIBCPP_INLINE_VISIBILITY constexpr span(const array& __arr) noexcept : __data{__arr.data()} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span( _Container& __c, enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr) : __data{_VSTD::data(__c)} { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (container))"); } template inline _LIBCPP_INLINE_VISIBILITY constexpr span(const _Container& __c, enable_if_t<__is_span_compatible_container::value, nullptr_t> = nullptr) : __data{_VSTD::data(__c)} { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (const container)"); } template inline _LIBCPP_INLINE_VISIBILITY constexpr span(const span<_OtherElementType, _Extent>& __other, enable_if_t< is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr) : __data{__other.data()} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span(const span<_OtherElementType, dynamic_extent>& __other, enable_if_t< is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr) noexcept : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } // ~span() noexcept = default; template inline _LIBCPP_INLINE_VISIBILITY constexpr span first() const noexcept { static_assert(_Count >= 0, "Count must be >= 0 in span::first()"); static_assert(_Count <= _Extent, "Count out of range in span::first()"); return {data(), _Count}; } template inline _LIBCPP_INLINE_VISIBILITY constexpr span last() const noexcept { static_assert(_Count >= 0, "Count must be >= 0 in span::last()"); static_assert(_Count <= _Extent, "Count out of range in span::last()"); return {data() + size() - _Count, _Count}; } _LIBCPP_INLINE_VISIBILITY constexpr span first(index_type __count) const noexcept { _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)"); return {data(), __count}; } _LIBCPP_INLINE_VISIBILITY constexpr span last(index_type __count) const noexcept { _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)"); return {data() + size() - __count, __count}; } template inline _LIBCPP_INLINE_VISIBILITY constexpr auto subspan() const noexcept -> span { _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()"); return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; } inline _LIBCPP_INLINE_VISIBILITY constexpr span subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept { _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)"); _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; _LIBCPP_ASSERT(__offset + __count <= size(), "count + offset out of range in span::subspan(offset, count)"); return {data() + __offset, __count}; } _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return _Extent; } _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; } _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept { _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span[] index out of bounds"); return __data[__idx]; } _LIBCPP_INLINE_VISIBILITY constexpr reference operator()(index_type __idx) const noexcept { _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span() index out of bounds"); return __data[__idx]; } _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } // [span.iter], span iterator support _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); } _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); } _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); } _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); } _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept { pointer __p = __data; __data = __other.__data; __other.__data = __p; } _LIBCPP_INLINE_VISIBILITY span __as_bytes() const noexcept { return {reinterpret_cast(data()), size_bytes()}; } _LIBCPP_INLINE_VISIBILITY span __as_writeable_bytes() const noexcept { return {reinterpret_cast(data()), size_bytes()}; } private: pointer __data; }; template class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { private: public: // constants and types using element_type = _Tp; using value_type = remove_cv_t<_Tp>; using index_type = ptrdiff_t; using difference_type = ptrdiff_t; using pointer = _Tp *; using const_pointer = const _Tp *; // not in standard using reference = _Tp &; using const_reference = const _Tp &; // not in standard using iterator = __wrap_iter; using const_iterator = __wrap_iter; using reverse_iterator = _VSTD::reverse_iterator; using const_reverse_iterator = _VSTD::reverse_iterator; static constexpr index_type extent = dynamic_extent; // [span.cons], span constructors, copy, assignment, and destructor _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {} constexpr span (const span&) noexcept = default; constexpr span& operator=(const span&) noexcept = default; _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr}, __size{__count} {} _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{distance(__f, __l)} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span(array& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span(const array& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span( _Container& __c, enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr) : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span(const _Container& __c, enable_if_t<__is_span_compatible_container::value, nullptr_t> = nullptr) : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {} template inline _LIBCPP_INLINE_VISIBILITY constexpr span(const span<_OtherElementType, _OtherExtent>& __other, enable_if_t< is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr) noexcept : __data{__other.data()}, __size{__other.size()} {} // ~span() noexcept = default; template inline _LIBCPP_INLINE_VISIBILITY constexpr span first() const noexcept { static_assert(_Count >= 0); _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()"); return {data(), _Count}; } template inline _LIBCPP_INLINE_VISIBILITY constexpr span last() const noexcept { static_assert(_Count >= 0); _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()"); return {data() + size() - _Count, _Count}; } _LIBCPP_INLINE_VISIBILITY constexpr span first(index_type __count) const noexcept { _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)"); return {data(), __count}; } _LIBCPP_INLINE_VISIBILITY constexpr span last (index_type __count) const noexcept { _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)"); return {data() + size() - __count, __count}; } template inline _LIBCPP_INLINE_VISIBILITY constexpr span<_Tp, dynamic_extent> subspan() const noexcept { _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()"); _LIBCPP_ASSERT(_Count == dynamic_extent || _Offset + _Count <= size(), "Count out of range in span::subspan()"); return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; } constexpr span inline _LIBCPP_INLINE_VISIBILITY subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept { _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)"); _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "count out of range in span::subspan(offset, count)"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; _LIBCPP_ASSERT(__offset + __count <= size(), "Offset + count out of range in span::subspan(offset, count)"); return {data() + __offset, __count}; } _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return __size; } _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return __size * sizeof(element_type); } _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; } _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept { _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span[] index out of bounds"); return __data[__idx]; } _LIBCPP_INLINE_VISIBILITY constexpr reference operator()(index_type __idx) const noexcept { _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span() index out of bounds"); return __data[__idx]; } _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } // [span.iter], span iterator support _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); } _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); } _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); } _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); } _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept { pointer __p = __data; __data = __other.__data; __other.__data = __p; index_type __sz = __size; __size = __other.__size; __other.__size = __sz; } _LIBCPP_INLINE_VISIBILITY span __as_bytes() const noexcept { return {reinterpret_cast(data()), size_bytes()}; } _LIBCPP_INLINE_VISIBILITY span __as_writeable_bytes() const noexcept { return {reinterpret_cast(data()), size_bytes()}; } private: pointer __data; index_type __size; }; template constexpr bool operator==(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) { return equal(__lhs.begin(), __lhs.end(), __rhs.begin(), __rhs.end()); } template constexpr bool operator!=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) { return !(__rhs == __lhs); } template constexpr bool operator< (const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) { return lexicographical_compare (__lhs.begin(), __lhs.end(), __rhs.begin(), __rhs.end()); } template constexpr bool operator<=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) { return !(__rhs < __lhs); } template constexpr bool operator> (const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) { return __rhs < __lhs; } template constexpr bool operator>=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) { return !(__lhs < __rhs); } // as_bytes & as_writeable_bytes template auto as_bytes(span<_Tp, _Extent> __s) noexcept -> decltype(__s.__as_bytes()) { return __s.__as_bytes(); } template auto as_writeable_bytes(span<_Tp, _Extent> __s) noexcept -> typename enable_if, decltype(__s.__as_writeable_bytes())>::type { return __s.__as_writeable_bytes(); } template constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept { __lhs.swap(__rhs); } // Deduction guides template span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; template span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; template span(const array<_Tp, _Sz>&) -> span; template span(_Container&) -> span; template span(const _Container&) -> span; #endif // _LIBCPP_STD_VER > 17 _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_SPAN