cnorxz/src/xfor/xfor.h

81 lines
2 KiB
C
Raw Normal View History

2018-01-05 13:56:16 +01:00
#ifndef __xfor_h__
#define __xfor_h__
#include <cstdlib>
#include <memory>
2018-01-08 18:38:13 +01:00
#include <tuple>
2018-01-05 13:56:16 +01:00
namespace MultiArrayHelper
{
2018-01-08 18:38:13 +01:00
namespace {
template <class Op>
using to_size_t = size_t;
}
template <class IndexClass, class Expr, class... Ops>
2018-01-05 13:56:16 +01:00
class For
{
public:
For(For&& in) = default;
For& operator=(For&& in) = default;
template <typename... Args>
2018-01-08 18:38:13 +01:00
For(const std::shared_ptr<IndexClass>& indPtr,
std::tuple<to_size_t<Ops>...>&& ext, const Args&... args);
2018-01-05 13:56:16 +01:00
2018-01-08 18:38:13 +01:00
For(const std::shared_ptr<IndexClass>& indPtr,
Expr&& expr, std::tuple<to_size_t<Ops>...>&& ext);
2018-01-08 18:38:13 +01:00
inline void operator()(size_t mlast, const std::tuple<to_size_t<Ops>...>& last) const;
2018-01-05 13:56:16 +01:00
private:
For() = default;
2018-01-08 18:38:13 +01:00
mutable std::shared_ptr<IndexClass> mIndPtr;
const Expr mExpr;
const std::tuple<to_size_t<Ops>...> mExt;
2018-01-07 16:57:01 +01:00
};
2018-01-05 13:56:16 +01:00
2018-01-08 18:38:13 +01:00
2018-01-05 13:56:16 +01:00
} // namespace MultiArrayHelper
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayHelper
{
2018-01-08 18:38:13 +01:00
template <class IndexClass, class Expr, class... Ops>
2018-01-05 13:56:16 +01:00
template <typename... Args>
2018-01-08 18:38:13 +01:00
For<IndexClass,Expr,Ops...>::For(const std::shared_ptr<IndexClass>& indPtr,
std::tuple<to_size_t<Ops>...>&& ext,
const Args&... args) :
mIndPtr(indPtr), mExpr(args...), mExt(ext) {}
2018-01-05 13:56:16 +01:00
2018-01-08 18:38:13 +01:00
template <class IndexClass, class Expr, class... Ops>
For<IndexClass,Expr,Ops...>::For(const std::shared_ptr<IndexClass>& indPtr,
Expr&& expr, std::tuple<to_size_t<Ops>...>&& ext) :
mIndPtr(indPtr), mExpr(expr), mExt(ext) {}
2018-01-08 18:38:13 +01:00
template <class IndexClass, class Expr, class... Ops>
inline void For<IndexClass,Expr,Ops...>::operator()(size_t mlast,
const std::tuple<to_size_t<Ops>...>& last) const
2018-01-05 13:56:16 +01:00
{
2018-01-08 18:38:13 +01:00
static const size_t opNum = sizeof...(Ops);
2018-01-05 13:56:16 +01:00
auto& ind = *mIndPtr;
const size_t max = ind.max();
for(ind = 0; ind.pos() != max; ++ind){
2018-01-08 18:38:13 +01:00
const size_t mnpos = mlast * max + ind.pos();
const std::tuple<to_size_t<Ops>...> npos( /* !!! fancy code !!! */ );
mExpr(mnpos, npos);
2018-01-05 13:56:16 +01:00
}
}
} // namespace MultiArrayHelper
#endif