From bcd875f7d24d809ade5d829b32044dd39e9ebd03 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 8 Aug 2018 18:20:08 +0200 Subject: [PATCH] again devel of map range + usage of std::map single index --- src/include/{ranges => }/map_range.h | 141 ++++++++++++++++++++++----- src/include/pack_num.h | 14 +++ src/include/ranges/single_range.h | 18 ++-- 3 files changed, 141 insertions(+), 32 deletions(-) rename src/include/{ranges => }/map_range.h (81%) diff --git a/src/include/ranges/map_range.h b/src/include/map_range.h similarity index 81% rename from src/include/ranges/map_range.h rename to src/include/map_range.h index ca9d473..bfad766 100644 --- a/src/include/ranges/map_range.h +++ b/src/include/map_range.h @@ -25,6 +25,52 @@ namespace MultiArrayTools { using namespace MultiArrayHelper; } + + template + auto mkMapOp(const MA& ma, + const std::tuple...>& itp) + -> ConstOperation + { + return PackNum::mkMapOp(ma, itp); + } + + template + class OpExpr + { + public: + typedef SingleIndex OIType; + typedef MapF::IndexPack IndexPack; + static constexpr size_t LAYER = Expr::LAYER + 1; + static constexpr size_t SIZE = Expr::SIZE; + + private: + OpExpr() = default; + + const IndexClass* mIndPtr; + size_t mSPos; + size_t mMax; + Expr mExpr; + + typedef decltype(mOp.rootSteps(iPtrNum).extend( mExpr.rootSteps(iPtrNum) )) ExtType; + ExtType mExt; + + typedef decltype(mkMapOp(std::declval(), std::declval())) Op; + Op mOp; + + public: + OpExpr(const OpExpr& in) = default; + OpExpr(OpExpr&& in) = default; + OpExpr& operator=(const OpExpr& in) = default; + OpExpr& operator=(OpExpr&& in) = default; + + OpExpr(const MapF& mapf, const IndexPack& ipack, const std::shared_ptr oind, Expr ex); + + inline void operator()(size_t mlast, ExtType last) const; + inline void operator()(size_t mlast = 0) const; + + auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; + + }; template class MapIndex : public IndexInterface, @@ -117,7 +163,7 @@ namespace MultiArrayTools template auto ifor(Exprs exs) const - -> decltype(RPackNum::mkFor(mIPack, SinleExpression( mOutIndex, exs ) )); + -> decltype(RPackNum::mkFor(mIPack, OpExpr( range()->map() , mOutIndex, exs ) )); /* template @@ -145,12 +191,9 @@ namespace MultiArrayTools typedef MapRange oType; MapRangeFactory() = delete; - MapRangeFactory(const std::shared_ptr&... rs); - MapRangeFactory(const typename MapRange::Space& space); + MapRangeFactory(const MapF& mapf, const std::shared_ptr&... rs); + MapRangeFactory(const MapF& mapf, const typename MapRange::Space& space); - template - MapRangeFactory(const std::shared_ptr >& cr); - virtual std::shared_ptr create() override; private: @@ -184,6 +227,7 @@ namespace MultiArrayTools MapRange(const MapF& mapf, const Space& space); Space mSpace; + const MapF& mMapf; shred_ptr mOutRange; public: @@ -197,6 +241,7 @@ namespace MultiArrayTools auto getPtr() const -> decltype( std::get( mSpace ) )&; shred_ptr outRange() const; + const MapF& map() const; virtual size_t dim() const final; virtual size_t size() const final; @@ -237,6 +282,52 @@ namespace MultiArrayTools using namespace MultiArrayHelper; } + /************** + * OpExpr * + **************/ + + template + OpExpr::OpExpr(const MapF& mapf, const IndexPack& ipack, + const std::shared_ptr oind, Expr ex) : + mIndPtr(oind.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mExpr(expr), + mExt(expr.rootSteps( reinterpret_cast( mIndPtr.get() ))), + mOp(mapf, ipack) + { + assert(mIndPtr != nullptr); + //VCHECK(mIndPtr->id()); + //VCHECK(mIndPtr->max()); + } + + template + inline void OpExpr::operator()(size_t mlast, + ExtType last) const + { + const ExtType npos = last + mExt*pos; + constexpr size_t NEXT = Op::SIZE; + const size_t pos = mIndPtr->getMeta( mOp.get( npos ) ); + const size_t mnpos = PosForward::value(mlast, mMax, pos); + mExpr(mnpos, Getter::template getX( npos ) ); + } + + template + inline void OpExpr::operator()(size_t mlast) const + { + const ExtType last; + const ExtType npos = last + mExt*pos; + constexpr size_t NEXT = Op::SIZE; + const size_t pos = mIndPtr->at( mOp.get( npos ) ).pos(); + const size_t mnpos = PosForward::value(mlast, mMax, pos); + mExpr(mnpos, Getter::template getX( npos )); + } + + template + auto OpExpr::rootSteps(std::intptr_t iPtrNum) const + -> ExtType + { + return mOp.rootSteps(iPtrNum).extend( mExpr.rootSteps(iPtrNum) ); + } + + // -> define in range_base.cc //std::shared_ptr mkMULTI(const char** dp); @@ -454,11 +545,12 @@ namespace MultiArrayTools template template auto MapIndex::ifor(Exprs exs) const - -> decltype(RPackNum::mkFor(mIPack, exs)) + -> decltype(RPackNum::mkFor + (mIPack, OpExpr( range()->map(), mIPack, mOutIndex, exs ) ) ) { - return RPackNum::mkFor(mIPack, exs); + return RPackNum::mkFor(mIPack, OpExpr( range()->map(), mIPack, mOutIndex, exs ) ); } - + /* template template auto MapIndex::iforh(Exprs exs) const @@ -466,28 +558,21 @@ namespace MultiArrayTools { return RPackNum::mkForh(mIPack, exs); } - + */ /************************* * MapRangeFactory * *************************/ template - MapRangeFactory::MapRangeFactory(const std::shared_ptr&... rs) + MapRangeFactory::MapRangeFactory(const MapF& mapf, const std::shared_ptr&... rs) { - mProd = std::shared_ptr< MapRange >( new MapRange( rs... ) ); + mProd = std::shared_ptr< MapRange >( new MapRange( mapf, rs... ) ); } template - MapRangeFactory::MapRangeFactory(const typename MapRange::Space& st) + MapRangeFactory::MapRangeFactory(const MapF& mapf, const typename MapRange::Space& st) { - mProd = std::shared_ptr< MapRange >( new MapRange( st ) ); - } - - template - template - MapRangeFactory::MapRangeFactory(const std::shared_ptr >& cr) - { - mProd = std::shared_ptr< MapRange >( new MapRange( cr->space() ) ); + mProd = std::shared_ptr< MapRange >( new MapRange( mapf, st ) ); } template @@ -526,7 +611,9 @@ namespace MultiArrayTools ******************/ template - MapRange::MapRange(const std::shared_ptr&... rs) : mSpace(std::make_tuple(rs...)) + MapRange::MapRange(const MapF& mapf, const std::shared_ptr&... rs) : + mSpace(std::make_tuple(rs...)), + mMapf(mapf) { std::vector outmeta; // set !!!! @@ -535,7 +622,9 @@ namespace MultiArrayTools } template - MapRange::MapRange(const Space& space) : mSpace( space ) + MapRange::MapRange(const MapF& mapf, const Space& space) : + mSpace( space ), + mMapf(mapf) { std::vector outmeta; // set !!!! @@ -562,6 +651,12 @@ namespace MultiArrayTools { return mOutRange; } + + template + const MapF& map() const + { + return mMapf; + } template size_t MapRange::dim() const diff --git a/src/include/pack_num.h b/src/include/pack_num.h index bb38001..d25b26e 100644 --- a/src/include/pack_num.h +++ b/src/include/pack_num.h @@ -107,6 +107,13 @@ namespace MultiArrayHelper { return std::get(rtp1).get() == std::get(rtp2).get() and PackNum::checkIfSameInstance(rtp1,rtp2); } + + template + static inline auto mkMapOp(const MA& ma, const ITuple& itp, const std::shared_ptr&... inds) + -> decltype(PackNum::mkMapOp(ma, itp, std::get(itp), inds...)) + { + return PackNum::mkMapOp(ma, itp, std::get(itp), inds...); + } }; template<> @@ -178,6 +185,13 @@ namespace MultiArrayHelper return std::get<0>(rtp1).get() == std::get<0>(rtp2).get(); } + template + static inline auto mkMapOp(const MA& ma, const ITuple& itp, const std::shared_ptr&... inds) + -> decltype(ma.exec(std::get<0>(itp), inds...)) + { + return ma.exec(std::get<0>(itp), inds...); + } + }; diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index 9dca047..c865fcf 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -6,6 +6,7 @@ #include #include #include +#include //#include "base_def.h" //#include "ranges/rpack_num.h" @@ -136,6 +137,7 @@ namespace MultiArrayTools SingleRange(const std::vector& space); std::vector mSpace; + std::map mMSpace; }; } @@ -349,7 +351,12 @@ namespace MultiArrayTools template SingleRange::SingleRange(const std::vector& space) : RangeInterface >(), - mSpace(space) {} + mSpace(space) + { + for(size_t i = 0; i != mSpace.size(); ++i){ + mMSpace[mSpace[i]] = i; + } + } template const U& SingleRange::get(size_t pos) const @@ -360,14 +367,7 @@ namespace MultiArrayTools template size_t SingleRange::getMeta(const U& metaPos) const { - size_t cnt = 0; - for(auto& x: mSpace){ - if(x == metaPos){ - return cnt; - } - ++cnt; - } - return cnt; + return mMSpace.at(metaPos); } template