// -*- C++ -*- #ifndef __cxz_container_index_h__ #define __cxz_container_index_h__ #include #include #include #include "ranges/range_base.h" #include "ranges/index_base.h" #include "mbase_def.h" #include "statics/static_for.h" #include "ranges/range_helper.h" namespace CNORXZ { template class ConstContainerIndex : public IndexInterface, std::tuple >, public std::iterator { public: typedef IndexInterface, std::tuple > IB; typedef std::tuple MetaType; typedef std::tuple...> IndexPack; typedef ContainerRange RangeType; static constexpr IndexType sType() { return IndexType::CONT; } static constexpr size_t sDim() { return sizeof...(Indices); } static constexpr size_t totalDim() { return (... * Indices::totalDim()); } static constexpr SpaceType STYPE = SpaceType::ANY; static constexpr bool PARALLEL = std::tuple_element<0,std::tuple>::type::PARALLEL; template using CIX = ConstContainerIndex; template friend class CIX; private: ConstContainerIndex() = default; bool mNonTrivialBlocks = false; bool mExternControl = false; IndexPack mIPack; std::array mBlockSizes; const T* mData = nullptr; std::intptr_t mObjPtrNum; protected: size_t mCPos; public: ConstContainerIndex(const ConstContainerIndex& in) = default; ConstContainerIndex& operator=(const ConstContainerIndex& in) = default; ConstContainerIndex(const ConstContainerIndex& in, bool copy); ConstContainerIndex& copy(const ConstContainerIndex& in); template ConstContainerIndex& operator=(const ConstContainerIndex& in); template ConstContainerIndex(const std::shared_ptr& range, std::intptr_t objPtrNum); template ConstContainerIndex(const std::shared_ptr& range, std::intptr_t objPtrNum, const std::array& blockSizes); template size_t getBlockSize() const { return std::get(mBlockSizes); } const IndexPack& pack() const { return mIPack; } ConstContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true ConstContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices ConstContainerIndex& operator()(const std::tuple...>& inds); ConstContainerIndex& operator()(); // -> sync; just to shorten the code // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== IndexType type() const; ConstContainerIndex& operator++(); ConstContainerIndex& operator--(); ConstContainerIndex& operator=(size_t pos); int pp(std::intptr_t idxPtrNum); int mm(std::intptr_t idxPtrNum); std::string stringMeta() const; MetaType meta() const; ConstContainerIndex& at(const MetaType& metaPos); size_t dim() const; bool first() const; bool last() const; bool sliceMode() const; std::shared_ptr range(); template auto& get() const; template auto getPtr() const; size_t getStepSize(size_t n); template auto ifor(size_t step, Exprs exs) const; template auto iforh(size_t step, Exprs exs) const; template auto pifor(size_t step, Exprs exs) const; std::intptr_t container() const; ConstContainerIndex& format(const std::array& blocks); // Iterator Stuff ConstContainerIndex& setData(const T* data); const T& operator*() const; const T* operator->() const; //T& operator*(); //T* operator->(); ConstContainerIndex operator++(int); ConstContainerIndex operator--(int); ConstContainerIndex& operator+=(int diff); ConstContainerIndex& operator-=(int diff); ConstContainerIndex operator+(int num) const; ConstContainerIndex operator-(int num) const; int operator-(const ConstContainerIndex& it) const; const T& operator[](int num) const; bool operator<(const ConstContainerIndex& it) const; bool operator>(const ConstContainerIndex& it) const; bool operator<=(const ConstContainerIndex& it) const; bool operator>=(const ConstContainerIndex& it) const; }; template class ContainerIndex : public ConstContainerIndex { public: typedef ConstContainerIndex CCI; typedef CCI IB; typedef typename CCI::MetaType MetaType; typedef typename CCI::IndexPack IndexPack; typedef typename CCI::RangeType RangeType; static constexpr IndexType sType() { return CCI::sType(); } static constexpr size_t sDim() { return CCI::sDim(); } static constexpr size_t totalDim() { return CCI::totalDim(); } static constexpr SpaceType STYPE = CCI::STYPE; static constexpr bool PARALLEL = CCI::PARALLEL; template using CIX = ContainerIndex; template friend class CIX; private: ContainerIndex() = default; T* mMData = nullptr; public: ContainerIndex(const ContainerIndex& in) = default; ContainerIndex& operator=(const ContainerIndex& in) = default; ContainerIndex(const ContainerIndex& in, bool copy) : CCI(in,copy) { mMData = in.mMData; } ContainerIndex(const ConstContainerIndex& in, T* data) : CCI(in) { mMData = data; } ContainerIndex(const ConstContainerIndex& in, T* data, bool copy) : CCI(in,copy) { mMData = data; } ContainerIndex& copy(const ContainerIndex& in) { CCI::copy(in); mMData = in.mMData; } template ContainerIndex& operator=(const ContainerIndex& in) { CCI::operator=(in); return *this; } template ContainerIndex(const std::shared_ptr& range, std::intptr_t objPtrNum) : CCI(range, objPtrNum) {} template ContainerIndex(const std::shared_ptr& range, std::intptr_t objPtrNum, const std::array& blockSizes) : CCI(range, objPtrNum, blockSizes) {} template size_t getBlockSize() const { return CCI::template getBlockSize(); } const IndexPack& pack() const { CCI::pack(); return *this; } ContainerIndex& sync() { return CCI::sync(); return *this; } ContainerIndex& operator()(const std::shared_ptr&... inds) { CCI::operator()(inds...); return *this; } ContainerIndex& operator()(const std::tuple...>& inds) { CCI::operator()(inds); return *this; } ContainerIndex& operator()() { CCI::operator()(); return *this; } // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== IndexType type() const { return CCI::type(); } ContainerIndex& operator++() { CCI::operator++(); return *this; } ContainerIndex& operator--() { CCI::operator--(); return *this; } ContainerIndex& operator=(size_t pos) { CCI::operator=(pos); return *this; } int pp(std::intptr_t idxPtrNum) { return CCI::pp(idxPtrNum); } int mm(std::intptr_t idxPtrNum) { return CCI::mm(idxPtrNum); } std::string stringMeta() const { return CCI::stringMeta; } MetaType meta() const { return CCI::meta(); } ContainerIndex& at(const MetaType& metaPos) { CCI::at(metaPos); return *this; } size_t dim() const { return CCI::dim(); } bool first() const { return CCI::first(); } bool last() const { return CCI::last(); } bool sliceMode() const { return CCI::sliceMode(); } std::shared_ptr range() { return CCI::range(); } template auto& get() const { return CCI::template get(); } template auto getPtr() const { return CCI::template getPtr(); } size_t getStepSize(size_t n) { return getStepSize(n); } template auto ifor(size_t step, Exprs exs) const { return CCI::ifor(step, exs); } template auto iforh(size_t step, Exprs exs) const { return CCI::iforh(step, exs); } template auto pifor(size_t step, Exprs exs) const { return CCI::pifor(step, exs); } std::intptr_t container() const { return CCI::container(); } ContainerIndex& format(const std::array& blocks) { CCI::format(blocks); return *this; } // Iterator Stuff ContainerIndex& setData(T* data) { CCI::setData(data); mMData = data; return *this; } const T& operator*() const { return CCI::operator*(); } const T* operator->() const { return CCI::operator->(); } T& operator*() { return mMData[CCI::mCPos]; } T* operator->() { return &mMData[CCI::mCPos]; } ContainerIndex operator++(int) { auto tmp = *this; ++(*this); return tmp; } ContainerIndex operator--(int) { auto tmp = *this; --(*this); return tmp; } ContainerIndex& operator+=(int diff) { CCI::operator+=(diff); return *this; } ContainerIndex& operator-=(int diff) { CCI::operator-=(diff); return *this; } ContainerIndex operator+(int num) const { CCI::operator+(num); return *this; } ContainerIndex operator-(int num) const { CCI::operator-(num); return *this; } int operator-(const ContainerIndex& it) const { return CCI::operator-(it); } const T& operator[](int num) const { return CCI::operator[](num); } bool operator<(const ContainerIndex& it) const { return CCI::operator<(it); } bool operator>(const ContainerIndex& it) const { return CCI::operator>(it); } bool operator<=(const ContainerIndex& it) const { return CCI::operator<=(it); } bool operator>=(const ContainerIndex& it) const { return CCI::operator>=(it); } }; } // end namespace CNORXZ #endif