diff --git a/src/include/base/types.h b/src/include/base/types.h index c68a38e..3debdc4 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -56,6 +56,60 @@ namespace CNORXZ // definition: ranges/range_base.h class RangeBase; + typedef Sptr RangePtr; + + // definition: ranges/index_base.h + template + class IndexInterface; + + template + using IndexPtr = Sptr>; + + // definition: ranges/urange.h + template + class URange; // generic simple range (uni-dimensional) + + // definition: ranges/urange.h + template + class UIndex; + + // definition: ranges/crange.h + class CRange; // classic range, trivial meta data + + // definition: ranges/crange.h + class CIndex; + + // definition: ranges/srange.h + template + class SRange; // generic static size range + + // definition: ranges/srange.h + template + class SIndex; + + // definition: ranges/mrange.h + template + class MRange; // multi range + + // definition: ranges/mrange.h + template + class MIndex; + + // definition: ranges/xindex.h + class XIndexBase; // dynamic index wrapper + + // definition: ranges/yrange.h + class YRange; // dynamic multi range + + // definition: ranges/yrange.h + class YIndex; + + // definition: ranges/pindex.h + template + class PIndex; // partial index (index over sub-ranges and permutations) + + // there should be also a static analogue + /********************* * derived types * *********************/ diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index dbdcd22..160d6e7 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -22,7 +22,12 @@ namespace CNORXZ I& operator=(SizeT pos) { return THIS() = pos; } I& operator++() { return THIS()++; } I& operator--() { return THIS()--;} - + I operator+(Int n) const { return THIS() + n; } + I operator-(Int n) const { return THIS() - n; } + I& operator+=(Int n) { return THIS() += n; } + I& operator-=(Int n) { return THIS() -= n; } + Int operator-(const IndexInterface& i) const { return mPos - i.mPos; } + SizeT pos() const; SizeT max() const; PtrId ptrId() const; @@ -40,7 +45,7 @@ namespace CNORXZ Int mm(PtrId idxPtrNum) { return THIS().mm(idxPtrNum); } SizeT dim() const { return THIS().dim(); } - RangePtr range() const { return mRangePtr; } + auto range() const { return THIS().range(); } SizeT getStepSize(SizeT n) const { return THIS().getStepSize(n); } String stringMeta() const { return THIS().stringMeta(); } @@ -66,16 +71,20 @@ namespace CNORXZ IndexInterface(IndexInterface&& in); IndexInterface& operator=(IndexInterface&& in); IndexInterface(const RangePtr& range, SizeT pos); - - RangePtr mRangePtr = nullptr; + + IndexPtr mRel = nullptr; SizeT mPos = 0; SizeT mMax = 0; PtrId mPtrId = 0; }; - template - using IndexPtr = Sptr>; - + // to define relative indices: + template + IndexPtr operator+(const IndexPtr& i, Int n); + + template + IndexPtr operator-(const IndexPtr& i, Int n); + } #endif diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index 992451f..a06de4b 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -1,270 +1,107 @@ // -*- C++ -*- -#ifndef __cxz_single_range_h__ -#define __cxz_single_range_h__ +#ifndef __cxz_urange_h__ +#define __cxz_urange_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 "xfor/for_type.h" namespace CNORXZ { - namespace - { - using namespace CNORXZInternal; - } - - template - class GenSingleIndex : public IndexInterface,U> + template + class UIndex : public IndexInterface,MetaType> { public: - typedef IndexInterface,U> IB; - typedef U MetaType; - typedef GenSingleRange RangeType; - typedef GenSingleIndex IType; + typedef IndexInterface,MetaType> IB; + typedef URange RangeType; - GenSingleIndex(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 constexpr bool PARALLEL = true; - - // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== + UIndex(const RangePtr& range); - IndexType type() const; + UIndex& operator=(SizeT pos); + UIndex& operator++(); + UIndex& operator--(); + UIndex operator+(Int n) const; + UIndex operator-(Int n) const; + UIndex& operator+=(Int n); + UIndex& operator-=(Int n); + + const MetaType& operator*() const; + const MetaType* operator->() const; - GenSingleIndex& operator=(size_t pos); - GenSingleIndex& operator++(); - GenSingleIndex& operator--(); + Int pp(PtrId idxPtrNum); + Int mm(PtrId idxPtrNum); - int pp(std::intptr_t idxPtrNum); - int mm(std::intptr_t idxPtrNum); + SizeT dim() const; // = 1 + Sptr range() const; + SizeT getStepSize(SizeT n) const; - std::string stringMeta() const; - U meta() const; - const U* metaPtr() const; - GenSingleIndex& at(const U& metaPos); - size_t posAt(const U& metaPos) const; - - bool isMeta(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); + String stringMeta() const; + MetaType meta() const; + UIndex& at(const MetaType& metaPos); template - auto ifor(size_t step, Expr ex) const - -> For,Expr>; + auto ifor(SizeT step, Expr ex) const + -> For,Expr>; template - auto iforh(size_t step, Expr ex) const - -> For,Expr,ForType::HIDDEN>; + auto iforh(SizeT step, Expr ex) const + -> For,Expr,ForType::HIDDEN>; template - auto pifor(size_t step, Expr ex) const - -> PFor,Expr>; + auto pifor(SizeT step, Expr ex) const + -> PFor,Expr>; private: - std::shared_ptr mExplicitRangePtr; - const U* mMetaPtr; + Sptr mRangePtr; + const MetaType* mMetaPtr; }; - template - class GenSingleRangeFactory : public RangeFactoryBase + template + class URangeFactory : public RangeFactoryBase { public: - - typedef GenSingleRange oType; + URangeFactory() = delete; + URangeFactory(const Vector& space); + URangeFactory(Vector&& space); - GenSingleRangeFactory() = delete; - GenSingleRangeFactory(const vector& space); - GenSingleRangeFactory(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 vector& in) - { - for(size_t i = 0; i != in.size(); ++i){ - mMap[in[i]] = i; - } - } - - size_t at(const U& in) const - { - auto x = mMap.find(in); - if(x != mMap.end()){ - return x->second; - } - else { - return mMap.size(); - } - } - size_t count(const U& in) const { return mMap.count(in); } + protected: + void make(); }; - template - struct CheckStatic - { - static constexpr size_t ISSTATIC = true; - static constexpr size_t SIZE = S; - }; - - template <> - struct CheckStatic - { - static constexpr size_t ISSTATIC = false; - static constexpr size_t SIZE = MUI; - }; - - template - struct CheckDefault - { - static constexpr size_t ISDEFAULT = false; - static constexpr size_t HASMETACONT = true; - }; - - template <> - struct CheckDefault - { - static constexpr size_t ISDEFAULT = true; - static constexpr size_t HASMETACONT = false; - }; - - template - struct ToCMeta - { - static inline size_t apply(char* target, const U& elem) - { - *reinterpret_cast(target) = elem; - return sizeof(U); - } - - static inline size_t size(const U& elem) - { - return sizeof(U); - } - }; - - template - struct ToCMeta> - { - static inline size_t apply(char* target, const vector& elem) - { - size_t o = 0; - for(auto& e: elem){ - o += ToCMeta::apply(target+o, e); - } - return o; - } - - static inline size_t size(const vector& elem) - { - size_t out = 0; - for(auto& x: elem){ - out += ToCMeta::size(x); - } - return out; - } - }; - - template - class GenSingleRange : public RangeInterface > + template + class URange : public RangeInterface> { public: typedef RangeBase RB; - typedef GenSingleIndex IndexType; - typedef GenSingleRange RangeType; - typedef U MetaType; - typedef GenSingleRangeFactory FType; + typedef UIndex IndexType; + typedef URangeFactory FType; - virtual size_t size() const final; - virtual size_t dim() const final; + virtual SizeT size() const final; + virtual SizeT 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 U& metaPos) const; - - const U& get(size_t pos) const; - size_t getMeta(const U& metaPos) const; + virtual std::string stringMeta(SizeT pos) const final; + const MetaType& get(SizeT pos) const; + SizeT getMeta(const MetaType& metaPos) const; virtual IndexType begin() const final; virtual IndexType end() const final; - friend GenSingleRangeFactory; + friend URangeFactory; - static GenSingleRangeFactory factory() - { - static_assert( not CheckDefault::HASMETACONT, - "asked for default factory for meta data containing range" ); - return GenSingleRangeFactory(vector()); - } - - static constexpr bool defaultable = CheckDefault::ISDEFAULT; - static constexpr size_t ISSTATIC = CheckStatic::ISSTATIC; - static constexpr size_t SIZE = CheckStatic::SIZE; - static constexpr bool HASMETACONT = CheckDefault::HASMETACONT; - protected: - GenSingleRange() = delete; - GenSingleRange(const GenSingleRange& in) = delete; - - GenSingleRange(const vector& space); - GenSingleRange(vector&& space); + URange() = delete; + URange(const URange& in) = delete; + URange(const vector& space); + URange(vector&& space); - vector mSpace; - MetaMap mMSpace; + Vector mSpace; // SORTED!!! }; - template - using SingleRange = GenSingleRange; - - template - using SingleIndex = GenSingleIndex; - - template - using SingleRangeFactory = GenSingleRangeFactory; - } /* ========================= * @@ -273,41 +110,9 @@ namespace CNORXZ namespace CNORXZ { - /****************** - * GenSingleIndex * - ******************/ - - 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); - } - }; + /***************** + * UIndex * + *****************/ template GenSingleIndex::GenSingleIndex(const std::shared_ptr >& range) :