add dindex (-> standard index of RangeBase)

This commit is contained in:
Christian Zimmermann 2022-09-18 16:19:27 +02:00
parent 4212ed7ee9
commit e542a37bf3
13 changed files with 270 additions and 38 deletions

View file

@ -154,6 +154,9 @@ namespace CNORXZ
typedef Sptr<XIndexBase> XIndexPtr; typedef Sptr<XIndexBase> XIndexPtr;
// definition: ranges/dindex.h
class DIndex;
// definition: ranges/yrange.h // definition: ranges/yrange.h
class YRange; // dynamic multi range class YRange; // dynamic multi range

View file

@ -0,0 +1,17 @@
#ifndef __cxz_dindex_cc_h__
#define __cxz_dindex_cc_h__
#include "dindex.h"
namespace CNORXZ
{
template <class Index, typename Meta>
DIndex::DIndex(const IndexInterface<Index,Meta>& i) :
IndexInterface<DIndex,DType>(i.pos()),
mI(std::make_shared<XIndex<Index,Meta>>(i))
{}
}
#endif

View file

@ -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<DIndex,DType>
{
public:
typedef IndexInterface<DIndex,DType> 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 <class Index, typename Meta>
DIndex(const IndexInterface<Index,Meta>& 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

View file

@ -3,6 +3,7 @@
#define __cxz_range_base_cc_h__ #define __cxz_range_base_cc_h__
#include "range_base.h" #include "range_base.h"
#include "dindex.h"
namespace CNORXZ namespace CNORXZ
{ {
@ -19,9 +20,9 @@ namespace CNORXZ
} }
template <class Index, typename Meta> template <class Index, typename Meta>
XIndexPtr RangeInterface<Index,Meta>::index(SizeT pos) const DIndex RangeInterface<Index,Meta>::index(SizeT pos) const
{ {
return std::make_shared<XIndex<Index,Meta>>( this->begin()+pos ); return XIndexPtr(std::make_shared<XIndex<Index,Meta>>( this->begin()+pos ));
} }
template <class Range> template <class Range>

View file

@ -50,14 +50,14 @@ namespace CNORXZ
virtual const TypeInfo& type() const = 0; virtual const TypeInfo& type() const = 0;
virtual const TypeInfo& metaType() const = 0; virtual const TypeInfo& metaType() const = 0;
virtual String stringMeta(SizeT pos) 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;
bool operator!=(const RangeBase& in) const; bool operator!=(const RangeBase& in) const;
PtrId id() const; PtrId id() const;
XIndexPtr begin() const; DIndex begin() const;
XIndexPtr end() const; DIndex end() const;
RangePtr orig() const; RangePtr orig() const;
friend RangeFactoryBase; friend RangeFactoryBase;
@ -81,7 +81,7 @@ namespace CNORXZ
Index begin() const; Index begin() const;
Index end() const; Index end() const;
virtual XIndexPtr index(SizeT pos) const override final; virtual DIndex index(SizeT pos) const override final;
protected: protected:
RangeInterface() = default; RangeInterface() = default;

View file

@ -4,3 +4,4 @@
#include "xindex.cc.h" #include "xindex.cc.h"
#include "urange.cc.h" #include "urange.cc.h"
#include "crange.cc.h" #include "crange.cc.h"
#include "dindex.cc.h"

View file

@ -8,5 +8,6 @@
//#include "value_range.h" //#include "value_range.h"
#include "xindex.h" #include "xindex.h"
#include "yrange.h" #include "yrange.h"
#include "dindex.h"
#include "ranges.cc.h" #include "ranges.cc.h"

View file

@ -13,19 +13,28 @@ namespace CNORXZ
template <class Index, typename Meta> template <class Index, typename Meta>
XIndex<Index,Meta>::XIndex(const IndexPtr<Index,Meta>& i) : XIndex<Index,Meta>::XIndex(const IndexPtr<Index,Meta>& i) :
XIndexBase(i->pos()),
mI(i) {} mI(i) {}
template <class Index, typename Meta> template <class Index, typename Meta>
XIndex<Index,Meta>::XIndex(const IndexInterface<Index,Meta>& i) : XIndex<Index,Meta>::XIndex(const IndexInterface<Index,Meta>& i) :
XIndexBase(i.pos()),
mI(std::make_shared<Index>(i.THIS())) {} mI(std::make_shared<Index>(i.THIS())) {}
template <class Index, typename Meta>
XIndexPtr XIndex<Index,Meta>::copy() const
{
return std::make_shared<XIndex<Index,Meta>>(mI->THIS());
}
template <class Index, typename Meta>
SizeT XIndex<Index,Meta>::pos() const
{
return mI->pos();
}
template <class Index, typename Meta> template <class Index, typename Meta>
XIndex<Index,Meta>& XIndex<Index,Meta>::operator=(SizeT pos) XIndex<Index,Meta>& XIndex<Index,Meta>::operator=(SizeT pos)
{ {
*mI = pos; *mI = pos;
IB::mPos = mI->pos();
return *this; return *this;
} }
@ -33,7 +42,6 @@ namespace CNORXZ
XIndex<Index,Meta>& XIndex<Index,Meta>::operator++() XIndex<Index,Meta>& XIndex<Index,Meta>::operator++()
{ {
++(*mI); ++(*mI);
IB::mPos = mI->pos();
return *this; return *this;
} }
@ -41,7 +49,6 @@ namespace CNORXZ
XIndex<Index,Meta>& XIndex<Index,Meta>::operator--() XIndex<Index,Meta>& XIndex<Index,Meta>::operator--()
{ {
--(*mI); --(*mI);
IB::mPos = mI->pos();
return *this; return *this;
} }
@ -61,7 +68,6 @@ namespace CNORXZ
XIndex<Index,Meta>& XIndex<Index,Meta>::operator+=(Int n) XIndex<Index,Meta>& XIndex<Index,Meta>::operator+=(Int n)
{ {
(*mI) += n; (*mI) += n;
IB::mPos = mI->pos();
return *this; return *this;
} }
@ -69,7 +75,6 @@ namespace CNORXZ
XIndex<Index,Meta>& XIndex<Index,Meta>::operator-=(Int n) XIndex<Index,Meta>& XIndex<Index,Meta>::operator-=(Int n)
{ {
(*mI) -= n; (*mI) -= n;
IB::mPos = mI->pos();
return *this; return *this;
} }
@ -89,7 +94,6 @@ namespace CNORXZ
Int XIndex<Index,Meta>::pp(PtrId idxPtrNum) Int XIndex<Index,Meta>::pp(PtrId idxPtrNum)
{ {
Int out = mI->pp(idxPtrNum); Int out = mI->pp(idxPtrNum);
IB::mPos = mI->pos();
return out; return out;
} }
@ -97,7 +101,6 @@ namespace CNORXZ
Int XIndex<Index,Meta>::mm(PtrId idxPtrNum) Int XIndex<Index,Meta>::mm(PtrId idxPtrNum)
{ {
Int out = mI->mm(idxPtrNum); Int out = mI->mm(idxPtrNum);
IB::mPos = mI->pos();
return out; return out;
} }
@ -136,7 +139,6 @@ namespace CNORXZ
{ {
// check!!! // check!!!
mI->at(std::any_cast<const Meta&>(meta.get())); mI->at(std::any_cast<const Meta&>(meta.get()));
IB::mPos = mI->pos();
return *this; return *this;
} }
/* /*

View file

@ -9,19 +9,18 @@
namespace CNORXZ namespace CNORXZ
{ {
// Future IndexWrapper // Future IndexWrapper
class XIndexBase : public IndexInterface<XIndexBase,DType> class XIndexBase
{ {
public: public:
typedef IndexInterface<XIndexBase,DType> IB;
DEFAULT_MEMBERS(XIndexBase); DEFAULT_MEMBERS(XIndexBase);
XIndexBase(SizeT pos) : IndexInterface<XIndexBase,DType>(pos) {} virtual XIndexPtr copy() const = 0;
virtual SizeT pos() const = 0;
virtual XIndexBase& operator=(SizeT pos) = 0; virtual XIndexBase& operator=(SizeT pos) = 0;
virtual XIndexBase& operator++() = 0; virtual XIndexBase& operator++() = 0;
virtual XIndexBase& operator--() = 0; virtual XIndexBase& operator--() = 0;
virtual Sptr<XIndexBase> operator+(Int n) const = 0; virtual XIndexPtr operator+(Int n) const = 0;
virtual Sptr<XIndexBase> operator-(Int n) const = 0; virtual XIndexPtr operator-(Int n) const = 0;
virtual XIndexBase& operator+=(Int n) = 0; virtual XIndexBase& operator+=(Int n) = 0;
virtual XIndexBase& operator-=(Int n) = 0; virtual XIndexBase& operator-=(Int n) = 0;
@ -52,18 +51,24 @@ namespace CNORXZ
class XIndex : public XIndexBase class XIndex : public XIndexBase
{ {
public: 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<Index,Meta>& i); XIndex(const IndexPtr<Index,Meta>& i);
XIndex(const IndexInterface<Index,Meta>& i); XIndex(const IndexInterface<Index,Meta>& i);
virtual XIndexPtr copy() const override final;
virtual SizeT pos() const override final;
virtual XIndex& operator=(SizeT pos) override final; virtual XIndex& operator=(SizeT pos) override final;
virtual XIndex& operator++() override final; virtual XIndex& operator++() override final;
virtual XIndex& operator--() override final; virtual XIndex& operator--() override final;
virtual Sptr<XIndexBase> operator+(Int n) const override final; virtual XIndexPtr operator+(Int n) const override final;
virtual Sptr<XIndexBase> 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;
virtual XIndex& operator-=(Int n) override final; virtual XIndex& operator-=(Int n) override final;

View file

@ -5,6 +5,7 @@ set(libcnorxz_a_SOURCES
${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/yrange.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/yrange.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/crange.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/crange.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/dindex.cc
) )
add_library(cnorxz_obj OBJECT add_library(cnorxz_obj OBJECT

144
src/lib/ranges/dindex.cc Normal file
View file

@ -0,0 +1,144 @@
#include "ranges/ranges.h"
namespace CNORXZ
{
DIndex::DIndex(const DIndex& i) :
IndexInterface<DIndex,DType>(i),
mI(i.mI->copy())
{}
DIndex::DIndex(DIndex&& i) :
IndexInterface<DIndex,DType>(i),
mI(i.mI)
{
i.mI = nullptr;
}
DIndex& DIndex::operator=(const DIndex& i)
{
IndexInterface<DIndex,DType>::operator=(i);
mI = i.mI->copy();
IB::mPos = mI->pos();
return *this;
}
DIndex& DIndex::operator=(DIndex&& i)
{
IndexInterface<DIndex,DType>::operator=(i);
mI = i.mI;
IB::mPos = mI->pos();
i.mI = nullptr;
return *this;
}
DIndex::DIndex(const XIndexPtr& i) :
IndexInterface<DIndex,DType>(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;
}

View file

@ -1,5 +1,5 @@
#include "ranges/range_base.h" #include "ranges/ranges.h"
namespace CNORXZ namespace CNORXZ
{ {
@ -51,15 +51,15 @@ namespace CNORXZ
PtrId RangeBase::id() const PtrId RangeBase::id() const
{ {
return reinterpret_cast<std::intptr_t>(this); return reinterpret_cast<PtrId>(this);
} }
XIndexPtr RangeBase::begin() const DIndex RangeBase::begin() const
{ {
return this->index(0); return this->index(0);
} }
XIndexPtr RangeBase::end() const DIndex RangeBase::end() const
{ {
return this->index(this->size()); return this->index(this->size());
} }

View file

@ -35,16 +35,16 @@ namespace {
EXPECT_EQ(crx->begin().pos(), 0); EXPECT_EQ(crx->begin().pos(), 0);
EXPECT_EQ(crx->end().pos(), mSize); EXPECT_EQ(crx->end().pos(), mSize);
EXPECT_EQ(*cr->begin() != *cr->end(), true); EXPECT_EQ(cr->begin() != cr->end(), true);
EXPECT_EQ(cr->begin()->pos(), 0); EXPECT_EQ(cr->begin().pos(), 0);
EXPECT_EQ(cr->end()->pos(), mSize); EXPECT_EQ(cr->end().pos(), mSize);
SizeT cnt = 0; SizeT cnt = 0;
auto endxi = cr->end(); auto endxi = cr->end();
for(auto xi = cr->begin(); *xi != *endxi; ++(*xi)){ for(auto xi = cr->begin(); xi != endxi; ++xi){
EXPECT_EQ(xi->pos(), cnt); EXPECT_EQ(xi.pos(), cnt);
EXPECT_EQ(*(*xi), cnt); EXPECT_EQ(*xi, cnt);
++cnt; ++cnt;
} }