From bf83c26a12cecd595b146a646599b59104661df9 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 25 Dec 2023 22:07:39 +0100 Subject: [PATCH] index reformat: apply format change to same instance (no copy) --- src/include/ranges/crange.h | 2 +- src/include/ranges/dindex.h | 2 +- src/include/ranges/index_base.cc.h | 11 ----- src/include/ranges/index_base.h | 6 +-- src/include/ranges/index_format.h | 6 +++ src/include/ranges/mrange.cc.h | 72 ++++++++++++++++-------------- src/include/ranges/mrange.h | 2 +- src/include/ranges/prange.cc.h | 11 +++-- src/include/ranges/prange.h | 2 +- src/include/ranges/srange.cc.h | 2 +- src/include/ranges/srange.h | 2 +- src/include/ranges/urange.cc.h | 2 +- src/include/ranges/urange.h | 2 +- src/include/ranges/xindex.cc.h | 5 ++- src/include/ranges/xindex.h | 4 +- src/include/ranges/yrange.h | 2 +- src/lib/ranges/crange.cc | 2 +- src/lib/ranges/dindex.cc | 5 ++- src/lib/ranges/yrange.cc | 17 +++---- 19 files changed, 75 insertions(+), 82 deletions(-) diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index 707b796..45d2ef6 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -111,7 +111,7 @@ namespace CNORXZ SizeT deepMax() const; /** @copydoc IndexInterface::reformat() */ - CIndex reformat(const Vector& f, const Vector& s) const; + CIndex& reformat(const Vector& f, const Vector& s); /** @copydoc IndexInterface::ifor() */ template diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index d0402d1..5511625 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -67,7 +67,7 @@ namespace CNORXZ Vector deepMax() const; /** @copydoc IndexInterface::reformat() */ - DIndex reformat(const Vector& f, const Vector& s) const; + DIndex& reformat(const Vector& f, const Vector& s); template decltype(auto) ifor(const Xpr& xpr, F&& f) const; diff --git a/src/include/ranges/index_base.cc.h b/src/include/ranges/index_base.cc.h index e9d5f2e..4ba1774 100644 --- a/src/include/ranges/index_base.cc.h +++ b/src/include/ranges/index_base.cc.h @@ -157,17 +157,6 @@ namespace CNORXZ return i->xpr(i); } - template - decltype(auto) reformat(const Sptr& i, const Vector& f, const Vector& s) - { - if constexpr(has_sub::value){ - return moveToPtr( i->reformat(f,s) ); - } - else { - i->reformat(f,s); // checks - return i; - } - } } #endif diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index ef59ec7..e13b027 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -179,9 +179,8 @@ namespace CNORXZ /** reformat index, create new index instance @param f new format @param s new sub-index sizes - @return new reformatted index instance */ - decltype(auto) reformat(const Vector& f, const Vector& s) const { return THIS().reformat(f,s); } + I& reformat(const Vector& f, const Vector& s) { return THIS().reformat(f,s); } /** create a for-loop expression @@ -234,9 +233,6 @@ namespace CNORXZ template decltype(auto) xpr(const Sptr& i); - // do not copy single-indices: - template - decltype(auto) reformat(const Sptr& i, const Vector& f, const Vector& s); } #endif diff --git a/src/include/ranges/index_format.h b/src/include/ranges/index_format.h index 21353ca..9f80d32 100644 --- a/src/include/ranges/index_format.h +++ b/src/include/ranges/index_format.h @@ -24,6 +24,8 @@ namespace CNORXZ class MFormat { public: + typedef Arr InputType; + SP_DEFAULT_MEMBERS(constexpr,MFormat); explicit constexpr MFormat(const Arr& b); @@ -50,6 +52,8 @@ namespace CNORXZ class GMFormat { public: + typedef Tuple InputType; + SP_DEFAULT_MEMBERS(constexpr,GMFormat); explicit constexpr GMFormat(const Tuple& b); explicit constexpr GMFormat(Tuple&& b); @@ -78,6 +82,8 @@ namespace CNORXZ class YFormat { public: + typedef Vector InputType; + DEFAULT_MEMBERS(YFormat); explicit YFormat(const Vector& b); diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 621ce7b..7fb78b7 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -539,41 +539,45 @@ namespace CNORXZ } template - decltype(auto) - GMIndex::reformat(const Vector& f, const Vector& s) const + GMIndex& + GMIndex::reformat(const Vector& f, const Vector& s) { - CXZ_ASSERT(f.size() == s.size(), "input error: f.size() != s.size()"); - // f: input format - // s: input sizes - SizeT j = 0; - SizeT j0 = 0; - Arr nformat; - auto npack = iter<0,NI>( [&](auto i) { - SizeT si = 1; - if(mIPack[i]->lmax().val() == 1){ - //std::get(npack) = mIPack[i]; - return mIPack[i]; - } - // CHECK!!! - for(; si < mIPack[i]->lmax().val(); ++j){ - si *= s[i]; - CXZ_ASSERT(j < f.size(), "incompatible index formats"); - } - CXZ_ASSERT(si == mIPack[i]->lmax().val(), - "incompatible index formats: " << toString(s) << " vs " //!!! - ); - Vector nf(j-j0); - Vector ns(j-j0); - std::copy(f.begin()+j0,f.begin()+j,nf.begin()); - nformat[i] = *std::min_element(nf.begin(), nf.end()); - std::for_each(nf.begin(), nf.end(), [&](SizeT& x) - { CXZ_ASSERT(x % nformat[i].val() == 0, "incompatible"); x /= nformat[i].val(); } ); - std::copy(s.begin()+j0,s.begin()+j,ns.begin()); - return CNORXZ::reformat(mIPack[i],nf,ns); - }, [](const auto&... e) { return std::make_tuple( e... ); } ); - GMIndex,Indices...> oi { MFormat(nformat),SPack(npack) }; - oi = lex(); - return oi; + if constexpr(std::is_same::value){ + CXZ_ASSERT(CNORXZ::formatIsTrivial(f,s), + "cannot reformat MIndex with format type = None"); + return *this; + } + else { + CXZ_ASSERT(f.size() == s.size(), "input error: f.size() != s.size()"); + // f: input format + // s: input sizes + SizeT j = 0; + SizeT j0 = 0; + typename FormatT::InputType nformat; + auto npack = iter<0,NI>( [&](auto i) { + SizeT si = 1; + if(mIPack[i]->lmax().val() != 1){ + // CHECK!!! + for(; si < mIPack[i]->lmax().val(); ++j){ + si *= s[i]; + CXZ_ASSERT(j < f.size(), "incompatible index formats"); + } + CXZ_ASSERT(si == mIPack[i]->lmax().val(), + "incompatible index formats: " << toString(s) << " vs " /*!!!*/); + Vector nf(j-j0); + Vector ns(j-j0); + std::copy(f.begin()+j0,f.begin()+j,nf.begin()); + std::get(nformat) = *std::min_element(nf.begin(), nf.end()); + std::for_each(nf.begin(), nf.end(), [&](SizeT& x) + { CXZ_ASSERT(x % std::get(nformat).val() == 0, "incompatible"); + x /= std::get(nformat).val(); } ); + std::copy(s.begin()+j0,s.begin()+j,ns.begin()); + mIPack[i]->reformat(nf,ns); + } + }, NoF {}); + mFormat = FormatT(nformat); + return *this; + } } template diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index 4e960c2..2d9b1c0 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -94,7 +94,7 @@ namespace CNORXZ auto deepMax() const; /** @copydoc IndexInterface::reformat() */ - decltype(auto) reformat(const Vector& f, const Vector& s) const; + GMIndex& reformat(const Vector& f, const Vector& s); GMIndex& setFormat(const FormatT& bs); diff --git a/src/include/ranges/prange.cc.h b/src/include/ranges/prange.cc.h index b5d82c9..ce687ee 100644 --- a/src/include/ranges/prange.cc.h +++ b/src/include/ranges/prange.cc.h @@ -174,13 +174,12 @@ namespace CNORXZ } template - PIndex PIndex::reformat(const Vector& f, const Vector& s) const + PIndex& PIndex::reformat(const Vector& f, const Vector& s) { - CXZ_ASSERT(f.size() == 1, "expected format of dimension 1, got " << toString(f)); - CXZ_ASSERT(s.size() == 1, "expected sizes of dimension 1, got " << toString(s)); - CXZ_ASSERT(f[0] == 1, "trivial format ([1]), got " << toString(f)); - CXZ_ASSERT(s[0] == lmax().val(), "expected size to be equal to index size (" << - lmax().val() << "), got " << s[0]); + CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0] + << " vs " << lmax().val()); + CXZ_ASSERT(CNORXZ::formatIsTrivial(f,s), "format is not trivial: f = " << toString(f) + << ", s = " << toString(s)); return *this; } diff --git a/src/include/ranges/prange.h b/src/include/ranges/prange.h index 1f25453..562a3e6 100644 --- a/src/include/ranges/prange.h +++ b/src/include/ranges/prange.h @@ -58,7 +58,7 @@ namespace CNORXZ decltype(auto) deepMax() const; /** @copydoc IndexInterface::reformat() */ - PIndex reformat(const Vector& f, const Vector& s) const; + PIndex& reformat(const Vector& f, const Vector& s); String stringMeta() const; decltype(auto) meta() const; diff --git a/src/include/ranges/srange.cc.h b/src/include/ranges/srange.cc.h index a42918f..a5e9e55 100644 --- a/src/include/ranges/srange.cc.h +++ b/src/include/ranges/srange.cc.h @@ -184,7 +184,7 @@ namespace CNORXZ } template - SIndex SIndex::reformat(const Vector& f, const Vector& s) const + SIndex& SIndex::reformat(const Vector& f, const Vector& s) { CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0] << " vs " << lmax().val()); diff --git a/src/include/ranges/srange.h b/src/include/ranges/srange.h index 34991f3..ea18367 100644 --- a/src/include/ranges/srange.h +++ b/src/include/ranges/srange.h @@ -64,7 +64,7 @@ namespace CNORXZ SizeT deepMax() const; /** @copydoc IndexInterface::reformat() */ - SIndex reformat(const Vector& f, const Vector& s) const; + SIndex& reformat(const Vector& f, const Vector& s); template decltype(auto) ifor(const Xpr& xpr, F&& f) const; diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h index c7bc3ad..52b5dce 100644 --- a/src/include/ranges/urange.cc.h +++ b/src/include/ranges/urange.cc.h @@ -170,7 +170,7 @@ namespace CNORXZ } template - UIndex UIndex::reformat(const Vector& f, const Vector& s) const + UIndex& UIndex::reformat(const Vector& f, const Vector& s) { CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0] << " vs " << lmax().val()); diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index 9ddcbf4..8357503 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -70,7 +70,7 @@ namespace CNORXZ SizeT deepMax() const; /** @copydoc IndexInterface::reformat() */ - UIndex reformat(const Vector& f, const Vector& s) const; + UIndex& reformat(const Vector& f, const Vector& s); template decltype(auto) ifor(const Xpr& xpr, F&& f) const; diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index 2f4537c..ff5db88 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -165,9 +165,10 @@ namespace CNORXZ } template - XIndexPtr XIndex::reformat(const Vector& f, const Vector& s) const + XIndex& XIndex::reformat(const Vector& f, const Vector& s) { - return xindexPtr( moveToPtr( mI->reformat(f,s) ) ); + mI->reformat(f,s); + return *this; } template diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index 96b2ffe..bbb9411 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -53,7 +53,7 @@ namespace CNORXZ virtual RangePtr prange(const XIndexPtr& last) const = 0; virtual Vector deepFormat() const = 0; virtual Vector deepMax() const = 0; - virtual XIndexPtr reformat(const Vector& f, const Vector& s) const = 0; + virtual XIndexBase& reformat(const Vector& f, const Vector& s) = 0; virtual String stringMeta() const = 0; virtual DType meta() const = 0; @@ -107,7 +107,7 @@ namespace CNORXZ virtual RangePtr prange(const XIndexPtr& last) const override final; virtual Vector deepFormat() const override final; virtual Vector deepMax() const override final; - virtual XIndexPtr reformat(const Vector& f, const Vector& s) const override final; + virtual XIndex& reformat(const Vector& f, const Vector& s) override final; virtual String stringMeta() const override final; virtual DType meta() const override final; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index f67d7a5..48febe8 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -80,7 +80,7 @@ namespace CNORXZ const YFormat& format() const; const YFormat& lexFormat() const; YIndex& setFormat(const YFormat& bs); - YIndex reformat(const Vector& f, const Vector& s) const; + YIndex& reformat(const Vector& f, const Vector& s); /** @copydoc IndexInterface::formatIsTrivial() */ bool formatIsTrivial() const; diff --git a/src/lib/ranges/crange.cc b/src/lib/ranges/crange.cc index a5674b0..cf6ec9e 100644 --- a/src/lib/ranges/crange.cc +++ b/src/lib/ranges/crange.cc @@ -144,7 +144,7 @@ namespace CNORXZ return lmax().val(); } - CIndex CIndex::reformat(const Vector& f, const Vector& s) const + CIndex& CIndex::reformat(const Vector& f, const Vector& s) { CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0] << " vs " << lmax().val()); diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc index c1f7235..c1dc73a 100644 --- a/src/lib/ranges/dindex.cc +++ b/src/lib/ranges/dindex.cc @@ -182,9 +182,10 @@ namespace CNORXZ return mI->deepMax(); } - DIndex DIndex::reformat(const Vector& f, const Vector& s) const + DIndex& DIndex::reformat(const Vector& f, const Vector& s) { - return DIndex(mI->reformat(f,s)); + mI->reformat(f,s); + return *this; } bool DIndex::formatIsTrivial() const diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 6a95961..10b3b8b 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -413,27 +413,25 @@ namespace CNORXZ return o; } - YIndex YIndex::reformat(const Vector& f, const Vector& s) const + YIndex& YIndex::reformat(const Vector& f, const Vector& s) { CXZ_ASSERT(f.size() == s.size(), "input error: f.size() != s.size()"); // f: input format // s: input sizes SizeT j = 0; SizeT j0 = 0; - Vector nformat(dim()); - Vector npack(dim()); + Vector nformat; for(SizeT i = 0; i != dim(); ++i){ SizeT si = 1; if(mIs[i]->lmax().val() == 1){ - npack[i] = mIs[i]; + continue; } for(; si < mIs[i]->lmax().val(); ++j){ si *= s[i]; CXZ_ASSERT(j < f.size(), "incompatible index formats"); } CXZ_ASSERT(si == mIs[i]->lmax().val(), - "incompatible index formats: " << toString(s) << " vs " //!!! - ); + "incompatible index formats: " << toString(s) << " vs " /*!!!*/); Vector nf(j-j0); Vector ns(j-j0); std::copy(f.begin()+j0,f.begin()+j,nf.begin()); @@ -441,11 +439,10 @@ namespace CNORXZ std::for_each(nf.begin(), nf.end(), [&](SizeT& x) { CXZ_ASSERT(x % nformat[i].val() == 0, "incompatible"); x /= nformat[i].val(); } ); std::copy(s.begin()+j0,s.begin()+j,ns.begin()); - npack[i] = mIs[i]->reformat(nf,ns); + mIs[i]->reformat(nf,ns); } - YIndex oi ( YFormat(nformat), npack ); - oi = lex(); - return oi; + mFormat = YFormat(nformat); + return *this; } bool YIndex::formatIsTrivial() const