From 2f5f29f577ae7cc21b2ae5473cea7f0d5ff35997 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 20 Nov 2022 23:18:58 +0100 Subject: [PATCH] mrange: different members for blockSizes and lexBlockSizes --- src/include/base/types.h | 7 +- src/include/ranges/crange.cc.h | 2 +- src/include/ranges/crange.h | 4 +- src/include/ranges/dindex.h | 4 +- src/include/ranges/index_base.h | 4 +- src/include/ranges/mrange.cc.h | 282 ++++++++++++++++++++------------ src/include/ranges/mrange.h | 90 +++++----- src/include/ranges/urange.cc.h | 10 +- src/include/ranges/urange.h | 4 +- src/include/ranges/xindex.cc.h | 8 +- src/include/ranges/xindex.h | 8 +- src/include/ranges/yrange.h | 8 +- src/include/xpr/pos_type.cc.h | 34 +++- src/include/xpr/pos_type.h | 11 ++ src/lib/ranges/crange.cc | 8 +- src/lib/ranges/dindex.cc | 4 +- src/lib/ranges/yrange.cc | 39 ++--- src/tests/range_unit_test.cc | 12 +- 18 files changed, 326 insertions(+), 213 deletions(-) diff --git a/src/include/base/types.h b/src/include/base/types.h index 57d9b4e..74601db 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -82,6 +82,9 @@ namespace CNORXZ B = (mutable) Array F = Functional, Map,... ***/ + + // default template parameter + class None {}; // definition: base/dtype.h class DType; @@ -174,8 +177,8 @@ namespace CNORXZ class MRange; // multi range // definition: ranges/mrange.h - template - class MIndex; + template + class GMIndex; // definition: ranges/xindex.h class XIndexBase; // dynamic index wrapper diff --git a/src/include/ranges/crange.cc.h b/src/include/ranges/crange.cc.h index 63b344e..c078f22 100644 --- a/src/include/ranges/crange.cc.h +++ b/src/include/ranges/crange.cc.h @@ -9,7 +9,7 @@ namespace CNORXZ template decltype(auto) CIndex::ifor(const Xpr& xpr, F&& f) const { - return For<0,Xpr,F>(this->pmax(), this->id(), xpr, std::forward(f)); + return For<0,Xpr,F>(this->pmax().val(), this->id(), xpr, std::forward(f)); } } diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index eb49167..b74cf6a 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -28,8 +28,8 @@ namespace CNORXZ CIndex& operator-=(Int n); SizeT lex() const; - SizeT pmax() const; - SizeT lmax() const; + UPos pmax() const; + UPos lmax() const; IndexId<0> id() const; SizeT operator*() const; diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index 16e42c3..be0e785 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -33,8 +33,8 @@ namespace CNORXZ DIndex& operator-=(Int n); SizeT lex() const; - SizeT pmax() const; - SizeT lmax() const; + UPos pmax() const; + UPos lmax() const; IndexId<0> id() const; DType operator*() const; diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index 6e2fe8c..de56d77 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -30,8 +30,8 @@ namespace CNORXZ SizeT pos() const; // 'memory' pos SizeT lex() const { return THIS().lex(); } // lexicographic pos - SizeT pmax() const { return THIS().pmax(); } // memory pos max - SizeT lmax() const { return THIS().lmax(); } // lexicographic pos max + SizeT pmax() const { return static_cast(THIS().pmax()); } // memory pos max + SizeT lmax() const { return static_cast(THIS().lmax()); } // lexicographic pos max PtrId ptrId() const; decltype(auto) id() const { return THIS().id(); } diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 48fc971..4f5664d 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -6,13 +6,13 @@ namespace CNORXZ { - /*********************** - * MIndex (private) * - ***********************/ + /************************ + * GMIndex (private) * + ************************/ - template + template template - constexpr decltype(auto) MIndex::mkIPack(Isq is) const + constexpr decltype(auto) GMIndex::mkIPack(Isq is) const { static_assert(sizeof...(Is) == NI, "sequence sioze does not match number of indices"); @@ -20,56 +20,88 @@ namespace CNORXZ "subranges not available" ); return std::make_tuple( std::make_shared( mRange->sub(Is) )... ); } - - template + + template + constexpr decltype(auto) GMIndex::mkLMax(const IndexPack& ipack) + { + return iter<0,NI>( [&](auto i) { return std::get(ipack)->lmax(); }, + [](auto... e) { return (e * ...); }); + } + + template + constexpr decltype(auto) GMIndex::mkPMax(const IndexPack& ipack, const BlockType& blockSizes) + { + if constexpr(std::is_same::value){ + return mkLMax(ipack); + } + else { + return iter<0,NI> + ( [&](auto i) + { return (std::get(ipack)->pmax() - SPos<1>()) * std::get(blockSizes); }, + [](auto... e) { return (e + ...); }) + SPos<1>(); + } + } + + template template - constexpr decltype(auto) MIndex::mkBlockSizes(const IndexPack& ipack, Isq is) + constexpr decltype(auto) GMIndex::mkLexBlockSizes(const IndexPack& ipack, Isq is) { return std::make_tuple ( iter - // replace UPos by SPos where possible !!! - ( [&](auto i) { return UPos(std::get(ipack)->pmax()); }, + ( [&](auto i) { return std::get(ipack)->pmax(); }, [&](const auto&... as) { return (as * ...); } )..., SPos<1>() ); } - template + template template - inline void MIndex::up() + inline void GMIndex::up() { auto& i = std::get(mIPack); if constexpr(I != 0){ - if(i->lex() == i->lmax()-1){ - IB::mPos -= std::get(mBlockSizes).val() * i->pos(); + if(i->lex() == i->lmax().val()-1){ + IB::mPos -= std::get(blockSizes()).val() * i->pos(); + if constexpr(not std::is_same::value){ + mLex -= std::get(lexBlockSizes()).val() * i->lex(); + } (*i) = 0; up(); return; } } - IB::mPos += std::get(mBlockSizes).val(); + IB::mPos += std::get(blockSizes()).val(); + if constexpr(not std::is_same::value){ + mLex += std::get(lexBlockSizes()).val(); + } ++(*i); } - template + template template - inline void MIndex::down() + inline void GMIndex::down() { auto& i = std::get(mIPack); if constexpr(I != 0){ if(i->lex() == 0){ - (*i) = i->lmax()-1; - IB::mPos += std::get(mBlockSizes).val() * i->pos(); + (*i) = i->lmax().val()-1; + IB::mPos += std::get(blockSizes()).val() * i->pos(); + if constexpr(not std::is_same::value){ + mLex += std::get(lexBlockSizes()).val() * i->lex(); + } down(); return; } } - IB::mPos -= std::get(mBlockSizes).val(); + IB::mPos -= std::get(blockSizes()).val(); + if constexpr(not std::is_same::value){ + mLex -= std::get(lexBlockSizes()).val(); + } --(*i); } - template + template template - constexpr decltype(auto) MIndex::mkIFor(const Xpr& xpr, F&& f) const + constexpr decltype(auto) GMIndex::mkIFor(const Xpr& xpr, F&& f) const { if constexpr(I == sizeof...(Indices)-1){ return std::get(mIPack)->ifor(xpr,f); @@ -79,169 +111,202 @@ namespace CNORXZ } } - /************** - * MIndex * - **************/ + /*************** + * GMIndex * + ***************/ - template - MIndex::MIndex(const MIndex& i) : - IndexInterface,Tuple>(0), + template + constexpr GMIndex::GMIndex(const GMIndex& i) : + IndexInterface,Tuple>(0), mRange(rangeCast(i.range())), mIPack(mkIPack(Isqr<0,NI>{})), - mBlockSizes(mkBlockSizes(mIPack,Isqr<0,NI-1>{})) + mLexBlockSizes(mkLexBlockSizes(mIPack,Isqr<0,NI-1>{})), + mBlockSizes(i.mBlockSizes), + mLMax(mkLMax(mIPack)), + mPMax(mkPMax(mIPack,mBlockSizes)) { - mPMax = iter<0,NI>( [&](auto i) { return std::get(mIPack)->pmax(); }, - [](auto... e) { return (e * ...); }); *this = i.pos(); } - template - MIndex& MIndex::operator=(const MIndex& i) + template + constexpr GMIndex& GMIndex::operator=(const GMIndex& i) { - IndexInterface,Tuple>::operator=(0); + IndexInterface,Tuple>::operator=(0); mRange = rangeCast(i.range()); mIPack = mkIPack(Isqr<0,NI>{}); - mBlockSizes = mkBlockSizes(mIPack,Isqr<0,NI-1>{}); - mPMax = iter<0,NI>( [&](auto i) { return std::get(mIPack)->pmax(); }, - [](auto... e) { return (e * ...); }); + mLexBlockSizes = mkLexBlockSizes(mIPack,Isqr<0,NI-1>{}); + mBlockSizes = i.mBlockSizes; + mLMax = mkLMax(mIPack); + mPMax = mkPMax(mIPack,mBlockSizes); return *this = i.pos(); } - template - MIndex::MIndex(const RangePtr& range, SizeT lexpos) : - IndexInterface,Tuple>(0), + template + constexpr GMIndex::GMIndex(const RangePtr& range, SizeT lexpos) : + IndexInterface,Tuple>(0), mRange(rangeCast(range)), mIPack(mkIPack(Isqr<0,NI>{})), - mBlockSizes(mkBlockSizes(mIPack,Isqr<0,NI-1>{})) + mLexBlockSizes(mkLexBlockSizes(mIPack,Isqr<0,NI-1>{})), + mBlockSizes(), + mLMax(mkLMax(mIPack)), + mPMax(mkPMax(mIPack,mBlockSizes)) { - mPMax = iter<0,NI>( [&](auto i) { return std::get(mIPack)->pmax(); }, - [](auto... e) { return (e * ...); }); *this = lexpos; } - template - MIndex& MIndex::operator=(SizeT lexpos) + template + constexpr GMIndex::GMIndex(const RangePtr& range, const BlockType& blockSizes, SizeT lexpos) : + IndexInterface,Tuple>(0), + mRange(rangeCast(range)), + mIPack(mkIPack(Isqr<0,NI>{})), + mLexBlockSizes(mkLexBlockSizes(mIPack,Isqr<0,NI-1>{})), + mBlockSizes(blockSizes), + mLMax(mkLMax(mIPack)), + mPMax(mkPMax(mIPack,mBlockSizes)) { - // Adapt in GMIndex - IB::mPos = lexpos; - iter<0,NI>( [&](auto i) { *std::get(mIPack) = (IB::mPos / std::get(mBlockSizes).val()) % std::get(mIPack)->pmax(); }, NoF{} ); + *this = lexpos; + } + + template + GMIndex& GMIndex::operator=(SizeT lexpos) + { + if(lexpos >= lmax().val()){ + if constexpr(not std::is_same::value){ mLex = lmax().val(); } + IB::mPos = pmax().val(); + return *this; + } + if constexpr(not std::is_same::value){ mLex = lexpos; } + IB::mPos = iter<0,NI>( [&](auto i) { + *std::get(mIPack) = (lex() / std::get(lexBlockSizes()).val()) % std::get(mIPack)->lmax().val(); + return std::get(blockSizes()).val() * std::get(mIPack)->pos(); + }, [](const auto&... e) { return (e + ...); } ); return *this; } - template - MIndex& MIndex::operator++() + template + GMIndex& GMIndex::operator++() { - // End state is defined by high-index being end while all other indices are zero - if(lex() != lmax()){ + if(lex() == lmax().val()-1){ + return *this = lmax().val(); + } + if(lex() != lmax().val()){ up(); } return *this; } - template - MIndex& MIndex::operator--() + template + GMIndex& GMIndex::operator--() { + if(lex() == lmax().val()){ + return *this = lmax().val()-1; + } if(lex() != 0){ down(); } return *this; } - template - MIndex MIndex::operator+(Int n) const + template + GMIndex GMIndex::operator+(Int n) const { - MIndex o(*this); + GMIndex o(*this); return o += n; } - template - MIndex MIndex::operator-(Int n) const + template + GMIndex GMIndex::operator-(Int n) const { - MIndex o(*this); + GMIndex o(*this); return o -= n; } - template - MIndex& MIndex::operator+=(Int n) + template + GMIndex& GMIndex::operator+=(Int n) { if(-n > static_cast(lex())){ (*this) = 0; } const SizeT p = lex() + n; - if(p > lmax()){ - (*this) = lmax(); + if(p > lmax().val()){ + (*this) = lmax().val(); } (*this) = p; return *this; } - template - MIndex& MIndex::operator-=(Int n) + template + GMIndex& GMIndex::operator-=(Int n) { if(n > static_cast(lex())){ (*this) = 0; } const SizeT p = lex() + n; - if(p > lmax()){ - (*this) = lmax(); + if(p > lmax().val()){ + (*this) = lmax().val(); } (*this) = p; return *this; } - template - SizeT MIndex::lex() const + template + SizeT GMIndex::lex() const { - return IB::mPos; + if constexpr(std::is_same::value){ + return IB::mPos; + } + else { + return mLex; + } } - template - SizeT MIndex::pmax() const + template + constexpr decltype(auto) GMIndex::pmax() const { return mPMax; } - template - SizeT MIndex::lmax() const + template + constexpr decltype(auto) GMIndex::lmax() const { return mPMax; } - template - IndexId<0> MIndex::id() const + template + IndexId<0> GMIndex::id() const { return IndexId<0>(this->ptrId()); } - template - typename MIndex::MetaType MIndex::operator*() const + template + typename GMIndex::MetaType GMIndex::operator*() const { return meta(); } - template - SizeT MIndex::dim() const + template + constexpr SizeT GMIndex::dim() const { return NI; } - template - Sptr::RangeType> MIndex::range() const + template + Sptr::RangeType> GMIndex::range() const { return mRange; } - template + template template - decltype(auto) MIndex::stepSize(const IndexId& id) const + decltype(auto) GMIndex::stepSize(const IndexId& id) const { return iter<0,NI> - ( [&](auto i) { return std::get(mIPack)->stepSize(id) * std::get(mBlockSizes); }, + ( [&](auto i) { return std::get(mIPack)->stepSize(id) * std::get(blockSizes()); }, [](const auto&... ss) { return ( ss + ... ); }); } - template - String MIndex::stringMeta() const + template + String GMIndex::stringMeta() const { const String blim = "("; const String elim = ")"; @@ -253,50 +318,61 @@ namespace CNORXZ } ); } - template - typename MIndex::MetaType MIndex::meta() const + template + typename GMIndex::MetaType GMIndex::meta() const { return iter<0,NI>( [&](auto i) { return std::get(mIPack)->meta(); }, [](const auto&... xs) { return std::make_tuple(xs...); } ); } - template - MIndex& MIndex::at(const MetaType& metaPos) + template + GMIndex& GMIndex::at(const MetaType& metaPos) { iter<0,NI>( [&](auto i) { std::get(mIPack)->at( std::get(metaPos) ); }, NoF {} ); IB::mPos = iter<0,NI> - ( [&](auto i) { return std::get(mIPack)->pos()*std::get(mBlockSizes).val(); }, + ( [&](auto i) { return std::get(mIPack)->pos()*std::get(blockSizes()).val(); }, [](const auto&... xs) { return (xs + ...); }); return *this; } - template + template template - constexpr decltype(auto) MIndex::ifor(const Xpr& xpr, F&& f) const + constexpr decltype(auto) GMIndex::ifor(const Xpr& xpr, F&& f) const { return mkIFor<0>(xpr, f); } - template - MIndex& MIndex::operator()(const Sptr& mi) + template + GMIndex& GMIndex::operator()(const Sptr& mi) { mIPack = mi.mIPack; IB::mPos = iter<0,NI> - ( [&](auto i) { return std::get(mIPack)->pos()*std::get(mBlockSizes).val(); }, + ( [&](auto i) { return std::get(mIPack)->pos()*std::get(blockSizes()).val(); }, [](const auto&... xs) { return (xs + ...); }); return *this; } - template - const typename MIndex::IndexPack& MIndex::pack() const + template + const typename GMIndex::IndexPack& GMIndex::pack() const { return mIPack; } - template - const auto& MIndex::blockSizes() const + template + const auto& GMIndex::blockSizes() const { - return mBlockSizes; + if constexpr(std::is_same::value){ + return mLexBlockSizes; + } + else { + return mBlockSizes; + } + } + + template + const auto& GMIndex::lexBlockSizes() const + { + return mLexBlockSizes; } diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index 5ad50cd..6c051b0 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -11,14 +11,14 @@ namespace CNORXZ { - template - class MIndex : public IndexInterface, - Tuple > + template + class GMIndex : public IndexInterface, + Tuple > { public: - typedef IndexInterface, - Tuple > IB; + typedef IndexInterface, + Tuple> IB; typedef Tuple...> IndexPack; typedef Tuple MetaType; typedef MRange RangeType; @@ -26,31 +26,32 @@ namespace CNORXZ // NO DEFAULT HERE !!! // ( have to assign sub-indices (ptr!) correctly ) - MIndex() = default; - MIndex(MIndex&& i) = default; - MIndex& operator=(MIndex&& i) = default; + constexpr GMIndex() = default; + constexpr GMIndex(GMIndex&& i) = default; + constexpr GMIndex& operator=(GMIndex&& i) = default; - MIndex(const MIndex& i); - MIndex& operator=(const MIndex& i); + constexpr GMIndex(const GMIndex& i); + constexpr GMIndex& operator=(const GMIndex& i); - MIndex(const RangePtr& range, SizeT lexpos = 0); + constexpr GMIndex(const RangePtr& range, SizeT lexpos = 0); + constexpr GMIndex(const RangePtr& range, const BlockType& blockSizes, SizeT lexpos = 0); - MIndex& operator=(SizeT pos); - MIndex& operator++(); - MIndex& operator--(); - MIndex operator+(Int n) const; - MIndex operator-(Int n) const; - MIndex& operator+=(Int n); - MIndex& operator-=(Int n); + GMIndex& operator=(SizeT pos); + GMIndex& operator++(); + GMIndex& operator--(); + GMIndex operator+(Int n) const; + GMIndex operator-(Int n) const; + GMIndex& operator+=(Int n); + GMIndex& operator-=(Int n); SizeT lex() const; - SizeT pmax() const; - SizeT lmax() const; + constexpr decltype(auto) pmax() const; + constexpr decltype(auto) lmax() const; IndexId<0> id() const; MetaType operator*() const; - - SizeT dim() const; + + constexpr SizeT dim() const; Sptr range() const; template @@ -58,20 +59,25 @@ namespace CNORXZ String stringMeta() const; MetaType meta() const; - MIndex& at(const MetaType& metaPos); + GMIndex& at(const MetaType& metaPos); template constexpr decltype(auto) ifor(const Xpr& xpr, F&& f) const; // replace sub-index instances; only use if you know what you are doing! - MIndex& operator()(const Sptr& mi); + GMIndex& operator()(const Sptr& mi); const IndexPack& pack() const; const auto& blockSizes() const; + const auto& lexBlockSizes() const; private: template - static constexpr decltype(auto) mkBlockSizes(const IndexPack& ipack, Isq is); + static constexpr decltype(auto) mkLexBlockSizes(const IndexPack& ipack, Isq is); + + static constexpr decltype(auto) mkLMax(const IndexPack& ipack); + + static constexpr decltype(auto) mkPMax(const IndexPack& ipack, const BlockType& blockSizes); template constexpr decltype(auto) mkIPack(Isq is) const; @@ -87,31 +93,19 @@ namespace CNORXZ Sptr mRange; IndexPack mIPack; - typedef RemoveRef{}))> BlockTuple; - BlockTuple mBlockSizes; - SizeT mPMax = 0; // = LMax here, add new variable in GMIndex! + typedef RemoveRef{}))> LexBlockType; + LexBlockType mLexBlockSizes; + BlockType mBlockSizes; + SizeT mLex; + typedef RemoveRef LMaxT; + LMaxT mLMax; + typedef RemoveRef PMaxT; + PMaxT mPMax; }; - // modified blockSizes; to be used for Slices; can be created from MIndices - template - class GMIndex : public MIndexType - { - public: - // override everything that modyfies IB::mPos or uses mBlockSizes!!! - - constexpr GMIndex(const MIndexType& mi, const BlockType& b); - - template - constexpr decltype(auto) ifor(const Xpr& xpr, F&& f) const; - - private: - BlockType mLexBlockSizes; - - template - constexpr decltype(auto) mkPos(Isq is) const; - }; - - // NOT THREAD SAVE + template + using MIndex = GMIndex; + template class MRangeFactory : public RangeFactoryBase { diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h index 5243d17..ae8405f 100644 --- a/src/include/ranges/urange.cc.h +++ b/src/include/ranges/urange.cc.h @@ -75,15 +75,15 @@ namespace CNORXZ } template - SizeT UIndex::pmax() const + UPos UIndex::pmax() const { - return mRangePtr->size(); + return UPos(mRangePtr->size()); } template - SizeT UIndex::lmax() const + UPos UIndex::lmax() const { - return mRangePtr->size(); + return UPos(mRangePtr->size()); } template @@ -140,7 +140,7 @@ namespace CNORXZ template decltype(auto) UIndex::ifor(const Xpr& xpr, F&& f) const { - return For<0,Xpr,F>(this->pmax(), this->id(), xpr, std::forward(f)); + return For<0,Xpr,F>(this->pmax().val(), this->id(), xpr, std::forward(f)); } /********************** diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index 44c0245..904d87e 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -31,8 +31,8 @@ namespace CNORXZ UIndex& operator-=(Int n); SizeT lex() const; - SizeT pmax() const; - SizeT lmax() const; + UPos pmax() const; + UPos lmax() const; IndexId<0> id() const; const MetaT& operator*() const; diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index 07c2c4c..c0c829f 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -86,15 +86,15 @@ namespace CNORXZ } template - SizeT XIndex::pmax() const + UPos XIndex::pmax() const { - return mI->pmax(); + return UPos(mI->pmax()); } template - SizeT XIndex::lmax() const + UPos XIndex::lmax() const { - return mI->lmax(); + return UPos(mI->lmax()); } template diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index e615754..f8f1837 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -26,8 +26,8 @@ namespace CNORXZ virtual XIndexBase& operator-=(Int n) = 0; virtual SizeT lex() const = 0; - virtual SizeT pmax() const = 0; - virtual SizeT lmax() const = 0; + virtual UPos pmax() const = 0; + virtual UPos lmax() const = 0; virtual IndexId<0> id() const = 0; virtual DType operator*() const = 0; @@ -74,8 +74,8 @@ namespace CNORXZ virtual XIndex& operator-=(Int n) override final; virtual SizeT lex() const override final; - virtual SizeT pmax() const override final; - virtual SizeT lmax() const override final; + virtual UPos pmax() const override final; + virtual UPos lmax() const override final; virtual IndexId<0> id() const override final; virtual DType operator*() const override final; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index a5551a3..61599dc 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -35,8 +35,8 @@ namespace CNORXZ YIndex& operator-=(Int n); SizeT lex() const; - SizeT pmax() const; - SizeT lmax() const; + UPos pmax() const; + UPos lmax() const; IndexId<0> id() const; DType operator*() const; @@ -69,8 +69,8 @@ namespace CNORXZ Vector mBlockSizes; // dim() elements only!!! Vector mLexBlockSizes; // dim() elements only!!! SizeT mLex = 0; - SizeT mPMax = 0; - SizeT mLMax = 0; + UPos mPMax = 0; + UPos mLMax = 0; }; class YRangeFactory : public RangeFactoryBase diff --git a/src/include/xpr/pos_type.cc.h b/src/include/xpr/pos_type.cc.h index d675474..40dffa5 100644 --- a/src/include/xpr/pos_type.cc.h +++ b/src/include/xpr/pos_type.cc.h @@ -83,6 +83,12 @@ namespace CNORXZ return UPos(N); } + template + constexpr SPos::operator SizeT() const + { + return val(); + } + /************ * UPos * ************/ @@ -138,7 +144,12 @@ namespace CNORXZ { return extend(a); } - + + constexpr UPos::operator SizeT() const + { + return val(); + } + /************ * FPos * ************/ @@ -185,6 +196,11 @@ namespace CNORXZ return extend(a); } + constexpr FPos::operator SizeT() const + { + return val(); + } + /************* * SFPos * *************/ @@ -265,6 +281,12 @@ namespace CNORXZ return FPos(N, &sMs[0]); } + template + constexpr SFPos::operator SizeT() const + { + return val(); + } + /************ * MPos * ************/ @@ -456,6 +478,11 @@ namespace CNORXZ return MPos(*this,a); } + inline DPos::operator SizeT() const + { + return val(); + } + /*************** * DPosRef * ***************/ @@ -531,6 +558,11 @@ namespace CNORXZ //return DPos(mP->vextend( a )); return MPos(*this,a); } + + inline DPosRef::operator SizeT() const + { + return val(); + } /************ * EPos * diff --git a/src/include/xpr/pos_type.h b/src/include/xpr/pos_type.h index de1e367..cac728f 100644 --- a/src/include/xpr/pos_type.h +++ b/src/include/xpr/pos_type.h @@ -36,6 +36,7 @@ namespace CNORXZ constexpr decltype(auto) operator<<(const PosT& a) const; explicit constexpr operator UPos() const; + explicit constexpr operator SizeT() const; }; class UPos @@ -68,6 +69,8 @@ namespace CNORXZ template constexpr decltype(auto) operator<<(const PosT& a) const; + + explicit constexpr operator SizeT() const; }; class FPos @@ -98,6 +101,8 @@ namespace CNORXZ template constexpr decltype(auto) operator<<(const PosT& a) const; + + explicit constexpr operator SizeT() const; }; template @@ -129,6 +134,8 @@ namespace CNORXZ constexpr decltype(auto) operator<<(const PosT& a) const; explicit constexpr operator FPos() const; + + explicit constexpr operator SizeT() const; }; template @@ -204,6 +211,8 @@ namespace CNORXZ template inline decltype(auto) operator<<(const PosT& a) const; + + explicit inline operator SizeT() const; }; class DPosRef @@ -238,6 +247,8 @@ namespace CNORXZ template inline decltype(auto) operator<<(const PosT& a) const; + + explicit inline operator SizeT() const; }; // for common call of extension vector elements diff --git a/src/lib/ranges/crange.cc b/src/lib/ranges/crange.cc index 6be9932..9f3dc03 100644 --- a/src/lib/ranges/crange.cc +++ b/src/lib/ranges/crange.cc @@ -56,14 +56,14 @@ namespace CNORXZ return IB::mPos; } - SizeT CIndex::lmax() const + UPos CIndex::lmax() const { - return mRangePtr->size(); + return UPos(mRangePtr->size()); } - SizeT CIndex::pmax() const + UPos CIndex::pmax() const { - return mRangePtr->size(); + return UPos(mRangePtr->size()); } IndexId<0> CIndex::id() const diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc index cb83a12..5aa2f9d 100644 --- a/src/lib/ranges/dindex.cc +++ b/src/lib/ranges/dindex.cc @@ -87,12 +87,12 @@ namespace CNORXZ return mI->lex(); } - SizeT DIndex::pmax() const + UPos DIndex::pmax() const { return mI->pmax(); } - SizeT DIndex::lmax() const + UPos DIndex::lmax() const { return mI->lmax(); } diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index dde2762..1000f71 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -25,7 +25,7 @@ namespace CNORXZ for(SizeT i = o.size(); i != 0; --i){ const SizeT j = i-1; o[j] = b; - b *= mIs[j]->pmax(); + b *= mIs[j]->pmax().val(); } return o; } @@ -37,7 +37,7 @@ namespace CNORXZ for(SizeT i = o.size(); i != 0; --i){ const SizeT j = i-1; o[j] = b; - b *= mIs[j]->lmax(); + b *= mIs[j]->lmax().val(); } return o; } @@ -47,7 +47,7 @@ namespace CNORXZ auto& idx = mIs[i]; // it is guaranteed that the last accessible position // is one less than the max position (=end) - if(i != 0 and idx->lex() == idx->lmax()-1){ + if(i != 0 and idx->lex() == idx->lmax().val()-1){ IB::mPos -= mBlockSizes[i] * idx->pos(); mLex -= mLexBlockSizes[i] * idx->lex(); (*idx) = 0; @@ -63,7 +63,7 @@ namespace CNORXZ { auto& idx = mIs[i]; if(i != 0 and idx->pos() == 0){ - (*idx) = idx->lmax()-1; + (*idx) = idx->lmax().val()-1; IB::mPos += mBlockSizes[i] * idx->pos(); mLex += mLexBlockSizes[i] * idx->lex(); down(i-1); @@ -89,7 +89,7 @@ namespace CNORXZ { SizeT o = 0; for(SizeT i = 0; i != mIs.size(); ++i){ - o += (mIs[i]->pmax()-1) * mBlockSizes[i]; + o += (mIs[i]->pmax().val()-1) * mBlockSizes[i]; } return o+1; } @@ -97,7 +97,7 @@ namespace CNORXZ inline SizeT YIndex::mkLMax() const { return std::accumulate(mIs.begin(), mIs.end(),1, - [](const auto& res, const auto& el) { return res * el->lmax(); } ); + [](const auto& res, const auto& el) { return res * el->lmax().val(); } ); } @@ -143,15 +143,15 @@ namespace CNORXZ YIndex& YIndex::operator=(SizeT lexpos) { - if(lexpos >= lmax()){ - mLex = lmax(); - IB::mPos = pmax(); + if(lexpos >= lmax().val()){ + mLex = lmax().val(); + IB::mPos = pmax().val(); return *this; } mLex = lexpos; IB::mPos = 0; for(SizeT i = 0; i != mIs.size(); ++i){ - *mIs[i] = (lex() / mLexBlockSizes[i]) % mIs[i]->lmax(); + *mIs[i] = (lex() / mLexBlockSizes[i]) % mIs[i]->lmax().val(); IB::mPos += mBlockSizes[i] * mIs[i]->pos(); } return *this; @@ -159,22 +159,19 @@ namespace CNORXZ YIndex& YIndex::operator++() { - auto& i0 = mIs[0]; - if(i0->lex() != i0->lmax()){ - up(mIs.size()-1); + if(lex() == lmax().val()-1){ + return *this = lmax().val(); } - // no else! up() changes i0! - if(i0->lex() == i0->lmax()){ - IB::mPos = pmax(); + if(lex() != lmax().val()){ + up(mIs.size()-1); } return *this; } YIndex& YIndex::operator--() { - auto& i0 = mIs[0]; - if(i0->lex() == i0->lmax()){ - IB::mPos = mBlockSizes[0] * i0->pmax(); + if(lex() == lmax().val()){ + return *this = lmax().val()-1; } if(lex() != 0){ down(mIs.size()-1); @@ -215,12 +212,12 @@ namespace CNORXZ return mLex; } - SizeT YIndex::pmax() const + UPos YIndex::pmax() const { return mPMax; } - SizeT YIndex::lmax() const + UPos YIndex::lmax() const { return mLMax; } diff --git a/src/tests/range_unit_test.cc b/src/tests/range_unit_test.cc index 9cc4748..99042ca 100644 --- a/src/tests/range_unit_test.cc +++ b/src/tests/range_unit_test.cc @@ -231,8 +231,8 @@ namespace auto yrx = std::dynamic_pointer_cast(yr); auto yi = yrx->begin(); - EXPECT_EQ(yi.pmax(), yr->size()); - EXPECT_EQ(yi.lmax(), yr->size()); + EXPECT_EQ(yi.pmax().val(), yr->size()); + EXPECT_EQ(yi.lmax().val(), yr->size()); EXPECT_EQ(yi.range(), yr); EXPECT_EQ(yi.range(), yrx); EXPECT_EQ(yi.dim(), 2u); @@ -258,11 +258,11 @@ namespace EXPECT_EQ(b.stringMeta(), toString(mmj)); } } - yi += yi.lmax() + 10; - EXPECT_EQ(yi.lex(), yi.lmax()); - EXPECT_EQ(yi.pos(), yi.pmax()); + yi += yi.lmax().val() + 10; + EXPECT_EQ(yi.lex(), yi.lmax().val()); + EXPECT_EQ(yi.pos(), yi.pmax().val()); - yi -= yi.lmax() + 20; + yi -= yi.lmax().val() + 20; EXPECT_EQ(yi.lex(), 0u); EXPECT_EQ(yi.pos(), 0u); }