cnorxz/src/xfor/xfor.h

58 lines
1.1 KiB
C
Raw Normal View History

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);
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...) {}
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