From 792c3bd84e280ab559a687fac9a271d5ea20e301 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 28 Jan 2023 18:34:24 +0100 Subject: [PATCH] WIP: prange --- src/include/ranges/prange.cc.h | 207 +++++++++++++ src/include/ranges/prange.h | 535 +++++---------------------------- 2 files changed, 283 insertions(+), 459 deletions(-) create mode 100644 src/include/ranges/prange.cc.h diff --git a/src/include/ranges/prange.cc.h b/src/include/ranges/prange.cc.h new file mode 100644 index 0000000..a5bc54e --- /dev/null +++ b/src/include/ranges/prange.cc.h @@ -0,0 +1,207 @@ + +#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 + { + /*!!!*/ + } + + template + template + decltype(auto) PIndex::slice(const Sptr& ind) const + { + /*!!!*/ + } + + template + template + decltype(auto) PIndex::ifor(const Xpr& xpr, F&& f) const + { + /*return For<0,Xpr,F>(this->pmax().val(), this->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