#ifndef AL_SPAN_H #define AL_SPAN_H #include #include #include #include #include #include #include "almalloc.h" #include "altraits.h" namespace al { inline constexpr size_t dynamic_extent{static_cast(-1)}; template class span; namespace detail_ { template struct is_span_ : std::false_type { }; template struct is_span_> : std::true_type { }; template inline constexpr bool is_span_v = is_span_>::value; template struct is_std_array_ : std::false_type { }; template struct is_std_array_> : std::true_type { }; template inline constexpr bool is_std_array_v = is_std_array_>::value; template inline constexpr bool has_size_and_data = false; template inline constexpr bool has_size_and_data())),decltype(std::data(std::declval()))>> = true; template inline constexpr bool is_valid_container_type = !is_span_v && !is_std_array_v && !std::is_array::value && has_size_and_data; template inline constexpr bool is_array_compatible = std::is_convertible::value; /* NOLINT(*-avoid-c-arrays) */ template inline constexpr bool is_valid_container = is_valid_container_type && is_array_compatible()))>,T>; } // namespace detail_ #define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true template class span { public: using element_type = T; using value_type = std::remove_cv_t; using index_type = size_t; using difference_type = ptrdiff_t; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using iterator = pointer; using const_iterator = const_pointer; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; static constexpr size_t extent{E}; template constexpr span() noexcept { } template constexpr explicit span(U iter, index_type) : mData{::al::to_address(iter)} { } template::value)> constexpr explicit span(U first, V) : mData{::al::to_address(first)} { } constexpr span(type_identity_t (&arr)[E]) noexcept /* NOLINT(*-avoid-c-arrays) */ : span{std::data(arr), std::size(arr)} { } constexpr span(std::array &arr) noexcept : span{std::data(arr), std::size(arr)} { } template::value)> constexpr span(const std::array &arr) noexcept : span{std::data(arr), std::size(arr)} { } template)> constexpr explicit span(U&& cont) : span{std::data(cont), std::size(cont)} { } template::value && detail_::is_array_compatible && N == dynamic_extent)> constexpr explicit span(const span &span_) noexcept : span{std::data(span_), std::size(span_)} { } template::value && detail_::is_array_compatible && N == extent)> constexpr span(const span &span_) noexcept : span{std::data(span_), std::size(span_)} { } constexpr span(const span&) noexcept = default; constexpr span& operator=(const span &rhs) noexcept = default; [[nodiscard]] constexpr auto front() const -> reference { return mData[0]; } [[nodiscard]] constexpr auto back() const -> reference { return mData[E-1]; } [[nodiscard]] constexpr auto operator[](index_type idx) const -> reference { return mData[idx]; } [[nodiscard]] constexpr auto data() const noexcept -> pointer { return mData; } [[nodiscard]] constexpr auto size() const noexcept -> index_type { return E; } [[nodiscard]] constexpr auto size_bytes() const noexcept -> index_type { return E * sizeof(value_type); } [[nodiscard]] constexpr auto empty() const noexcept -> bool { return E == 0; } [[nodiscard]] constexpr auto begin() const noexcept -> iterator { return mData; } [[nodiscard]] constexpr auto end() const noexcept -> iterator { return mData+E; } [[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator { return mData; } [[nodiscard]] constexpr auto cend() const noexcept -> const_iterator { return mData+E; } [[nodiscard]] constexpr auto rbegin() const noexcept -> reverse_iterator { return reverse_iterator{end()}; } [[nodiscard]] constexpr auto rend() const noexcept -> reverse_iterator { return reverse_iterator{begin()}; } [[nodiscard]] constexpr auto crbegin() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cend()}; } [[nodiscard]] constexpr auto crend() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cbegin()}; } template [[nodiscard]] constexpr auto first() const -> span { static_assert(E >= C, "New size exceeds original capacity"); return span{mData, C}; } template [[nodiscard]] constexpr auto last() const -> span { static_assert(E >= C, "New size exceeds original capacity"); return span{mData+(E-C), C}; } template [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { static_assert(E >= O, "Offset exceeds extent"); static_assert(E-O >= C, "New size exceeds original capacity"); return span{mData+O, C}; } template [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { static_assert(E >= O, "Offset exceeds extent"); return span{mData+O, E-O}; } /* NOTE: Can't declare objects of a specialized template class prior to * defining the specialization. As a result, these methods need to be * defined later. */ [[nodiscard]] constexpr auto first(size_t count) const -> span; [[nodiscard]] constexpr auto last(size_t count) const -> span; [[nodiscard]] constexpr auto subspan(size_t offset, size_t count=dynamic_extent) const -> span; private: pointer mData{nullptr}; }; template class span { public: using element_type = T; using value_type = std::remove_cv_t; using index_type = size_t; using difference_type = ptrdiff_t; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using iterator = pointer; using const_iterator = const_pointer; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; static constexpr size_t extent{dynamic_extent}; constexpr span() noexcept = default; template constexpr span(U iter, index_type count) : mData{::al::to_address(iter)}, mDataLength{count} { } template::value)> constexpr span(U first, V last) : span{::al::to_address(first), static_cast(last - first)} { } template constexpr span(type_identity_t (&arr)[N]) noexcept /* NOLINT(*-avoid-c-arrays) */ : span{std::data(arr), std::size(arr)} { } template constexpr span(std::array &arr) noexcept : span{std::data(arr), std::size(arr)} { } template::value)> constexpr span(const std::array &arr) noexcept : span{std::data(arr), std::size(arr)} { } template)> constexpr span(U&& cont) : span{std::data(cont), std::size(cont)} { } template::value || extent != N) && detail_::is_array_compatible)> constexpr span(const span &span_) noexcept : span{std::data(span_), std::size(span_)} { } constexpr span(const span&) noexcept = default; constexpr span& operator=(const span &rhs) noexcept = default; [[nodiscard]] constexpr auto front() const -> reference { return mData[0]; } [[nodiscard]] constexpr auto back() const -> reference { return mData[mDataLength-1]; } [[nodiscard]] constexpr auto operator[](index_type idx) const -> reference { return mData[idx]; } [[nodiscard]] constexpr auto data() const noexcept -> pointer { return mData; } [[nodiscard]] constexpr auto size() const noexcept -> index_type { return mDataLength; } [[nodiscard]] constexpr auto size_bytes() const noexcept -> index_type { return mDataLength * sizeof(value_type); } [[nodiscard]] constexpr auto empty() const noexcept -> bool { return mDataLength == 0; } [[nodiscard]] constexpr auto begin() const noexcept -> iterator { return mData; } [[nodiscard]] constexpr auto end() const noexcept -> iterator { return mData+mDataLength; } [[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator { return mData; } [[nodiscard]] constexpr auto cend() const noexcept -> const_iterator { return mData+mDataLength; } [[nodiscard]] constexpr auto rbegin() const noexcept -> reverse_iterator { return reverse_iterator{end()}; } [[nodiscard]] constexpr auto rend() const noexcept -> reverse_iterator { return reverse_iterator{begin()}; } [[nodiscard]] constexpr auto crbegin() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cend()}; } [[nodiscard]] constexpr auto crend() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cbegin()}; } template [[nodiscard]] constexpr auto first() const -> span { if(C > mDataLength) throw std::out_of_range{"Subspan count out of range"}; return span{mData, C}; } [[nodiscard]] constexpr auto first(size_t count) const -> span { if(count > mDataLength) throw std::out_of_range{"Subspan count out of range"}; return span{mData, count}; } template [[nodiscard]] constexpr auto last() const -> span { if(C > mDataLength) throw std::out_of_range{"Subspan count out of range"}; return span{mData+mDataLength-C, C}; } [[nodiscard]] constexpr auto last(size_t count) const -> span { if(count > mDataLength) throw std::out_of_range{"Subspan count out of range"}; return span{mData+mDataLength-count, count}; } template [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { if(O > mDataLength) throw std::out_of_range{"Subspan offset out of range"}; if(C > mDataLength-O) throw std::out_of_range{"Subspan length out of range"}; return span{mData+O, C}; } template [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { if(O > mDataLength) throw std::out_of_range{"Subspan offset out of range"}; return span{mData+O, mDataLength-O}; } [[nodiscard]] constexpr auto subspan(size_t offset, size_t count=dynamic_extent) const -> span { if(offset > mDataLength) throw std::out_of_range{"Subspan offset out of range"}; if(count != dynamic_extent) { if(count > mDataLength-offset) throw std::out_of_range{"Subspan length out of range"}; return span{mData+offset, count}; } return span{mData+offset, mDataLength-offset}; } private: pointer mData{nullptr}; index_type mDataLength{0}; }; template [[nodiscard]] constexpr inline auto span::first(size_t count) const -> span { if(count > size()) throw std::out_of_range{"Subspan count out of range"}; return span{mData, count}; } template [[nodiscard]] constexpr inline auto span::last(size_t count) const -> span { if(count > size()) throw std::out_of_range{"Subspan count out of range"}; return span{mData+size()-count, count}; } template [[nodiscard]] constexpr inline auto span::subspan(size_t offset, size_t count) const -> span { if(offset > size()) throw std::out_of_range{"Subspan offset out of range"}; if(count != dynamic_extent) { if(count > size()-offset) throw std::out_of_range{"Subspan length out of range"}; return span{mData+offset, count}; } return span{mData+offset, size()-offset}; } template span(T, EndOrSize) -> span())>>; template span(T (&)[N]) -> span; /* NOLINT(*-avoid-c-arrays) */ template span(std::array&) -> span; template span(const std::array&) -> span; template)> span(C&&) -> span()))>>; #undef REQUIRES } // namespace al #endif /* AL_SPAN_H */