This commit is contained in:
Christian Zimmermann 2018-01-08 18:38:13 +01:00
parent 6460276bc4
commit a340bda404
2 changed files with 40 additions and 19 deletions

View file

@ -4,10 +4,16 @@
#include "ranges/rheader.h" #include "ranges/rheader.h"
#include "xfor.h" #include "xfor.h"
#include <array>
#include <tuple>
namespace MultiArrayHelper namespace MultiArrayHelper
{ {
namespace {
template <class Op>
using to_size_t<Op> = size_t;
}
template <size_t N> template <size_t N>
struct XFPackNum struct XFPackNum
{ {
@ -24,7 +30,6 @@ namespace MultiArrayHelper
{ {
return XFPackNum<N-1>::mkForTp(iTuple, std::get<N>(iTuple), args...) return XFPackNum<N-1>::mkForTp(iTuple, std::get<N>(iTuple), args...)
} }
}; };
template <> template <>
@ -43,7 +48,6 @@ namespace MultiArrayHelper
{ {
return XFPackNum<std::tuple_size<IndexTuple>::value-1>::mkFor( std::get<0>(iTuple), args... ); return XFPackNum<std::tuple_size<IndexTuple>::value-1>::mkFor( std::get<0>(iTuple), args... );
} }
}; };
} // end namespace MultiArrayHelper } // end namespace MultiArrayHelper

View file

@ -4,11 +4,16 @@
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <tuple>
namespace MultiArrayHelper namespace MultiArrayHelper
{ {
namespace {
template <class IndexClass, class Expr> template <class Op>
using to_size_t = size_t;
}
template <class IndexClass, class Expr, class... Ops>
class For class For
{ {
public: public:
@ -17,19 +22,24 @@ namespace MultiArrayHelper
For& operator=(For&& in) = default; For& operator=(For&& in) = default;
template <typename... Args> template <typename... Args>
For(const std::shared_ptr<IndexClass>& indPtr, const Args&... args); For(const std::shared_ptr<IndexClass>& indPtr,
std::tuple<to_size_t<Ops>...>&& ext, const Args&... args);
For(const std::shared_ptr<IndexClass>& indPtr, Expr&& expr); For(const std::shared_ptr<IndexClass>& indPtr,
Expr&& expr, std::tuple<to_size_t<Ops>...>&& ext);
inline void operator()(size_t start = 0); inline void operator()(size_t mlast, const std::tuple<to_size_t<Ops>...>& last) const;
private: private:
For() = default; For() = default;
std::shared_ptr<IndexClass> mIndPtr; mutable std::shared_ptr<IndexClass> mIndPtr;
Expr mExpr; const Expr mExpr;
const std::tuple<to_size_t<Ops>...> mExt;
}; };
} // namespace MultiArrayHelper } // namespace MultiArrayHelper
/* ========================= * /* ========================= *
@ -39,22 +49,29 @@ namespace MultiArrayHelper
namespace MultiArrayHelper namespace MultiArrayHelper
{ {
template <class IndexClass, class Expr> template <class IndexClass, class Expr, class... Ops>
template <typename... Args> template <typename... Args>
For<IndexClass,Expr>::For(const std::shared_ptr<IndexClass>& indPtr, For<IndexClass,Expr,Ops...>::For(const std::shared_ptr<IndexClass>& indPtr,
const Args&... args) : mIndPtr(indPtr), mExpr(args...) {} std::tuple<to_size_t<Ops>...>&& ext,
const Args&... args) :
mIndPtr(indPtr), mExpr(args...), mExt(ext) {}
template <class IndexClass, class Expr> template <class IndexClass, class Expr, class... Ops>
For<IndexClass,Expr>::For(const std::shared_ptr<IndexClass>& indPtr, For<IndexClass,Expr,Ops...>::For(const std::shared_ptr<IndexClass>& indPtr,
Expr&& expr) : mIndPtr(indPtr), mExpr(expr) {} Expr&& expr, std::tuple<to_size_t<Ops>...>&& ext) :
mIndPtr(indPtr), mExpr(expr), mExt(ext) {}
template <class IndexClass, class Expr> template <class IndexClass, class Expr, class... Ops>
inline void For<IndexClass,Expr>::operator()(size_t start) inline void For<IndexClass,Expr,Ops...>::operator()(size_t mlast,
const std::tuple<to_size_t<Ops>...>& last) const
{ {
static const size_t opNum = sizeof...(Ops);
auto& ind = *mIndPtr; auto& ind = *mIndPtr;
const size_t max = ind.max(); const size_t max = ind.max();
for(ind = 0; ind.pos() != max; ++ind){ for(ind = 0; ind.pos() != max; ++ind){
mExpr(start * max + ind.pos()); // CHECK!! const size_t mnpos = mlast * max + ind.pos();
const std::tuple<to_size_t<Ops>...> npos( /* !!! fancy code !!! */ );
mExpr(mnpos, npos);
} }
} }