From fb94a382bbe20754502fb31a3fe8dbde4b12c77c Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 8 Nov 2022 22:36:16 +0100 Subject: [PATCH] base/isq.h + base/iter.* + functional/fundamental.h + adaptions in mrange (WIP) --- src/include/base/base.cc.h | 1 + src/include/base/base.h | 2 + src/include/base/isq.h | 30 +++++ src/include/base/iter.cc.h | 28 +++++ src/include/base/iter.h | 17 +++ src/include/base/types.h | 11 ++ src/include/functional/fundamental.h | 15 +++ src/include/ranges/mrange.cc.h | 163 +++++++-------------------- src/include/ranges/mrange.h | 56 ++------- 9 files changed, 151 insertions(+), 172 deletions(-) create mode 100644 src/include/base/isq.h create mode 100644 src/include/base/iter.cc.h create mode 100644 src/include/base/iter.h create mode 100644 src/include/functional/fundamental.h diff --git a/src/include/base/base.cc.h b/src/include/base/base.cc.h index cd0374a..995301f 100644 --- a/src/include/base/base.cc.h +++ b/src/include/base/base.cc.h @@ -2,3 +2,4 @@ #include "dtype.cc.h" #include "obj_handle.cc.h" #include "to_string.cc.h" +#include "iter.cc.h" diff --git a/src/include/base/base.h b/src/include/base/base.h index 5897c99..c75373f 100644 --- a/src/include/base/base.h +++ b/src/include/base/base.h @@ -22,6 +22,8 @@ #include "types.h" #include "obj_handle.h" #include "dtype.h" +#include "isq.h" +#include "iter.h" #include "base.cc.h" diff --git a/src/include/base/isq.h b/src/include/base/isq.h new file mode 100644 index 0000000..b4d77c3 --- /dev/null +++ b/src/include/base/isq.h @@ -0,0 +1,30 @@ + +#ifndef __cxz_isq_h__ +#define __cxz_isq_h__ + +#include +#include +#include "types.h" + +namespace CNORXZ +{ + template + std::index_sequence<(Is+O)...> mkIsqAdd(std::index_sequence is) { return {}; } + + template + struct MkIsq + { + static auto make() + { + static_assert(B <= E, "begin of sequence must be smaller than end of sequence"); + return mkIsqAdd( std::make_index_sequence ); + } + + typedef decltype(make()) type; + }; + + template + using Isqr = MkIsq::type; +} + +#endif diff --git a/src/include/base/iter.cc.h b/src/include/base/iter.cc.h new file mode 100644 index 0000000..8289dad --- /dev/null +++ b/src/include/base/iter.cc.h @@ -0,0 +1,28 @@ + +#ifndef __cxz_iter_cc_h__ +#define __cxz_iter_cc_h__ + +#include "iter.h" +#include "functional/fundamental.h" + +namespace CNORXZ +{ + template + constexpr decltype(auto) iteri(const G& g, const F& f, Isq is) + { + if constexpr(std::is_same::value){ + ( g(std::integral_constant{}, args...), ... ); + } + else { + return f( g(std::integral_constant{}, args...) ... ); + } + } + + template + constexpr decltype(auto) iter(const G& g, const F& f) + { + return iteri(g, f, Isqr{}); + } +} + +#endif diff --git a/src/include/base/iter.h b/src/include/base/iter.h new file mode 100644 index 0000000..abc088e --- /dev/null +++ b/src/include/base/iter.h @@ -0,0 +1,17 @@ + +#ifndef __cxz_iter_h__ +#define __cxz_iter_h__ + +#include +#include "types.h" + +namespace CNORXZ +{ + template + constexpr decltype(auto) iteri(const G& g, const F& f, Isq is); + + template + constexpr decltype(auto) iter(const G& g, const F& f); +} + +#endif diff --git a/src/include/base/types.h b/src/include/base/types.h index dba8140..07c288f 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -10,6 +10,7 @@ #include #include #include +#include namespace CNORXZ { @@ -43,10 +44,20 @@ namespace CNORXZ template using TupleElem = std::tuple_element>; + template + using RemoveRef = std::remove_reference::type; + template using Map = std::map; typedef std::type_info TypeInfo; + + template + using Isq = std::index_sequence; + // cxz helper functions/classes: -> isq.h + + template + using Isqf = std::index_sequence_for; /********************* * library types * diff --git a/src/include/functional/fundamental.h b/src/include/functional/fundamental.h new file mode 100644 index 0000000..9a7d707 --- /dev/null +++ b/src/include/functional/fundamental.h @@ -0,0 +1,15 @@ + +#ifndef __cxz_fundamental_h__ +#define __cxz_fundamental_h__ + +#include + +namespace CNORXZ +{ + struct NoF + { + constexpr void operator()() const {}; + }; +} + +#endif diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 7648e7c..ce7a328 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -12,85 +12,25 @@ namespace CNORXZ * MIndex (private) * ***********************/ - template - template - constexpr decltype(auto) MIndex::indexSequencePlus(std::index_sequence is) const - { - return std::index_sequence {}; - } - - template - template - constexpr decltype(auto) MIndex::mkIndexSequence() const - { - return indexSequencePlus(std::make_index_sequence{}); - } - - template - template - constexpr decltype(auto) MIndex::accumulatei(const G& g, const F& f, std::index_sequence is) const - { - 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 - constexpr decltype(auto) MIndex::accumulate(const G& g, const F& f) const - { - 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) MIndex::mkIPack(SizeT pos, std::index_sequence is) const + constexpr decltype(auto) MIndex::mkIPack(SizeT pos, Isq is) const { - static_assert(sizeof...(Is) == sizeof...(Indices), + static_assert(sizeof...(Is) == NI, "sequence size does not match number of indices"); return std::make_tuple( std::make_shared( mRange->sub(Is) )... ); - //, (pos / std::get(mBlockSizes) ) % )... ); } template - template - inline void MIndex::setIPack(std::index_sequence is) const + constexpr decltype(auto) MIndex::mkBlockSizes() 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 std::make_tuple - ( accumulate - ( [](const auto& i) { return i->max(); }, - [](const auto&... as) { return (as * ...); } )..., + ( iter + ( [&](auto i) { return std::get(mIPack)->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() @@ -125,33 +65,6 @@ namespace CNORXZ --(*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 @@ -164,12 +77,6 @@ namespace CNORXZ } } - template - inline void ati(const MetaType& meta, std::index_sequence is) const - { - ( std::get(mIPack)->at( std::get(meta) ), ... ); - } - /************** * MIndex * **************/ @@ -178,8 +85,8 @@ namespace CNORXZ template MIndex::MIndex(const Sptr& range, SizeT pos) : IndexInterface,Tuple>(0), - mIPack(mkIPack(IB::mPos, std::make_index_sequence{})), - mBlockSizes(mkBlockSizes(IB::mPos), std::make_index_sequence{}), + mIPack(mkIPack(IB::mPos, Isqr<0,NI>{})), + mBlockSizes(mkBlockSizes(IB::mPos), Isqr<0,NI>{}), mRange(range), { (*this) = pos; @@ -189,7 +96,9 @@ namespace CNORXZ MIndex& MIndex::operator()(const Sptr& mi) { mIPack = mi.mIPack; - IB::mPos = mkPos(std::make_index_sequence{}); + IB::mPos = iter<0,NI> + ( [&](auto i) { return std::get(mIPack)*std::get(mBlockSizes); }, + [](const auto&... xs) { return (xs + ...); }); return *this; } @@ -197,7 +106,7 @@ namespace CNORXZ MIndex& MIndex::operator=(SizeT pos) { IB::mPos = pos; - setIPack(std::make_index_sequence{}); + iter<0,NI>( [&](auto i) { std::get(mIPack) = (IB::mPos / std::get(mBlockSize)) % std::get(mIPack)->max() }, NoF{} ); return *this; } @@ -207,7 +116,7 @@ namespace CNORXZ // 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(); + up(); } return *this; } @@ -216,7 +125,7 @@ namespace CNORXZ MIndex& MIndex::operator--() { if(IB::mPos != 0){ - down(); + down(); } return *this; } @@ -224,27 +133,37 @@ namespace CNORXZ template String MIndex::stringMeta() const { - return mkStringMeta(std::make_index_sequence{}); + const String blim = "["; + const String elim = "]"; + const String dlim = ","; + return iter<1,NI> + ( [&](auto i) { return std::get(mIPack)->stringMeta(); }, + [&](const auto&... xs) { + return blim + std::get<0>(mIPack)->stringMeta() + ( (dlim + xs) + ... ) + elim; + } ); } template typename MIndex::MetaType MIndex::meta() const { - return mkMeta(std::make_index_sequence{}); + 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) { - ati(metaPos, std::make_index_sequence{}); - IB::mPos = mkPos(std::make_index_sequence{}); + iter<0,NI>( [&](auto i) { std::get(mIPack)->at( std::get(meta) ) }, NoF {} ); + IB::mPos = iter<0,NI> + ( [&](auto i) { return std::get(mIPack)*std::get(mBlockSizes); }, + [](const auto&... xs) { return (xs + ...); }); return *this; } template SizeT MIndex::dim() { - return sizeof...(Indices); + return NI; } template @@ -258,8 +177,9 @@ namespace CNORXZ template decltype(auto) MIndex::stepSize(const IndexId& id) const; { - return accumulate2( [](const auto& i, const auto& b) { return i->stepSize(id) * b }, - [](const auto&... ss) { return ( ss + ... ); }); + return iter<0,NI> + ( [&](auto i) { return std::get(mIPack)->stepSize(id) * std::get(mBlockSize); }, + [](const auto&... ss) { return ( ss + ... ); }); } template @@ -307,7 +227,7 @@ namespace CNORXZ template MRange::MRange(const Tuple...>& rs) : mRs(rs), - mA( mkA( std::make_index_sequence{} ) ) + mA( mkA( std::make_index_sequence{} ) ) {} template @@ -320,13 +240,14 @@ namespace CNORXZ template SizeT MRange::size() const { - return this->sizei(std::make_index_sequence {}); + return iter<0,NR>( [&](auto i) { return std::get(mRs)->size() }, + [](const auto&... xs) { return (xs * ...); } ); } template SizeT MRange::dim() const { - return sizeof...(Ranges); + return NR; } template @@ -359,16 +280,10 @@ namespace CNORXZ ************************/ template - template - decltype(auto) MRange::mkA(std::index_sequence is) const + decltype(auto) MRange::mkA() const { - return Arr { std::get(mRs)... }; - } - - template - SizeT MRange::sizei(std::index_sequence is) const - { - return ( std::get(mRs)->size() * ... ); + return iter<0,NR>([&](auto i) { return std::get(mRs); }, + [](const auto&... xs) { return Arr { xs... } } ); } } diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index 074a074..0b584f0 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -22,6 +22,7 @@ namespace CNORXZ typedef Tuple...> IndexPack; typedef Tuple MetaType; typedef MRange RangeType; + static constexpr SizeT NI = sizeof...(Indices); // NO DEFAULT HERE !!! // ( have to assign sub-indices (ptr!) correctly ) @@ -69,43 +70,14 @@ namespace CNORXZ MIndex() = default; IndexPack mIPack; - //Arr mBlockSizes; - typedef decltype(mkBlockSizes(std::make_index_sequence{})) BlockTuple; + typedef decltype(mkBlockSizes(Isqr<0,NI-1>{})) BlockTuple; BlockTuple mBlockSizes; Sptr mRange; - // shift to utils: - template - constexpr decltype(auto) indexSequencePlus(std::index_sequence is) const; - - // shift to utils: - template - constexpr decltype(auto) mkIndexSequence() const; - - - 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; + constexpr decltype(auto) mkIPack(SizeT pos, Isq 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; + constexpr decltype(auto) mkBlockSizes() const; template inline void up(); @@ -113,17 +85,8 @@ namespace CNORXZ 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 @@ -142,7 +105,7 @@ namespace CNORXZ BlockType mBlockSizes; template - constexpr decltype(auto) mkPos(std::index_sequence is) const; + constexpr decltype(auto) mkPos(Isq is) const; }; // NOT THREAD SAVE @@ -169,6 +132,7 @@ namespace CNORXZ typedef RangeBase RB; typedef MIndex IndexType; typedef Tuple MetaType; + static constexpr SizeT NR = sizeof...(Ranges); friend MRangeFactory; @@ -190,14 +154,10 @@ namespace CNORXZ MRange(const Tuple...>& rs); Tuple...> mRs; - Arr mA; + Arr mA; private: - template - decltype(auto) mkA(std::index_sequence is) const; - - template - SizeT sizei(std::index_sequence is) const; + decltype(auto) mkA() const; }; }