expression base + dynamic expressio (xfor)

This commit is contained in:
Christian Zimmermann 2018-10-21 22:52:01 +02:00
parent 610994e52f
commit 7dc015a97a

View file

@ -15,6 +15,40 @@ namespace MultiArrayHelper
// 'HIDDEN FOR' CLASS for nested for loops in contractions a.s.o. // 'HIDDEN FOR' CLASS for nested for loops in contractions a.s.o.
// (NO COUNTING OF MASTER POSITION !!!!!) // (NO COUNTING OF MASTER POSITION !!!!!)
template <class Expr>
class ExpressionBase
{
public:
static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE;
protected:
Expr mExpr;
public:
typedef decltype(mExpr.rootSteps()) ExtType;
protected:
ExtType mExt;
public:
ExpressionBase() = default;
ExpressionBase(const ExpressionBase& in) = default;
ExpressionBase(ExpressionBase&& in) = default;
ExpressionBase& operator=(const ExpressionBase& in) = default;
ExpressionBase& operator=(ExpressionBase&& in) = default;
ExpressionBase(ExtType ext, Expr&& expr);
virtual void operator()(size_t mlast, ExtType last) const = 0;
virtual void operator()(size_t mlast = 0) const = 0;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
auto extension() const -> ExtType;
};
template <ForType FT = ForType::DEFAULT> template <ForType FT = ForType::DEFAULT>
struct PosForward struct PosForward
{ {
@ -64,7 +98,7 @@ namespace MultiArrayHelper
}; };
template <class IndexClass, class Expr> template <class IndexClass, class Expr>
class SingleExpression class SingleExpression : public ExpressionBase<Expr>
{ {
private: private:
SingleExpression() = default; SingleExpression() = default;
@ -72,13 +106,12 @@ namespace MultiArrayHelper
const IndexClass* mIndPtr; const IndexClass* mIndPtr;
size_t mSPos; size_t mSPos;
size_t mMax; size_t mMax;
Expr mExpr;
typedef decltype(mExpr.rootSteps()) ExtType;
ExtType mExt;
public: public:
typedef ExpressionBase<Expr> EB;
using EB::rootSteps;
typedef typename ExpressionBase<Expr>::ExtType ExtType;
static constexpr size_t LAYER = Expr::LAYER + 1; static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE; static constexpr size_t SIZE = Expr::SIZE;
@ -88,38 +121,33 @@ namespace MultiArrayHelper
SingleExpression& operator=(SingleExpression&& in) = default; SingleExpression& operator=(SingleExpression&& in) = default;
SingleExpression(const std::shared_ptr<IndexClass>& indPtr, SingleExpression(const std::shared_ptr<IndexClass>& indPtr,
Expr expr); Expr expr);
SingleExpression(const IndexClass* indPtr, SingleExpression(const IndexClass* indPtr,
Expr expr); Expr expr);
inline void operator()(size_t mlast, ExtType last) const; inline void operator()(size_t mlast, ExtType last) const override final;
inline void operator()(size_t mlast = 0) const; inline void operator()(size_t mlast = 0) const override final;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
}; };
template <class IndexClass, class Expr, ForType FT> template <class IndexClass, class Expr, ForType FT>
class For class For : public ExpressionBase<Expr>
{ {
private: private:
For() = default; For() = default;
//For(const For& in) = default;
//For& operator=(const For& in) = default;
const IndexClass* mIndPtr; const IndexClass* mIndPtr;
size_t mSPos; size_t mSPos;
size_t mMax; size_t mMax;
size_t mStep; size_t mStep;
Expr mExpr;
typedef decltype(mExpr.rootSteps()) ExtType;
ExtType mExt;
public: public:
typedef ExpressionBase<Expr> EB;
using EB::rootSteps;
typedef typename ExpressionBase<Expr>::ExtType ExtType;
static constexpr size_t LAYER = Expr::LAYER + 1; static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE; static constexpr size_t SIZE = Expr::SIZE;
@ -134,11 +162,8 @@ namespace MultiArrayHelper
For(const IndexClass* indPtr, For(const IndexClass* indPtr,
size_t step, Expr expr); size_t step, Expr expr);
inline void operator()(size_t mlast, ExtType last) const override final;
inline void operator()(size_t mlast, ExtType last) const; inline void operator()(size_t mlast = 0) const override final;
inline void operator()(size_t mlast = 0) const;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
}; };
@ -148,6 +173,35 @@ namespace MultiArrayHelper
template <> template <>
inline size_t exceptMax<1>(size_t max) { return 1; } inline size_t exceptMax<1>(size_t max) { return 1; }
template <class Expr>
class DynamicalExpression : public ExpressionBase<Expr>
{
private:
DynamicalExpression() = default;
std::shared_ptr<ExpressionBase<Expr>> mNext;
public:
typedef ExpressionBase<Expr> EB;
using EB::rootSteps;
typedef typename ExpressionBase<Expr>::ExtType ExtType;
static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE;
DynamicalExpression(const DynamicalExpression& in) = default;
DynamicalExpression(DynamicalExpression&& in) = default;
DynamicalExpression& operator=(const DynamicalExpression& in) = default;
DynamicalExpression& operator=(DynamicalExpression&& in) = default;
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next,
Expr expr);
inline void operator()(size_t mlast, ExtType last) const override final;
inline void operator()(size_t mlast = 0) const override final;
};
} // namespace MultiArrayHelper } // namespace MultiArrayHelper
/* ========================= * /* ========================= *
@ -159,11 +213,40 @@ namespace MultiArrayHelper
namespace MultiArrayHelper namespace MultiArrayHelper
{ {
/**********************
* ExpressionBase *
**********************/
template <class Expr>
ExpressionBase<Expr>::ExpressionBase(ExtType ext, Expr&& expr) :
mExpr(std::forward<Expr>(expr)),
mExt(ext)
{}
template <class Expr>
auto ExpressionBase<Expr>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
{
return mExpr.rootSteps(iPtrNum);
}
template <class Expr>
auto ExpressionBase<Expr>::extension() const
-> ExtType
{
return mExt;
}
/*****************
* F o r *
*****************/
template <class IndexClass, class Expr, ForType FT> template <class IndexClass, class Expr, ForType FT>
For<IndexClass,Expr,FT>::For(const std::shared_ptr<IndexClass>& indPtr, For<IndexClass,Expr,FT>::For(const std::shared_ptr<IndexClass>& indPtr,
size_t step, Expr expr) : size_t step, Expr expr) :
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr(expr), ExpressionBase<Expr>(expr.rootSteps( reinterpret_cast<std::intptr_t>( indPtr.get() )),
mExt(expr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr.get() ))) std::forward<Expr>(expr)),
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step)
{ {
assert(mIndPtr != nullptr); assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id()); //VCHECK(mIndPtr->id());
@ -173,10 +256,10 @@ namespace MultiArrayHelper
template <class IndexClass, class Expr, ForType FT> template <class IndexClass, class Expr, ForType FT>
For<IndexClass,Expr,FT>::For(const IndexClass* indPtr, For<IndexClass,Expr,FT>::For(const IndexClass* indPtr,
size_t step, Expr expr) : size_t step, Expr expr) :
ExpressionBase<Expr>(expr.rootSteps( reinterpret_cast<std::intptr_t>( indPtr ) ),
std::forward<Expr>(expr)),
mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()),
mStep(step), mStep(step)
mExpr(std::forward<Expr>( expr )),
mExt(expr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr ) ))
{ {
assert(mIndPtr != nullptr); assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id()); //VCHECK(mIndPtr->id());
@ -192,8 +275,8 @@ namespace MultiArrayHelper
//for(size_t pos = mSPos; pos != mMax; ++pos){ //for(size_t pos = mSPos; pos != mMax; ++pos){
//const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos); //const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos);
const size_t mnpos = PosForward<FT>::valuex(mlast, mStep, pos); const size_t mnpos = PosForward<FT>::valuex(mlast, mStep, pos);
const ExtType npos = last + mExt*pos; const ExtType npos = last + EB::mExt*pos;
mExpr(mnpos, npos); EB::mExpr(mnpos, npos);
} }
} }
@ -206,26 +289,22 @@ namespace MultiArrayHelper
//for(size_t pos = mSPos; pos != mMax; ++pos){ //for(size_t pos = mSPos; pos != mMax; ++pos){
//const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos); //const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos);
const size_t mnpos = PosForward<FT>::valuex(mlast, mStep, pos); const size_t mnpos = PosForward<FT>::valuex(mlast, mStep, pos);
const ExtType npos = last + mExt*pos; const ExtType npos = last + EB::mExt*pos;
mExpr(mnpos, npos); EB::mExpr(mnpos, npos);
} }
} }
template <class IndexClass, class Expr, ForType FT>
auto For<IndexClass,Expr,FT>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
{
return mExpr.rootSteps(iPtrNum);
}
/************************
* SingleExpression *
************************/
template <class IndexClass, class Expr> template <class IndexClass, class Expr>
SingleExpression<IndexClass,Expr>::SingleExpression(const std::shared_ptr<IndexClass>& indPtr, SingleExpression<IndexClass,Expr>::SingleExpression(const std::shared_ptr<IndexClass>& indPtr,
Expr expr) : Expr expr) :
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mExpr(expr), ExpressionBase<Expr>(expr.rootSteps( reinterpret_cast<std::intptr_t>( indPtr.get() )),
mExt(expr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr.get() ))) std::forward<Expr>(expr)),
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max())
{ {
assert(mIndPtr != nullptr); assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id()); //VCHECK(mIndPtr->id());
@ -235,9 +314,9 @@ namespace MultiArrayHelper
template <class IndexClass, class Expr> template <class IndexClass, class Expr>
SingleExpression<IndexClass,Expr>::SingleExpression(const IndexClass* indPtr, SingleExpression<IndexClass,Expr>::SingleExpression(const IndexClass* indPtr,
Expr expr) : Expr expr) :
mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), ExpressionBase<Expr>(expr.rootSteps( reinterpret_cast<std::intptr_t>( indPtr ) ),
mExpr(std::forward<Expr>( expr )), std::forward<Expr>(expr)),
mExt(expr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr ) )) mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max())
{ {
assert(mIndPtr != nullptr); assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id()); //VCHECK(mIndPtr->id());
@ -251,8 +330,8 @@ namespace MultiArrayHelper
//typedef typename IndexClass::RangeType RangeType; //typedef typename IndexClass::RangeType RangeType;
const size_t pos = mIndPtr->pos(); const size_t pos = mIndPtr->pos();
const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos); const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos);
const ExtType npos = last + mExt*pos; const ExtType npos = last + EB::mExt*pos;
mExpr(mnpos, npos); EB::mExpr(mnpos, npos);
} }
template <class IndexClass, class Expr> template <class IndexClass, class Expr>
@ -262,15 +341,33 @@ namespace MultiArrayHelper
const ExtType last; const ExtType last;
const size_t pos = mIndPtr->pos(); const size_t pos = mIndPtr->pos();
const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos); const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos);
const ExtType npos = last + mExt*pos; const ExtType npos = last + EB::mExt*pos;
mExpr(mlast, last); EB::mExpr(mlast, last);
} }
template <class IndexClass, class Expr> /***************************
auto SingleExpression<IndexClass,Expr>::rootSteps(std::intptr_t iPtrNum) const * DynamicalExpression *
-> ExtType ***************************/
template <class Expr>
DynamicalExpression<Expr>::
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next,
Expr expr) :
ExpressionBase<Expr>(next->extension(), std::forward<Expr>(expr)),
mNext(next)
{}
template <class Expr>
inline void DynamicalExpression<Expr>::operator()(size_t mlast, ExtType last) const
{ {
return mExpr.rootSteps(iPtrNum); (*mNext)(mlast,last);
}
template <class Expr>
inline void DynamicalExpression<Expr>::operator()(size_t mlast) const
{
(*mNext)(mlast);
} }