From eb5276c967b4ba4188eba8dd69ca351bcf76d7a3 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 22 Oct 2023 01:33:54 +0200 Subject: [PATCH] tests and fix bugs regarding prange --- src/include/ranges/crange.h | 2 +- src/include/ranges/mrange.cc.h | 4 +- src/include/ranges/mrange.h | 2 +- src/include/ranges/prange.cc.h | 27 +++++++----- src/include/ranges/prange.h | 2 +- src/include/ranges/srange.cc.h | 8 ++-- src/include/ranges/srange.h | 2 +- src/include/ranges/urange.cc.h | 8 ++-- src/include/ranges/urange.h | 2 +- src/include/ranges/xindex.cc.h | 4 +- src/include/ranges/xindex.h | 4 +- src/include/ranges/yrange.h | 2 +- src/lib/ranges/crange.cc | 9 ++-- src/lib/ranges/yrange.cc | 8 ++-- src/tests/range_unit_test.cc | 81 +++++++++++++++++++++++++++++++++- 15 files changed, 125 insertions(+), 40 deletions(-) diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index 430a507..3252c54 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -47,7 +47,7 @@ namespace CNORXZ CIndex& at(const SizeT& metaPos); COpRoot xpr(const Sptr& _this) const; - RangePtr prange(const CIndex& end) const; + RangePtr prange(const CIndex& last) const; SizeT deepFormat() const; /* diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 28732bb..4278acc 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -480,12 +480,12 @@ namespace CNORXZ } template - RangePtr GMIndex::prange(const MIndex& end) const + RangePtr GMIndex::prange(const MIndex& last) const { return iter<0,NI> ( [&](auto i) { typedef typename std::remove_reference::type::RangeType RangeT; - return std::dynamic_pointer_cast( mIPack[i]->prange( *end.pack()[i] ) ); + return std::dynamic_pointer_cast( mIPack[i]->prange( *last.pack()[i] ) ); }, [](const auto&... e) { return mrange(e...); } ); diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index 051afb1..17abf67 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -81,7 +81,7 @@ namespace CNORXZ const SPack& pack() const; const auto& format() const; const auto& lexFormat() const; - RangePtr prange(const MIndex& end) const; + RangePtr prange(const MIndex& last) const; auto deepFormat() const; GMIndex& setFormat(const FormatT& bs); diff --git a/src/include/ranges/prange.cc.h b/src/include/ranges/prange.cc.h index 021e2cb..239375d 100644 --- a/src/include/ranges/prange.cc.h +++ b/src/include/ranges/prange.cc.h @@ -13,9 +13,13 @@ namespace CNORXZ template PIndex::PIndex(const RangePtr& range, SizeT pos) : IndexInterface,typename IndexT::MetaType>(pos), - mRangePtr(rangeCast(range)), - mOrig(std::make_shared(mRangePtr->orig(),mRangePtr->parts()[pos])) - {} + mRangePtr(rangeCast(range))//, + //mOrig(std::make_shared(mRangePtr->orig(),mRangePtr->parts()[pos])) + { + auto o = mRangePtr->orig(); + auto p = mRangePtr->parts()[pos]; + mOrig = std::make_shared(o,p); + } template PIndex& PIndex::operator=(SizeT lexpos) @@ -124,14 +128,14 @@ namespace CNORXZ } template - RangePtr PIndex::prange(const PIndex& end) const + RangePtr PIndex::prange(const PIndex& last) const { - CXZ_ASSERT(end > *this, "got end index position smaller than begin index position"); + CXZ_ASSERT(last > *this, "got last index position smaller than begin index position"); auto oi = *orig(); - auto oend = *end.orig(); + auto olast = *last.orig(); const SizeT beginPos = oi.lex(); - Vector parts(oend.lex()-beginPos); - for(auto i = oi; i != oend; ++i){ + Vector parts(olast.lex()-beginPos+1); + for(auto i = oi; i != olast+1; ++i){ parts[i.lex()-beginPos] = i.lex(); } return CNORXZ::prange(mRangePtr->orig(), parts); @@ -280,7 +284,7 @@ namespace CNORXZ template SizeT PRange::size() const { - return mParts.size(); + return mParts.size()-1; } template @@ -345,7 +349,10 @@ namespace CNORXZ template PRange::PRange(const Sptr& range, const Vector& _parts) : mRange(range), mParts(_parts) - {} + { + const auto max = std::max_element( mParts.begin(), mParts.end() ); + mParts.push_back( *max ); + } template Vector PRange::key() const diff --git a/src/include/ranges/prange.h b/src/include/ranges/prange.h index fab6ca6..a8d6ee1 100644 --- a/src/include/ranges/prange.h +++ b/src/include/ranges/prange.h @@ -41,7 +41,7 @@ namespace CNORXZ template UPos stepSize(const IndexId& id) const; - RangePtr prange(const PIndex& end) const; + RangePtr prange(const PIndex& last) const; decltype(auto) deepFormat() const; String stringMeta() const; diff --git a/src/include/ranges/srange.cc.h b/src/include/ranges/srange.cc.h index 0f72b9e..a974b50 100644 --- a/src/include/ranges/srange.cc.h +++ b/src/include/ranges/srange.cc.h @@ -143,12 +143,12 @@ namespace CNORXZ } template - RangePtr SIndex::prange(const SIndex& end) const + RangePtr SIndex::prange(const SIndex& last) const { - CXZ_ASSERT(end > *this, "got end index position smaller than begin index position"); + CXZ_ASSERT(last > *this, "got last index position smaller than begin index position"); const SizeT beginPos = lex(); - Vector parts(end.lex()-beginPos); - for(auto i = *this; i != end; ++i){ + Vector parts(last.lex()-beginPos+1); + for(auto i = *this; i != last+1; ++i){ parts[i.lex()-beginPos] = i.lex(); } return CNORXZ::prange(mRangePtr, parts); diff --git a/src/include/ranges/srange.h b/src/include/ranges/srange.h index 5757ac3..1ab85d1 100644 --- a/src/include/ranges/srange.h +++ b/src/include/ranges/srange.h @@ -48,7 +48,7 @@ namespace CNORXZ SIndex& at(const MetaT& metaPos); decltype(auto) xpr(const Sptr>& _this) const; - RangePtr prange(const SIndex& end) const; + RangePtr prange(const SIndex& last) const; SizeT deepFormat() const; /* template diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h index 56f2fbb..43fbfe3 100644 --- a/src/include/ranges/urange.cc.h +++ b/src/include/ranges/urange.cc.h @@ -128,12 +128,12 @@ namespace CNORXZ } template - RangePtr UIndex::prange(const UIndex& end) const + RangePtr UIndex::prange(const UIndex& last) const { - CXZ_ASSERT(end > *this, "got end index position smaller than begin index position"); + CXZ_ASSERT(last > *this, "got last index position smaller than begin index position"); const SizeT beginPos = lex(); - Vector parts(end.lex()-beginPos); - for(auto i = *this; i != end; ++i){ + Vector parts(last.lex()-beginPos+1); + for(auto i = *this; i != last+1; ++i){ parts[i.lex()-beginPos] = i.lex(); } return CNORXZ::prange(mRangePtr, parts); diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index 92aeb78..0ef9c21 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -55,7 +55,7 @@ namespace CNORXZ UIndex& at(const MetaT& metaPos); decltype(auto) xpr(const Sptr>& _this) const; - RangePtr prange(const UIndex& end) const; + RangePtr prange(const UIndex& last) const; SizeT deepFormat() const; /* diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index c9b44e6..5695349 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -128,9 +128,9 @@ namespace CNORXZ } template - RangePtr XIndex::prange(const XIndexPtr& end) const + RangePtr XIndex::prange(const XIndexPtr& last) const { - return mI->prange( *std::dynamic_pointer_cast(end) ); + return mI->prange( std::dynamic_pointer_cast>(last)->get() ); } template diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index ce79f44..fad8459 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -38,7 +38,7 @@ namespace CNORXZ virtual SizeT dim() const = 0; virtual RangePtr range() const = 0; virtual UPos stepSize(const IndexId<0>& id) const = 0; - virtual RangePtr prange(const XIndexPtr& end) const = 0; + virtual RangePtr prange(const XIndexPtr& last) const = 0; virtual Vector deepFormat() const = 0; //virtual Vector pack() const = 0; //virtual Vector format() const = 0; @@ -93,7 +93,7 @@ namespace CNORXZ virtual SizeT dim() const override final; virtual RangePtr range() const override final; virtual UPos stepSize(const IndexId<0>& id) const override final; - virtual RangePtr prange(const XIndexPtr& end) const override final; + virtual RangePtr prange(const XIndexPtr& last) const override final; virtual Vector deepFormat() const override final; //virtual Vector pack() const override final; //virtual Vector format() const override final; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index 6cfeb71..5e50206 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -66,7 +66,7 @@ namespace CNORXZ YIndex& operator()(); const DPack& pack() const; - RangePtr prange(const YIndex& end) const; + RangePtr prange(const YIndex& last) const; Vector deepFormat() const; const YFormat& format() const; const YFormat& lexFormat() const; diff --git a/src/lib/ranges/crange.cc b/src/lib/ranges/crange.cc index a42f413..a52e6ba 100644 --- a/src/lib/ranges/crange.cc +++ b/src/lib/ranges/crange.cc @@ -117,12 +117,13 @@ namespace CNORXZ return coproot(m.data(), _this); } - RangePtr CIndex::prange(const CIndex& end) const + RangePtr CIndex::prange(const CIndex& last) const { - CXZ_ASSERT(end > *this, "got end index position smaller than begin index position"); + CXZ_ASSERT(last > *this, "got last index position (" << last.lex() + << ") smaller than begin index position (" << lex() << ")"); const SizeT beginPos = lex(); - Vector parts(end.lex() - beginPos); - for(auto i = *this; i != end; ++i){ + Vector parts(last.lex() - beginPos + 1); + for(auto i = *this; i != last+1; ++i){ parts[i.lex()-beginPos] = i.lex(); } return CNORXZ::prange(mRangePtr, parts); diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index cae29b8..8927a44 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -368,13 +368,13 @@ namespace CNORXZ return mIs; } - RangePtr YIndex::prange(const YIndex& end) const + RangePtr YIndex::prange(const YIndex& last) const { - CXZ_ASSERT(dim() == end.dim(), "end index has different number of dimensions (" - << end.dim() << ") than begin index (" << dim() << ")"); + CXZ_ASSERT(dim() == last.dim(), "end index has different number of dimensions (" + << last.dim() << ") than begin index (" << dim() << ")"); Vector v(dim()); for(SizeT i = 0; i != dim(); ++i){ - v[i] = mIs[i]->prange( end.pack()[i] ); + v[i] = mIs[i]->prange( last.pack()[i] ); } return YRangeFactory(v).create(); } diff --git a/src/tests/range_unit_test.cc b/src/tests/range_unit_test.cc index b01a311..b6c4802 100644 --- a/src/tests/range_unit_test.cc +++ b/src/tests/range_unit_test.cc @@ -120,9 +120,32 @@ namespace for(auto x: *crx){ EXPECT_EQ(x, cnt2++); } - } + TEST_F(CR_Test, prange) + { + auto crx = std::dynamic_pointer_cast(cr); + auto beg = crx->begin(); + auto end = crx->end(); + + const SizeT begPos = 1; + const SizeT endPos = mSize-2; + const SizeT pSize = endPos - begPos; + beg += begPos; + end -= mSize-endPos+1; + auto pr = beg.prange(end); + auto prx = std::dynamic_pointer_cast>(pr); + + EXPECT_EQ(pr->size(), pSize); + EXPECT_EQ(prx->size(), pSize); + + for(auto i = prx->begin(); i != prx->end(); ++i){ + EXPECT_EQ(*i, i.lex()+begPos); + } + + EXPECT_EQ(prx->orig(),cr); + } + TEST_F(UR_Test, Basics) { auto urx = std::dynamic_pointer_cast>(ur); @@ -155,6 +178,29 @@ namespace } } + TEST_F(UR_Test, prange) + { + auto urx = std::dynamic_pointer_cast>(ur); + auto beg = urx->begin(); + auto end = urx->end(); + + const SizeT begPos = 1; + const SizeT endPos = mMeta.size() - 2; + const SizeT pSize = endPos - begPos; + beg += begPos; + end -= mMeta.size() - endPos + 1; + auto pr = beg.prange(end); + auto prx = std::dynamic_pointer_cast>>(pr); + + EXPECT_EQ(pr->size(), pSize); + EXPECT_EQ(prx->size(), pSize); + + for(auto i = prx->begin(); i != prx->end(); ++i){ + EXPECT_EQ(*i, mMeta[i.lex()+begPos]); + } + EXPECT_EQ(prx->orig(),ur); + } + TEST_F(MR_Test, Basics2d) { auto mrx = std::dynamic_pointer_cast>>(mr); @@ -437,7 +483,38 @@ namespace } } } - + + TEST_F(YR_Test, prange) + { + auto yrx = std::dynamic_pointer_cast(yr); + auto beg = yrx->begin(); + auto last = yrx->end(); + + const SizeT begI = 1; + const SizeT lastI = mSize - 2 - 1; + const SizeT begJ = 1; + const SizeT lastJ = mMeta.size() - 1; + + const SizeT begPos = begI * mMeta.size() + begJ; + const SizeT lastPos = lastI * mMeta.size() + lastJ; + const SizeT pSize = (lastI-begI+1)*(lastJ-begJ+1); + beg += begPos; + last = lastPos; + auto pr = beg.prange(last); + auto prx = std::dynamic_pointer_cast(pr); + + EXPECT_EQ(pr->size(), pSize); + EXPECT_EQ(prx->size(), pSize); + + const SizeT mmsize = lastJ-begJ+1; + auto mkm = [&](SizeT i) { + return Vector({DType(i/mmsize+begI),DType(mMeta[i % mmsize+begJ])}); }; + + for(auto i = prx->begin(); i != prx->end(); ++i){ + EXPECT_TRUE(*i == mkm(i.lex())); + } + } + // RCast_Test }