// -*- C++ -*- #ifndef __single_range_h__ #define __single_range_h__ #include #include #include #include #include "base_def.h" //#include "ranges/rpack_num.h" #include "ranges/index_base.h" #include "ranges/range_base.h" #include "ranges/x_to_string.h" #include "ranges/type_map.h" #include "xfor/for_type.h" //#include "xfor/xfor.h" namespace MultiArrayTools { namespace { using namespace MultiArrayHelper; } template class SingleIndex : public IndexInterface,U> { public: typedef IndexInterface,U> IB; typedef U MetaType; typedef SingleRange RangeType; typedef SingleIndex IType; //DEFAULT_MEMBERS_X(SingleIndex); SingleIndex(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 = TYPE; // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== IndexType type() const; SingleIndex& operator=(size_t pos); SingleIndex& operator++(); SingleIndex& operator--(); int pp(std::intptr_t idxPtrNum); int mm(std::intptr_t idxPtrNum); std::string stringMeta() const; U meta() const; const U* metaPtr() const; SingleIndex& at(const U& metaPos); size_t posAt(const U& metaPos) const; size_t dim(); // = 1 bool last(); bool first(); std::shared_ptr range(); template void getPtr(); size_t getStepSize(size_t n); std::string id() const; void print(size_t offset); template auto ifor(size_t step, Expr ex) const -> For,Expr>; template auto iforh(size_t step, Expr ex) const -> For,Expr,ForType::HIDDEN>; private: std::shared_ptr mExplicitRangePtr; const U* mMetaPtr; }; template class SingleRangeFactory : public RangeFactoryBase { public: typedef SingleRange oType; SingleRangeFactory() = delete; SingleRangeFactory(const std::vector& space); std::shared_ptr create(); }; template class MetaMap { private: std::map mMap; public: MetaMap() = default; MetaMap(const MetaMap& in) = default; MetaMap(MetaMap&& in) = default; MetaMap& operator=(const MetaMap& in) = default; MetaMap& operator=(MetaMap&& in) = default; //MetaMap(const std::map& in) : mMap(in) {} MetaMap(const std::vector& in) { for(size_t i = 0; i != in.size(); ++i){ mMap[in[i]] = i; } } size_t at(const U& in) const { return mMap.at(in); } }; template <> class MetaMap > { private: std::vector mMap; int min1; int min2; int max1; int max2; size_t s1; size_t s2; public: typedef std::array U; MetaMap() = default; MetaMap(const MetaMap& in) = default; MetaMap(MetaMap&& in) = default; MetaMap& operator=(const MetaMap& in) = default; MetaMap& operator=(MetaMap&& in) = default; MetaMap(const std::vector& in) : min1(in[0][0]), min2(in[0][1]), max1(in[0][0]), max2(in[0][1]) { for(auto& x: in){ if(min1 > x[0]) min1 = x[0]; if(min2 > x[1]) min2 = x[1]; if(max1 < x[0]+1) max1 = x[0]+1; if(max2 < x[1]+1) max2 = x[1]+1; } s1 = max1 - min1; s2 = max2 - min2; mMap.resize(s1*s2,-1); for(size_t i = 0; i != in.size(); ++i){ const size_t mpos = (in[i][0] - min1) * s2 + (in[i][1] - min2); mMap[ mpos ] = i; } } size_t at(const U& in) const { //CHECK; const size_t mpos = (in[0] - min1) * s2 + (in[1] - min2); assert(mpos < mMap.size()); assert(mMap[ mpos ] != static_cast( -1 ) ); return mMap[ mpos ]; } }; template class SingleRange : public RangeInterface > { public: typedef RangeBase RB; typedef SingleIndex IndexType; typedef SingleRange RangeType; typedef U MetaType; typedef SingleRangeFactory FType; //typedef typename RangeInterface >::IndexType IndexType; virtual size_t size() const final; virtual size_t dim() const final; virtual SpaceType spaceType() const final; virtual std::string stringMeta(size_t pos) const final; virtual std::vector data() const final; const U& get(size_t pos) const; size_t getMeta(const U& metaPos) const; virtual IndexType begin() const final; virtual IndexType end() const final; friend SingleRangeFactory; static constexpr bool defaultable = false; static constexpr size_t ISSTATIC = 0; static constexpr size_t SIZE = -1; static constexpr bool HASMETACONT = true; protected: SingleRange() = delete; SingleRange(const SingleRange& in) = delete; SingleRange(const std::vector& space); std::vector mSpace; //std::map mMSpace; MetaMap mMSpace; }; } /* ========================= * * --- TEMPLATE CODE --- * * ========================= */ namespace MultiArrayTools { /****************** * SingleIndex * ******************/ template struct MetaPtrHandle { template static const typename Range::MetaType* set(Range* r) { return &r->get(0); } template static inline U getMeta(U* metaPtr, size_t pos, std::shared_ptr r) { return metaPtr[pos]; } }; template <> struct MetaPtrHandle { template static const typename Range::MetaType* set(Range* r) { return nullptr; } template static inline U getMeta(U* metaPtr, size_t pos, std::shared_ptr r) { return r->get(pos); } }; template SingleIndex::SingleIndex(const std::shared_ptr >& range) : IndexInterface,U>(range, 0), mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)), mMetaPtr(MetaPtrHandle::RangeType::HASMETACONT>::set ( dynamic_cast(IB::mRangePtr.get() ) ) ) {} template IndexType SingleIndex::type() const { return IndexType::SINGLE; } template SingleIndex& SingleIndex::operator=(size_t pos) { IB::mPos = pos; return *this; } template SingleIndex& SingleIndex::operator++() { ++IB::mPos; return *this; } template SingleIndex& SingleIndex::operator--() { --IB::mPos; return *this; } template int SingleIndex::pp(std::intptr_t idxPtrNum) { ++(*this); return 1; } template int SingleIndex::mm(std::intptr_t idxPtrNum) { --(*this); return 1; } template std::string SingleIndex::stringMeta() const { return std::dynamic_pointer_cast const>( IB::mRangePtr )->stringMeta(IB::mPos); } template U SingleIndex::meta() const { return MetaPtrHandle::RangeType::HASMETACONT>::getMeta ( mMetaPtr, IB::mPos, mExplicitRangePtr ); } template const U* SingleIndex::metaPtr() const { return mMetaPtr; } template SingleIndex& SingleIndex::at(const U& metaPos) { (*this) = std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); return *this; } template size_t SingleIndex::posAt(const U& metaPos) const { return std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); } template size_t SingleIndex::dim() // = 1 { return 1; } template bool SingleIndex::last() { return IB::mPos == IB::mMax - 1; } template bool SingleIndex::first() { return IB::mPos == 0; } template std::shared_ptr::RangeType> SingleIndex::range() { return std::dynamic_pointer_cast( IB::mRangePtr ); } template template void SingleIndex::getPtr() {} template size_t SingleIndex::getStepSize(size_t n) { return 1; } template std::string SingleIndex::id() const { return std::string("sin") + std::to_string(IB::mId); } template void SingleIndex::print(size_t offset) { if(offset == 0){ std::cout << " === " << std::endl; } for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } std::cout << id() << "[" << reinterpret_cast(this) << "](" << IB::mRangePtr << "): " << meta() << std::endl; } template template auto SingleIndex::ifor(size_t step, Expr ex) const -> For,Expr> { //static const size_t LAYER = typename Expr::LAYER; return For,Expr>(this, step, ex); } template template auto SingleIndex::iforh(size_t step, Expr ex) const -> For,Expr,ForType::HIDDEN> { //static const size_t LAYER = typename Expr::LAYER; return For,Expr,ForType::HIDDEN>(this, step, ex); } /******************** * SingleRange * ********************/ template SingleRangeFactory::SingleRangeFactory(const std::vector& space) { mProd = std::shared_ptr( new SingleRange( space ) ); } template std::shared_ptr SingleRangeFactory::create() { setSelf(); return mProd; } /******************** * SingleRange * ********************/ template SingleRange::SingleRange(const std::vector& space) : RangeInterface >(), mSpace(space), mMSpace(mSpace) { //for(size_t i = 0; i != mSpace.size(); ++i){ // mMSpace[mSpace[i]] = i; //} } template const U& SingleRange::get(size_t pos) const { return mSpace[pos]; } template size_t SingleRange::getMeta(const U& metaPos) const { return mMSpace.at(metaPos); } template size_t SingleRange::size() const { return mSpace.size(); } template size_t SingleRange::dim() const { return 1; } template SpaceType SingleRange::spaceType() const { return TYPE; } template std::string SingleRange::stringMeta(size_t pos) const { return xToString(get(pos)); } template std::vector SingleRange::data() const { DataHeader h; h.spaceType = static_cast( TYPE ); h.metaSize = metaSize(mSpace); h.metaType = NumTypeMap::num; h.multiple = 0; std::vector out; out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); stringCat(out, mSpace); //const char* scp = reinterpret_cast(mSpace.data()); //out.insert(out.end(), scp, scp + h.metaSize); return out; } template typename SingleRange::IndexType SingleRange::begin() const { SingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } template typename SingleRange::IndexType SingleRange::end() const { SingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; } } #include "range_types/header.h" #endif