From 6326b32d6a3dbd16fc70616b74dcb627c2ebcc5f Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 3 Apr 2019 16:26:53 +0200 Subject: [PATCH] generalize map range --- src/include/map_range.cc.h | 46 ++++++++++++++++++++++---------------- src/include/map_range.h | 25 ++++++++++++--------- src/tests/op_unit_test.cc | 9 ++++---- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/include/map_range.cc.h b/src/include/map_range.cc.h index 8b69fd3..bc9c0a7 100644 --- a/src/include/map_range.cc.h +++ b/src/include/map_range.cc.h @@ -14,35 +14,33 @@ namespace MultiArrayTools * OpExpr * **************/ - template - OpExpr::OpExpr(const Op& mapf, const std::shared_ptr& oind, + template + OpExpr::OpExpr(const Op& mapf, const Index* ind, size_t step, Expr ex) : - mIndPtr(oind.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), + mIndPtr(ind), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr( ex ), mOp(mapf), //mExt(ex.rootSteps( reinterpret_cast( mIndPtr ))) mExt( mOp.rootSteps( reinterpret_cast( mIndPtr ) ).extend ( ex.rootSteps( reinterpret_cast( mIndPtr ) ) ) ) { - assert(mIndPtr != nullptr); + assert(mIndPtr != nullptr); } - template - inline void OpExpr::operator()(size_t mlast, + template + inline void OpExpr::operator()(size_t mlast, ExtType last) { constexpr size_t NEXT = Op::SIZE; const ExtType nxpos = last; const size_t pos = mIndPtr->posAt( mOp.get( nxpos ) ); - //VCHECK(pos); const ExtType npos = last + mExt*pos; - //VCHECK(npos.next().next().val()); const size_t mnpos = PosForward::valuex(mlast, mStep, pos); mExpr(mnpos, Getter::template getX( npos ) ); } - template - inline void OpExpr::operator()(size_t mlast) + template + inline void OpExpr::operator()(size_t mlast) { const ExtType last; constexpr size_t NEXT = Op::SIZE; @@ -53,8 +51,8 @@ namespace MultiArrayTools mExpr(mnpos, Getter::template getX( npos )); } - template - auto OpExpr::rootSteps(std::intptr_t iPtrNum) const + template + auto OpExpr::rootSteps(std::intptr_t iPtrNum) const -> ExtType { return mOp.rootSteps(iPtrNum).extend( mExpr.rootSteps(iPtrNum) ); @@ -142,7 +140,7 @@ namespace MultiArrayTools } template - GenMapIndex& GenMapIndex::operator()(std::shared_ptr&... indices) + GenMapIndex& GenMapIndex::operator()(const std::shared_ptr&... indices) { RPackNum::swapIndices(mIPack, indices...); RPackNum::setIndexPack(mIPack, IB::mPos); @@ -227,6 +225,12 @@ namespace MultiArrayTools return *this; } + template + size_t GenMapIndex::posAt(const MetaType& metaPos) const + { + return range()->outRange()->getMeta(metaPos); + } + template size_t GenMapIndex::dim() const { @@ -291,12 +295,12 @@ namespace MultiArrayTools template auto GenMapIndex::ifor(size_t step, Exprs exs) const -> decltype(RPackNum::mkForh - (step, mIPack, mBlockSizes, OpExpr - ( range()->map(), mOutIndex, step, exs ) ) ) + (step, mIPack, mBlockSizes, OpExpr,Exprs,XSTYPE> + ( range()->map(), this, step, exs ) ) ) { return RPackNum::mkForh - (step, mIPack, mBlockSizes, OpExpr - ( range()->map(), mOutIndex, step, exs ) ); + (0, mIPack, mBlockSizes, OpExpr,Exprs,XSTYPE> + ( range()->map(), this, step, exs ) ); } template @@ -598,11 +602,15 @@ namespace MultiArrayTools template auto GenMapRange::explMapMultiplicity() const - -> MultiArray + -> ConstSlice { - auto tmp = mMapMult; + /* + auto tmp = mMapMult; return tmp.format( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis )) ); + */ + return mMapMult.slformat(std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ))); } template diff --git a/src/include/map_range.h b/src/include/map_range.h index 237d07c..68307a6 100644 --- a/src/include/map_range.h +++ b/src/include/map_range.h @@ -48,19 +48,21 @@ namespace MultiArrayTools - template + template //template class OpExpr { public: - typedef SingleIndex OIType; + typedef typename Index::OIType OIType; + //typedef SingleIndex OIType; static constexpr size_t LAYER = Expr::LAYER + 1; static constexpr size_t SIZE = Expr::SIZE + Op::SIZE; private: OpExpr() = default; - - const OIType* mIndPtr; + + const Index* mIndPtr; + //const OIType* mIndPtr; size_t mSPos; size_t mMax; size_t mStep; @@ -76,7 +78,7 @@ namespace MultiArrayTools OpExpr& operator=(const OpExpr& in) = default; OpExpr& operator=(OpExpr&& in) = default; - OpExpr(const Op& mapf, const std::shared_ptr& oind, size_t step, Expr ex); + OpExpr(const Op& mapf, const Index* ind, size_t step, Expr ex); inline void operator()(size_t mlast, ExtType last); inline void operator()(size_t mlast = 0); @@ -102,7 +104,7 @@ namespace MultiArrayTools typedef GenMapIndex IType; typedef SingleIndex OIType; - static constexpr IndexType sType() { return IndexType::MULTI; } + static constexpr IndexType sType() { return IndexType::SINGLE; } static constexpr size_t sDim() { return sizeof...(Indices); } static constexpr size_t totalDim() { return mkTotalDim(); } @@ -150,7 +152,7 @@ namespace MultiArrayTools // MultiIndices CANNOT be influences be its subindices, so there is // NO foreign/external controll) // Do NOT share index instances between two or more MapIndex instances - GenMapIndex& operator()(std::shared_ptr&... indices); + GenMapIndex& operator()(const std::shared_ptr&... indices); // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== @@ -167,7 +169,8 @@ namespace MultiArrayTools std::string stringMeta() const; MetaType meta() const; GenMapIndex& at(const MetaType& metaPos); - + size_t posAt(const MetaType& metaPos) const; + size_t dim() const; bool first() const; bool last() const; @@ -184,7 +187,7 @@ namespace MultiArrayTools template auto ifor(size_t step, Exprs exs) const -> decltype(RPackNum::mkForh - (step, mIPack, mBlockSizes, OpExpr( range()->map(), mOutIndex, step, exs ) ) ); + (step, mIPack, mBlockSizes, OpExpr( range()->map(), this, step, exs ) ) ); // first step arg not used! template @@ -276,7 +279,7 @@ namespace MultiArrayTools template auto getPtr() const -> decltype( std::get( mSpace ) )&; - std::shared_ptr outRange() const; + std::shared_ptr outRange() const; const Op& map() const; virtual size_t dim() const final; @@ -297,7 +300,7 @@ namespace MultiArrayTools virtual IndexType end() const final; const MultiArray& mapMultiplicity() const; - MultiArray explMapMultiplicity() const; + ConstSlice explMapMultiplicity() const; vector mapPos() const; diff --git a/src/tests/op_unit_test.cc b/src/tests/op_unit_test.cc index fc6a5b8..2e56ffa 100644 --- a/src/tests/op_unit_test.cc +++ b/src/tests/op_unit_test.cc @@ -376,9 +376,8 @@ namespace { ///auto jj = mkMapI( std::make_shared >(), ii1, ii1 ); res(jj) += ma1(ii1,ii2); - auto mult = mr->mapMultiplicity(); - auto jjx = jj->outIndex(); - res2(jj) += ma1(ii1,ii2) / staticcast( mult(jjx) ); + auto mult = mr->explMapMultiplicity(); + res2(jj) += ma1(ii1,ii2) / staticcast( mult(jj) ); MultiArray form = res.format(mpr1ptr->outRange()); MultiArray form2 = res2.format(mpr1ptr->outRange()); @@ -386,7 +385,7 @@ namespace { EXPECT_EQ( jj->range()->outRange()->size(), static_cast( 10 ) ); EXPECT_EQ( jj->range()->mapMultiplicity().at(9), static_cast( 3 ) ); EXPECT_EQ( jj->range()->mapMultiplicity().at(3), static_cast( 1 ) ); - + EXPECT_EQ( form.at(3), -31.71 ); EXPECT_EQ( form.at(7), -77.16 ); EXPECT_EQ( form.at(9), -18.81 + 72.31 -50.91 ); @@ -397,7 +396,7 @@ namespace { EXPECT_EQ( form.at(12), -42.53 ); EXPECT_EQ( form.at(16), 80.41 ); EXPECT_EQ( form.at(18), 6.35 ); - + EXPECT_EQ( form2.at(3), -31.71 ); EXPECT_EQ( form2.at(7), -77.16 ); EXPECT_EQ( xround( form2.at(9) ), xround( (-18.81 + 72.31 -50.91) / 3. ) );