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
|
namespace MultiArrayTools
|
||||||
{
|
{
|
||||||
|
|
||||||
template <size_t BEG, size_t END>
|
template <size_t BEG, size_t END, size_t OFF>
|
||||||
struct sfor
|
struct sfor
|
||||||
{
|
{
|
||||||
template <typename F>
|
template <typename Incr, typename F, typename Conc>
|
||||||
static void execs(const F& f)
|
static inline auto exec(Incr incr, F f, Conc conc)
|
||||||
{
|
{
|
||||||
if(BEG < END){
|
constexpr auto idx = std::integral_constant<size_t, BEG>{};
|
||||||
f(std::integral_constant<size_t, BEG>{});
|
constexpr auto idxm = std::integral_constant<size_t, BEG+OFF>{};
|
||||||
sfor<BEG+1,END>::execs(f);
|
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>
|
template <typename Incr, typename F, typename Create, typename... Args>
|
||||||
static void execsr(const F& f)
|
static inline auto unpack(Incr incr, F f, Create create, const Args&... args)
|
||||||
{
|
{
|
||||||
if(BEG < END){
|
constexpr auto idx = std::integral_constant<size_t, BEG>{};
|
||||||
f(std::integral_constant<size_t, END-1>{});
|
constexpr auto idxm = std::integral_constant<size_t, BEG+OFF>{};
|
||||||
sfor<BEG,END-1>::execsr(f);
|
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 <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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t END, size_t OFF>
|
||||||
struct sfor<N,N>
|
struct sfor<END,END,OFF>
|
||||||
{
|
{
|
||||||
template <typename F>
|
template <typename Incr, typename F, typename Conc>
|
||||||
static void execs(const F& f) {}
|
static inline auto exec(Incr incr, F f, Conc conc)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename Incr, typename F, typename Create, typename... Args>
|
||||||
static void execsr(const F& f) {}
|
static inline auto unpack(Incr incr, F f, Create create, const Args&... args)
|
||||||
|
{
|
||||||
template <typename F>
|
return create(args...);
|
||||||
static void exec(const F& f) {}
|
}
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
static void execr(const F& f) {}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MA_SSFOR(i,beg,end,expr) MultiArrayTools::sfor<beg,end>::execs([&](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_SSRFOR(i,end,beg,expr) MultiArrayTools::sfor<beg,end>::execsr([&](auto i){ expr })
|
#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_SFOR(i,beg,end,expr) MultiArrayTools::sfor<beg,end>::exec([&](auto i){ expr })
|
#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...); })
|
||||||
#define MA_SRFOR(i,end,beg,expr) MultiArrayTools::sfor<beg,end>::execr([&](auto i){ expr })
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue