From 38e011c9797df97185cb70491612910918f6316b Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 4 Dec 2022 03:17:29 +0100 Subject: [PATCH] first tests on index multiplication --- src/include/ranges/dindex.h | 3 +++ src/include/ranges/mrange.cc.h | 8 +++++--- src/include/ranges/range_base.h | 1 + src/include/ranges/xindex.cc.h | 4 ++-- src/include/ranges/xindex.h | 4 ++-- src/include/ranges/yrange.h | 4 ++-- src/lib/ranges/dindex.cc | 9 ++++++++- src/lib/ranges/yrange.cc | 12 +++++++----- src/tests/darray_unit_test.cc | 1 - src/tests/range_unit_test.cc | 20 ++++++++++++++++++++ 10 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index be0e785..05a1273 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -13,6 +13,8 @@ namespace CNORXZ { public: typedef IndexInterface IB; + typedef DType MetaType; + typedef RangeBase RangeType; DEFAULT_C(DIndex); DIndex(const DIndex& i); @@ -20,6 +22,7 @@ namespace CNORXZ DIndex& operator=(const DIndex& i); DIndex& operator=(DIndex&& i); DIndex(const XIndexPtr& i); + DIndex(const RangePtr& r, SizeT lexpos = 0); template DIndex(const IndexInterface& i); diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 10df5fc..cc00fda 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -120,10 +120,12 @@ namespace CNORXZ constexpr decltype(auto) GMIndex::mkIFor(const Xpr& xpr, F&& f) const { if constexpr(I == sizeof...(Indices)-1){ - return std::get(mIPack)->ifor(xpr,f); + return std::get(mIPack)->ifor(xpr,std::forward(f)); } else { - return std::get(mIPack)->ifor( mkIFor( xpr, f ), f ); + auto f1 = f; + auto f2 = f1; + return std::get(mIPack)->ifor( mkIFor( xpr, std::move(f1) ), std::move(f2) ); } } @@ -408,7 +410,7 @@ namespace CNORXZ template constexpr decltype(auto) GMIndex::ifor(const Xpr& xpr, F&& f) const { - return mkIFor<0>(xpr, f); + return mkIFor<0>(xpr, std::forward(f)); } template diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 96c4a29..15ca474 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -42,6 +42,7 @@ namespace CNORXZ class RangeBase { public: + typedef DIndex IndexType; virtual ~RangeBase() = default; diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index c0c829f..4b7e748 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -149,9 +149,9 @@ namespace CNORXZ template DXpr XIndex::ifor(const DXpr& xpr, - const std::function& f) const + std::function&& f) const { - return DXpr(mI->ifor(xpr, f)); + return DXpr(mI->ifor(xpr, std::forward>(f))); } } diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index f8f1837..582be8e 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -41,7 +41,7 @@ namespace CNORXZ virtual XIndexBase& at(const DType& meta) = 0; virtual DXpr ifor(const DXpr& xpr, - const std::function& f) const = 0; + std::function&& f) const = 0; }; //Sptr& operator++(Sptr& i); @@ -89,7 +89,7 @@ namespace CNORXZ virtual XIndexBase& at(const DType& meta) override final; virtual DXpr ifor(const DXpr& xpr, - const std::function& f) const override final; + std::function&& f) const override final; private: IndexPtr mI; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index f7a40fa..8d66e45 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -51,7 +51,7 @@ namespace CNORXZ DType meta() const; YIndex& at(const DType& meta); - DXpr ifor(const DXpr& xpr, const std::function& f) const; + DXpr ifor(const DXpr& xpr, std::function&& f) const; YIndex& operator()(const Sptr& i); YIndex& operator()(); @@ -69,7 +69,7 @@ namespace CNORXZ inline void up(SizeT i); inline void down(SizeT i); inline decltype(auto) mkIFor(SizeT i, const DXpr& xpr, - const std::function& f) const; + std::function&& f) const; inline SizeT mkPMax() const; inline SizeT mkLMax() const; diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc index 5aa2f9d..a974454 100644 --- a/src/lib/ranges/dindex.cc +++ b/src/lib/ranges/dindex.cc @@ -36,7 +36,14 @@ namespace CNORXZ IndexInterface(i->pos()), mI(i) {} - + + DIndex::DIndex(const RangePtr& r, SizeT lexpos) : + IndexInterface(0) + { + *this = r->begin(); + *this = lexpos; + } + DIndex& DIndex::operator=(SizeT lexpos) { *mI = lexpos; diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index a35bd6b..b40adf4 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -92,13 +92,15 @@ namespace CNORXZ } inline decltype(auto) YIndex::mkIFor(SizeT i, const DXpr& xpr, - const std::function& f) const + std::function&& f) const { if(i == mIs.size()-1){ - return mIs[i]->ifor( xpr, f ); + return mIs[i]->ifor( xpr, std::forward>(f) ); } else { - return mIs[i]->ifor( mkIFor(i+1, xpr, f), f ); + auto f1 = f; + auto f2 = f1; + return mIs[i]->ifor( mkIFor(i+1, xpr, std::move(f1)), std::move(f2) ); } } @@ -325,9 +327,9 @@ namespace CNORXZ return *this; } - DXpr YIndex::ifor(const DXpr& xpr, const std::function& f) const + DXpr YIndex::ifor(const DXpr& xpr, std::function&& f) const { - return mkIFor(0, xpr, f); + return mkIFor(0, xpr, std::forward>(f)); } YIndex& YIndex::operator()(const Sptr& i) diff --git a/src/tests/darray_unit_test.cc b/src/tests/darray_unit_test.cc index a3b7345..08333a9 100644 --- a/src/tests/darray_unit_test.cc +++ b/src/tests/darray_unit_test.cc @@ -77,7 +77,6 @@ namespace for(auto ci = cr1x->begin(); ci != cr1x->end(); ++ci){ for(auto ui = ur1x->begin(); ui != ur1x->end(); ++ui){ const SizeT p = ci.lex()*ssize + ui.lex(); - EXPECT_EQ((ci*ui).lex(), p); // test this in rutest!!! EXPECT_EQ( a[ci*ui], a.data()[p] ); } } diff --git a/src/tests/range_unit_test.cc b/src/tests/range_unit_test.cc index add59a0..121d7e9 100644 --- a/src/tests/range_unit_test.cc +++ b/src/tests/range_unit_test.cc @@ -409,6 +409,26 @@ namespace EXPECT_EQ(yi.lex(), 0u); EXPECT_EQ(yi.pos(), 0u); } + + TEST_F(YR_Test, IndexMultiplication) + { + const SizeT ssize = mMeta.size(); + auto crx = std::dynamic_pointer_cast(cr); + auto urx = std::dynamic_pointer_cast>(ur); + for(auto cix = crx->begin(); cix != crx->end(); ++cix){ + for(auto uix = urx->begin(); uix != urx->end(); ++uix){ + const SizeT p = cix.lex()*ssize + uix.lex(); + EXPECT_EQ((cix*uix).lex(), p); + } + } + for(auto ci = cr->begin(); ci != cr->end(); ++ci){ + for(auto ui = ur->begin(); ui != ur->end(); ++ui){ + const SizeT p = ci.lex()*ssize + ui.lex(); + EXPECT_EQ((ci*ui).lex(), p); + } + } + } + // RCast_Test }