From 0197e7db6e71bf3a6c8b860affba4de9fb59cda2 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 6 Nov 2022 22:58:46 +0100 Subject: [PATCH] more on MIndex + operator << for PosTypes --- src/include/operation/op_types.cc.h | 1 + src/include/ranges/mrange.cc.h | 232 ++++++++++++++++++---------- src/include/ranges/mrange.h | 57 ++++++- src/include/ranges/urange.cc.h | 14 -- src/include/ranges/urange.h | 3 - src/include/xpr/pos_type.cc.h | 45 +++++- src/include/xpr/pos_type.h | 31 +++- 7 files changed, 265 insertions(+), 118 deletions(-) diff --git a/src/include/operation/op_types.cc.h b/src/include/operation/op_types.cc.h index 0146c2e..344710a 100644 --- a/src/include/operation/op_types.cc.h +++ b/src/include/operation/op_types.cc.h @@ -264,6 +264,7 @@ namespace CNORXZ template constexpr decltype(auto) Operation::rootSteps(const IndexId& id) const { + // !!! return mOps.rootSteps(id); } diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 37691ac..7648e7c 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -32,6 +32,13 @@ namespace CNORXZ { return f( g(std::get(mIPack))... ); } + + template + template + constexpr decltype(auto) MIndex::accumulate2i(const G& g, const F& f, std::index_sequence is) const + { + return f( g(std::get(mIPack), std::get(mBlockSizes))... ); + } template template @@ -40,25 +47,127 @@ namespace CNORXZ return accumulatei(g, f, mkIndexSequence()) } + template + template + constexpr decltype(auto) MIndex::accumulate2(const G& g, const F& f) const + { + return accumulate2i(g, f, mkIndexSequence()) + } + template template - constexpr decltype(auto) mkIPack(SizeT pos, std::index_sequence is) const + constexpr decltype(auto) MIndex::mkIPack(SizeT pos, std::index_sequence is) const { static_assert(sizeof...(Is) == sizeof...(Indices), "sequence size does not match number of indices"); return std::make_tuple( std::make_shared( mRange->sub(Is) )... ); + //, (pos / std::get(mBlockSizes) ) % )... ); } template template - constexpr decltype(auto) mkBlockSizes(std::index_sequence is) const + inline void MIndex::setIPack(std::index_sequence is) const + { + ( (std::get(mIPack) = (IB::mPos / std::get(mBlockSize)) % std::get(mIPack)->max() ),... ); + } + + template + template + constexpr decltype(auto) MIndex::mkBlockSizes(std::index_sequence is) const { static_assert(sizeof...(Is) == sizeof...(Indices)-1, "set lowest block size manually"); - return Arr - { accumulate( [](const auto& i) { return i->max(); }, - [](const auto&... as) { return (as * ...); } ), - 1 }; + return std::make_tuple + ( accumulate + ( [](const auto& i) { return i->max(); }, + [](const auto&... as) { return (as * ...); } )..., + SPos<1>() ); + } + + template + template + constexpr decltype(auto) MIndex::mkPos(std::index_sequence is) const + { + return ( (std::get(mIPack)*std::get(mBlockSizes)) + ... ); + } + + template + template + inline void MIndex::up() + { + auto& i = std::get(mIPack); + if constexpr(I != 0){ + if(i->pos() == i->max()-1){ + IB::mPos -= std::get(mBlockSize) * i->pos(); + (*i) = 0; + upi(); + return; + } + } + IB::mPos += std::get(mBlockSize); + ++(*i); + } + + template + template + inline void MIndex::down() + { + auto& i = std::get(mIPack); + if constexpr(I != 0){ + if(i->pos() == 0){ + (*i) = i->max()-1; + IB::mPos += std::get(mBlockSize) * i->pos(); + downi(); + return; + } + } + IB::mPos -= std::get(mBlockSize); + --(*i); + } + + template + template + inline String MIndex::mkStringMeta(std::index_sequence is) const + { + const String blim = "["; + const String elim = "]"; + const String dlim = ","; + static_assert(sizeof...(Is) == sizeof...(Indices)-1, + "set last position outside the index pack"); + if constexpr(sizeof...(Is) == 0){ + return blim + std::get<0>(mIPack)->stringMeta() + elim; + } + else { + return blim + + ( ( std::get(mIPack)->stringMeta() + dlim ) + ... + + std::get(mIPack)->stringMeta() ) + + elim; + } + } + + template + template + inline typename MIndex::MetaType MIndex::mkMeta(std::index_sequence is) const + { + return std::make_tuple( std::get(mIPack)->meta()... ); + } + + template + template + constexpr decltype(auto) MIndex::mkIFor(const Xpr& xpr, F&& f) const + { + if constexpr(I == sizeof..(Indices)-1){ + return std::get(mIPack)->ifor(xpr,f); + } + else { + return std::get(mIPack)->ifor( mkIFor( xpr, f ), f ); + } + } + + template + inline void ati(const MetaType& meta, std::index_sequence is) const + { + ( std::get(mIPack)->at( std::get(meta) ), ... ); } /************** @@ -68,18 +177,19 @@ namespace CNORXZ template template MIndex::MIndex(const Sptr& range, SizeT pos) : - IndexInterface,Tuple>(pos), + IndexInterface,Tuple>(0), mIPack(mkIPack(IB::mPos, std::make_index_sequence{})), mBlockSizes(mkBlockSizes(IB::mPos), std::make_index_sequence{}), - mRange(range) - {} + mRange(range), + { + (*this) = pos; + } template MIndex& MIndex::operator()(const Sptr& mi) { - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) { std::get(mIPack) = std::get(indices); return 0; } ); - RangeHelper::setIndexPack(mIPack, IB::mPos); + mIPack = mi.mIPack; + IB::mPos = mkPos(std::make_index_sequence{}); return *this; } @@ -87,78 +197,47 @@ namespace CNORXZ MIndex& MIndex::operator=(SizeT pos) { IB::mPos = pos; - RangeHelper::setIndexPack(mIPack, pos); + setIPack(std::make_index_sequence{}); return *this; } template MIndex& MIndex::operator++() { - sfor_m - ( [&](auto i) { - auto& si = *std::get( mIPack ); - if(si.last() and i != 0) { si = 0; return true; } - else { ++si; return false; } - return false; - } ); - ++IB::mPos; + // End state is defined by high-index being end while all other indices are zero + auto& i0 = std::get<0>(mIPack); + if(i0->pos() != i0->max()){ + up(); + } return *this; } template MIndex& MIndex::operator--() { - sfor_m - ( [&](auto i) { - auto& si = *std::get( mIPack ); - if(si.first() and i != 0) { si = si.max()-1; return true; } - else { --si; return false; } - return false; - } ); - --IB::mPos; + if(IB::mPos != 0){ + down(); + } return *this; } - - template - int MIndex::pp(PtrId idxPtrNum) - { - const int tmp = RangeHelper::ppx(mIPack, mBlockSizes, idxPtrNum); - IB::mPos += tmp; - return tmp; - } - - template - int MIndex::mm(PtrId idxPtrNum) - { - const int tmp = RangeHelper::mmx(mIPack, mBlockSizes, idxPtrNum); - IB::mPos -= tmp; - return tmp; - } - template String MIndex::stringMeta() const { - return std::dynamic_pointer_cast( IB::mRangePtr )->stringMeta(IB::mPos); + return mkStringMeta(std::make_index_sequence{}); } template typename MIndex::MetaType MIndex::meta() const { - MetaType metaTuple; - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) { std::get(metaTuple) = std::get(mIPack)->meta(); return 0; } ); - return metaTuple; + return mkMeta(std::make_index_sequence{}); } template MIndex& MIndex::at(const MetaType& metaPos) { - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) { std::get(mIPack)->at( std::get(metaPos) ); return 0; } ); - IB::mPos = sfor_m - ( [&](auto i) { return std::get(mIPack); }, - [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + ati(metaPos, std::make_index_sequence{}); + IB::mPos = mkPos(std::make_index_sequence{}); return *this; } @@ -176,22 +255,19 @@ namespace CNORXZ } template - SizeT MIndex::getStepSize(SizeT n) + template + decltype(auto) MIndex::stepSize(const IndexId& id) const; { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return mBlockSizes[n+1]; + return accumulate2( [](const auto& i, const auto& b) { return i->stepSize(id) * b }, + [](const auto&... ss) { return ( ss + ... ); }); } - /* - template - template - auto MIndex::ifor(SizeT step, Exprs exs) const + + template + template + constexpr decltype(auto) MIndex::ifor(const Xpr& xpr, F&& f) const { - return RangeHelper::mkFor<0>(step, mIPack, mBlockSizes, exs); + return mkIFor<0>(xpr, f); } - */ /********************* * MRangeFactory * @@ -256,19 +332,7 @@ namespace CNORXZ template String MRange::stringMeta(SizeT pos) const { - - } - - template - IndexType MRange::begin() const - { - - } - - template - IndexType MRange::end() const - { - + return (begin()+pos).stringMeta(); } template @@ -280,14 +344,14 @@ namespace CNORXZ template const MetaType MRange::get(SizeT pos) const { - + return (begin()+pos)->meta(); } template SizeT MRange::getMeta(const MetaType& metaPos) const { - assert(0); - //return RangeHelper::getMeta(mSpace,metaPos); + auto i = begin(); + return i.at(metaPos).pos(); } /************************ diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index 1a7cb3a..074a074 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -52,9 +52,6 @@ namespace CNORXZ SizeT operator*() const; SizeT operator->() const; - int pp(PtrId idxPtrNum); - int mm(PtrId idxPtrNum); - SizeT dim(); Sptr range(); @@ -66,13 +63,15 @@ namespace CNORXZ MIndex& at(const MetaType& metaPos); template - decltype(auto) ifor(const Xpr& xpr, F&& f) const; + constexpr decltype(auto) ifor(const Xpr& xpr, F&& f) const; private: MIndex() = default; IndexPack mIPack; - Arr mBlockSizes; + //Arr mBlockSizes; + typedef decltype(mkBlockSizes(std::make_index_sequence{})) BlockTuple; + BlockTuple mBlockSizes; Sptr mRange; // shift to utils: @@ -87,16 +86,64 @@ namespace CNORXZ template constexpr decltype(auto) accumulatei(const G& g, const F& f, std::index_sequence is) const; + template + constexpr decltype(auto) accumulate2i(const G& g, const F& f, std::index_sequence is) const; + template constexpr decltype(auto) accumulate(const G& g, const F& f) const; + template + constexpr decltype(auto) accumulate2(const G& g, const F& f) const; + template constexpr decltype(auto) mkIPack(SizeT pos, std::index_sequence is) const; + template + inline void setIPack(std::index_sequence is) const; + template constexpr decltype(auto) mkBlockSizes(std::index_sequence is) const; + + template + constexpr decltype(auto) mkPos(std::index_sequence is) const; + + template + inline void up(); + + template + inline void down(); + + template + inline String mkStringMeta(std::index_sequence is) const; + + template + inline MetaType mkMeta(std::index_sequence is) const; + + template + constexpr decltype(auto) mkIFor(const Xpr& xpr, F&& f) const; + + template + inline void ati(const MetaType& meta, std::index_sequence is) const; }; + // 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 mBlockSizes; + + template + constexpr decltype(auto) mkPos(std::index_sequence is) const; + }; // NOT THREAD SAVE template diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h index 746da68..897e836 100644 --- a/src/include/ranges/urange.cc.h +++ b/src/include/ranges/urange.cc.h @@ -92,20 +92,6 @@ namespace CNORXZ return mMetaPtr + IB::mPos; } - template - Int UIndex::pp(PtrId idxPtrNum) - { - ++(*this); - return 1; - } - - template - Int UIndex::mm(PtrId idxPtrNum) - { - --(*this); - return 1; - } - template String UIndex::stringMeta() const { diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index 939665c..4798c56 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -35,9 +35,6 @@ namespace CNORXZ const MetaType& operator*() const; const MetaType* operator->() const; - Int pp(PtrId idxPtrNum); - Int mm(PtrId idxPtrNum); - SizeT dim() const; // = 1 Sptr range() const; diff --git a/src/include/xpr/pos_type.cc.h b/src/include/xpr/pos_type.cc.h index c635180..db8c28d 100644 --- a/src/include/xpr/pos_type.cc.h +++ b/src/include/xpr/pos_type.cc.h @@ -65,11 +65,18 @@ namespace CNORXZ template template - constexpr auto SPos::extend(const PosT& a) const + constexpr decltype(auto) SPos::extend(const PosT& a) const { return MPos,PosT>(*this,a); } + template + template + constexpr decltype(auto) SPos::operator<<(const PosT& a) const + { + return extend(a); + } + template constexpr SPos::operator UPos() const { @@ -121,11 +128,17 @@ namespace CNORXZ } template - constexpr auto UPos::extend(const PosT& p1) const + constexpr decltype(auto) UPos::extend(const PosT& p1) const { return MPos(*this, p1); } + template + constexpr decltype(auto) UPos::operator<<(const PosT& a) const + { + return extend(a); + } + /************ * FPos * ************/ @@ -160,12 +173,18 @@ namespace CNORXZ return UPos(mExt * mMap[a.val()]); } - template - constexpr auto FPos::extend(const PosT1& a) const + template + constexpr decltype(auto) FPos::extend(const PosT& a) const { return MPos(*this,a); } + template + constexpr decltype(auto) FPos::operator<<(const PosT& a) const + { + return extend(a); + } + /************* * SFPos * *************/ @@ -228,11 +247,18 @@ namespace CNORXZ template template - constexpr auto SFPos::extend(const PosT& a) const + constexpr decltype(auto) SFPos::extend(const PosT& a) const { return MPos,PosT>(*this,a); } + template + template + constexpr decltype(auto) SFPos::operator<<(const PosT& a) const + { + return extend(a); + } + template constexpr SFPos::operator FPos() const { @@ -317,12 +343,19 @@ namespace CNORXZ template template - constexpr auto MPos::extend(const PosT& a) const + constexpr decltype(auto) MPos::extend(const PosT& a) const { typedef decltype(mNext.extend(a)) ONPosT; return MPos(*this, mNext.extend(a)); } + template + template + constexpr decltype(auto) MPos::operator<<(const PosT& a) const + { + return extend(a); + } + /************ * DPos * ************/ diff --git a/src/include/xpr/pos_type.h b/src/include/xpr/pos_type.h index 152fbd4..de1e367 100644 --- a/src/include/xpr/pos_type.h +++ b/src/include/xpr/pos_type.h @@ -30,8 +30,11 @@ namespace CNORXZ constexpr auto operator()(const UPos& a) const; template - constexpr auto extend(const PosT& a) const; + constexpr decltype(auto) extend(const PosT& a) const; + template + constexpr decltype(auto) operator<<(const PosT& a) const; + explicit constexpr operator UPos() const; }; @@ -61,8 +64,10 @@ namespace CNORXZ constexpr UPos operator()(const PosT& a) const; template - constexpr auto extend(const PosT& a) const; + constexpr decltype(auto) extend(const PosT& a) const; + template + constexpr decltype(auto) operator<<(const PosT& a) const; }; class FPos @@ -88,9 +93,11 @@ namespace CNORXZ template constexpr UPos operator()(const PosT& a) const; - template - constexpr auto extend(const PosT1& a) const; + template + constexpr decltype(auto) extend(const PosT& a) const; + template + constexpr decltype(auto) operator<<(const PosT& a) const; }; template @@ -116,7 +123,10 @@ namespace CNORXZ constexpr auto operator()(const UPos& a) const; template - constexpr auto extend(const PosT& a) const; + constexpr decltype(auto) extend(const PosT& a) const; + + template + constexpr decltype(auto) operator<<(const PosT& a) const; explicit constexpr operator FPos() const; }; @@ -151,7 +161,10 @@ namespace CNORXZ constexpr auto operator()(const PosT& a) const; template - constexpr auto extend(const PosT& a) const; + constexpr decltype(auto) extend(const PosT& a) const; + + template + constexpr decltype(auto) operator<<(const PosT& a) const; }; // treat as scalar pos!!! @@ -188,6 +201,9 @@ namespace CNORXZ template inline decltype(auto) extend(const PosT& a) const; + + template + inline decltype(auto) operator<<(const PosT& a) const; }; class DPosRef @@ -219,6 +235,9 @@ namespace CNORXZ template inline decltype(auto) extend(const PosT& a) const; + + template + inline decltype(auto) operator<<(const PosT& a) const; }; // for common call of extension vector elements