generalize static_for.h
This commit is contained in:
parent
cf1850923f
commit
48084b2a5e
1 changed files with 33 additions and 44 deletions
|
@ -7,62 +7,51 @@
|
|||
namespace MultiArrayTools
|
||||
{
|
||||
|
||||
template <size_t BEG, size_t END>
|
||||
template <size_t BEG, size_t END, size_t OFF>
|
||||
struct sfor
|
||||
{
|
||||
template <typename F>
|
||||
static void execs(const F& f)
|
||||
template <typename Incr, typename F, typename Conc>
|
||||
static inline auto exec(Incr incr, F f, Conc conc)
|
||||
{
|
||||
if(BEG < END){
|
||||
f(std::integral_constant<size_t, BEG>{});
|
||||
sfor<BEG+1,END>::execs(f);
|
||||
}
|
||||
constexpr auto idx = std::integral_constant<size_t, BEG>{};
|
||||
constexpr auto idxm = std::integral_constant<size_t, BEG+OFF>{};
|
||||
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<incr(idx),END,OFF>::exec(incr,f,conc));
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
static void execsr(const F& f)
|
||||
|
||||
template <typename Incr, typename F, typename Create, typename... Args>
|
||||
static inline auto unpack(Incr incr, F f, Create create, const Args&... args)
|
||||
{
|
||||
if(BEG < END){
|
||||
f(std::integral_constant<size_t, END-1>{});
|
||||
sfor<BEG,END-1>::execsr(f);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
static void exec(const F& f)
|
||||
{
|
||||
f(std::integral_constant<size_t, BEG>{});
|
||||
sfor<BEG+1,END>::exec(f);
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
static void execr(const F& f)
|
||||
{
|
||||
f(std::integral_constant<size_t, END-1>{});
|
||||
sfor<BEG,END-1>::execr(f);
|
||||
constexpr auto idx = std::integral_constant<size_t, BEG>{};
|
||||
constexpr auto idxm = std::integral_constant<size_t, BEG+OFF>{};
|
||||
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<incr(idx),END,OFF>::unpack(incr, f, create, args..., tmp);
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t N>
|
||||
struct sfor<N,N>
|
||||
template <size_t END, size_t OFF>
|
||||
struct sfor<END,END,OFF>
|
||||
{
|
||||
template <typename F>
|
||||
static void execs(const F& f) {}
|
||||
template <typename Incr, typename F, typename Conc>
|
||||
static inline auto exec(Incr incr, F f, Conc conc)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
static void execsr(const F& f) {}
|
||||
|
||||
template <typename F>
|
||||
static void exec(const F& f) {}
|
||||
|
||||
template <typename F>
|
||||
static void execr(const F& f) {}
|
||||
template <typename Incr, typename F, typename Create, typename... Args>
|
||||
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<beg,end>::execs([&](auto i){ expr })
|
||||
#define MA_SSRFOR(i,end,beg,expr) MultiArrayTools::sfor<beg,end>::execsr([&](auto i){ expr })
|
||||
#define MA_SFOR(i,beg,end,expr) MultiArrayTools::sfor<beg,end>::exec([&](auto i){ expr })
|
||||
#define MA_SRFOR(i,end,beg,expr) MultiArrayTools::sfor<beg,end>::execr([&](auto i){ expr })
|
||||
#define MA_SFOR(i,beg,end,incr,expr) static_for<beg,end,0>::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<beg,end,-1>::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<beg,end,0>::unpack([&](auto i) constexpr { return incr; }, [&](auto i){ expr }, [&](auto... args) { return cre(args...); })
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue