more on MIndex + operator << for PosTypes

This commit is contained in:
Christian Zimmermann 2022-11-06 22:58:46 +01:00
parent 172110c5de
commit 0197e7db6e
7 changed files with 265 additions and 118 deletions

View file

@ -264,6 +264,7 @@ namespace CNORXZ
template <class F, class... Ops>
constexpr decltype(auto) Operation<F,Ops...>::rootSteps(const IndexId<I>& id) const
{
// !!!
return mOps.rootSteps(id);
}

View file

@ -33,6 +33,13 @@ namespace CNORXZ
return f( g(std::get<Is>(mIPack))... );
}
template <class... Indices>
template <class G, class F, SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::accumulate2i(const G& g, const F& f, std::index_sequence<Is...> is) const
{
return f( g(std::get<Is>(mIPack), std::get<Is>(mBlockSizes))... );
}
template <class... Indices>
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) MIndex<Indices...>::accumulate(const G& g, const F& f) const
@ -41,24 +48,126 @@ namespace CNORXZ
}
template <class... Indices>
template <SizeT... Is>
constexpr decltype(auto) mkIPack(SizeT pos, std::index_sequence<Is...> is) const
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) MIndex<Indices...>::accumulate2(const G& g, const F& f) const
{
static_assert(sizeof...(Is) == sizeof...(Indices),
"sequence size does not match number of indices");
return std::make_tuple( std::make_shared<Indices>( mRange->sub(Is) )... );
return accumulate2i(g, f, mkIndexSequence<B,E>())
}
template <class... Indices>
template <SizeT... Is>
constexpr decltype(auto) mkBlockSizes(std::index_sequence<Is...> is) const
constexpr decltype(auto) MIndex<Indices...>::mkIPack(SizeT pos, std::index_sequence<Is...> is) const
{
static_assert(sizeof...(Is) == sizeof...(Indices),
"sequence size does not match number of indices");
return std::make_tuple( std::make_shared<Indices>( mRange->sub(Is) )... );
//, (pos / std::get<Is>(mBlockSizes) ) % )... );
}
template <class... Indices>
template <SizeT... Is>
inline void MIndex<Indices...>::setIPack(std::index_sequence<Is...> is) const
{
( (std::get<Is>(mIPack) = (IB::mPos / std::get<Is>(mBlockSize)) % std::get<Is>(mIPack)->max() ),... );
}
template <class... Indices>
template <SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::mkBlockSizes(std::index_sequence<Is...> is) const
{
static_assert(sizeof...(Is) == sizeof...(Indices)-1,
"set lowest block size manually");
return Arr<SizeT,sizeof...(Indices)>
{ accumulate<Is,sizeof...(Indices)>( [](const auto& i) { return i->max(); },
[](const auto&... as) { return (as * ...); } ),
1 };
return std::make_tuple
( accumulate<Is,sizeof...(Indices)>
( [](const auto& i) { return i->max(); },
[](const auto&... as) { return (as * ...); } )...,
SPos<1>() );
}
template <class... Indices>
template <SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::mkPos(std::index_sequence<Is...> is) const
{
return ( (std::get<Is>(mIPack)*std::get<Is>(mBlockSizes)) + ... );
}
template <class... Indices>
template <SizeT I>
inline void MIndex<Indices...>::up()
{
auto& i = std::get<I>(mIPack);
if constexpr(I != 0){
if(i->pos() == i->max()-1){
IB::mPos -= std::get<I>(mBlockSize) * i->pos();
(*i) = 0;
upi<N-1>();
return;
}
}
IB::mPos += std::get<I>(mBlockSize);
++(*i);
}
template <class... Indices>
template <SizeT I>
inline void MIndex<Indices...>::down()
{
auto& i = std::get<I>(mIPack);
if constexpr(I != 0){
if(i->pos() == 0){
(*i) = i->max()-1;
IB::mPos += std::get<I>(mBlockSize) * i->pos();
downi<N-1>();
return;
}
}
IB::mPos -= std::get<I>(mBlockSize);
--(*i);
}
template <class... Indices>
template <SizeT... Is>
inline String MIndex<Indices...>::mkStringMeta(std::index_sequence<Is...> 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<Is>(mIPack)->stringMeta() + dlim ) + ... +
std::get<sizeof...(Indices)-1>(mIPack)->stringMeta() )
+ elim;
}
}
template <class... Indices>
template <SizeT... Is>
inline typename MIndex<Indices...>::MetaType MIndex<Indices...>::mkMeta(std::index_sequence<Is...> is) const
{
return std::make_tuple( std::get<Is>(mIPack)->meta()... );
}
template <class... Indices>
template <SizeT I, class Xpr, class F>
constexpr decltype(auto) MIndex<Indices...>::mkIFor(const Xpr& xpr, F&& f) const
{
if constexpr(I == sizeof..(Indices)-1){
return std::get<I>(mIPack)->ifor(xpr,f);
}
else {
return std::get<I>(mIPack)->ifor( mkIFor<I+1>( xpr, f ), f );
}
}
template <SizeT... Is>
inline void ati(const MetaType& meta, std::index_sequence<Is...> is) const
{
( std::get<Is>(mIPack)->at( std::get<Is>(meta) ), ... );
}
/**************
@ -68,18 +177,19 @@ namespace CNORXZ
template <class... Indices>
template <class MRange>
MIndex<Indices...>::MIndex(const Sptr<MRange>& range, SizeT pos) :
IndexInterface<MIndex<Indices...>,Tuple<typename Indices::MetaType...>>(pos),
IndexInterface<MIndex<Indices...>,Tuple<typename Indices::MetaType...>>(0),
mIPack(mkIPack(IB::mPos, std::make_index_sequence<sizeof...(Indices)>{})),
mBlockSizes(mkBlockSizes(IB::mPos), std::make_index_sequence<sizeof...(Indices)>{}),
mRange(range)
{}
mRange(range),
{
(*this) = pos;
}
template <class... Indices>
MIndex<Indices...>& MIndex<Indices...>::operator()(const Sptr<MIndex>& mi)
{
sfor_pn<0,sizeof...(Indices)>
( [&](auto i) { std::get<i>(mIPack) = std::get<i>(indices); return 0; } );
RangeHelper::setIndexPack<sizeof...(Indices)-1>(mIPack, IB::mPos);
mIPack = mi.mIPack;
IB::mPos = mkPos(std::make_index_sequence<sizeof...(Indices)>{});
return *this;
}
@ -87,78 +197,47 @@ namespace CNORXZ
MIndex<Indices...>& MIndex<Indices...>::operator=(SizeT pos)
{
IB::mPos = pos;
RangeHelper::setIndexPack<sizeof...(Indices)-1>(mIPack, pos);
setIPack(std::make_index_sequence<sizeof...(Indices)>{});
return *this;
}
template <class... Indices>
MIndex<Indices...>& MIndex<Indices...>::operator++()
{
sfor_m<sizeof...(Indices),0>
( [&](auto i) {
auto& si = *std::get<i>( 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<sizeof...(Indices)-1>();
}
return *this;
}
template <class... Indices>
MIndex<Indices...>& MIndex<Indices...>::operator--()
{
sfor_m<sizeof...(Indices),0>
( [&](auto i) {
auto& si = *std::get<i>( 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<sizeof...(Indices)-1>();
}
return *this;
}
template <class... Indices>
int MIndex<Indices...>::pp(PtrId idxPtrNum)
{
const int tmp = RangeHelper::ppx<sizeof...(Indices)-1>(mIPack, mBlockSizes, idxPtrNum);
IB::mPos += tmp;
return tmp;
}
template <class... Indices>
int MIndex<Indices...>::mm(PtrId idxPtrNum)
{
const int tmp = RangeHelper::mmx<sizeof...(Indices)-1>(mIPack, mBlockSizes, idxPtrNum);
IB::mPos -= tmp;
return tmp;
}
template <class... Indices>
String MIndex<Indices...>::stringMeta() const
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr )->stringMeta(IB::mPos);
return mkStringMeta(std::make_index_sequence<sizeof...(Indices)-1>{});
}
template <class... Indices>
typename MIndex<Indices...>::MetaType MIndex<Indices...>::meta() const
{
MetaType metaTuple;
sfor_pn<0,sizeof...(Indices)>
( [&](auto i) { std::get<i>(metaTuple) = std::get<i>(mIPack)->meta(); return 0; } );
return metaTuple;
return mkMeta(std::make_index_sequence<sizeof...(Indices)>{});
}
template <class... Indices>
MIndex<Indices...>& MIndex<Indices...>::at(const MetaType& metaPos)
{
sfor_pn<0,sizeof...(Indices)>
( [&](auto i) { std::get<i>(mIPack)->at( std::get<i>(metaPos) ); return 0; } );
IB::mPos = sfor_m<sizeof...(Indices),0>
( [&](auto i) { return std::get<i>(mIPack); },
[&](auto a, auto b) {return a->pos() + b*a->max();}, 0 );
ati(metaPos, std::make_index_sequence<sizeof...(Indices)>{});
IB::mPos = mkPos(std::make_index_sequence<sizeof...(Indices)>{});
return *this;
}
@ -176,22 +255,19 @@ namespace CNORXZ
}
template <class... Indices>
SizeT MIndex<Indices...>::getStepSize(SizeT n)
template <SizeT I>
decltype(auto) MIndex<Indices...>::stepSize(const IndexId<I>& 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 <class... Indices>
template <class Exprs>
auto MIndex<Indices...>::ifor(SizeT step, Exprs exs) const
template <class Xpr, class F>
constexpr decltype(auto) MIndex<Indices...>::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 <class... Ranges>
String MRange<Ranges...>::stringMeta(SizeT pos) const
{
}
template <class... Ranges>
IndexType MRange<Ranges...>::begin() const
{
}
template <class... Ranges>
IndexType MRange<Ranges...>::end() const
{
return (begin()+pos).stringMeta();
}
template <class... Ranges>
@ -280,14 +344,14 @@ namespace CNORXZ
template <class... Ranges>
const MetaType MRange<Ranges...>::get(SizeT pos) const
{
return (begin()+pos)->meta();
}
template <class... Ranges>
SizeT MRange<Ranges...>::getMeta(const MetaType& metaPos) const
{
assert(0);
//return RangeHelper::getMeta<sizeof...(Ranges)-1>(mSpace,metaPos);
auto i = begin();
return i.at(metaPos).pos();
}
/************************

View file

@ -52,9 +52,6 @@ namespace CNORXZ
SizeT operator*() const;
SizeT operator->() const;
int pp(PtrId idxPtrNum);
int mm(PtrId idxPtrNum);
SizeT dim();
Sptr<RangeType> range();
@ -66,13 +63,15 @@ namespace CNORXZ
MIndex& at(const MetaType& metaPos);
template <class Xpr, class F>
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<SizeT,sizeof...(Indices)> mBlockSizes;
//Arr<SizeT,sizeof...(Indices)> mBlockSizes;
typedef decltype(mkBlockSizes(std::make_index_sequence<sizeof...(Indices)-1>{})) BlockTuple;
BlockTuple mBlockSizes;
Sptr<RangeType> mRange;
// shift to utils:
@ -87,16 +86,64 @@ namespace CNORXZ
template <class G, class F, SizeT... Is>
constexpr decltype(auto) accumulatei(const G& g, const F& f, std::index_sequence<Is...> is) const;
template <class G, class F, SizeT... Is>
constexpr decltype(auto) accumulate2i(const G& g, const F& f, std::index_sequence<Is...> is) const;
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) accumulate(const G& g, const F& f) const;
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) accumulate2(const G& g, const F& f) const;
template <SizeT... Is>
constexpr decltype(auto) mkIPack(SizeT pos, std::index_sequence<Is...> is) const;
template <SizeT... Is>
inline void setIPack(std::index_sequence<Is...> is) const;
template <SizeT... Is>
constexpr decltype(auto) mkBlockSizes(std::index_sequence<Is...> is) const;
template <SizeT... Is>
constexpr decltype(auto) mkPos(std::index_sequence<Is...> is) const;
template <SizeT I>
inline void up();
template <SizeT I>
inline void down();
template <SizeT... Is>
inline String mkStringMeta(std::index_sequence<Is...> is) const;
template <SizeT... Is>
inline MetaType mkMeta(std::index_sequence<Is...> is) const;
template <SizeT I, class Xpr, class F>
constexpr decltype(auto) mkIFor(const Xpr& xpr, F&& f) const;
template <SizeT... Is>
inline void ati(const MetaType& meta, std::index_sequence<Is...> is) const;
};
// modified blockSizes; to be used for Slices; can be created from MIndices
template <class MIndexType, class BlockType>
class GMIndex : public MIndexType
{
public:
// override everything that modyfies IB::mPos or uses mBlockSizes!!!
constexpr GMIndex(const MIndexType& mi, const BlockType& b);
template <class Xpr, class F>
constexpr decltype(auto) ifor(const Xpr& xpr, F&& f) const;
private:
BlockType mBlockSizes;
template <SizeT... Is>
constexpr decltype(auto) mkPos(std::index_sequence<Is...> is) const;
};
// NOT THREAD SAVE
template <class... Ranges>

View file

@ -92,20 +92,6 @@ namespace CNORXZ
return mMetaPtr + IB::mPos;
}
template <typename MetaType>
Int UIndex<MetaType>::pp(PtrId idxPtrNum)
{
++(*this);
return 1;
}
template <typename MetaType>
Int UIndex<MetaType>::mm(PtrId idxPtrNum)
{
--(*this);
return 1;
}
template <typename MetaType>
String UIndex<MetaType>::stringMeta() const
{

View file

@ -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<RangeType> range() const;

View file

@ -65,11 +65,18 @@ namespace CNORXZ
template <SizeT N>
template <class PosT>
constexpr auto SPos<N>::extend(const PosT& a) const
constexpr decltype(auto) SPos<N>::extend(const PosT& a) const
{
return MPos<SPos<N>,PosT>(*this,a);
}
template <SizeT N>
template <class PosT>
constexpr decltype(auto) SPos<N>::operator<<(const PosT& a) const
{
return extend(a);
}
template <SizeT N>
constexpr SPos<N>::operator UPos() const
{
@ -121,11 +128,17 @@ namespace CNORXZ
}
template <class PosT>
constexpr auto UPos::extend(const PosT& p1) const
constexpr decltype(auto) UPos::extend(const PosT& p1) const
{
return MPos<UPos,PosT>(*this, p1);
}
template <class PosT>
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 <class PosT1>
constexpr auto FPos::extend(const PosT1& a) const
template <class PosT>
constexpr decltype(auto) FPos::extend(const PosT& a) const
{
return MPos<FPos,PosT1>(*this,a);
}
template <class PosT>
constexpr decltype(auto) FPos::operator<<(const PosT& a) const
{
return extend(a);
}
/*************
* SFPos *
*************/
@ -228,11 +247,18 @@ namespace CNORXZ
template <SizeT N, SizeT... Ms>
template <class PosT>
constexpr auto SFPos<N,Ms...>::extend(const PosT& a) const
constexpr decltype(auto) SFPos<N,Ms...>::extend(const PosT& a) const
{
return MPos<SFPos<N,Ms...>,PosT>(*this,a);
}
template <SizeT N, SizeT... Ms>
template <class PosT>
constexpr decltype(auto) SFPos<N,Ms...>::operator<<(const PosT& a) const
{
return extend(a);
}
template <SizeT N, SizeT... Ms>
constexpr SFPos<N,Ms...>::operator FPos() const
{
@ -317,12 +343,19 @@ namespace CNORXZ
template <class BPosT, class NPosT>
template <class PosT>
constexpr auto MPos<BPosT,NPosT>::extend(const PosT& a) const
constexpr decltype(auto) MPos<BPosT,NPosT>::extend(const PosT& a) const
{
typedef decltype(mNext.extend(a)) ONPosT;
return MPos<BPosT,ONPosT>(*this, mNext.extend(a));
}
template <class BPosT, class NPosT>
template <class PosT>
constexpr decltype(auto) MPos<BPosT,NPosT>::operator<<(const PosT& a) const
{
return extend(a);
}
/************
* DPos *
************/

View file

@ -30,7 +30,10 @@ namespace CNORXZ
constexpr auto operator()(const UPos& a) const;
template <class PosT>
constexpr auto extend(const PosT& a) const;
constexpr decltype(auto) extend(const PosT& a) const;
template <class PosT>
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 <class PosT>
constexpr auto extend(const PosT& a) const;
constexpr decltype(auto) extend(const PosT& a) const;
template <class PosT>
constexpr decltype(auto) operator<<(const PosT& a) const;
};
class FPos
@ -88,9 +93,11 @@ namespace CNORXZ
template <class PosT>
constexpr UPos operator()(const PosT& a) const;
template <class PosT1>
constexpr auto extend(const PosT1& a) const;
template <class PosT>
constexpr decltype(auto) extend(const PosT& a) const;
template <class PosT>
constexpr decltype(auto) operator<<(const PosT& a) const;
};
template <SizeT N, SizeT... Ms>
@ -116,7 +123,10 @@ namespace CNORXZ
constexpr auto operator()(const UPos& a) const;
template <class PosT>
constexpr auto extend(const PosT& a) const;
constexpr decltype(auto) extend(const PosT& a) const;
template <class PosT>
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 <class PosT>
constexpr auto extend(const PosT& a) const;
constexpr decltype(auto) extend(const PosT& a) const;
template <class PosT>
constexpr decltype(auto) operator<<(const PosT& a) const;
};
// treat as scalar pos!!!
@ -188,6 +201,9 @@ namespace CNORXZ
template <class PosT>
inline decltype(auto) extend(const PosT& a) const;
template <class PosT>
inline decltype(auto) operator<<(const PosT& a) const;
};
class DPosRef
@ -219,6 +235,9 @@ namespace CNORXZ
template <class PosT>
inline decltype(auto) extend(const PosT& a) const;
template <class PosT>
inline decltype(auto) operator<<(const PosT& a) const;
};
// for common call of extension vector elements