diff --git a/src/include/base/to_string.cc.h b/src/include/base/to_string.cc.h index dac3678..34e2adf 100644 --- a/src/include/base/to_string.cc.h +++ b/src/include/base/to_string.cc.h @@ -55,6 +55,12 @@ namespace CNORXZ } ); } + template + String ToString>::func(const std::pair& p) + { + return String("(") + toString(p.first) + "," + toString(p.second) + ")"; + } + template String toString(const T& a) { diff --git a/src/include/base/to_string.h b/src/include/base/to_string.h index 08f055b..f5cd2e5 100644 --- a/src/include/base/to_string.h +++ b/src/include/base/to_string.h @@ -35,7 +35,13 @@ namespace CNORXZ { static String func(const Tuple& t); }; - + + template + struct ToString> + { + static String func(const std::pair& t); + }; + template <> struct ToString { diff --git a/src/include/ranges/prange.cc.h b/src/include/ranges/prange.cc.h new file mode 100644 index 0000000..c283682 --- /dev/null +++ b/src/include/ranges/prange.cc.h @@ -0,0 +1,211 @@ + +#ifndef __cxz_prange_cc_h__ +#define __cxz_prange_cc_h__ + +#include "prange.h" + +namespace CNORXZ +{ + /************** + * PIndex * + **************/ + + template + PIndex::PIndex(const RangePtr& range, SizeT pos) : + IndexInterface(pos), + mRangePtr(rangeCast(range)), + mIndex(mRangePtr->orig(),mRangePtr->parts()[pos]) + {} + + template + PIndex& PIndex::operator=(SizeT lexpos) + { + IB::mPos = lexpos; + *mOrig = mRangePtr->parts()[IB::mPos]; + return *this; + } + + template + PIndex& PIndex::operator++() + { + ++IB::mPos; + *mOrig = mRangePtr->parts()[IB::mPos]; + return *this; + } + + template + PIndex& PIndex::operator--() + { + --IB::mPos; + *mOrig = mRangePtr->parts()[IB::mPos]; + return *this; + } + + template + PIndex PIndex::operator+(Int n) const + { + return PIndex(mRangePtr, IB::mPos + n); + } + + template + PIndex PIndex::operator-(Int n) const + { + return PIndex(mRangePtr, IB::mPos - n); + } + + template + PIndex& PIndex::operator+=(Int n) + { + IB::mPos += n; + *mOrig = mRangePtr->parts()[IB::mPos]; + return *this; + } + + template + PIndex& PIndex::operator-=(Int n) + { + IB::mPos -= n; + *mOrig = mRangePtr->parts()[IB::mPos]; + return *this; + } + + template + SizeT PIndex::lex() const + { + return IB::mPos; + } + + template + UPos PIndex::pmax() const + { + return UPos(mRangePtr->size()); + } + + template + UPos PIndex::lmax() const + { + return UPos(mRangePtr->size()); + } + + template + IndexId<0> PIndex::id() const + { + return IndexId<0>(this->ptrId()); + } + + template + const typename PIndex::MetaType& PIndex::operator*() const + { + return **mOrig; + } + + template + SizeT PIndex::dim() const + { + return 1; + } + + template + Sptr PIndex::range() const + { + return mRangePtr; + } + + template + template + UPos PIndex::stepSize(const IndexId& id) const + { + if(id == this->id()){ + return UPos(1); + } + else { + return mOrig->stepSize(id); + } + } + + template + String PIndex::stringMeta() const + { + return mOrig->stringMeta(); + } + + template + const MetaT& PIndex::meta() const + { + return mOrig->meta(); + } + + template + PIndex& PIndex::at(const MetaT& metaPos) + { + mOrig->at(metaPos); + mkPos(); + return *this; + } + + template + decltype(auto) PIndex::xpr(const Sptr>& _this) const + { + return poperation( mOrig->xpr(mOrig), mRangePtr->parts(), _this ); + } + + template + template + decltype(auto) PIndex::format(const Sptr& ind) const + { + return ind; + } + + template + template + decltype(auto) PIndex::slice(const Sptr& ind) const + { + if(ind != nullptr){ + if(ind->dim() != 0){ + return Sptr>(); + } + } + return std::make_shared>(*this); + } + + template + template + decltype(auto) PIndex::ifor(const Xpr& xpr, F&& f) const + { + return PFor<0,0,Xpr,F>(this->lmax().val(), this->id(), mOrig->id(), xpr, std::forward(f)); + } + + template + PIndex& PIndex::operator()() + { + mkPos() + return *this; + } + + template + PIndex& PIndex::operator()(const Sptr& i) + { + mOrig = i; + mkPos(); + return *this; + } + + /************************ + * PIndex (private) * + ************************/ + + template + void PIndex::mkPos() + { + const SizeT opos = mOrig->lex(); + IB::mPos = 0; + for(const auto& x: mRangePtr->parts()){ + if(x == opos){ + return *this; + } + ++IB::mPos; + } + CXZ_ERROR("meta position '" << metaPos << "' not part of range"); + } + +} diff --git a/src/include/ranges/prange.h b/src/include/ranges/prange.h index 7c5f0a3..cd39093 100644 --- a/src/include/ranges/prange.h +++ b/src/include/ranges/prange.h @@ -1,498 +1,115 @@ -#ifndef __cxz_subrange_h__ -#define __cxz_subrange_h__ +#ifndef __cxz_prange_h__ +#define __cxz_prange_h__ -#include -#include -#include -#include - -#include "base_def.h" +#include "base/base.h" #include "ranges/index_base.h" #include "ranges/range_base.h" -#include "ranges/x_to_string.h" -#include "ranges/type_map.h" - -#include "xpr/for_type.h" +#include "xpr/xpr.h" namespace CNORXZ { - namespace - { - using namespace CNORXZInternal; - } template - class SubIndex : public IndexInterface,typename Index::MetaType> + class PIndex : public IndexInterface { public: - - typedef IndexInterface,typename Index::MetaType> IB; - typedef typename Index::MetaType MetaType; - typedef SubRange RangeType; - typedef SubIndex IType; - SubIndex(const std::shared_ptr& range); + typedef public IndexInterface IB; + typedef PRange RangeType; + typedef typename Index::MetaType MetaType; - static constexpr IndexType sType() { return IndexType::SINGLE; } - static constexpr size_t totalDim() { return 1; } - static constexpr size_t sDim() { return 1; } - - static constexpr SpaceType STYPE = Index::STYPE; - - IndexType type() const; - - SubIndex& operator=(size_t pos); - SubIndex& operator++(); - SubIndex& operator--(); - - SubIndex& operator()(const std::shared_ptr& ind); // set full index - - std::string stringMeta() const; - MetaType meta() const; - const MetaType* metaPtr() const; - SubIndex& at(const MetaType& metaPos); - size_t posAt(const MetaType& metaPos) const; - - bool isMeta(const MetaType& metaPos) const; + PIndex(const RangePtr& range, SizeT pos = 0); - size_t dim(); // = 1 - bool last(); - bool first(); + PIndex& operator=(SizeT lexpos); + PIndex& operator++(); + PIndex& operator--(); + PIndex operator+(Int n) const; + PIndex operator-(Int n) const; + PIndex& operator+=(Int n); + PIndex& operator-=(Int n); - std::shared_ptr range(); + SizeT lex() const; + UPos pmax() const; + UPos lmax() const; + IndexId<0> id() const; - template - void getPtr(); - - size_t getStepSize(size_t n); + const MetaT& operator*() const; - template - auto ifor(size_t step, Expr ex) const - -> For,SubExpr>; + SizeT dim() const; + Sptr range() const; - template - auto iforh(size_t step, Expr ex) const - -> For,SubExpr,ForType::HIDDEN>; + template + UPos stepSize(const IndexId& id) const; - template - auto pifor(size_t step, Expr ex) const - -> decltype(ifor(step, ex)); // no multithreading here (check!!) + String stringMeta() const; + const MetaT& meta() const; + PIndex& at(const MetaT& metaPos); + decltype(auto) xpr(const Sptr>& _this) const; + template + decltype(auto) format(const Sptr& ind) const; + + template + decltype(auto) slice(const Sptr& ind) const; + + template + decltype(auto) ifor(const Xpr& xpr, F&& f) const; + + PIndex& operator()(); + PIndex& operator()(const Sptr& i); + const Sptr& orig() const; + private: - std::shared_ptr mExplicitRangePtr; - //const U* mMetaPtr; - std::shared_ptr mFullIndex; + Sptr mRangePtr; + Sptr mOrig; + + void mkPos(); }; template - class SubRangeFactory : public RangeFactoryBase + class PRangeFactory : public RangeFactoryBase { public: - typedef SubRange oType; + PRangeFactory(const Sptr& range, const Vector& _parts); + + private: + PRangeFactory() = default; + virtual void make() override final; - SubRangeFactory() = delete; - SubRangeFactory(const std::shared_ptr& fullRange, - const vector& subset); - std::shared_ptr create(); + RangePtr mRef; }; template - class SubRange : public RangeInterface> + class PRange : public RangeInterface> { + public: + typedef RangeBase RB; + typedef PIndex IndexType; + + friend PRangeFactory; + + virtual SizeT size() const override final; + virtual SizeT dim() const override final; + virtual String stringMeta(SizeT pos) const override final; + virtual const TypeInfo& type() const override final; + virtual const TypeInfo& metaType() const override final; + virtual RangePtr extend(const RangePtr& r) const override final; + + RangePtr orig() const; + const Vector& parts() const; + RangePtr derive() const; + private: - SubRange() = delete; - SubRange(const SubRange& in) = delete; + PRange() = delete; + PRange(const PRange& in) = delete; + PRange(const Sptr& range, const Vector& _parts); - SubRange(const std::shared_ptr& fullRange, const vector& subset); - - std::shared_ptr mFullRange; - vector mSubSet; - - public: - typedef RangeBase RB; - typedef SubIndex IndexType; - typedef SubRange RangeType; - typedef typename IndexType::MetaType MetaType; - typedef SubRangeFactory FType; - - virtual size_t size() const final; - virtual size_t dim() const final; - - virtual SpaceType spaceType() const final; - virtual DataHeader dataHeader() const final; - - virtual vector typeNum() const final; - virtual size_t cmeta(char* target, size_t pos) const final; - virtual size_t cmetaSize() const final; - virtual std::string stringMeta(size_t pos) const final; - virtual vector data() const final; - - bool isMeta(const MetaType& metaPos) const; - - auto get(size_t pos) const - -> decltype(mFullRange->get(mSubSet[pos])); - size_t getMeta(const MetaType& metaPos) const; - - virtual IndexType begin() const final; - virtual IndexType end() const final; - - std::shared_ptr fullRange() const; - const vector& subset() const; - std::shared_ptr> outRange() const; - - friend SubRangeFactory; - - static constexpr bool defaultable = false; - static constexpr size_t ISSTATIC = 0; - static constexpr size_t SIZE = -1; - static constexpr bool HASMETACONT = false; - + Sptr mRange; + Vector mParts; }; } // namespace CNORXZ -namespace CNORXZ -{ - - /***************** - * SubIndex * - *****************/ - - template - SubIndex::SubIndex(const std::shared_ptr& range) : - IndexInterface,typename Index::MetaType>(range, 0), - mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)) - { - mFullIndex = std::make_shared(mExplicitRangePtr->fullRange()); - } - - - template - IndexType SubIndex::type() const - { - return IndexType::SINGLE; - } - - template - SubIndex& SubIndex::operator=(size_t pos) - { - IB::mPos = pos; - (*mFullIndex) = mExplicitRangePtr->subset()[IB::mPos]; - return *this; - } - - template - SubIndex& SubIndex::operator++() - { - ++IB::mPos; - (*mFullIndex) = mExplicitRangePtr->subset()[IB::mPos]; - return *this; - } - - template - SubIndex& SubIndex::operator--() - { - --IB::mPos; - (*mFullIndex) = mExplicitRangePtr->subset()[IB::mPos]; - return *this; - } - - template - SubIndex& SubIndex::operator()(const std::shared_ptr& ind) - { - assert(mFullIndex->range() == ind->range()); - mFullIndex = ind; - return *this; - } - - template - int SubIndex::pp(std::intptr_t idxPtrNum) - { - ++(*this); - return 1; - } - - template - int SubIndex::mm(std::intptr_t idxPtrNum) - { - --(*this); - return 1; - } - - template - std::string SubIndex::stringMeta() const - { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->stringMeta(IB::mPos); - } - - template - typename SubIndex::MetaType SubIndex::meta() const - { - MetaType* x = nullptr; - return MetaPtrHandle::RangeType::HASMETACONT>::getMeta - ( x, IB::mPos, mExplicitRangePtr ); - } - - template - const typename SubIndex::MetaType* SubIndex::metaPtr() const - { - assert(0); // not sure where it is used - return mFullIndex->metaPtr(); - } - - template - SubIndex& SubIndex::at(const MetaType& metaPos) - { - (*this) = mExplicitRangePtr->getMeta( metaPos ); - return *this; - } - - template - size_t SubIndex::posAt(const MetaType& metaPos) const - { - return mExplicitRangePtr->getMeta( metaPos ); - } - - template - bool SubIndex::isMeta(const MetaType& metaPos) const - { - return mExplicitRangePtr->isMeta( metaPos ); - } - - template - size_t SubIndex::dim() - { - return 1; - } - - template - bool SubIndex::last() - { - return IB::mPos == IB::mMax - 1; - } - - template - bool SubIndex::first() - { - return IB::mPos == 0; - } - - template - std::shared_ptr::RangeType> SubIndex::range() - { - return mExplicitRangePtr; - } - - template - template - void SubIndex::getPtr() {} - - template - size_t SubIndex::getStepSize(size_t n) - { - return 1; - } - - template - template - auto SubIndex::ifor(size_t step, Expr ex) const - -> For,SubExpr> - { - return For,SubExpr> - (this, step, SubExpr - ( mFullIndex, reinterpret_cast(this), - &mExplicitRangePtr->subset(), ex ) ); - } - - template - template - auto SubIndex::iforh(size_t step, Expr ex) const - -> For,SubExpr,ForType::HIDDEN> - { - return For,SubExpr,ForType::HIDDEN> - (this, step, SubExpr - ( mFullIndex, reinterpret_cast(this), - &mExplicitRangePtr->subset(), ex ) ); - } - - template - template - auto SubIndex::pifor(size_t step, Expr ex) const - -> decltype(ifor(step, ex)) - { - return ifor(step, ex); - } - - /************************ - * SubRangeFactory * - ************************/ - - template - SubRangeFactory::SubRangeFactory(const std::shared_ptr& fullRange, - const vector& subset) - { - mProd = std::shared_ptr( new SubRange( fullRange, subset ) ); - } - - template - std::shared_ptr SubRangeFactory::create() - { - setSelf(); - return mProd; - } - - /***************** - * SubRange * - *****************/ - - template - SubRange::SubRange(const std::shared_ptr& fullRange, - const vector& subset) : - RangeInterface>(), - mFullRange(fullRange), mSubSet(subset) {} - - template - size_t SubRange::size() const - { - return mSubSet.size(); - } - - template - size_t SubRange::dim() const - { - return 1; - } - - template - SpaceType SubRange::spaceType() const - { - return SpaceType::ANY; - } - - template - DataHeader SubRange::dataHeader() const - { - DataHeader h; - h.spaceType = static_cast( SpaceType::ANY ); - h.metaSize = metaSize(mSubSet); - h.metaType = NumTypeMap::num(); - h.multiple = 0; - return h; - } - - template - vector SubRange::typeNum() const - { - return mFullRange->typeNum(); - } - - template - size_t SubRange::cmeta(char* target, size_t pos) const - { - return mFullRange->cmeta(target, mSubSet[pos]); - } - - template - size_t SubRange::cmetaSize() const - { - return mFullRange->cmetaSize(); - } - - template - std::string SubRange::stringMeta(size_t pos) const - { - return xToString(get(pos)); - } - - template - vector SubRange::data() const - { - DataHeader h = dataHeader(); - vector out; - out.reserve(h.metaSize + sizeof(DataHeader)); - char* hcp = reinterpret_cast(&h); - out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); - vector subvec(mSubSet.size()); - size_t i = 0; - for(auto& x: mSubSet){ - subvec[i++] = mFullRange->get(x); - } - stringCat(out, subvec); - return out; - } - - template - bool SubRange::isMeta(const MetaType& metaPos) const - { - for(size_t i = 0; i != size(); ++i){ - if(get(i) == metaPos){ - return true; - } - } - return false; - } - - template - auto SubRange::get(size_t pos) const - -> decltype(mFullRange->get(mSubSet[pos])) - { - return mFullRange->get( mSubSet[pos] ); - } - - template - size_t SubRange::getMeta(const MetaType& metaPos) const - { - for(size_t i = 0; i != size(); ++i){ - if(get(i) == metaPos){ - return i; - } - } - return -1; - } - - template - typename SubRange::IndexType SubRange::begin() const - { - SubRange::IndexType i( std::dynamic_pointer_cast> - ( std::shared_ptr( RB::mThis ) ) ); - i = 0; - return i; - } - - template - typename SubRange::IndexType SubRange::end() const - { - SubRange::IndexType i( std::dynamic_pointer_cast> - ( std::shared_ptr( RB::mThis ) ) ); - i = size(); - return i; - } - - template - std::shared_ptr SubRange::fullRange() const - { - return mFullRange; - } - - template - const vector& SubRange::subset() const - { - return mSubSet; - } - - template - std::shared_ptr::MetaType,SpaceType::ANY>> SubRange::outRange() const - { - vector ometa(mSubSet.size()); - size_t i = 0; - for(auto& x: mSubSet){ - ometa[i++] = mFullRange->get(x); - } - SingleRangeFactory srf(ometa); - return std::dynamic_pointer_cast>( srf.create() ); - } -} - #endif diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h index 89a0bfe..87b877d 100644 --- a/src/include/ranges/urange.cc.h +++ b/src/include/ranges/urange.cc.h @@ -158,10 +158,10 @@ namespace CNORXZ { if(ind != nullptr){ if(ind->dim() != 0) { - return Sptr(); + return Sptr>(); } } - return std::make_shared(*this); + return std::make_shared>(*this); } template @@ -233,7 +233,7 @@ namespace CNORXZ { std::sort(mSpace.begin(), mSpace.end(), std::less()); auto itdupl = std::adjacent_find(mSpace.begin(), mSpace.end()); - CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << *itdupl); + CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << toString(*itdupl)); } diff --git a/src/include/xpr/for.cc.h b/src/include/xpr/for.cc.h index cec435b..90d861c 100644 --- a/src/include/xpr/for.cc.h +++ b/src/include/xpr/for.cc.h @@ -28,16 +28,16 @@ namespace CNORXZ { if constexpr(std::is_same::type,NoF>::value){ for(SizeT i = 0; i != mSize; ++i){ - const auto pos = last + mExt * UPos(i); + const auto pos = last + mExt( UPos(i) ); mXpr(pos); } } else { typedef typename - std::remove_reference::type OutT; + std::remove_reference::type OutT; auto o = OutT(); for(SizeT i = 0; i != mSize; ++i){ - const auto pos = last + mExt * UPos(i); + const auto pos = last + mExt( UPos(i) ); mF(o, mXpr(pos)); } return o; @@ -49,15 +49,15 @@ namespace CNORXZ { if constexpr(std::is_same::type,NoF>::value){ for(SizeT i = 0; i != mSize; ++i){ - const auto pos = mExt * UPos(i); + const auto pos = mExt( UPos(i) ); mXpr(pos); } } else { - typedef typename std::remove_reference::type OutT; + typedef typename std::remove_reference::type OutT; auto o = OutT(); for(SizeT i = 0; i != mSize; ++i){ - const auto pos = mExt * UPos(i); + const auto pos = mExt( UPos(i) ); mF(o, mXpr(pos)); } return o; @@ -136,7 +136,7 @@ namespace CNORXZ constexpr decltype(auto) SFor::exec(const PosT& last) const { constexpr SPos i; - const auto pos = last + mExt * i; + const auto pos = last + mExt( i ); if constexpr(I < N-1){ return mF(mXpr(pos),exec(last)); } @@ -150,7 +150,7 @@ namespace CNORXZ constexpr decltype(auto) SFor::exec() const { constexpr SPos i; - const auto pos = mExt * i; + const auto pos = mExt( i ); if constexpr(I < N-1){ return mF(mXpr(pos),exec()); } @@ -164,7 +164,7 @@ namespace CNORXZ inline void SFor::exec2(const PosT& last) const { constexpr SPos i; - const auto pos = last + mExt * i; + const auto pos = last + mExt( i ); if constexpr(I < N-1){ mXpr(pos); exec2(last); @@ -180,7 +180,7 @@ namespace CNORXZ inline void SFor::exec2() const { constexpr SPos i; - const auto pos = mExt * i; + const auto pos = mExt( i ); if constexpr(I < N-1){ mXpr(pos); exec2(); @@ -207,6 +207,94 @@ namespace CNORXZ return SFor(id, xpr, NoF {}); } + /************ + * PFor * + ************/ + + template + constexpr PFor::PFor(SizeT size, const IndexId& id1, const IndexId& id2, + const SizeT* map, const Xpr& xpr, F&& f) : + mSize(size), + mId1(id1), + mId2(id2), + mXpr(xpr), + mExt1(mXpr.rootSteps(mId1)), + mExt2(mXpr.rootSteps(mId2)), + mPart(1, map), + mF(f) + {} + + template + template + inline decltype(auto) PFor::operator()(const PosT& last) const + { + if constexpr(std::is_same::type,NoF>::value){ + for(SizeT i = 0; i != mSize; ++i){ + const auto pos1 = last + mExt1( UPos(i) ); + const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) ); + mXpr(pos2); + } + } + else { + typedef typename + std::remove_reference::type OutT; + auto o = OutT(); + for(SizeT i = 0; i != mSize; ++i){ + const auto pos1 = last + mExt1( UPos(i) ); + const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) ); + mF(o, mXpr(pos2)); + } + return o; + } + } + + template + inline decltype(auto) PFor::operator()() const + { + if constexpr(std::is_same::type,NoF>::value){ + for(SizeT i = 0; i != mSize; ++i){ + const auto pos1 = mExt1( UPos(i) ); + const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) ); + mXpr(pos2); + } + } + else { + typedef typename std::remove_reference::type OutT; + auto o = OutT(); + for(SizeT i = 0; i != mSize; ++i){ + const auto pos1 = mExt1( UPos(i) ); + const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) ); + mF(o, mXpr(pos2)); + } + return o; + } + } + + template + template + inline decltype(auto) PFor::rootSteps(const IndexId& id) const + { + return mXpr.rootSteps(id); + } + + /************************* + * PFor (non-member) * + *************************/ + + template + constexpr decltype(auto) mkPFor(SizeT size, const IndexId& id1, const IndexId& id2, + const Xpr& xpr, F&& f) + { + return PFor(size, id1, id2, xpr, std::forward(f)); + } + + template + constexpr decltype(auto) mkPFor(SizeT size, const IndexId& id1, const IndexId& id2, + const Xpr& xpr) + { + return PFor(size, id1, id2, xpr, NoF {}); + } + /************ * TFor * ************/ @@ -232,7 +320,7 @@ namespace CNORXZ template inline decltype(auto) TFor::operator()(const PosT& last) const { - typedef typename std::remove_reference::type OutT; + typedef typename std::remove_reference::type OutT; int i = 0; const int size = static_cast(mSize); Vector ov; @@ -245,7 +333,7 @@ namespace CNORXZ auto xpr = mXpr; #pragma omp for for(i = 0; i < size; i++){ - const auto pos = last + mExt * UPos(i); + const auto pos = last + mExt( UPos(i) ); xpr(pos); } } @@ -262,7 +350,7 @@ namespace CNORXZ template inline decltype(auto) TFor::operator()() const { - typedef typename std::remove_reference::type OutT; + typedef typename std::remove_reference::type OutT; int i = 0; const int size = static_cast(mSize); Vector ov; @@ -275,7 +363,7 @@ namespace CNORXZ auto xpr = mXpr; #pragma omp for for(i = 0; i < size; i++){ - const auto pos = mExt * UPos(i); + const auto pos = mExt( UPos(i) ); xpr(pos); } } diff --git a/src/include/xpr/for.h b/src/include/xpr/for.h index 06709c2..1ef46fd 100644 --- a/src/include/xpr/for.h +++ b/src/include/xpr/for.h @@ -85,6 +85,43 @@ namespace CNORXZ template constexpr decltype(auto) mkSFor(const IndexId& id, const Xpr& xpr); + // partial for: + template + class PFor : public XprInterface> + { + public: + DEFAULT_MEMBERS(PFor); + + constexpr PFor(SizeT size, const IndexId& id1, const IndexId& id2, + const SizeT* map, const Xpr& xpr, F&& f); + + template + inline decltype(auto) operator()(const PosT& last) const; + + inline decltype(auto) operator()() const; + + template + inline decltype(auto) rootSteps(const IndexId& id) const; + + private: + SizeT mSize = 0; + IndexId mId1; + IndexId mId2; + Xpr mXpr; + typedef decltype(mXpr.rootSteps(mId1)) XPosT1; + typedef decltype(mXpr.rootSteps(mId2)) XPosT2; + XPosT1 mExt1; + XPosT2 mExt2; + FPos mPart; + F mF; + }; + + template + constexpr decltype(auto) mkFor(SizeT size, const IndexId& id, const Xpr& xpr, F&& f); + + template + constexpr decltype(auto) mkFor(SizeT size, const IndexId& id, const Xpr& xpr); + // multi-threading template class TFor : public XprInterface> diff --git a/src/opt/hdf5/include/h5_group.cc.h b/src/opt/hdf5/include/h5_group.cc.h index b9f0bab..b1b1b50 100644 --- a/src/opt/hdf5/include/h5_group.cc.h +++ b/src/opt/hdf5/include/h5_group.cc.h @@ -37,7 +37,7 @@ namespace CNORXZ template Group& Group::addTable(const String& name, const ArrayBase>& data, - const RangePtr& fields) + const Vector& fnames) { CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); Vector nvec({name}); @@ -46,7 +46,7 @@ namespace CNORXZ mCont.extend(extr); auto ii = mCont.begin(); ii.at(dvec); // 'at' returns YIndex&, so cannot use it inline... - auto tab = std::make_shared>(name, this, fields); + auto tab = std::make_shared>(name, this, fnames); for(auto& d: data){ tab->appendRecord(d); } diff --git a/src/opt/hdf5/include/h5_group.h b/src/opt/hdf5/include/h5_group.h index edc1f71..571c752 100644 --- a/src/opt/hdf5/include/h5_group.h +++ b/src/opt/hdf5/include/h5_group.h @@ -40,7 +40,7 @@ namespace CNORXZ template Group& addTable(const String& name, const ArrayBase>& data, - const RangePtr& fields); + const Vector& fnames); protected: MArray mCont; diff --git a/src/opt/hdf5/include/h5_table.cc.h b/src/opt/hdf5/include/h5_table.cc.h index 5ae7e30..5e4c4d9 100644 --- a/src/opt/hdf5/include/h5_table.cc.h +++ b/src/opt/hdf5/include/h5_table.cc.h @@ -18,13 +18,19 @@ namespace CNORXZ } template - STable::STable(const String& name, const ContentBase* _parent, const RangePtr& fields) : + STable::STable(const String& name, const ContentBase* _parent, + const Vector& fnames) : Table(name, _parent) { constexpr SizeT N = sizeof...(Ts); if(mFields == nullptr){ - CXZ_ASSERT(fields != nullptr, "field names have to be initialized"); - mFields = fields; + CXZ_ASSERT(fnames.size() != 0, "field names have to be initialized"); + Vector fields(fnames.size()); + for(SizeT i = 0; i != fields.size(); ++i){ + fields[i].first = i; + fields[i].second = fnames[i]; + } + mFields = URangeFactory(fields).create(); } CXZ_ASSERT(mFields->size() == sizeof...(Ts), "expected tuple of size = " << mFields->size() << ", got: " << sizeof...(Ts)); diff --git a/src/opt/hdf5/include/h5_table.h b/src/opt/hdf5/include/h5_table.h index 0d2ee92..603631d 100644 --- a/src/opt/hdf5/include/h5_table.h +++ b/src/opt/hdf5/include/h5_table.h @@ -12,6 +12,8 @@ namespace CNORXZ class Table : public ContentBase { public: + typedef std::pair FieldID; + DEFAULT_MEMBERS(Table); Table(const String& name, const ContentBase* _parent); ~Table(); @@ -44,7 +46,7 @@ namespace CNORXZ { public: DEFAULT_MEMBERS(STable); - STable(const String& name, const ContentBase* _parent, const RangePtr& fields); + STable(const String& name, const ContentBase* _parent, const Vector& fnames); STable& appendRecord(const Tuple& t); STable& appendRecord(const MArray>& t); diff --git a/src/opt/hdf5/lib/h5_table.cc b/src/opt/hdf5/lib/h5_table.cc index c2902bb..d05e3de 100644 --- a/src/opt/hdf5/lib/h5_table.cc +++ b/src/opt/hdf5/lib/h5_table.cc @@ -20,11 +20,12 @@ namespace CNORXZ SizeT typesize = 0; H5TBget_field_info(mParent->id(), mName.c_str(), fieldsptr.data(), sizes.data(), offsets.data(), &typesize); - Vector fields(nfields); + Vector fields(nfields); for(SizeT i = 0; i != nfields; ++i){ - fields[i] = fieldsptr[i]; + fields[i].first = i; + fields[i].second = fieldsptr[i]; } - mFields = URangeFactory( std::move(fields) ).create(); + mFields = URangeFactory( std::move(fields) ).create(); mSizes = MArray(mFields, std::move(sizes)); mOffsets = MArray(mFields, std::move(offsets)); this->open(); @@ -87,7 +88,12 @@ namespace CNORXZ Table& Table::initFieldNames(const Vector& fnames) { CXZ_ASSERT(mFields == nullptr, "fields already initialized"); - mFields = URangeFactory(fnames).create(); + Vector fields(fnames.size()); + for(SizeT i = 0; i != fields.size(); ++i){ + fields[i].first = i; + fields[i].second = fnames[i]; + } + mFields = URangeFactory(fields).create(); return *this; } @@ -96,9 +102,9 @@ namespace CNORXZ const Int compress = 0; mRecords = CRangeFactory(n).create(); Vector fields(mFields->size()); - auto fr = std::dynamic_pointer_cast>(mFields); + auto fr = std::dynamic_pointer_cast>(mFields); for(auto fi = fr->begin(); fi != fr->end(); ++fi){ - fields[fi.lex()] = (*fi).c_str(); + fields[fi.lex()] = (*fi).second.c_str(); } const herr_t err = H5TBmake_table (mName.c_str(), mParent->id(), mName.c_str(), mFields->size(), mRecords->size(), dsize, diff --git a/src/opt/hdf5/tests/h5_basic_unit_test.cc b/src/opt/hdf5/tests/h5_basic_unit_test.cc index 1399651..172c707 100644 --- a/src/opt/hdf5/tests/h5_basic_unit_test.cc +++ b/src/opt/hdf5/tests/h5_basic_unit_test.cc @@ -40,7 +40,7 @@ namespace { mFileName = testh5file; mGrps = { "gr1", "gr2" }; - mFs = URangeFactory(Vector({"field1","second","real"})).create(); + mFs = {"field1","second","real"}; Vector> v ( { {0, -6, 3.141}, {3, -8, 0.789}, @@ -55,7 +55,7 @@ namespace String mFileName; Vector mGrps; - RangePtr mFs; + Vector mFs; MArray> mTabA; };