im com (...)

This commit is contained in:
Christian Zimmermann 2018-10-23 20:02:01 +02:00
parent d7aea8a164
commit 1b8c2edf45
2 changed files with 175 additions and 131 deletions

View file

@ -70,47 +70,22 @@ namespace MultiArrayTools
virtual std::intptr_t get() const = 0; virtual std::intptr_t get() const = 0;
template <class Expr>
auto ifor(size_t step, Expr ex)
-> DynamicalExpression<Expr>;
template <class Expr>
auto iforh(size_t step, Expr ex)
-> DynamicalExpression<Expr>;
}; };
typedef IndexWrapperBase IndexW; typedef IndexWrapperBase IndexW;
template <class Expr>
class MakeForBase
{
public:
virtual DynamicalExpression<Expr> ifor(size_t step, Expr ex) const = 0;
virtual DynamicalExpression<Expr> iforh(size_t step, Expr ex) const = 0;
};
template <class Expr, class Index>
class MakeFor : public MakeForBase<Expr>
{
private:
std::shared_ptr<Index> mI;
MakeFor() = default;
public:
MakeFor(const std::shared_ptr<Index>& i);
virtual DynamicalExpression<Expr> ifor(size_t step, Expr ex) const override;
virtual DynamicalExpression<Expr> iforh(size_t step, Expr ex) const override;
};
template <class Index> template <class Index>
class IndexWrapper : public IndexWrapperBase class IndexWrapper : public IndexWrapperBase
{ {
private: protected:
IndexWrapper() = default; IndexWrapper() = default;
private:
std::shared_ptr<Index> mI; std::shared_ptr<Index> mI;
public: public:
static constexpr size_t EXPRMAX = IndexWrapperBase::EXPRMAX;
IndexWrapper(const IndexWrapper& in) = default; IndexWrapper(const IndexWrapper& in) = default;
IndexWrapper(IndexWrapper&& in) = default; IndexWrapper(IndexWrapper&& in) = default;
IndexWrapper& operator=(const IndexWrapper& in) = default; IndexWrapper& operator=(const IndexWrapper& in) = default;
@ -144,12 +119,13 @@ namespace MultiArrayTools
virtual size_t getStepSize(size_t n) final { return mI->getStepSize(n); } virtual size_t getStepSize(size_t n) final { return mI->getStepSize(n); }
virtual std::intptr_t get() const final { return reinterpret_cast<std::intptr_t>(mI.get()); } virtual std::intptr_t get() const final { return reinterpret_cast<std::intptr_t>(mI.get()); }
}; };
class DynamicIndex : public IndexInterface<DynamicIndex,DynamicMetaT> class DynamicIndex : public IndexInterface<DynamicIndex,DynamicMetaT>
{ {
private: private:
std::vector<std::shared_ptr<IndexWrapperBase>> mIVec; std::vector<std::pair<std::shared_ptr<IndexWrapperBase>,size_t>> mIVec;
public: public:
typedef IndexInterface<DynamicIndex,DynamicMetaT> IB; typedef IndexInterface<DynamicIndex,DynamicMetaT> IB;
typedef DynamicMetaT MetaType; typedef DynamicMetaT MetaType;
@ -306,42 +282,24 @@ namespace MultiArrayTools
namespace MultiArrayTools namespace MultiArrayTools
{ {
/************************* /*************************
* IndexWrapperBase * * IndexForWrapper *
*************************/ *************************/
template <class Expr> template <class Index, class Expr>
auto IndexWrapperBase::ifor(size_t step, Expr ex) auto IndexForWrapper<Index,Expr>::ifor(size_t step, Expr ex)
-> DynamicalExpression<Expr> -> DynamicalExpression<Expr>
{
}
template <class Expr>
auto IndexWrapperBase::iforh(size_t step, Expr ex)
-> DynamicalExpression<Expr>
{
}
/****************
* MakeFor *
****************/
template <class Expr, class Index>
MakeFor<Expr,Index>::MakeFor(const Index& i) : mI(i) {}
template <class Expr, class Index>
DynamicalExpression<Expr> MakeFor<Expr,Index>::ifor(size_t step, Expr ex) const
{ {
return DynamicalExpression<Expr>(std::make_shared<For<Index,Expr>>(mI,step,ex)); return DynamicalExpression<Expr>(std::make_shared<For<Index,Expr>>(mI,step,ex));
} }
template <class Expr, class Index> template <class Index, class Expr>
DynamicalExpression<Expr> MakeFor<Expr,Index>::iforh(size_t step, Expr ex) const auto IndexForWrapper<Index,Expr>::iforh(size_t step, Expr ex)
-> DynamicalExpression<Expr>
{ {
return DynamicalExpression<Expr>(std::make_shared<For<Index,Expr,FT::HIDDEN>>(mI,step,ex)); return DynamicalExpression<Expr>(std::make_shared<For<Index,Expr,FT::HIDDEN>>(mI,step,ex));
} }
/*********************** /***********************
* DynamicRange * * DynamicRange *
***********************/ ***********************/
@ -452,7 +410,24 @@ namespace MultiArrayTools
return std::dynamic_pointer_cast<MultiRange<Ranges...> >( mrf.create() ); return std::dynamic_pointer_cast<MultiRange<Ranges...> >( mrf.create() );
} }
template <class Expr>
auto DynamicIndex::ifor(size_t step, Expr ex) const
-> DynamicalExpression
{
//for...
ExpressionHolder<Expr> epxr(ex); // derived from ExpressionBase; just to resolve underlying types!
IVec[0].first->ifor( ... mIVec[i].first->ifor(expr) );
// ...
}
template <class Expr>
auto DynamicIndex::iforh(size_t step, Expr ex) const
-> DynamicalExpression
{
//for...
// ...
}
} }

View file

@ -15,22 +15,10 @@ 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> typedef std::pair<size_t*,size_t> DExt;
class ExpressionBase 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: public:
ExpressionBase() = default; ExpressionBase() = default;
@ -39,14 +27,12 @@ namespace MultiArrayHelper
ExpressionBase& operator=(const ExpressionBase& in) = default; ExpressionBase& operator=(const ExpressionBase& in) = default;
ExpressionBase& operator=(ExpressionBase&& in) = default; ExpressionBase& operator=(ExpressionBase&& in) = default;
ExpressionBase(ExtType ext, Expr&& expr); virtual void operator()(size_t mlast, DExt last) const = 0;
ExpressionBase(ExtType ext);
virtual void operator()(size_t mlast, ExtType last) const = 0;
virtual void operator()(size_t mlast = 0) const = 0; virtual void operator()(size_t mlast = 0) const = 0;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; virtual DExt dRootSteps(std::intptr_t iPtrNum = 0) const = 0;
auto extension() const -> ExtType; virtual DExt dExtension() const = 0;
}; };
@ -99,7 +85,7 @@ namespace MultiArrayHelper
}; };
template <class IndexClass, class Expr> template <class IndexClass, class Expr>
class SingleExpression : public ExpressionBase<Expr> class SingleExpression : public ExpressionBase
{ {
private: private:
SingleExpression() = default; SingleExpression() = default;
@ -108,10 +94,12 @@ namespace MultiArrayHelper
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; typedef ExpressionBase 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;
@ -128,13 +116,19 @@ namespace MultiArrayHelper
Expr expr); Expr expr);
inline void operator()(size_t mlast, ExtType last) const override final; inline void operator()(size_t mlast, DExt 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 override final;
DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final;
DExt dExtension() const override final;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
auto extension() const -> ExtType;
}; };
template <class IndexClass, class Expr, ForType FT> template <class IndexClass, class Expr, ForType FT>
class For : public ExpressionBase<Expr> class For : public ExpressionBase
{ {
private: private:
For() = default; For() = default;
@ -144,10 +138,14 @@ namespace MultiArrayHelper
size_t mMax; size_t mMax;
size_t mStep; size_t mStep;
Expr mExpr;
typedef decltype(mExpr.rootSteps()) ExtType;
ExtType mExt;
mutable ExtType mRootSteps;
public: public:
typedef ExpressionBase<Expr> EB; typedef ExpressionBase 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;
@ -163,9 +161,16 @@ 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, DExt 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 override final;
DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final;
DExt dExtension() const override final;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
auto extension() const -> ExtType;
}; };
template <size_t N> template <size_t N>
@ -174,18 +179,17 @@ 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
class DynamicalExpression : public ExpressionBase<Expr>
{ {
private: private:
DynamicalExpression() = default; DynamicalExpression() = default;
std::shared_ptr<ExpressionBase<Expr>> mNext; std::shared_ptr<ExpressionBase> mNext;
public: public:
typedef ExpressionBase<Expr> EB; typedef ExpressionBase EB;
using EB::rootSteps; using EB::rootSteps;
typedef typename ExpressionBase<Expr>::ExtType ExtType; typedef typename ExpressionBase::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;
@ -195,11 +199,14 @@ namespace MultiArrayHelper
DynamicalExpression& operator=(const DynamicalExpression& in) = default; DynamicalExpression& operator=(const DynamicalExpression& in) = default;
DynamicalExpression& operator=(DynamicalExpression&& in) = default; DynamicalExpression& operator=(DynamicalExpression&& in) = default;
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next); DynamicalExpression(const std::shared_ptr<ExpressionBase>& next);
inline void operator()(size_t mlast, ExtType last) const override final; inline void operator()(size_t mlast, DExt last) const override final;
inline void operator()(size_t mlast = 0) const override final; inline void operator()(size_t mlast = 0) const override final;
DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final;
DExt dExtension() const override final;
}; };
} // namespace MultiArrayHelper } // namespace MultiArrayHelper
@ -218,29 +225,16 @@ namespace MultiArrayHelper
**********************/ **********************/
template <class Expr> template <class Expr>
ExpressionBase<Expr>::ExpressionBase(ExtType ext, Expr&& expr) : ExpressionBase::ExpressionBase(ExtType ext, Expr&& expr) :
mExpr(std::forward<Expr>(expr)), mExpr(std::forward<Expr>(expr)),
mExt(ext) mExt(ext)
{} {}
template <class Expr> template <class Expr>
ExpressionBase<Expr>::ExpressionBase(ExtType ext) : ExpressionBase::ExpressionBase(ExtType ext) :
mExt(ext) 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 * * F o r *
@ -249,9 +243,8 @@ namespace MultiArrayHelper
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) :
ExpressionBase<Expr>(expr.rootSteps( reinterpret_cast<std::intptr_t>( indPtr.get() )), mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step),
std::forward<Expr>(expr)), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr )))
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step)
{ {
assert(mIndPtr != nullptr); assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id()); //VCHECK(mIndPtr->id());
@ -261,16 +254,20 @@ 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 ) ), mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step),
std::forward<Expr>(expr)), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr )))
mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()),
mStep(step)
{ {
assert(mIndPtr != nullptr); assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id()); //VCHECK(mIndPtr->id());
//VCHECK(mIndPtr->max()); //VCHECK(mIndPtr->max());
} }
template <class IndexClass, class Expr, ForType FT>
inline void For<IndexClass,Expr,FT>::operator()(size_t mlast, DExt last) const
{
operator()(mlast, *reinterpret_cast<ExtType*>(last));
}
template <class IndexClass, class Expr, ForType FT> template <class IndexClass, class Expr, ForType FT>
inline void For<IndexClass,Expr,FT>::operator()(size_t mlast, inline void For<IndexClass,Expr,FT>::operator()(size_t mlast,
ExtType last) const ExtType last) const
@ -300,6 +297,35 @@ namespace MultiArrayHelper
} }
template <class IndexClass, class Expr, ForType FT>
auto For<IndexClass,Expr,FT>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
{
return mExpr.rootSteps(iPtrNum);
}
template <class IndexClass, class Expr, ForType FT>
auto For<IndexClass,Expr,FT>::extension() const
-> ExtType
{
return mExt;
}
template <class IndexClass, class Expr, ForType FT>
DExt For<IndexClass,Expr,FT>::dRootSteps(std::intptr_t iPtrNum = 0) const
{
mRootSteps = rootSteps(iPtrNum);
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mRootSteps),
sizeof(ExtType)/sizeof(size_t));
}
template <class IndexClass, class Expr, ForType FT>
DExt For<IndexClass,Expr,FT>::dExtension() const
{
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mExt),
sizeof(ExtType)/sizeof(size_t));
}
/************************ /************************
* SingleExpression * * SingleExpression *
************************/ ************************/
@ -307,9 +333,8 @@ namespace MultiArrayHelper
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) :
ExpressionBase<Expr>(expr.rootSteps( reinterpret_cast<std::intptr_t>( indPtr.get() )), mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()),
std::forward<Expr>(expr)), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr )))
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max())
{ {
assert(mIndPtr != nullptr); assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id()); //VCHECK(mIndPtr->id());
@ -319,15 +344,20 @@ 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) :
ExpressionBase<Expr>(expr.rootSteps( reinterpret_cast<std::intptr_t>( indPtr ) ), mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()),
std::forward<Expr>(expr)), mExpr(expr), mExt(mExpr.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());
//VCHECK(mIndPtr->max()); //VCHECK(mIndPtr->max());
} }
template <class IndexClass, class Expr>
inline void SingleExpression<IndexClass,Expr>::operator()(size_t mlast, DExt last) const
{
operator()(mlast, *reinterpret_cast<ExtType*>(last));
}
template <class IndexClass, class Expr> template <class IndexClass, class Expr>
inline void SingleExpression<IndexClass,Expr>::operator()(size_t mlast, inline void SingleExpression<IndexClass,Expr>::operator()(size_t mlast,
ExtType last) const ExtType last) const
@ -350,6 +380,34 @@ namespace MultiArrayHelper
EB::mExpr(mlast, last); EB::mExpr(mlast, last);
} }
template <class IndexClass, class Expr>
auto SingleExpression<IndexClass,Expr>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
{
return mExpr.rootSteps(iPtrNum);
}
template <class IndexClass, class Expr>
auto SingleExpression<IndexClass,Expr>::extension() const
-> ExtType
{
return mExt;
}
template <class IndexClass, class Expr>
DExt SingleExpression<IndexClass,Expr>::dRootSteps(std::intptr_t iPtrNum = 0) const
{
mRootSteps = rootSteps(iPtrNum);
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mRootSteps),
sizeof(ExtType)/sizeof(size_t));
}
template <class IndexClass, class Expr>
DExt SingleExpression<IndexClass,Expr>::dExtension() const
{
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mExt),
sizeof(ExtType)/sizeof(size_t));
}
/*************************** /***************************
* DynamicalExpression * * DynamicalExpression *
@ -357,8 +415,7 @@ namespace MultiArrayHelper
template <class Expr> template <class Expr>
DynamicalExpression<Expr>:: DynamicalExpression<Expr>::
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next) : DynamicalExpression(const std::shared_ptr<ExpressionBase>& next) :
ExpressionBase<Expr>(next->extension()),
mNext(next) mNext(next)
{} {}
@ -374,6 +431,18 @@ namespace MultiArrayHelper
(*mNext)(mlast); (*mNext)(mlast);
} }
template <class Expr>
DExt DynamicalExpression<Expr>::dRootSteps(std::intptr_t iPtrNum = 0) const
{
return mNext->dRootSteps(iPtrNum);
}
template <class Expr>
DExt DynamicalExpression<Expr>::dExtension() const
{
return mNext->dExtension();
}
} // namespace MultiArrayHelper } // namespace MultiArrayHelper