2018-01-05 13:56:16 +01:00
|
|
|
|
|
|
|
#ifndef __xfor_h__
|
|
|
|
#define __xfor_h__
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace MultiArrayHelper
|
|
|
|
{
|
|
|
|
|
|
|
|
template <class IndexClass, class Expr>
|
|
|
|
class For
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
For(For&& in) = default;
|
|
|
|
For& operator=(For&& in) = default;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
For(const std::shared_ptr<IndexClass>& indPtr, const Args&... args);
|
|
|
|
|
2018-01-07 22:33:34 +01:00
|
|
|
For(const std::shared_ptr<IndexClass>& indPtr, Expr&& expr);
|
|
|
|
|
2018-01-05 13:56:16 +01:00
|
|
|
inline void operator()(size_t start = 0);
|
|
|
|
|
|
|
|
private:
|
|
|
|
For() = default;
|
|
|
|
|
|
|
|
std::shared_ptr<IndexClass> mIndPtr;
|
|
|
|
Expr mExpr;
|
2018-01-07 16:57:01 +01:00
|
|
|
};
|
2018-01-05 13:56:16 +01:00
|
|
|
|
|
|
|
} // namespace MultiArrayHelper
|
|
|
|
|
|
|
|
/* ========================= *
|
|
|
|
* --- TEMPLATE CODE --- *
|
|
|
|
* ========================= */
|
|
|
|
|
|
|
|
namespace MultiArrayHelper
|
|
|
|
{
|
|
|
|
|
|
|
|
template <class IndexClass, class Expr>
|
|
|
|
template <typename... Args>
|
|
|
|
For<IndexClass,Expr>::For(const std::shared_ptr<IndexClass>& indPtr,
|
|
|
|
const Args&... args) : mIndPtr(indPtr), mExpr(args...) {}
|
|
|
|
|
2018-01-07 22:33:34 +01:00
|
|
|
template <class IndexClass, class Expr>
|
|
|
|
For<IndexClass,Expr>::For(const std::shared_ptr<IndexClass>& indPtr,
|
|
|
|
Expr&& expr) : mIndPtr(indPtr), mExpr(expr) {}
|
|
|
|
|
2018-01-05 13:56:16 +01:00
|
|
|
template <class IndexClass, class Expr>
|
|
|
|
inline void For<IndexClass,Expr>::operator()(size_t start)
|
|
|
|
{
|
|
|
|
auto& ind = *mIndPtr;
|
|
|
|
const size_t max = ind.max();
|
|
|
|
for(ind = 0; ind.pos() != max; ++ind){
|
|
|
|
mExpr(start * max + ind.pos()); // CHECK!!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace MultiArrayHelper
|
|
|
|
|
|
|
|
#endif
|