From 44f6149a2331ef74024efd9d86384b673361ddbc Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 11 Apr 2021 22:11:20 +0200 Subject: [PATCH] rewrite Operation::get (not final solution) --- src/include/multi_array_operation.cc.h | 32 +++++++- src/include/multi_array_operation.h | 6 ++ src/include/statics/static_for.h | 104 ++++++++++++------------- 3 files changed, 85 insertions(+), 57 deletions(-) diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 6c0644f..0c9f60b 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -1,5 +1,7 @@ #include "multi_array_operation.h" +#include "xfor/exttype.h" + /* ========================= * * --- TEMPLATE CODE --- * @@ -978,16 +980,38 @@ namespace MultiArrayTools mOps(ops...), mF(ff) { - static_assert( not FISSTATIC, "using instance of function supposed to be static" ); + static_assert( not FISSTATIC, "using instance of static function" ); } + template + template + inline auto Operation::getSubX(ET pos) const + { + // somehow get rid of the second condition (should NOT be needed if everything is correct) + if constexpr(I == J or sizeof...(Ops)-1 == I){ + return std::get(mOps.mOps).get(pos); + } + else { + typedef typename std::remove_reference(mOps.mOps))>::type + NextOpType; + return getSubX(getX(pos)); + } + } + template template inline auto Operation::get(ET pos) const { - typedef std::tuple OpTuple; - return PackNum:: - template mkOpExpr(mF, pos, mOps.mOps); + auto cre = [&](auto... args) + { + if constexpr(OpFunction::FISSTATIC){ + return OpFunction::apply(args...); + } + else { + return (*mF)(args...); + } + }; + return MA_CFOR2(i,0,sizeof...(Ops),i+1,return getSub(pos);,cre); } template diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index fedbec5..1554ed1 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -803,6 +803,12 @@ namespace MultiArrayTools Operation(const Ops&... ops); Operation(std::shared_ptr ff, const Ops&... ops); + + template + inline auto getSubX(ET pos) const; + + template + inline auto getSub(ET pos) const { return getSubX<0,J>(pos); } template inline auto get(ET pos) const; diff --git a/src/include/statics/static_for.h b/src/include/statics/static_for.h index 8176f9f..141f8eb 100644 --- a/src/include/statics/static_for.h +++ b/src/include/statics/static_for.h @@ -6,67 +6,65 @@ namespace MultiArrayTools { - - template - struct sfor + template + inline auto sfor(Incr incr, F f, Conc conc) { - template - static inline auto exec(Incr incr, F f, Conc conc) - { - constexpr auto idx = std::integral_constant{}; - constexpr auto idxm = std::integral_constant{}; - //static_assert(abs(idx.value - END) >= abs(incr(idx) - END), - // "this turns out to be a static endless loop"); - auto tmp = f(idxm); - if constexpr(incr(idx) == END){ - return tmp; - } - else { - return conc(tmp, sfor::exec(incr,f,conc)); - } + constexpr auto idx = std::integral_constant{}; + constexpr auto idxm = std::integral_constant{}; + //static_assert(abs(idx.value - END) >= abs(incr(idx) - END), + // "this turns out to be a static endless loop"); + auto tmp = f(idxm); + if constexpr(incr(idx) == END){ + return tmp; } + else { + return conc(tmp, sfor(incr,f,conc)); + } + } - template - static inline auto exec(Incr incr, F f, Conc conc, const Arg& arg) - { - constexpr auto idx = std::integral_constant{}; - constexpr auto idxm = std::integral_constant{}; - //static_assert(abs(idx.value - END) >= abs(incr(idx) - END), - // "this turns out to be a static endless loop"); - auto tmp = f(idxm); - if constexpr(incr(idx) == END){ - return conc(tmp, arg); - } - else { - return conc(tmp, sfor::exec(incr,f,conc,arg)); - } + template + inline auto sfor(Incr incr, F f, Conc conc, const Arg& arg) + { + constexpr auto idx = std::integral_constant{}; + constexpr auto idxm = std::integral_constant{}; + //static_assert(abs(idx.value - END) >= abs(incr(idx) - END), + // "this turns out to be a static endless loop"); + auto tmp = f(idxm); + if constexpr(incr(idx) == END){ + return conc(tmp, arg); } + else { + return conc(tmp, sfor(incr,f,conc,arg)); + } + } - template - static inline auto unpack(Incr incr, F f, Create create, const Args&... args) - { - constexpr auto idx = std::integral_constant{}; - constexpr auto idxm = std::integral_constant{}; - //static_assert(abs(idx.value - END) >= abs(incr(idx) - END), - // "this turns out to be a static endless loop"); - auto tmp = f(idxm); - if constexpr(BEG == END){ - return create(args...); - } - else { - return sfor::unpack(incr, f, create, args..., tmp); - } + template + inline auto unpack(Incr incr, F f, Create create, const Args&... args) + { + constexpr auto idx = std::integral_constant{}; + constexpr auto idxm = std::integral_constant{}; + //static_assert(abs(idx.value - END) >= abs(incr(idx) - END), + // "this turns out to be a static endless loop"); + if constexpr(BEG == END){ + return create(args...); } - }; + else { + auto tmp = f(idxm); + return unpack(incr, f, create, args..., tmp); + } + } + + } -#define MA_SFOR(i,beg,end,incr,expr) sfor::exec([&](auto i) constexpr { return incr; }, [&](auto i){ expr return 0; }, [&](auto f, auto next) { return 0; }) -#define MA_SCFOR(i,beg,end,incr,expr,conc) sfor::exec([&](auto i) constexpr { return incr; }, [&](auto i){ return expr; }, [&](auto f, auto next) { return f.conc(next); }) -#define MA_SRFOR(i,beg,end,decr,expr) sfor::exec([&](auto i) constexpr { return decr; }, [&](auto i){ expr return 0; }, [&](auto f, auto next) { return 0; }) -#define MA_SCRFOR(i,beg,end,decr,expr,conc) sfor::exec([&](auto i) constexpr { return decr; }, [&](auto i){ return expr; }, [&](auto f, auto next) { return f.conc(next); }) -#define MA_SCRAFOR(i,beg,end,decr,expr,conc,arg) sfor::exec([&](auto i) constexpr { return decr; }, [&](auto i){ return expr; }, [&](auto f, auto next) { return f.conc(next); }, arg) -#define MA_CFOR(i,beg,end,incr,expr,cre) sfor::unpack([&](auto i) constexpr { return incr; }, [&](auto i){ expr }, [&](auto... args) { return cre(args...); }) +#define MA_SFOR(i,beg,end,incr,expr) sfor([&](auto i) constexpr { return incr; }, [&](auto i){ expr return 0; }, [&](auto f, auto next) { return 0; }) +#define MA_SCFOR(i,beg,end,incr,expr,conc) sfor([&](auto i) constexpr { return incr; }, [&](auto i){ return expr; }, [&](auto f, auto next) { return f.conc(next); }) +#define MA_SRFOR(i,beg,end,decr,expr) sfor([&](auto i) constexpr { return decr; }, [&](auto i){ expr return 0; }, [&](auto f, auto next) { return 0; }) +#define MA_SCRFOR(i,beg,end,decr,expr,conc) sfor([&](auto i) constexpr { return decr; }, [&](auto i){ return expr; }, [&](auto f, auto next) { return f.conc(next); }) +#define MA_SCRAFOR(i,beg,end,decr,expr,conc,arg) sfor([&](auto i) constexpr { return decr; }, [&](auto i){ return expr; }, [&](auto f, auto next) { return f.conc(next); }, arg) +#define MA_CFOR(i,beg,end,incr,expr,cre) unpack([&](auto i) constexpr { return incr; }, [&](auto i){ expr }, [&](auto... args) { return cre(args...); }) +#define MA_CFOR2(i,beg,end,incr,expr,cre) unpack([&](auto i) constexpr { return incr; }, [&](auto i){ expr }, cre) -#define MA_SCFOR_X(i,beg,end,incr,expr,conc) sfor::exec([](auto i) constexpr { return incr; }, [](auto i){ return expr; }, [](auto f, auto next) { return f.conc(next); }) +#define MA_SCFOR_X(i,beg,end,incr,expr,conc) sfor([](auto i) constexpr { return incr; }, [](auto i){ return expr; }, [](auto f, auto next) { return f.conc(next); }) #endif