From 351378a0096375b1930616a797a9def1a0121fc4 Mon Sep 17 00:00:00 2001 From: Mykola Vankovych Date: Tue, 26 Jul 2022 17:23:11 +0200 Subject: [PATCH] adding xt::detail::get_fixed_size. --- include/xtensor/xadapt.hpp | 18 +++++++++--------- include/xtensor/xeval.hpp | 2 +- include/xtensor/xfixed.hpp | 4 ++-- include/xtensor/xfunction.hpp | 20 ++++++++++++++++++-- include/xtensor/xreducer.hpp | 6 +++--- include/xtensor/xshape.hpp | 24 +++++++++++++++++++----- 6 files changed, 52 insertions(+), 22 deletions(-) diff --git a/include/xtensor/xadapt.hpp b/include/xtensor/xadapt.hpp index 34cd52cdd..05acede95 100644 --- a/include/xtensor/xadapt.hpp +++ b/include/xtensor/xadapt.hpp @@ -230,11 +230,11 @@ namespace xt template >, detail::not_a_pointer>)> - inline xtensor_adaptor>::value, L> + inline xtensor_adaptor>::value, L> adapt(C&& container, const SC& shape, layout_type l = L) { static_assert(!xtl::is_integral::value, "shape cannot be a integer"); - constexpr std::size_t N = std::tuple_size>::value; + constexpr std::size_t N = detail::get_fixed_size>::value; using return_type = xtensor_adaptor, N, L>; return return_type(std::forward(container), shape, l); } @@ -252,7 +252,7 @@ namespace xt { static_assert(!xtl::is_integral::value, "shape cannot be a integer"); using buffer_type = xbuffer_adaptor>; - constexpr std::size_t N = std::tuple_size>::value; + constexpr std::size_t N = detail::get_fixed_size>::value; using return_type = xtensor_adaptor; return return_type(buffer_type(pointer, compute_size(shape)), shape, l); } @@ -267,11 +267,11 @@ namespace xt template >, detail::not_a_layout>)> - inline xtensor_adaptor>::value, layout_type::dynamic> + inline xtensor_adaptor>::value, layout_type::dynamic> adapt(C&& container, SC&& shape, SS&& strides) { static_assert(!xtl::is_integral>::value, "shape cannot be a integer"); - constexpr std::size_t N = std::tuple_size>::value; + constexpr std::size_t N = detail::get_fixed_size>::value; using return_type = xtensor_adaptor, N, layout_type::dynamic>; return return_type(std::forward(container), xtl::forward_sequence(shape), @@ -313,13 +313,13 @@ namespace xt */ template , XTL_REQUIRES(detail::has_fixed_size>)> - inline xtensor_adaptor, O, A>, std::tuple_size>::value, L> + inline xtensor_adaptor, O, A>, detail::get_fixed_size>::value, L> adapt(P&& pointer, typename A::size_type size, O ownership, const SC& shape, layout_type l = L, const A& alloc = A()) { static_assert(!xtl::is_integral::value, "shape cannot be a integer"); (void)ownership; using buffer_type = xbuffer_adaptor, O, A>; - constexpr std::size_t N = std::tuple_size>::value; + constexpr std::size_t N = detail::get_fixed_size>::value; using return_type = xtensor_adaptor; buffer_type buf(std::forward

(pointer), size, alloc); return return_type(std::move(buf), shape, l); @@ -339,13 +339,13 @@ namespace xt template , XTL_REQUIRES(detail::has_fixed_size>, detail::not_a_layout>)> - inline xtensor_adaptor, O, A>, std::tuple_size>::value, layout_type::dynamic> + inline xtensor_adaptor, O, A>, detail::get_fixed_size>::value, layout_type::dynamic> adapt(P&& pointer, typename A::size_type size, O ownership, SC&& shape, SS&& strides, const A& alloc = A()) { static_assert(!xtl::is_integral>::value, "shape cannot be a integer"); (void)ownership; using buffer_type = xbuffer_adaptor, O, A>; - constexpr std::size_t N = std::tuple_size>::value; + constexpr std::size_t N = detail::get_fixed_size>::value; using return_type = xtensor_adaptor; buffer_type buf(std::forward

(pointer), size, alloc); return return_type(std::move(buf), diff --git a/include/xtensor/xeval.hpp b/include/xtensor/xeval.hpp index 711ea6a47..9d6143003 100644 --- a/include/xtensor/xeval.hpp +++ b/include/xtensor/xeval.hpp @@ -102,7 +102,7 @@ namespace xt template using as_xtensor_container_t = xtensor::value_type, - std::tuple_size::shape_type>::value, + detail::get_fixed_size::shape_type>::value, layout_remove_any(L)>; } diff --git a/include/xtensor/xfixed.hpp b/include/xtensor/xfixed.hpp index 61ba009f4..77cd5b210 100644 --- a/include/xtensor/xfixed.hpp +++ b/include/xtensor/xfixed.hpp @@ -228,7 +228,7 @@ namespace xt constexpr T get_backstrides(const S& shape, const T& strides) noexcept { return detail::get_backstrides_impl(shape, strides, - std::make_index_sequence::value>{}); + std::make_index_sequence::value>{}); } template @@ -314,7 +314,7 @@ namespace xt using temporary_type = typename semantic_base::temporary_type; using expression_tag = Tag; - constexpr static std::size_t N = std::tuple_size::value; + constexpr static std::size_t N = detail::get_fixed_size::value; constexpr static std::size_t rank = N; xfixed_container() = default; diff --git a/include/xtensor/xfunction.hpp b/include/xtensor/xfunction.hpp index 0b21bf44f..73f1dcb04 100644 --- a/include/xtensor/xfunction.hpp +++ b/include/xtensor/xfunction.hpp @@ -22,6 +22,7 @@ #include #include "xaccessible.hpp" +#include "xaccumulator.hpp" #include "xexpression_traits.hpp" #include "xiterable.hpp" #include "xlayout.hpp" @@ -913,7 +914,7 @@ namespace xt // Optimization: no need to compare each subiterator since they all // are incremented decremented together. constexpr std::size_t temp = xtl::mpl::find_if::value; - constexpr std::size_t index = (temp == std::tuple_size::value) ? 0 : temp; + constexpr std::size_t index = (temp == detail::get_fixed_size::value) ? 0 : temp; return std::get(m_it) == std::get(rhs.m_it); } @@ -923,7 +924,7 @@ namespace xt // Optimization: no need to compare each subiterator since they all // are incremented decremented together. constexpr std::size_t temp = xtl::mpl::find_if::value; - constexpr std::size_t index = (temp == std::tuple_size::value) ? 0 : temp; + constexpr std::size_t index = (temp == detail::get_fixed_size::value) ? 0 : temp; return std::get(m_it) < std::get(rhs.m_it); } @@ -1059,6 +1060,21 @@ namespace xt auto step_leading_lambda = [](auto&& st) { st.step_leading(); }; for_each(step_leading_lambda, m_st); } + + namespace detail + { + template + struct has_fixed_size, std::enable_if_t::shape_type>::value>> + : std::true_type + { + }; + + template + struct get_fixed_size, std::enable_if_t>::value>> + : std::integral_constant::shape_type>::value> + { + }; + } } #endif diff --git a/include/xtensor/xreducer.hpp b/include/xtensor/xreducer.hpp index ae0b962bc..5f25ab0c2 100644 --- a/include/xtensor/xreducer.hpp +++ b/include/xtensor/xreducer.hpp @@ -117,9 +117,9 @@ namespace xt reducer_options(const T& tpl) { - xtl::mpl::static_if::value>([this, &tpl](auto no_compile) { + xtl::mpl::static_if::value>([this, &tpl](auto no_compile) { // use no_compile to prevent compilation if initial_val_idx is out of bounds! - this->initial_value = no_compile(std::get::value ? initial_val_idx : 0>(tpl)).value(); + this->initial_value = no_compile(std::get::value ? initial_val_idx : 0>(tpl)).value(); }, [](auto /*np_compile*/){} ); @@ -133,7 +133,7 @@ namespace xt std::true_type, std::false_type>; - constexpr static bool has_initial_value = initial_val_idx != std::tuple_size::value; + constexpr static bool has_initial_value = initial_val_idx != detail::get_fixed_size::value; R initial_value; diff --git a/include/xtensor/xshape.hpp b/include/xtensor/xshape.hpp index 798b63596..10af12da7 100644 --- a/include/xtensor/xshape.hpp +++ b/include/xtensor/xshape.hpp @@ -25,6 +25,12 @@ namespace xt { + namespace detail + { + template + struct get_fixed_size; + } + template using dynamic_shape = svector; @@ -231,9 +237,9 @@ namespace xt }; template - struct static_dimension_impl::value)>> + struct static_dimension_impl::value)>> { - static constexpr std::ptrdiff_t value = static_cast(std::tuple_size::value); + static constexpr std::ptrdiff_t value = static_cast(detail::get_fixed_size::value); }; } @@ -281,7 +287,7 @@ namespace xt }; template - struct max_array_size : std::integral_constant::value, max_array_size::value)> + struct max_array_size : std::integral_constant::value, max_array_size::value)> { }; @@ -375,17 +381,25 @@ namespace xt static constexpr bool value = true; }; - template + template struct has_fixed_size : std::false_type { }; template - struct has_fixed_size::value)>> + struct has_fixed_size::value)>> : std::true_type { }; + template + struct get_fixed_size; + + template + struct get_fixed_size::value)>> : std::integral_constant::value> + { + }; + template using only_array = xtl::conjunction, is_fixed, has_fixed_size>...>;