From 9cf294f48506a0e1b432612695ac0d5241f1357a Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 27 Oct 2018 14:58:34 +0200 Subject: [PATCH] dynamic index compiles --- src/include/ranges/dynamic_range.h | 115 +++++++++--------- src/include/ranges/range_base.h | 2 +- src/include/ranges/rbase_def.h | 2 +- src/include/xfor/xfor.h | 185 +++++++++++++++++++---------- 4 files changed, 184 insertions(+), 120 deletions(-) diff --git a/src/include/ranges/dynamic_range.h b/src/include/ranges/dynamic_range.h index c98d474..31604e8 100644 --- a/src/include/ranges/dynamic_range.h +++ b/src/include/ranges/dynamic_range.h @@ -55,26 +55,28 @@ namespace MultiArrayTools virtual std::string stringMeta() const = 0; virtual DynamicMetaT meta() const = 0; virtual const DynamicMetaT* metaPtr() const = 0; - virtual IndexWrapperBase& at(const U& metaPos) = 0; - virtual size_t posAt(const U& metaPos) const = 0; + //virtual IndexWrapperBase& at(const U& metaPos) = 0; + //virtual size_t posAt(const U& metaPos) const = 0; //virtual bool isMeta(const U& metaPos) const = 0; - virtual size_t dim() = 0; - virtual bool last() = 0; - virtual bool first() = 0; + virtual size_t dim() const = 0; + virtual bool last() const = 0; + virtual bool first() const = 0; - virtual std::shared_ptr range() = 0; + virtual std::shared_ptr range() const = 0; - virtual size_t getStepSize(size_t n) = 0; + virtual size_t getStepSize(size_t n) const = 0; virtual std::intptr_t get() const = 0; + virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const = 0; + virtual DynamicalExpression iforh(size_t step, DynamicalExpression ex) const = 0; }; typedef IndexWrapperBase IndexW; - template + template class IndexWrapper : public IndexWrapperBase { protected: @@ -84,8 +86,6 @@ namespace MultiArrayTools std::shared_ptr mI; public: - static constexpr size_t EXPRMAX = IndexWrapperBase::EXPRMAX; - IndexWrapper(const IndexWrapper& in) = default; IndexWrapper(IndexWrapper&& in) = default; IndexWrapper& operator=(const IndexWrapper& in) = default; @@ -97,7 +97,7 @@ namespace MultiArrayTools virtual IndexWrapperBase& operator=(size_t pos) final { (*mI) = pos; return *this; } virtual IndexWrapperBase& operator++() final { ++(*mI); return *this; } - virtual IndexWrapperBase& operator--() final { --(*mi); return *this; } + virtual IndexWrapperBase& operator--() final { --(*mI); return *this; } virtual int pp(std::intptr_t idxPtrNum) final { return mI->pp(idxPtrNum); } virtual int mm(std::intptr_t idxPtrNum) final { return mI->mm(idxPtrNum); } @@ -105,27 +105,36 @@ namespace MultiArrayTools virtual std::string stringMeta() const final { return mI->stringMeta(); } virtual DynamicMetaT meta() const final { return DynamicMetaT(mI->meta()); } virtual const DynamicMetaT* metaPtr() const final { return nullptr; } - virtual IndexWrapperBase& at(const U& metaPos) final { mI->at(metaPos); return *this; } - virtual size_t posAt(const U& metaPos) const final { return mI->posAt(metaPos); } + IndexWrapperBase& at(const typename Index::MetaType& metaPos) { mI->at(metaPos); return *this; } + size_t posAt(const typename Index::MetaType& metaPos) const { return mI->posAt(metaPos); } //virtual bool isMeta(const U& metaPos) const final { return mI->isMeta(); } - virtual size_t dim() final { return mI->dim(); } - virtual bool last() final { return mI->last(); } - virtual bool first() final { return mI->first(); } + virtual size_t dim() const final { return mI->dim(); } + virtual bool last() const final { return mI->last(); } + virtual bool first() const final { return mI->first(); } - virtual std::shared_ptr range() final { return mI->range(); } + virtual std::shared_ptr range() const final { return mI->range(); } - virtual size_t getStepSize(size_t n) final { return mI->getStepSize(n); } + virtual size_t getStepSize(size_t n) const final { return mI->getStepSize(n); } virtual std::intptr_t get() const final { return reinterpret_cast(mI.get()); } - + + virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const final { return mI->ifor(step, ex); } + virtual DynamicalExpression iforh(size_t step, DynamicalExpression ex) const final { return mI->iforh(step, ex); } + }; + typedef SingleRange DynamicRange; + class DynamicIndex : public IndexInterface { private: - std::vector,size_t>> mIVec; + std::vector,size_t>> mIVec; + + inline DynamicalExpression mkFor(size_t i, size_t step, + DynamicalExpression ex, bool hidden = false) const; + public: typedef IndexInterface IB; typedef DynamicMetaT MetaType; @@ -173,14 +182,15 @@ namespace MultiArrayTools template auto ifor(size_t step, Expr ex) const - -> DynamicalExpression; + -> DynamicalExpression; template auto iforh(size_t step, Expr ex) const - -> DynamicalExpression; + -> DynamicalExpression; }; + // NOT THREAD SAVE!! class DynamicRangeFactory : public RangeFactoryBase { @@ -209,8 +219,9 @@ namespace MultiArrayTools bool mProductCreated = false; }; - - class DynamicRange : public RangeInterface + + template <> + class SingleRange : public RangeInterface { public: static constexpr bool defaultable = true; @@ -219,19 +230,19 @@ namespace MultiArrayTools static constexpr bool HASMETACONT = false; typedef RangeBase RB; - typedef typename RangeInterface::IndexType IndexType; + typedef DynamicIndex IndexType; typedef DynamicRange RangeType; typedef DynamicMetaT MetaType; private: - DynamicRange() = default; - DynamicRange(const DynamicRange& in) = default; + SingleRange() = default; + SingleRange(const SingleRange& in) = default; template - DynamicRange(const std::tuple...>& origs); + SingleRange(const std::tuple...>& origs); template - DynamicRange(std::shared_ptr... origs); + SingleRange(std::shared_ptr... origs); size_t mSize = 1; bool mEmpty = true; @@ -281,24 +292,6 @@ namespace MultiArrayTools namespace MultiArrayTools { - /************************* - * IndexForWrapper * - *************************/ - - template - auto IndexForWrapper::ifor(size_t step, Expr ex) - -> DynamicalExpression - { - return DynamicalExpression(std::make_shared>(mI,step,ex)); - } - - template - auto IndexForWrapper::iforh(size_t step, Expr ex) - -> DynamicalExpression - { - return DynamicalExpression(std::make_shared>(mI,step,ex)); - } - /*********************** * DynamicRange * @@ -333,7 +326,7 @@ namespace MultiArrayTools * Functions * *****************/ - std::shared_ptr defaultRange(size_t size = 0); + //std::shared_ptr defaultRange(size_t size = 0); } namespace MultiArrayHelper @@ -358,7 +351,7 @@ namespace MultiArrayHelper std::shared_ptr r) { if(not r->isEmpty()){ - for(size_t i = r->anonymousDim(); i != 0; --i){ + for(size_t i = r->dim(); i != 0; --i){ v.insert(v.begin(), r->sub(i-1)); } } @@ -409,24 +402,34 @@ namespace MultiArrayTools MultiRangeFactory mrf(rtp); return std::dynamic_pointer_cast >( mrf.create() ); } + + inline DynamicalExpression DynamicIndex::mkFor(size_t i, size_t step, + DynamicalExpression ex, bool hidden) const + { + if(i != 0){ + auto& ii = *mIVec[i].first; + return mkFor(i-1, step, hidden ? ii.iforh(step, ex) : ii.ifor(step, ex)); + } + else { + auto& ii = *mIVec[0].first; + return hidden ? ii.iforh(step, ex) : ii.ifor(step, ex); + } + } template auto DynamicIndex::ifor(size_t step, Expr ex) const -> DynamicalExpression { - //for... - ExpressionHolder epxr(ex); // derived from ExpressionBase; just to resolve underlying types! - IVec[0].first->ifor( ... mIVec[i].first->ifor(expr) ); - // ... + DynamicalExpression expr(std::make_shared(ex)); + return mkFor(mIVec.size()-1, step, expr); } template auto DynamicIndex::iforh(size_t step, Expr ex) const -> DynamicalExpression { - //for... - - // ... + DynamicalExpression expr(std::make_shared(ex)); + return mkFor(mIVec.size()-1, step, expr, true); } } diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 28b5dfc..5c9e0f3 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -27,7 +27,7 @@ namespace MultiArrayTools #include "range_types/header.h" #undef include_range_type ANON = -1, // anonymous content - DYN = -2 // dynamic content + DYN = -3 // dynamic content }; struct DataHeader diff --git a/src/include/ranges/rbase_def.h b/src/include/ranges/rbase_def.h index e364c17..b9f7d41 100644 --- a/src/include/ranges/rbase_def.h +++ b/src/include/ranges/rbase_def.h @@ -79,7 +79,7 @@ namespace MultiArrayTools class DynamicRangeFactory; // dynamic_range.h - class DynamicRange; + //class DynamicRange; // value_range.h template diff --git a/src/include/xfor/xfor.h b/src/include/xfor/xfor.h index 5c9ac9e..38b5db9 100644 --- a/src/include/xfor/xfor.h +++ b/src/include/xfor/xfor.h @@ -15,7 +15,7 @@ namespace MultiArrayHelper // 'HIDDEN FOR' CLASS for nested for loops in contractions a.s.o. // (NO COUNTING OF MASTER POSITION !!!!!) - typedef std::pair DExt; + typedef std::pair DExt; class ExpressionBase { @@ -84,6 +84,43 @@ namespace MultiArrayHelper } }; + template + class ExpressionHolder : public ExpressionBase + { + private: + ExpressionHolder() = default; + + Expr mExpr; + typedef decltype(mExpr.rootSteps()) ExtType; + ExtType mExt; + + mutable ExtType mRootSteps; + + public: + typedef ExpressionBase EB; + + static constexpr size_t LAYER = Expr::LAYER + 1; + static constexpr size_t SIZE = Expr::SIZE; + + ExpressionHolder(const ExpressionHolder& in) = default; + ExpressionHolder(ExpressionHolder&& in) = default; + ExpressionHolder& operator=(const ExpressionHolder& in) = default; + ExpressionHolder& operator=(ExpressionHolder&& in) = default; + + ExpressionHolder(Expr expr); + + 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; + + 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 SingleExpression : public ExpressionBase { @@ -98,6 +135,8 @@ namespace MultiArrayHelper typedef decltype(mExpr.rootSteps()) ExtType; ExtType mExt; + mutable ExtType mRootSteps; + public: typedef ExpressionBase EB; @@ -187,25 +226,21 @@ namespace MultiArrayHelper std::shared_ptr mNext; public: - typedef ExpressionBase EB; - using EB::rootSteps; - typedef typename ExpressionBase::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& next); + DynamicalExpression(const std::shared_ptr& next) : + mNext(next) + {} inline void operator()(size_t mlast, DExt last) 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; + inline DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; + inline DExt dExtension() const override final; }; @@ -220,22 +255,6 @@ namespace MultiArrayHelper namespace MultiArrayHelper { - /********************** - * ExpressionBase * - **********************/ - - template - ExpressionBase::ExpressionBase(ExtType ext, Expr&& expr) : - mExpr(std::forward(expr)), - mExt(ext) - {} - - template - ExpressionBase::ExpressionBase(ExtType ext) : - mExt(ext) - {} - - /***************** * F o r * *****************/ @@ -243,7 +262,7 @@ namespace MultiArrayHelper template For::For(const std::shared_ptr& indPtr, size_t step, Expr expr) : - mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), + mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast( mIndPtr ))) { assert(mIndPtr != nullptr); @@ -254,7 +273,7 @@ namespace MultiArrayHelper template For::For(const IndexClass* indPtr, size_t step, Expr expr) : - mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), + mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast( mIndPtr ))) { assert(mIndPtr != nullptr); @@ -265,7 +284,7 @@ namespace MultiArrayHelper template inline void For::operator()(size_t mlast, DExt last) const { - operator()(mlast, *reinterpret_cast(last)); + operator()(mlast, *reinterpret_cast(last.first)); } template @@ -277,8 +296,8 @@ namespace MultiArrayHelper //for(size_t pos = mSPos; pos != mMax; ++pos){ //const size_t mnpos = PosForward::value(mlast, mMax, pos); const size_t mnpos = PosForward::valuex(mlast, mStep, pos); - const ExtType npos = last + EB::mExt*pos; - EB::mExpr(mnpos, npos); + const ExtType npos = last + mExt*pos; + mExpr(mnpos, npos); } } @@ -291,8 +310,8 @@ namespace MultiArrayHelper //for(size_t pos = mSPos; pos != mMax; ++pos){ //const size_t mnpos = PosForward::value(mlast, mMax, pos); const size_t mnpos = PosForward::valuex(mlast, mStep, pos); - const ExtType npos = last + EB::mExt*pos; - EB::mExpr(mnpos, npos); + const ExtType npos = last + mExt*pos; + mExpr(mnpos, npos); } } @@ -312,18 +331,18 @@ namespace MultiArrayHelper } template - DExt For::dRootSteps(std::intptr_t iPtrNum = 0) const + DExt For::dRootSteps(std::intptr_t iPtrNum) const { mRootSteps = rootSteps(iPtrNum); - return std::make_pair(reinterpret_cast(&mRootSteps), - sizeof(ExtType)/sizeof(size_t)); + return std::make_pair(reinterpret_cast(&mRootSteps), + sizeof(ExtType)/sizeof(size_t)); } template DExt For::dExtension() const { - return std::make_pair(reinterpret_cast(&mExt), - sizeof(ExtType)/sizeof(size_t)); + return std::make_pair(reinterpret_cast(&mExt), + sizeof(ExtType)/sizeof(size_t)); } /************************ @@ -355,7 +374,7 @@ namespace MultiArrayHelper template inline void SingleExpression::operator()(size_t mlast, DExt last) const { - operator()(mlast, *reinterpret_cast(last)); + operator()(mlast, *reinterpret_cast(last.first)); } template @@ -365,8 +384,8 @@ namespace MultiArrayHelper //typedef typename IndexClass::RangeType RangeType; const size_t pos = mIndPtr->pos(); const size_t mnpos = PosForward::value(mlast, mMax, pos); - const ExtType npos = last + EB::mExt*pos; - EB::mExpr(mnpos, npos); + const ExtType npos = last + mExt*pos; + mExpr(mnpos, npos); } template @@ -376,8 +395,8 @@ namespace MultiArrayHelper const ExtType last; const size_t pos = mIndPtr->pos(); const size_t mnpos = PosForward::value(mlast, mMax, pos); - const ExtType npos = last + EB::mExt*pos; - EB::mExpr(mlast, last); + const ExtType npos = last + mExt*pos; + mExpr(mlast, last); } template @@ -395,54 +414,96 @@ namespace MultiArrayHelper } template - DExt SingleExpression::dRootSteps(std::intptr_t iPtrNum = 0) const + DExt SingleExpression::dRootSteps(std::intptr_t iPtrNum) const { mRootSteps = rootSteps(iPtrNum); - return std::make_pair(reinterpret_cast(&mRootSteps), - sizeof(ExtType)/sizeof(size_t)); + return std::make_pair(reinterpret_cast(&mRootSteps), + sizeof(ExtType)/sizeof(size_t)); } template DExt SingleExpression::dExtension() const { - return std::make_pair(reinterpret_cast(&mExt), - sizeof(ExtType)/sizeof(size_t)); + return std::make_pair(reinterpret_cast(&mExt), + sizeof(ExtType)/sizeof(size_t)); } /*************************** * DynamicalExpression * ***************************/ - template - DynamicalExpression:: - DynamicalExpression(const std::shared_ptr& next) : - mNext(next) - {} - - template - inline void DynamicalExpression::operator()(size_t mlast, ExtType last) const + inline void DynamicalExpression::operator()(size_t mlast, DExt last) const { (*mNext)(mlast,last); } - template - inline void DynamicalExpression::operator()(size_t mlast) const + inline void DynamicalExpression::operator()(size_t mlast) const { (*mNext)(mlast); } - template - DExt DynamicalExpression::dRootSteps(std::intptr_t iPtrNum = 0) const + inline DExt DynamicalExpression::dRootSteps(std::intptr_t iPtrNum) const { return mNext->dRootSteps(iPtrNum); } - template - DExt DynamicalExpression::dExtension() const + inline DExt DynamicalExpression::dExtension() const { return mNext->dExtension(); } + /************************ + * ExpressionHolder * + ************************/ + + template + ExpressionHolder::ExpressionHolder(Expr expr) : mExpr(expr) {} + + template + inline void ExpressionHolder::operator()(size_t mlast, DExt last) const + { + mExpr(mlast,last); + } + + template + inline void ExpressionHolder::operator()(size_t mlast, ExtType last) const + { + mExpr(mlast,last); + } + + template + inline void ExpressionHolder::operator()(size_t mlast) const + { + mExpr(mlast); + } + + template + DExt ExpressionHolder::dRootSteps(std::intptr_t iPtrNum) const + { + return mExpr.dRootSteps(iPtrNum); + } + + template + DExt ExpressionHolder::dExtension() const + { + return mExpr.dExtension(); + } + + template + auto ExpressionHolder::rootSteps(std::intptr_t iPtrNum) const + -> ExtType + { + return mExpr.rootSteps(iPtrNum); + } + + template + auto ExpressionHolder::extension() const + -> ExtType + { + return mExpr.extension(); + } + + } // namespace MultiArrayHelper