#ifndef __dynamic_range_h__ #define __dynamic_range_h__ #include #include "ranges/rbase_def.h" #include "ranges/range_base.h" #include "ranges/index_base.h" #include "xfor/xfor.h" #include //#include "ranges/rpheader.h" #include "ranges/x_to_string.h" #include "ranges/type_map.h" #include "ranges/dynamic_meta.h" namespace MultiArrayTools { namespace { using namespace MultiArrayHelper; } //using MultiArrayHelper::DynamicExpression; //using MultiArrayHelper::ExpressionHolder; template class IndexWrapperBase { protected: std::shared_ptr mEc; public: IndexWrapperBase() = default; IndexWrapperBase(const IndexWrapperBase& in) = default; IndexWrapperBase(IndexWrapperBase&& in) = default; IndexWrapperBase& operator=(const IndexWrapperBase& in) = default; IndexWrapperBase& operator=(IndexWrapperBase&& in) = default; virtual IndexType type() const = 0; virtual IndexWrapperBase& operator=(size_t pos) = 0; virtual IndexWrapperBase& operator++() = 0; virtual IndexWrapperBase& operator--() = 0; virtual int pp(std::intptr_t idxPtrNum) = 0; virtual int mm(std::intptr_t idxPtrNum) = 0; virtual std::string stringMeta() const = 0; //virtual DynamicMetaT meta() const = 0; //virtual const DynamicMetaT* metaPtr() const = 0; //virtual IndexWrapperBase& at(const U& metaPos) = 0; //virtual size_t posAt(const U& metaPos) const = 0; //virtual bool isMeta(const U& metaPos) const = 0; virtual size_t pos() const = 0; virtual size_t max() const = 0; virtual size_t dim() const = 0; virtual bool last() const = 0; virtual bool first() const = 0; virtual std::shared_ptr range() const = 0; virtual size_t getStepSize(size_t n) const = 0; virtual size_t getStepSizeComp(std::intptr_t j) const = 0; virtual std::intptr_t get() const = 0; virtual std::intptr_t ptrNum() const = 0; template ExpressionHolder ifor(size_t step, ExpressionHolder ex) const; template ExpressionHolder iforh(size_t step, ExpressionHolder ex) const; template ExpressionHolder ifori(size_t step, Expr ex) const; template ExpressionHolder iforhi(size_t step, Expr ex) const; }; template using IndexW = IndexWrapperBase; template class IndexWrapper : public IndexWrapperBase { public: typedef IndexWrapperBase IWB; typedef typename Index::MetaType MetaType; static constexpr IndexType sType() { return IndexType::SINGLE; } protected: IndexWrapper() = default; private: std::shared_ptr mI; public: IndexWrapper(const IndexWrapper& in) = default; IndexWrapper(IndexWrapper&& in) = default; IndexWrapper& operator=(const IndexWrapper& in) = default; IndexWrapper& operator=(IndexWrapper&& in) = default; IndexWrapper(const std::shared_ptr& i) : mI(i) { IWB::mEc = ExpressionCollection::make(mI); } virtual IndexType type() const final { return mI->type(); } virtual IndexWrapper& operator=(size_t pos) final { (*mI) = pos; return *this; } virtual IndexWrapper& operator++() final { ++(*mI); return *this; } virtual IndexWrapper& operator--() final { --(*mI); return *this; } virtual size_t pos() const final { return mI->pos(); } virtual size_t max() const final { return mI->max(); } virtual int pp(std::intptr_t idxPtrNum) final { return mI->pp(idxPtrNum); } virtual int mm(std::intptr_t idxPtrNum) final { return mI->mm(idxPtrNum); } virtual std::string stringMeta() const final { return mI->stringMeta(); } //virtual DynamicMetaT meta() const final { return DynamicMetaT(mI->meta()); } //virtual const DynamicMetaT* metaPtr() const final { return nullptr; } IndexWrapper& at(const typename Index::MetaType& metaPos) { mI->at(metaPos); return *this; } size_t posAt(const typename Index::MetaType& metaPos) const { return mI->posAt(metaPos); } //virtual bool isMeta(const U& metaPos) const final { return mI->isMeta(); } virtual size_t dim() const final { return mI->dim(); } virtual bool last() const final { return mI->last(); } virtual bool first() const final { return mI->first(); } virtual std::shared_ptr range() const final { return mI->range(); } virtual size_t getStepSize(size_t n) const final { return mI->getStepSize(n); } virtual size_t getStepSizeComp(std::intptr_t j) const final; virtual std::intptr_t get() const final { return reinterpret_cast(mI.get()); } virtual std::intptr_t ptrNum() const final { return mI->ptrNum(); } }; //typedef SingleRange DynamicRange; template class DynamicIndex : public IndexInterface,DynamicMetaT> { private: typedef std::vector>,size_t>> IVecT; IVecT mIVec; public: typedef IndexInterface,DynamicMetaT> IB; typedef DynamicMetaT MetaType; typedef DynamicRange RangeType; typedef DynamicIndex IType; DynamicIndex(const std::shared_ptr& range); 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 = SpaceType::DYN; IndexType type() const; DynamicIndex& operator=(size_t pos); DynamicIndex& operator++(); DynamicIndex& operator--(); DynamicIndex& operator()(const IVecT& ivec); template DynamicIndex& operator()(const std::shared_ptr&... is); int pp(std::intptr_t idxPtrNum); int mm(std::intptr_t idxPtrNum); std::string stringMeta() const; MetaType meta() const; const MetaType* metaPtr() const; DynamicIndex& at(const MetaType& metaPos); size_t posAt(const MetaType& metaPos) const; //bool isMeta(const MetaType& metaPos) const; size_t dim() const; bool last() const; bool first() const; const IndexW& get(size_t n) const; std::shared_ptr range(); template void getPtr(); size_t getStepSize(size_t n) const; std::string id() const; void print(size_t offset); template ExpressionHolder ifor(size_t step, Expr ex) const; template ExpressionHolder iforh(size_t step, Expr ex) const; }; // NOT THREAD SAVE!! template class DynamicRangeFactory : public RangeFactoryBase { public: typedef DynamicRange oType; DynamicRangeFactory(); template DynamicRangeFactory(const std::tuple...>& origs); template DynamicRangeFactory(std::shared_ptr... origs); template void append(std::shared_ptr r); std::shared_ptr create(); private: std::shared_ptr checkIfCreated(const std::vector >& pvec); static std::map,std::vector > mAleadyCreated; bool mProductCreated = false; }; template class DynamicRange : public RangeInterface> { public: static constexpr bool defaultable = true; static constexpr size_t ISSTATIC = 0; static constexpr size_t SIZE = -1; static constexpr bool HASMETACONT = false; typedef RangeBase RB; typedef DynamicIndex IndexType; typedef DynamicRange RangeType; typedef DynamicMetaT MetaType; private: DynamicRange() = default; DynamicRange(const DynamicRange& in) = default; template DynamicRange(const std::tuple...>& origs); template DynamicRange(std::shared_ptr... origs); size_t mSize = 1; bool mEmpty = true; std::vector > mOrig; public: virtual size_t size() const final; virtual size_t dim() const final; MetaType get(size_t pos) const; size_t getMeta(const MetaType& metaPos) const; virtual IndexType begin() const final; virtual IndexType end() const final; virtual SpaceType spaceType() const final; virtual std::string stringMeta(size_t pos) const final; virtual std::vector data() const final; std::shared_ptr sub(size_t num) const; template std::shared_ptr fullsub(size_t num) const; template std::shared_ptr > scast(SIZET... sizes) const; // save cast void sreplace(const std::shared_ptr in, size_t num); bool isEmpty() const; friend DynamicRangeFactory; static DynamicRangeFactory factory() { return DynamicRangeFactory(); } }; } // namespace MultiArrayTools namespace MultiArrayHelper { using namespace MultiArrayTools; template inline void resolveSetRange(std::shared_ptr>& rp, const std::vector >& orig, size_t origpos, size_t size) { DynamicRangeFactory arf; for(size_t op = origpos; op != origpos + size; ++op){ //VCHECK(op); arf.append(orig[op]); } rp = std::dynamic_pointer_cast>( arf.create() ); } template inline void setRangeToVec(std::vector >& v, std::shared_ptr> r) { if(not r->isEmpty()){ for(size_t i = r->dim(); i != 0; --i){ v.insert(v.begin(), r->sub(i-1)); } } } template inline size_t getStepSize(const DynamicIndex& ii, std::intptr_t j) { size_t ss = 0; size_t sx = 1; for(size_t k = ii.dim(); k != 0; --k){ const size_t i = k-1; const auto& ni = ii.get(i); const size_t max = ni.max(); const size_t tmp = ni.getStepSizeComp(j); ss += tmp * ii.getStepSize(i); sx *= max; } return ss; } } //#include "dynamic_range.cc.h" #endif