#ifndef __cxz_mrange_cc_h__ #define __cxz_mrange_cc_h__ #include "mrange.h" #include "range_helper.h" #include "statics/static_for.h" namespace CNORXZ { /************** * MIndex * **************/ template template MIndex::MIndex(const Sptr& range) : IndexInterface,Tuple >(0) { std::get(mBlockSizes) = 1; sfor_mn ( [&](auto i) { auto r = range->template getPtr(); std::get(mIPack) = r->beginPtr(); *std::get(mIPack) = 0; std::get(mBlockSizes) = sfor_p ( [&](auto j) { return std::get(mIPack)->max(); } , [&](auto a, auto b) { return a * b; }); return 0; }); IB::mPos = sfor_m ( [&](auto i) { return std::get(mIPack); }, [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); } template MIndex& MIndex::operator()(Sptr&... indices) { return (*this)(std::make_tuple(indices...)); } template MIndex& MIndex::operator()(const Tuple...>& indices) { sfor_pn<0,sizeof...(Indices)> ( [&](auto i) { std::get(mIPack) = std::get(indices); return 0; } ); RangeHelper::setIndexPack(mIPack, IB::mPos); return *this; } template MIndex& MIndex::operator=(SizeT pos) { IB::mPos = pos; RangeHelper::setIndexPack(mIPack, pos); 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; 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; 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); } 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; } 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 ); return *this; } template SizeT MIndex::dim() { return sizeof...(Indices); } template Sptr::RangeType> MIndex::range() { return std::dynamic_pointer_cast( mRangePtr ); } template SizeT MIndex::getStepSize(SizeT n) { if(n >= sizeof...(Indices)){ assert(0); // throw !! } return mBlockSizes[n+1]; } /* template template auto MIndex::ifor(SizeT step, Exprs exs) const { return RangeHelper::mkFor<0>(step, mIPack, mBlockSizes, exs); } template template auto MIndex::iforh(SizeT step, Exprs exs) const { return RangeHelper::mkForh<0>(step, mIPack, mBlockSizes, exs); } template template auto MIndex::pifor(SizeT step, Exprs exs) const { return RangeHelper::mkPFor<0>(step, mIPack, mBlockSizes, exs); } */ /********************* * MRangeFactory * *********************/ template MRangeFactory::MRangeFactory(const Sptr&... rs) { mProd = Sptr< MRange >( new MRange( rs... ) ); } template MRangeFactory::MRangeFactory(const typename MRange::Space& st) { mProd = Sptr< MRange >( new MRange( st ) ); } template Sptr MRangeFactory::create() { mProd = checkIfCreated( std::dynamic_pointer_cast( mProd )->mSpace ); setSelf(); return mProd; } /****************** * MRange * ******************/ template MRange::MRange(const Sptr&... rs) : mSpace(std::make_tuple(rs...)) {} template MRange::MRange(const Space& space) : mSpace( space ) {} template SizeT MRange::getMeta(const MetaType& metaPos) const { return RangeHelper::getMeta(mSpace,metaPos); } template SizeT MRange::dim() const { return sdim; } template SizeT MRange::size() const { return sfor_p<0,sizeof...(Ranges)> ( [&](auto i) { return std::get(mSpace)->size(); }, [&](auto a, auto b) { return a * b; } ); } template String MRange::stringMeta(SizeT pos) const { auto i = begin(); i = pos; return "[" + RangeHelper::getStringMeta<0>(i) + "]"; } } #endif