diff --git a/src/include/base/types.h b/src/include/base/types.h index 455587d..505fbad 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -154,6 +154,9 @@ namespace CNORXZ typedef Sptr XIndexPtr; + // definition: ranges/dindex.h + class DIndex; + // definition: ranges/yrange.h class YRange; // dynamic multi range diff --git a/src/include/ranges/dindex.cc.h b/src/include/ranges/dindex.cc.h new file mode 100644 index 0000000..0257b44 --- /dev/null +++ b/src/include/ranges/dindex.cc.h @@ -0,0 +1,17 @@ + +#ifndef __cxz_dindex_cc_h__ +#define __cxz_dindex_cc_h__ + +#include "dindex.h" + +namespace CNORXZ +{ + template + DIndex::DIndex(const IndexInterface& i) : + IndexInterface(i.pos()), + mI(std::make_shared>(i)) + {} + +} + +#endif diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h new file mode 100644 index 0000000..0312a45 --- /dev/null +++ b/src/include/ranges/dindex.h @@ -0,0 +1,57 @@ + +#ifndef __cxz_dindex_h__ +#define __cxz_dindex_h__ + +#include "base/base.h" +#include "range_base.h" +#include "index_base.h" + +namespace CNORXZ +{ + class DIndex : public IndexInterface + { + public: + typedef IndexInterface IB; + + DEFAULT_C(DIndex); + DIndex(const DIndex& i); + DIndex(DIndex&& i); + DIndex& operator=(const DIndex& i); + DIndex& operator=(DIndex&& i); + DIndex(const XIndexPtr& i); + + template + DIndex(const IndexInterface& i); + + DIndex& operator=(SizeT pos); + DIndex& operator++(); + DIndex& operator--(); + DIndex operator+(Int n) const; + DIndex operator-(Int n) const; + DIndex& operator+=(Int n); + DIndex& operator-=(Int n); + + DType operator*() const; + DType operator->() const; + + Int pp(PtrId idxPtrNum); + Int mm(PtrId idxPtrNum); + + SizeT dim() const; + RangePtr range() const; + SizeT getStepSize(SizeT n) const; + + String stringMeta() const; + DType meta() const; + DIndex& at(const DType& meta); + + //DExpr ifor(SizeT step, DExpr ex) const; + //DExpr iforh(SizeT step, DExpr ex) const; + + private: + XIndexPtr mI; + }; + +} + +#endif diff --git a/src/include/ranges/range_base.cc.h b/src/include/ranges/range_base.cc.h index 1949265..a66abf2 100644 --- a/src/include/ranges/range_base.cc.h +++ b/src/include/ranges/range_base.cc.h @@ -3,6 +3,7 @@ #define __cxz_range_base_cc_h__ #include "range_base.h" +#include "dindex.h" namespace CNORXZ { @@ -19,9 +20,9 @@ namespace CNORXZ } template - XIndexPtr RangeInterface::index(SizeT pos) const + DIndex RangeInterface::index(SizeT pos) const { - return std::make_shared>( this->begin()+pos ); + return XIndexPtr(std::make_shared>( this->begin()+pos )); } template diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 10b5f37..0994070 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -50,14 +50,14 @@ namespace CNORXZ virtual const TypeInfo& type() const = 0; virtual const TypeInfo& metaType() const = 0; virtual String stringMeta(SizeT pos) const = 0; - virtual XIndexPtr index(SizeT pos = 0) const = 0; + virtual DIndex index(SizeT pos = 0) const = 0; bool operator==(const RangeBase& in) const; bool operator!=(const RangeBase& in) const; PtrId id() const; - XIndexPtr begin() const; - XIndexPtr end() const; + DIndex begin() const; + DIndex end() const; RangePtr orig() const; friend RangeFactoryBase; @@ -81,7 +81,7 @@ namespace CNORXZ Index begin() const; Index end() const; - virtual XIndexPtr index(SizeT pos) const override final; + virtual DIndex index(SizeT pos) const override final; protected: RangeInterface() = default; diff --git a/src/include/ranges/ranges.cc.h b/src/include/ranges/ranges.cc.h index a66fe57..606acb6 100644 --- a/src/include/ranges/ranges.cc.h +++ b/src/include/ranges/ranges.cc.h @@ -4,3 +4,4 @@ #include "xindex.cc.h" #include "urange.cc.h" #include "crange.cc.h" +#include "dindex.cc.h" diff --git a/src/include/ranges/ranges.h b/src/include/ranges/ranges.h index 271be12..3aa6ea2 100644 --- a/src/include/ranges/ranges.h +++ b/src/include/ranges/ranges.h @@ -8,5 +8,6 @@ //#include "value_range.h" #include "xindex.h" #include "yrange.h" +#include "dindex.h" #include "ranges.cc.h" diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index 55c5d9f..75032a5 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -13,19 +13,28 @@ namespace CNORXZ template XIndex::XIndex(const IndexPtr& i) : - XIndexBase(i->pos()), mI(i) {} template XIndex::XIndex(const IndexInterface& i) : - XIndexBase(i.pos()), mI(std::make_shared(i.THIS())) {} + template + XIndexPtr XIndex::copy() const + { + return std::make_shared>(mI->THIS()); + } + + template + SizeT XIndex::pos() const + { + return mI->pos(); + } + template XIndex& XIndex::operator=(SizeT pos) { *mI = pos; - IB::mPos = mI->pos(); return *this; } @@ -33,7 +42,6 @@ namespace CNORXZ XIndex& XIndex::operator++() { ++(*mI); - IB::mPos = mI->pos(); return *this; } @@ -41,7 +49,6 @@ namespace CNORXZ XIndex& XIndex::operator--() { --(*mI); - IB::mPos = mI->pos(); return *this; } @@ -61,7 +68,6 @@ namespace CNORXZ XIndex& XIndex::operator+=(Int n) { (*mI) += n; - IB::mPos = mI->pos(); return *this; } @@ -69,7 +75,6 @@ namespace CNORXZ XIndex& XIndex::operator-=(Int n) { (*mI) -= n; - IB::mPos = mI->pos(); return *this; } @@ -89,7 +94,6 @@ namespace CNORXZ Int XIndex::pp(PtrId idxPtrNum) { Int out = mI->pp(idxPtrNum); - IB::mPos = mI->pos(); return out; } @@ -97,7 +101,6 @@ namespace CNORXZ Int XIndex::mm(PtrId idxPtrNum) { Int out = mI->mm(idxPtrNum); - IB::mPos = mI->pos(); return out; } @@ -136,7 +139,6 @@ namespace CNORXZ { // check!!! mI->at(std::any_cast(meta.get())); - IB::mPos = mI->pos(); return *this; } /* diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index 773328c..4325a36 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -9,19 +9,18 @@ namespace CNORXZ { // Future IndexWrapper - class XIndexBase : public IndexInterface + class XIndexBase { public: - typedef IndexInterface IB; - DEFAULT_MEMBERS(XIndexBase); - XIndexBase(SizeT pos) : IndexInterface(pos) {} + virtual XIndexPtr copy() const = 0; + virtual SizeT pos() const = 0; virtual XIndexBase& operator=(SizeT pos) = 0; virtual XIndexBase& operator++() = 0; virtual XIndexBase& operator--() = 0; - virtual Sptr operator+(Int n) const = 0; - virtual Sptr operator-(Int n) const = 0; + virtual XIndexPtr operator+(Int n) const = 0; + virtual XIndexPtr operator-(Int n) const = 0; virtual XIndexBase& operator+=(Int n) = 0; virtual XIndexBase& operator-=(Int n) = 0; @@ -52,18 +51,24 @@ namespace CNORXZ class XIndex : public XIndexBase { public: - typedef XIndexBase XIB; - typedef XIB::IB IB; - - DEFAULT_MEMBERS(XIndex); + + DEFAULT_C(XIndex); + // no default copy/assignment (have to copy objects in shared ptr) + XIndex(const XIndex& i); + XIndex(XIndex&& i); + XIndex& operator=(const XIndex& i); + XIndex& operator=(XIndex&& i); XIndex(const IndexPtr& i); XIndex(const IndexInterface& i); + virtual XIndexPtr copy() const override final; + virtual SizeT pos() const override final; + virtual XIndex& operator=(SizeT pos) override final; virtual XIndex& operator++() override final; virtual XIndex& operator--() override final; - virtual Sptr operator+(Int n) const override final; - virtual Sptr operator-(Int n) const override final; + virtual XIndexPtr operator+(Int n) const override final; + virtual XIndexPtr operator-(Int n) const override final; virtual XIndex& operator+=(Int n) override final; virtual XIndex& operator-=(Int n) override final; diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 9d8f529..8661091 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -5,6 +5,7 @@ set(libcnorxz_a_SOURCES ${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/yrange.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/crange.cc + ${CMAKE_SOURCE_DIR}/src/lib/ranges/dindex.cc ) add_library(cnorxz_obj OBJECT diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc new file mode 100644 index 0000000..f091afe --- /dev/null +++ b/src/lib/ranges/dindex.cc @@ -0,0 +1,144 @@ + +#include "ranges/ranges.h" + +namespace CNORXZ +{ + DIndex::DIndex(const DIndex& i) : + IndexInterface(i), + mI(i.mI->copy()) + {} + + DIndex::DIndex(DIndex&& i) : + IndexInterface(i), + mI(i.mI) + { + i.mI = nullptr; + } + + DIndex& DIndex::operator=(const DIndex& i) + { + IndexInterface::operator=(i); + mI = i.mI->copy(); + IB::mPos = mI->pos(); + return *this; + } + + DIndex& DIndex::operator=(DIndex&& i) + { + IndexInterface::operator=(i); + mI = i.mI; + IB::mPos = mI->pos(); + i.mI = nullptr; + return *this; + } + + DIndex::DIndex(const XIndexPtr& i) : + IndexInterface(i->pos()), + mI(i) + {} + + DIndex& DIndex::operator=(SizeT pos) + { + *mI = pos; + IB::mPos = mI->pos(); + return *this; + } + + DIndex& DIndex::operator++() + { + ++(*mI); + IB::mPos = mI->pos(); + return *this; + } + + DIndex& DIndex::operator--() + { + --(*mI); + IB::mPos = mI->pos(); + return *this; + } + + DIndex DIndex::operator+(Int n) const + { + return (*mI) + n; + } + + DIndex DIndex::operator-(Int n) const + { + return (*mI) - n; + } + + DIndex& DIndex::operator+=(Int n) + { + (*mI) += n; + IB::mPos = mI->pos(); + return *this; + } + + DIndex& DIndex::operator-=(Int n) + { + + IB::mPos = mI->pos(); + return *this; + } + + DType DIndex::operator*() const + { + return *(*mI); + } + + DType DIndex::operator->() const + { + return *(*mI); + } + + Int DIndex::pp(PtrId idxPtrNum) + { + const Int out = mI->pp(idxPtrNum); + IB::mPos = mI->pos(); + return out; + } + + Int DIndex::mm(PtrId idxPtrNum) + { + const Int out = mI->mm(idxPtrNum); + IB::mPos = mI->pos(); + return out; + } + + SizeT DIndex::dim() const + { + return mI->dim(); + } + + RangePtr DIndex::range() const + { + return mI->range(); + } + + SizeT DIndex::getStepSize(SizeT n) const + { + return mI->getStepSize(n); + } + + String DIndex::stringMeta() const + { + return mI->stringMeta(); + } + + DType DIndex::meta() const + { + return mI->meta(); + } + + DIndex& DIndex::at(const DType& meta) + { + mI->at(meta); + IB::mPos = mI->pos(); + return *this; + } + + //DExpr DIndex::ifor(SizeT step, DExpr ex) const; + //DExpr DIndex::iforh(SizeT step, DExpr ex) const; + +} diff --git a/src/lib/ranges/range_base.cc b/src/lib/ranges/range_base.cc index 4e32e0a..1c12b37 100644 --- a/src/lib/ranges/range_base.cc +++ b/src/lib/ranges/range_base.cc @@ -1,5 +1,5 @@ -#include "ranges/range_base.h" +#include "ranges/ranges.h" namespace CNORXZ { @@ -51,15 +51,15 @@ namespace CNORXZ PtrId RangeBase::id() const { - return reinterpret_cast(this); + return reinterpret_cast(this); } - XIndexPtr RangeBase::begin() const + DIndex RangeBase::begin() const { return this->index(0); } - XIndexPtr RangeBase::end() const + DIndex RangeBase::end() const { return this->index(this->size()); } diff --git a/src/tests/range_unit_test.cc b/src/tests/range_unit_test.cc index 877269f..7ff4e78 100644 --- a/src/tests/range_unit_test.cc +++ b/src/tests/range_unit_test.cc @@ -35,16 +35,16 @@ namespace { EXPECT_EQ(crx->begin().pos(), 0); EXPECT_EQ(crx->end().pos(), mSize); - EXPECT_EQ(*cr->begin() != *cr->end(), true); - EXPECT_EQ(cr->begin()->pos(), 0); - EXPECT_EQ(cr->end()->pos(), mSize); + EXPECT_EQ(cr->begin() != cr->end(), true); + EXPECT_EQ(cr->begin().pos(), 0); + EXPECT_EQ(cr->end().pos(), mSize); SizeT cnt = 0; auto endxi = cr->end(); - for(auto xi = cr->begin(); *xi != *endxi; ++(*xi)){ - EXPECT_EQ(xi->pos(), cnt); - EXPECT_EQ(*(*xi), cnt); + for(auto xi = cr->begin(); xi != endxi; ++xi){ + EXPECT_EQ(xi.pos(), cnt); + EXPECT_EQ(*xi, cnt); ++cnt; }