From c40e1bc1ce4f43fed87508374679f82ed237d79d Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 14 Dec 2022 17:39:47 -0800 Subject: Better handle span sources from iterators --- common/alspan.h | 79 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 22 deletions(-) (limited to 'common/alspan.h') diff --git a/common/alspan.h b/common/alspan.h index dcc5750b..472a47d6 100644 --- a/common/alspan.h +++ b/common/alspan.h @@ -7,6 +7,8 @@ #include #include +#include "almalloc.h" + namespace al { template @@ -35,6 +37,13 @@ constexpr const T* data(std::initializer_list list) noexcept { return list.begin(); } +template +struct type_identity { using type = T; }; + +template +using type_identity_t = typename type_identity::type; + + constexpr size_t dynamic_extent{static_cast(-1)}; template @@ -66,13 +75,16 @@ namespace detail_ { constexpr bool has_size_and_data())), decltype(al::data(std::declval()))>> = true; + + template + constexpr bool is_convertible_v = std::is_convertible::value; } // namespace detail_ #define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true -#define IS_VALID_CONTAINER(C) \ +#define IS_VALID_CONTAINER(C, T) \ !detail_::is_span_v && !detail_::is_std_array_v && \ !std::is_array::value && detail_::has_size_and_data && \ - std::is_convertible()))>(*)[],element_type(*)[]>::value + detail_::is_convertible_v()))>,T> template class span { @@ -96,21 +108,31 @@ public: 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)} { } + 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 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 - && std::is_convertible::value)> - constexpr span(const span &span_) noexcept : span{al::data(span_), al::size(span_)} { } + + template + constexpr explicit span(U&& cont) : span{al::data(cont), al::size(cont)} { } + + template::value + && detail_::is_convertible_v && N == dynamic_extent)> + constexpr explicit span(const span &span_) noexcept + : span{al::data(span_), al::size(span_)} + { } + template::value + && detail_::is_convertible_v && 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; @@ -199,22 +221,31 @@ public: static constexpr size_t extent{dynamic_extent}; 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(U iter, index_type count) + : mData{to_address(iter)}, mDataEnd{to_address(iter)+count} + { } + template::value)> + constexpr span(U first, V last) + : mData{to_address(first)}, mDataEnd{to_address(last)} + { } + template - constexpr span(element_type (&arr)[N]) noexcept : span{al::data(arr), al::size(arr)} { } + 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 - constexpr span(const U &cont) : span{al::data(cont), al::size(cont)} { } + + template + constexpr span(U&& cont) : span{al::data(cont), al::size(cont)} { } + template::value || extent != N) - && std::is_convertible::value)> + && detail_::is_convertible_v)> constexpr span(const span &span_) noexcept : span{al::data(span_), al::size(span_)} { } constexpr span(const span&) noexcept = default; @@ -301,7 +332,11 @@ constexpr inline auto span::subspan(size_t offset, size_t count) const /* Helpers to deal with the lack of user-defined deduction guides (C++17). */ template -constexpr auto as_span(T *ptr, U count_or_end) { return span{ptr, count_or_end}; } +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 @@ -312,7 +347,7 @@ 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) +constexpr auto as_span(U&& cont) { using value_type = std::remove_pointer_t()))>; return span{al::data(cont), al::size(cont)}; -- cgit v1.2.3