From 48084b2a5e4ab48613b17c2a4cc2c9580c500ed7 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 10 Apr 2021 00:58:30 +0200 Subject: [PATCH] generalize static_for.h --- src/include/statics/static_for.h | 77 ++++++++++++++------------------ 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/src/include/statics/static_for.h b/src/include/statics/static_for.h index fb12618..abc0c87 100644 --- a/src/include/statics/static_for.h +++ b/src/include/statics/static_for.h @@ -7,62 +7,51 @@ namespace MultiArrayTools { - template + template struct sfor { - template - static void execs(const F& f) + template + static inline auto exec(Incr incr, F f, Conc conc) { - if(BEG < END){ - f(std::integral_constant{}); - sfor::execs(f); - } + 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); + return conc(tmp, static_for::exec(incr,f,conc)); } - - template - static void execsr(const F& f) + + template + static inline auto unpack(Incr incr, F f, Create create, const Args&... args) { - if(BEG < END){ - f(std::integral_constant{}); - sfor::execsr(f); - } - } - - template - static void exec(const F& f) - { - f(std::integral_constant{}); - sfor::exec(f); - } - - template - static void execr(const F& f) - { - f(std::integral_constant{}); - sfor::execr(f); + 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); + return static_for::unpack(incr, f, create, args..., tmp); } }; - template - struct sfor + template + struct sfor { - template - static void execs(const F& f) {} + template + static inline auto exec(Incr incr, F f, Conc conc) + { + return 0; + } - template - static void execsr(const F& f) {} - - template - static void exec(const F& f) {} - - template - static void execr(const F& f) {} + template + static inline auto unpack(Incr incr, F f, Create create, const Args&... args) + { + return create(args...); + } }; } -#define MA_SSFOR(i,beg,end,expr) MultiArrayTools::sfor::execs([&](auto i){ expr }) -#define MA_SSRFOR(i,end,beg,expr) MultiArrayTools::sfor::execsr([&](auto i){ expr }) -#define MA_SFOR(i,beg,end,expr) MultiArrayTools::sfor::exec([&](auto i){ expr }) -#define MA_SRFOR(i,end,beg,expr) MultiArrayTools::sfor::execr([&](auto i){ expr }) +#define MA_SFOR(i,beg,end,incr,expr) static_for::exec([&](auto i) constexpr { return incr; }, [&](auto i){ expr return 0; }, [&](auto f, auto next) { return 0; }) +#define MA_SRFOR(i,beg,end,decr,expr) static_for::exec([&](auto i) constexpr { return decr; }, [&](auto i){ expr return 0; }, [&](auto f, auto next) { return 0; }) +#define MA_CFOR(i,beg,end,incr,expr,cre) static_for::unpack([&](auto i) constexpr { return incr; }, [&](auto i){ expr }, [&](auto... args) { return cre(args...); }) #endif