#ifndef AL_SPAN_H #define AL_SPAN_H #include #include #include #include #include #include "almalloc.h" #include "altraits.h" namespace al { template constexpr auto size(const T &cont) noexcept(noexcept(cont.size())) -> decltype(cont.size()) { return cont.size(); } template constexpr size_t size(const T (&)[N]) noexcept { return N; } template constexpr auto data(T &cont) noexcept(noexcept(cont.data())) -> decltype(cont.data()) { return cont.data(); } template constexpr auto data(const T &cont) noexcept(noexcept(cont.data())) -> decltype(cont.data()) { return cont.data(); } template constexpr T* data(T (&arr)[N]) noexcept { return arr; } template constexpr const T* data(std::initializer_list list) noexcept { return list.begin(); } constexpr size_t dynamic_extent{static_cast(-1)}; template class span; namespace detail_ { template using void_t = void; template struct is_span_ : std::false_type { }; template struct is_span_> : std::true_type { }; template 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 constexpr bool is_std_array_v = is_std_array_>::value; template constexpr bool has_size_and_data = false; template constexpr bool has_size_and_data())), decltype(al::data(std::declval()))>> = true; template constexpr bool is_array_compatible = std::is_convertible::value; template constexpr bool is_valid_container = !is_span_v && !is_std_array_v && !std::is_array::value && has_size_and_data && 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{to_address(iter)} { } template::value)> constexpr explicit span(U first, V) : mData{to_address(first)} { } constexpr span(type_identity_t (&arr)[E]) noexcept : span{al::data(arr), al::size(arr)} { } constexpr span(std::array &arr) noexcept : span{al::data(arr), al::size(arr)} { } template::value)> constexpr span(const std::array &arr) noexcept : span{al::data(arr), al::size(arr)} { } template)> constexpr explicit span(U&& cont) : span{al::data(cont), al::size(cont)} { } template::value && detail_::is_array_compatible && N == dynamic_extent)> constexpr explicit span(const span &span_) noexcept : span{al::data(span_), al::size(span_)} { } template::value && detail_::is_array_compatible && N == extent)> constexpr span(const span &span_) noexcept : span{al::data(span_), al::size(span_)} { } constexpr span(const span&) noexcept = default; constexpr span& operator=(const span &rhs) noexcept = default; constexpr reference front() const { return *mData; } constexpr reference back() const { return *(mData+E-1); } constexpr reference operator[](index_type idx) const { return mData[idx]; } constexpr pointer data() const noexcept { return mData; } constexpr index_type size() const noexcept { return E; } constexpr index_type size_bytes() const noexcept { return E * sizeof(value_type); } constexpr bool empty() const noexcept { return E == 0; } constexpr iterator begin() const noexcept { return mData; } constexpr iterator end() const noexcept { return mData+E; } constexpr const_iterator cbegin() const noexcept { return mData; } constexpr const_iterator cend() const noexcept { return mData+E; } constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator{cend()}; } constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator{cbegin()}; } template constexpr span first() const { static_assert(E >= C, "New size exceeds original capacity"); return span{mData, C}; } template constexpr span last() const { static_assert(E >= C, "New size exceeds original capacity"); return span{mData+(E-C), C}; } template 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 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. */ constexpr span first(size_t count) const; constexpr span last(size_t count) const; constexpr span subspan(size_t offset, size_t count=dynamic_extent) const; 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{to_address(iter)}, mDataEnd{to_address(iter)+count} { } template::value)> constexpr span(U first, V last) : span{to_address(first), static_cast(last-first)} { } template constexpr span(type_identity_t (&arr)[N]) noexcept : span{al::data(arr), al::size(arr)} { } template constexpr span(std::array &arr) noexcept : span{al::data(arr), al::size(arr)} { } template::value)> constexpr span(const std::array &arr) noexcept : span{al::data(arr), al::size(arr)} { } template)> constexpr span(U&& cont) : span{al::data(cont), al::size(cont)} { } template::value || extent != N) && detail_::is_array_compatible)> constexpr span(const span &span_) noexcept : span{al::data(span_), al::size(span_)} { } constexpr span(const span&) noexcept = default; constexpr span& operator=(const span &rhs) noexcept = default; constexpr reference front() const { return *mData; } constexpr reference back() const { return *(mDataEnd-1); } constexpr reference operator[](index_type idx) const { return mData[idx]; } constexpr pointer data() const noexcept { return mData; } constexpr index_type size() const noexcept { return static_cast(mDataEnd-mData); } constexpr index_type size_bytes() const noexcept { return static_cast(mDataEnd-mData) * sizeof(value_type); } constexpr bool empty() const noexcept { return mData == mDataEnd; } constexpr iterator begin() const noexcept { return mData; } constexpr iterator end() const noexcept { return mDataEnd; } constexpr const_iterator cbegin() const noexcept { return mData; } constexpr const_iterator cend() const noexcept { return mDataEnd; } constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator{cend()}; } constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator{cbegin()}; } template constexpr span first() const { return span{mData, C}; } constexpr span first(size_t count) const { return (count >= size()) ? *this : span{mData, mData+count}; } template constexpr span last() const { return span{mDataEnd-C, C}; } constexpr span last(size_t count) const { return (count >= size()) ? *this : span{mDataEnd-count, mDataEnd}; } template constexpr auto subspan() const -> std::enable_if_t> { return span{mData+O, C}; } template constexpr auto subspan() const -> std::enable_if_t> { return span{mData+O, mDataEnd}; } constexpr span subspan(size_t offset, size_t count=dynamic_extent) const { return (offset > size()) ? span{} : (count >= size()-offset) ? span{mData+offset, mDataEnd} : span{mData+offset, mData+offset+count}; } private: pointer mData{nullptr}; pointer mDataEnd{nullptr}; }; template constexpr inline auto span::first(size_t count) const -> span { return (count >= size()) ? span{mData, extent} : span{mData, count}; } template constexpr inline auto span::last(size_t count) const -> span { return (count >= size()) ? span{mData, extent} : span{mData+extent-count, count}; } template constexpr inline auto span::subspan(size_t offset, size_t count) const -> span { return (offset > size()) ? span{} : (count >= size()-offset) ? span{mData+offset, mData+extent} : span{mData+offset, mData+offset+count}; } /* Helpers to deal with the lack of user-defined deduction guides (C++17). */ template constexpr auto as_span(T ptr, U count_or_end) { using value_type = typename std::pointer_traits::element_type; return span{ptr, count_or_end}; } template constexpr auto as_span(T (&arr)[N]) noexcept { return span{al::data(arr), al::size(arr)}; } template constexpr auto as_span(std::array &arr) noexcept { return span{al::data(arr), al::size(arr)}; } template constexpr auto as_span(const std::array &arr) noexcept { return span,N>{al::data(arr), al::size(arr)}; } template && !detail_::is_std_array_v && !std::is_array::value && detail_::has_size_and_data)> constexpr auto as_span(U&& cont) { using value_type = std::remove_pointer_t()))>; return span{al::data(cont), al::size(cont)}; } template constexpr auto as_span(span span_) noexcept { return span_; } #undef REQUIRES } // namespace al #endif /* AL_SPAN_H */