#ifndef AL_SPAN_H #define AL_SPAN_H #include #include #include #include #include namespace al { template constexpr auto size(T &cont) -> decltype(cont.size()) { return cont.size(); } template constexpr auto size(const T &cont) -> decltype(cont.size()) { return cont.size(); } template constexpr size_t size(T (&)[N]) noexcept { return N; } template constexpr size_t size(std::initializer_list list) noexcept { return list.size(); } template constexpr auto data(T &cont) -> decltype(cont.data()) { return cont.data(); } template constexpr auto data(const T &cont) -> 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(); } template(-1)> class span; namespace detail_ { template struct make_void { using type = void; }; template using void_t = typename make_void::type; template struct is_span_ : std::false_type { }; template struct is_span_> : std::true_type { }; template struct is_span : is_span_::type> { }; template struct is_std_array_ : std::false_type { }; template struct is_std_array_> : std::true_type { }; template struct is_std_array : is_std_array_::type> { }; template struct has_size_and_data : std::false_type { }; template struct has_size_and_data())), decltype(al::data(std::declval()))>> : std::true_type { }; } // namespace detail_ #define REQUIRES(...) typename std::enable_if<(__VA_ARGS__),int>::type = 0 #define USABLE_CONTAINER_DATA(...) \ std::is_convertible()))>::type(*)[],element_type(*)[]>::value #define IS_VALID_CONTAINER(C) \ !detail_::is_span::value && !detail_::is_std_array::value && \ !std::is_array::value && detail_::has_size_and_data::value && \ USABLE_CONTAINER_DATA(C&) template class span { static constexpr size_t dynamic_extent{static_cast(-1)}; public: using element_type = T; using value_type = typename std::remove_cv::type; 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 { } constexpr span(pointer ptr, index_type /*count*/) : mData{ptr} { } constexpr span(pointer first, pointer /*last*/) : mData{first} { } constexpr span(element_type (&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, REQUIRES(is_const)> 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 constexpr span(const U &cont) : span{al::data(cont), al::size(cont)} { } template::value && extent == N && std::is_convertible::value)> constexpr span(const span &span_) noexcept : span{al::data(span_), al::size(span_)} { } constexpr span(const span&) noexcept = default; 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 end(); } constexpr reverse_iterator rend() const noexcept { return begin(); } constexpr const_reverse_iterator crbegin() const noexcept { return cend(); } constexpr const_reverse_iterator crend() const noexcept { return 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 span subspan() const { static_assert(E >= O, "Offset exceeds extent"); static_assert(E-O >= RealC, "New size exceeds original capacity"); return span{mData+O, RealC}; } /* 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(-1)> { static constexpr size_t dynamic_extent{static_cast(-1)}; public: using element_type = T; using value_type = typename std::remove_cv::type; 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{static_cast(-1)}; constexpr span() noexcept = default; constexpr span(pointer ptr, index_type count) : mData{ptr}, mDataEnd{ptr+count} { } constexpr span(pointer first, pointer last) : mData{first}, mDataEnd{last} { } template constexpr span(element_type (&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, REQUIRES(is_const)> 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 constexpr span(const U &cont) : span{al::data(cont), al::size(cont)} { } template::value || extent != N) && std::is_convertible::value)> constexpr span(const span &span_) noexcept : span{al::data(span_), al::size(span_)} { } constexpr span(const span&) noexcept = default; 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 mDataEnd-mData; } constexpr index_type size_bytes() const noexcept { return (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 end(); } constexpr reverse_iterator rend() const noexcept { return begin(); } constexpr const_reverse_iterator crbegin() const noexcept { return cend(); } constexpr const_reverse_iterator crend() const noexcept { return 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 span subspan() const { return span{mData+O, C}; } 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}; } #undef IS_VALID_CONTAINER #undef USABLE_CONTAINER_DATA #undef REQUIRES } // namespace al #endif /* AL_SPAN_H */