// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===---------------------------------------------------------------------===// #ifndef _LIBCPP_SPAN #define _LIBCPP_SPAN /* span synopsis namespace std { // constants inline constexpr size_t dynamic_extent = numeric_limits::max(); template concept integral-constant-like = // exposition only, since C++26 is_integral_v && !is_same_v> && convertible_to && equality_comparable_with && bool_constant::value && bool_constant(T()) == T::value>::value; template constexpr size_t maybe-static-ext = dynamic_extent; // exposition only, since C++26 template constexpr size_t maybe-static-ext = {T::value}; // [views.span], class template span template class span; template inline constexpr bool ranges::enable_view> = true; template inline constexpr bool ranges::enable_borrowed_range> = true; // [span.objectrep], views of object representation template span as_bytes(span s) noexcept; template span< byte, ((Extent == dynamic_extent) ? dynamic_extent : (sizeof(ElementType) * Extent))> as_writable_bytes(span s) noexcept; template class span { public: // constants and types using element_type = ElementType; using value_type = remove_cv_t; using size_type = size_t; using difference_type = ptrdiff_t; using pointer = element_type*; using const_pointer = const element_type*; using reference = element_type&; using const_reference = const element_type&; using iterator = implementation-defined; using reverse_iterator = std::reverse_iterator; static constexpr size_type extent = Extent; // [span.cons], span constructors, copy, assignment, and destructor constexpr span() noexcept; template constexpr explicit(Extent != dynamic_extent) span(It first, size_type count); template constexpr explicit(Extent != dynamic_extent) span(It first, End last); template constexpr span(type_identity_t (&arr)[N]) noexcept; template constexpr span(array& arr) noexcept; template constexpr span(const array& arr) noexcept; template constexpr explicit(Extent != dynamic_extent) span(R&& r); constexpr explicit(extent != dynamic_extent) span(std::initializer_list il); // Since C++26 constexpr span(const span& other) noexcept = default; template constexpr explicit(Extent != dynamic_extent) span(const span& s) noexcept; 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(size_type count) const; constexpr span last(size_type count) const; constexpr span subspan(size_type offset, size_type count = dynamic_extent) const; // [span.obs], span observers constexpr size_type size() const noexcept; constexpr size_type size_bytes() const noexcept; [[nodiscard]] constexpr bool empty() const noexcept; // [span.elem], span element access constexpr reference operator[](size_type idx) const; constexpr reference at(size_type idx) const; // since C++26 constexpr reference front() const; constexpr reference back() const; constexpr pointer data() const noexcept; // [span.iterators], span iterator support constexpr iterator begin() const noexcept; constexpr iterator end() const noexcept; constexpr reverse_iterator rbegin() const noexcept; constexpr reverse_iterator rend() const noexcept; private: pointer data_; // exposition only size_type size_; // exposition only }; template span(It, EndOrSize) -> span>>; // until C++26 template span(It, EndOrSize) -> span>, maybe-static-ext>; // since C++26 template span(T (&)[N]) -> span; template span(array&) -> span; template span(const array&) -> span; template span(R&&) -> span>>; } // namespace std */ #include <__assert> #include <__concepts/convertible_to.h> #include <__concepts/equality_comparable.h> #include <__config> #include <__fwd/array.h> #include <__fwd/span.h> #include <__iterator/bounded_iter.h> #include <__iterator/concepts.h> #include <__iterator/iterator_traits.h> #include <__iterator/reverse_iterator.h> #include <__iterator/wrap_iter.h> #include <__memory/pointer_traits.h> #include <__ranges/concepts.h> #include <__ranges/data.h> #include <__ranges/enable_borrowed_range.h> #include <__ranges/enable_view.h> #include <__ranges/size.h> #include <__type_traits/integral_constant.h> #include <__type_traits/is_array.h> #include <__type_traits/is_const.h> #include <__type_traits/is_convertible.h> #include <__type_traits/is_integral.h> #include <__type_traits/is_same.h> #include <__type_traits/remove_const.h> #include <__type_traits/remove_cv.h> #include <__type_traits/remove_cvref.h> #include <__type_traits/remove_reference.h> #include <__type_traits/type_identity.h> #include <__utility/forward.h> #include // for byte #include #include #include // standard-mandated includes // [iterator.range] #include <__iterator/access.h> #include <__iterator/data.h> #include <__iterator/empty.h> #include <__iterator/reverse_access.h> #include <__iterator/size.h> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header #endif _LIBCPP_PUSH_MACROS #include <__undef_macros> _LIBCPP_BEGIN_NAMESPACE_STD #if _LIBCPP_STD_VER >= 20 template struct __is_std_span : false_type {}; template struct __is_std_span> : true_type {}; template concept __span_compatible_range = !__is_std_span>::value && // ranges::contiguous_range<_Range> && // ranges::sized_range<_Range> && // (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) && // !__is_std_array>::value && // !is_array_v> && // is_convertible_v> (*)[], _ElementType (*)[]>; template concept __span_array_convertible = is_convertible_v<_From (*)[], _To (*)[]>; template concept __span_compatible_iterator = contiguous_iterator<_It> && __span_array_convertible>, _Tp>; template concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>; template class _LIBCPP_TEMPLATE_VIS span { public: // constants and types using element_type = _Tp; using value_type = remove_cv_t<_Tp>; using size_type = size_t; using difference_type = ptrdiff_t; using pointer = _Tp*; using const_pointer = const _Tp*; using reference = _Tp&; using const_reference = const _Tp&; # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS using iterator = __bounded_iter; # else using iterator = __wrap_iter; # endif using reverse_iterator = std::reverse_iterator; static constexpr size_type extent = _Extent; // [span.cons], span constructors, copy, assignment, and destructor template requires(_Sz == 0) _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr} {} # if _LIBCPP_STD_VER >= 26 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(std::initializer_list __il) requires is_const_v : __data_{__il.begin()} { _LIBCPP_ASSERT_VALID_INPUT_RANGE( _Extent == __il.size(), "Size mismatch in span's constructor _Extent != __il.size()."); } # endif constexpr span(const span&) noexcept = default; constexpr span& operator=(const span&) noexcept = default; template <__span_compatible_iterator _It> _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, size_type __count) : __data_{std::to_address(__first)} { (void)__count; _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __count, "size mismatch in span's constructor (iterator, len)"); } template <__span_compatible_iterator _It, __span_compatible_sentinel_for<_It> _End> _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, _End __last) : __data_{std::to_address(__first)} { // [span.cons]/10 // Throws: When and what last - first throws. [[maybe_unused]] auto __dist = __last - __first; _LIBCPP_ASSERT_VALID_INPUT_RANGE(__dist >= 0, "invalid range in span's constructor (iterator, sentinel)"); _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( __dist == _Extent, "invalid range in span's constructor (iterator, sentinel): last - first != extent"); } _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t (&__arr)[_Extent]) noexcept : __data_{__arr} {} template <__span_array_convertible _OtherElementType> _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {} template requires __span_array_convertible _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {} template <__span_compatible_range _Range> _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_Range&& __r) : __data_{ranges::data(__r)} { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)"); } template <__span_array_convertible _OtherElementType> _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _Extent>& __other) noexcept : __data_{__other.data()} {} template <__span_array_convertible _OtherElementType> _LIBCPP_HIDE_FROM_ABI constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept : __data_{__other.data()} { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } template _LIBCPP_HIDE_FROM_ABI constexpr span first() const noexcept { static_assert(_Count <= _Extent, "span::first(): Count out of range"); return span{data(), _Count}; } template _LIBCPP_HIDE_FROM_ABI constexpr span last() const noexcept { static_assert(_Count <= _Extent, "span::last(): Count out of range"); return span{data() + size() - _Count, _Count}; } _LIBCPP_HIDE_FROM_ABI constexpr span first(size_type __count) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span::first(count): count out of range"); return {data(), __count}; } _LIBCPP_HIDE_FROM_ABI constexpr span last(size_type __count) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span::last(count): count out of range"); return {data() + size() - __count, __count}; } template _LIBCPP_HIDE_FROM_ABI constexpr auto subspan() const noexcept -> span { static_assert(_Offset <= _Extent, "span::subspan(): Offset out of range"); static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "span::subspan(): Offset + Count out of range"); using _ReturnType = span; return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; } _LIBCPP_HIDE_FROM_ABI constexpr span subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span::subspan(offset, count): offset out of range"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( __count <= size() - __offset, "span::subspan(offset, count): offset + count out of range"); return {data() + __offset, __count}; } _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return _Extent; } _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return _Extent == 0; } _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span::operator[](index): index out of range"); return __data_[__idx]; } # if _LIBCPP_STD_VER >= 26 _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const { if (__index >= size()) std::__throw_out_of_range("span"); return __data_[__index]; } # endif _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span::front() on empty span"); return __data_[0]; } _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span::back() on empty span"); return __data_[size() - 1]; } _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; } // [span.iter], span iterator support _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept { # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS return std::__make_bounded_iter(data(), data(), data() + size()); # else return iterator(data()); # endif } _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept { # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS return std::__make_bounded_iter(data() + size(), data(), data() + size()); # else return iterator(data() + size()); # endif } _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } _LIBCPP_HIDE_FROM_ABI span __as_bytes() const noexcept { return span{reinterpret_cast(data()), size_bytes()}; } _LIBCPP_HIDE_FROM_ABI span __as_writable_bytes() const noexcept { return span{reinterpret_cast(data()), size_bytes()}; } private: pointer __data_; }; template class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { public: // constants and types using element_type = _Tp; using value_type = remove_cv_t<_Tp>; using size_type = size_t; using difference_type = ptrdiff_t; using pointer = _Tp*; using const_pointer = const _Tp*; using reference = _Tp&; using const_reference = const _Tp&; # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS using iterator = __bounded_iter; # else using iterator = __wrap_iter; # endif using reverse_iterator = std::reverse_iterator; static constexpr size_type extent = dynamic_extent; // [span.cons], span constructors, copy, assignment, and destructor _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr}, __size_{0} {} # if _LIBCPP_STD_VER >= 26 _LIBCPP_HIDE_FROM_ABI constexpr span(std::initializer_list __il) requires is_const_v : __data_{__il.begin()}, __size_{__il.size()} {} # endif constexpr span(const span&) noexcept = default; constexpr span& operator=(const span&) noexcept = default; template <__span_compatible_iterator _It> _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, size_type __count) : __data_{std::to_address(__first)}, __size_{__count} {} template <__span_compatible_iterator _It, __span_compatible_sentinel_for<_It> _End> _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, _End __last) : __data_(std::to_address(__first)), __size_(__last - __first) { _LIBCPP_ASSERT_VALID_INPUT_RANGE(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)"); } template _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t (&__arr)[_Sz]) noexcept : __data_{__arr}, __size_{_Sz} {} template <__span_array_convertible _OtherElementType, size_t _Sz> _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {} template requires __span_array_convertible _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {} template <__span_compatible_range _Range> _LIBCPP_HIDE_FROM_ABI constexpr span(_Range&& __r) : __data_(ranges::data(__r)), __size_{ranges::size(__r)} {} template <__span_array_convertible _OtherElementType, size_t _OtherExtent> _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _OtherExtent>& __other) noexcept : __data_{__other.data()}, __size_{__other.size()} {} template _LIBCPP_HIDE_FROM_ABI constexpr span first() const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span::first(): Count out of range"); return span{data(), _Count}; } template _LIBCPP_HIDE_FROM_ABI constexpr span last() const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span::last(): Count out of range"); return span{data() + size() - _Count, _Count}; } _LIBCPP_HIDE_FROM_ABI constexpr span first(size_type __count) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span::first(count): count out of range"); return {data(), __count}; } _LIBCPP_HIDE_FROM_ABI constexpr span last(size_type __count) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span::last(count): count out of range"); return {data() + size() - __count, __count}; } template _LIBCPP_HIDE_FROM_ABI constexpr span subspan() const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Offset <= size(), "span::subspan(): Offset out of range"); _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count == dynamic_extent || _Count <= size() - _Offset, "span::subspan(): Offset + Count out of range"); return span{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; } constexpr span _LIBCPP_HIDE_FROM_ABI subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span::subspan(offset, count): offset out of range"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( __count <= size() - __offset, "span::subspan(offset, count): offset + count out of range"); return {data() + __offset, __count}; } _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return __size_; } _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept { return __size_ * sizeof(element_type); } [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return __size_ == 0; } _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span::operator[](index): index out of range"); return __data_[__idx]; } # if _LIBCPP_STD_VER >= 26 _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const { if (__index >= size()) std::__throw_out_of_range("span"); return __data_[__index]; } # endif _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span::front() on empty span"); return __data_[0]; } _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span::back() on empty span"); return __data_[size() - 1]; } _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; } // [span.iter], span iterator support _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept { # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS return std::__make_bounded_iter(data(), data(), data() + size()); # else return iterator(data()); # endif } _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept { # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS return std::__make_bounded_iter(data() + size(), data(), data() + size()); # else return iterator(data() + size()); # endif } _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } _LIBCPP_HIDE_FROM_ABI span __as_bytes() const noexcept { return {reinterpret_cast(data()), size_bytes()}; } _LIBCPP_HIDE_FROM_ABI span __as_writable_bytes() const noexcept { return {reinterpret_cast(data()), size_bytes()}; } private: pointer __data_; size_type __size_; }; template inline constexpr bool ranges::enable_borrowed_range > = true; template inline constexpr bool ranges::enable_view> = true; // as_bytes & as_writable_bytes template _LIBCPP_HIDE_FROM_ABI auto as_bytes(span<_Tp, _Extent> __s) noexcept { return __s.__as_bytes(); } template requires(!is_const_v<_Tp>) _LIBCPP_HIDE_FROM_ABI auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept { return __s.__as_writable_bytes(); } # if _LIBCPP_STD_VER >= 26 template concept __integral_constant_like = is_integral_v && !is_same_v> && convertible_to<_Tp, decltype(_Tp::value)> && equality_comparable_with<_Tp, decltype(_Tp::value)> && bool_constant<_Tp() == _Tp::value>::value && bool_constant(_Tp()) == _Tp::value>::value; template inline constexpr size_t __maybe_static_ext = dynamic_extent; template <__integral_constant_like _Tp> inline constexpr size_t __maybe_static_ext<_Tp> = {_Tp::value}; template span(_It, _EndOrSize) -> span>, __maybe_static_ext<_EndOrSize>>; # else template span(_It, _EndOrSize) -> span>>; # endif template span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; template span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; template span(const array<_Tp, _Sz>&) -> span; template span(_Range&&) -> span>>; #endif // _LIBCPP_STD_VER >= 20 _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 # include # include # include # include # include #endif #endif // _LIBCPP_SPAN