aboutsummaryrefslogtreecommitdiffstats
path: root/common/alspan.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2022-12-14 17:39:47 -0800
committerChris Robinson <[email protected]>2022-12-14 17:39:47 -0800
commitc40e1bc1ce4f43fed87508374679f82ed237d79d (patch)
tree3a596ae06129e1ddf6a55cd57f3931c36fabf883 /common/alspan.h
parent42090e93719748675384cc6ec9106fb39f869920 (diff)
Better handle span sources from iterators
Diffstat (limited to 'common/alspan.h')
-rw-r--r--common/alspan.h79
1 files changed, 57 insertions, 22 deletions
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 <iterator>
#include <type_traits>
+#include "almalloc.h"
+
namespace al {
template<typename T>
@@ -35,6 +37,13 @@ constexpr const T* data(std::initializer_list<T> list) noexcept
{ return list.begin(); }
+template<typename T>
+struct type_identity { using type = T; };
+
+template<typename T>
+using type_identity_t = typename type_identity<T>::type;
+
+
constexpr size_t dynamic_extent{static_cast<size_t>(-1)};
template<typename T, size_t E=dynamic_extent>
@@ -66,13 +75,16 @@ namespace detail_ {
constexpr bool has_size_and_data<T,
void_t<decltype(al::size(std::declval<T>())), decltype(al::data(std::declval<T>()))>>
= true;
+
+ template<typename T, typename U>
+ constexpr bool is_convertible_v = std::is_convertible<T(*)[],U(*)[]>::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<C> && !detail_::is_std_array_v<C> && \
!std::is_array<C>::value && detail_::has_size_and_data<C> && \
- std::is_convertible<std::remove_pointer_t<decltype(al::data(std::declval<C&>()))>(*)[],element_type(*)[]>::value
+ detail_::is_convertible_v<std::remove_pointer_t<decltype(al::data(std::declval<C&>()))>,T>
template<typename T, size_t E>
class span {
@@ -96,21 +108,31 @@ public:
template<bool is0=(extent == 0), REQUIRES(is0)>
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<typename U>
+ constexpr explicit span(U iter, index_type) : mData{to_address(iter)} { }
+ template<typename U, typename V, REQUIRES(!std::is_integral<V>::value)>
+ constexpr explicit span(U first, V) : mData{to_address(first)} { }
+
+ constexpr span(type_identity_t<element_type> (&arr)[E]) noexcept
+ : span{al::data(arr), al::size(arr)}
+ { }
constexpr span(std::array<value_type,E> &arr) noexcept : span{al::data(arr), al::size(arr)} { }
template<typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,E> &arr) noexcept
: span{al::data(arr), al::size(arr)}
{ }
- template<typename U, REQUIRES(IS_VALID_CONTAINER(U))>
- constexpr span(U &cont) : span{al::data(cont), al::size(cont)} { }
- template<typename U, REQUIRES(IS_VALID_CONTAINER(const U))>
- constexpr span(const U &cont) : span{al::data(cont), al::size(cont)} { }
- template<typename U, REQUIRES(!std::is_same<element_type,U>::value
- && std::is_convertible<U(*)[],element_type(*)[]>::value)>
- constexpr span(const span<U,E> &span_) noexcept : span{al::data(span_), al::size(span_)} { }
+
+ template<typename U, REQUIRES(IS_VALID_CONTAINER(U, element_type))>
+ constexpr explicit span(U&& cont) : span{al::data(cont), al::size(cont)} { }
+
+ template<typename U, index_type N, REQUIRES(!std::is_same<element_type,U>::value
+ && detail_::is_convertible_v<U,element_type> && N == dynamic_extent)>
+ constexpr explicit span(const span<U,N> &span_) noexcept
+ : span{al::data(span_), al::size(span_)}
+ { }
+ template<typename U, index_type N, REQUIRES(!std::is_same<element_type,U>::value
+ && detail_::is_convertible_v<U,element_type> && N == extent)>
+ constexpr span(const span<U,N> &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<typename U>
+ constexpr span(U iter, index_type count)
+ : mData{to_address(iter)}, mDataEnd{to_address(iter)+count}
+ { }
+ template<typename U, typename V, REQUIRES(!std::is_integral<V>::value)>
+ constexpr span(U first, V last)
+ : mData{to_address(first)}, mDataEnd{to_address(last)}
+ { }
+
template<size_t N>
- constexpr span(element_type (&arr)[N]) noexcept : span{al::data(arr), al::size(arr)} { }
+ constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept
+ : span{al::data(arr), al::size(arr)}
+ { }
template<size_t N>
constexpr span(std::array<value_type,N> &arr) noexcept : span{al::data(arr), al::size(arr)} { }
template<size_t N, typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,N> &arr) noexcept
: span{al::data(arr), al::size(arr)}
{ }
- template<typename U, REQUIRES(IS_VALID_CONTAINER(U))>
- constexpr span(U &cont) : span{al::data(cont), al::size(cont)} { }
- template<typename U, REQUIRES(IS_VALID_CONTAINER(const U))>
- constexpr span(const U &cont) : span{al::data(cont), al::size(cont)} { }
+
+ template<typename U, REQUIRES(IS_VALID_CONTAINER(U, element_type))>
+ constexpr span(U&& cont) : span{al::data(cont), al::size(cont)} { }
+
template<typename U, size_t N, REQUIRES((!std::is_same<element_type,U>::value || extent != N)
- && std::is_convertible<U(*)[],element_type(*)[]>::value)>
+ && detail_::is_convertible_v<U,element_type>)>
constexpr span(const span<U,N> &span_) noexcept : span{al::data(span_), al::size(span_)} { }
constexpr span(const span&) noexcept = default;
@@ -301,7 +332,11 @@ constexpr inline auto span<T,E>::subspan(size_t offset, size_t count) const
/* Helpers to deal with the lack of user-defined deduction guides (C++17). */
template<typename T, typename U>
-constexpr auto as_span(T *ptr, U count_or_end) { return span<T>{ptr, count_or_end}; }
+constexpr auto as_span(T ptr, U count_or_end)
+{
+ using value_type = typename std::pointer_traits<T>::element_type;
+ return span<value_type>{ptr, count_or_end};
+}
template<typename T, size_t N>
constexpr auto as_span(T (&arr)[N]) noexcept { return span<T,N>{al::data(arr), al::size(arr)}; }
template<typename T, size_t N>
@@ -312,7 +347,7 @@ constexpr auto as_span(const std::array<T,N> &arr) noexcept
{ return span<std::add_const_t<T>,N>{al::data(arr), al::size(arr)}; }
template<typename U, REQUIRES(!detail_::is_span_v<U> && !detail_::is_std_array_v<U>
&& !std::is_array<U>::value && detail_::has_size_and_data<U>)>
-constexpr auto as_span(U &cont)
+constexpr auto as_span(U&& cont)
{
using value_type = std::remove_pointer_t<decltype(al::data(std::declval<U&>()))>;
return span<value_type>{al::data(cont), al::size(cont)};