crange.basic test works

This commit is contained in:
Christian Zimmermann 2022-09-18 00:49:36 +02:00
parent a10770b4f1
commit 4212ed7ee9
7 changed files with 188 additions and 25 deletions

View file

@ -15,6 +15,7 @@ namespace CNORXZ
DType::DType(const T& d) : mD(d)
{
_mkToStr<T>();
_mkComp<T>();
}
template <typename T>
@ -22,6 +23,7 @@ namespace CNORXZ
{
mD = d;
_mkToStr<T>();
_mkComp<T>();
return *this;
}
@ -43,10 +45,10 @@ namespace CNORXZ
else {
auto& at = std::any_cast<const T&>(a);
auto& dt = std::any_cast<const T&>(mD);
if(std::equal_to(dt,at)){
if(std::equal_to<T>{}(dt,at)){
return 0;
}
else if(std::less(dt,at)){
else if(std::less<T>{}(dt,at)){
return -1;
}
else {

View file

@ -35,7 +35,7 @@ namespace CNORXZ
template <typename T>
DType& operator=(const T& d);
std::string str() const { return mToStr(); }
String str() const { return mToStr(); }
const std::any& get() const { return mD; }
bool operator==(const DType& a) const { return mComp(a.mD) == 0; }

View file

@ -100,6 +100,36 @@ namespace CNORXZ
assert(mPtrId == reinterpret_cast<PtrId>(this));
return mPtrId;
}
/****************************
* Non-member functions *
****************************/
template <class I, typename MetaType>
IndexPtr<I,MetaType>& operator++(const IndexPtr<I,MetaType>& i)
{
++(*i);
return i;
}
template <class I, typename MetaType>
IndexPtr<I,MetaType>& operator--(const IndexPtr<I,MetaType>& i)
{
--(*i);
return i;
}
template <class I, typename MetaType>
IndexPtr<I,MetaType> operator+(const IndexPtr<I,MetaType>& i, Int n)
{
return std::make_shared<IndexInterface<I,MetaType>>( *i + n );
}
template <class I, typename MetaType>
IndexPtr<I,MetaType> operator-(const IndexPtr<I,MetaType>& i, Int n)
{
return std::make_shared<IndexInterface<I,MetaType>>( *i - n );
}
}
#endif

View file

@ -59,6 +59,9 @@ namespace CNORXZ
auto iforh(SizeT step, const Expr ex) const
{ return THIS().template iforh<Expr>(step,ex); }
*/
protected:
SizeT mPos = 0;
private:
friend I; // why not protected???!!!
@ -70,10 +73,15 @@ namespace CNORXZ
IndexInterface& operator=(IndexInterface&& in);
IndexInterface(SizeT pos);
SizeT mPos = 0;
PtrId mPtrId = 0;
};
template <class I, typename MetaType>
IndexPtr<I,MetaType>& operator++(const IndexPtr<I,MetaType>& i);
template <class I, typename MetaType>
IndexPtr<I,MetaType>& operator--(const IndexPtr<I,MetaType>& i);
// to define relative indices:
template <class I, typename MetaType>
IndexPtr<I,MetaType> operator+(const IndexPtr<I,MetaType>& i, Int n);

View file

@ -6,21 +6,26 @@
namespace CNORXZ
{
/**************
* XIndex *
**************/
template <class Index, typename Meta>
XIndex<Index,Meta>::XIndex(const IndexPtr<Index,Meta>& i) : mI(i) {}
XIndex<Index,Meta>::XIndex(const IndexPtr<Index,Meta>& i) :
XIndexBase(i->pos()),
mI(i) {}
template <class Index, typename Meta>
XIndex<Index,Meta>::XIndex(const IndexInterface<Index,Meta>& i) :
XIndexBase(i.pos()),
mI(std::make_shared<Index>(i.THIS())) {}
template <class Index, typename Meta>
XIndex<Index,Meta>& XIndex<Index,Meta>::operator=(SizeT pos)
{
*mI = pos;
IB::mPos = mI->pos();
return *this;
}
@ -28,6 +33,7 @@ namespace CNORXZ
XIndex<Index,Meta>& XIndex<Index,Meta>::operator++()
{
++(*mI);
IB::mPos = mI->pos();
return *this;
}
@ -35,19 +41,64 @@ namespace CNORXZ
XIndex<Index,Meta>& XIndex<Index,Meta>::operator--()
{
--(*mI);
IB::mPos = mI->pos();
return *this;
}
template <class Index, typename Meta>
Sptr<XIndexBase> XIndex<Index,Meta>::operator+(Int n) const
{
return std::make_shared<XIndex<Index,Meta>>(*mI + n);
}
template <class Index, typename Meta>
Sptr<XIndexBase> XIndex<Index,Meta>::operator-(Int n) const
{
return std::make_shared<XIndex<Index,Meta>>(*mI - n);
}
template <class Index, typename Meta>
XIndex<Index,Meta>& XIndex<Index,Meta>::operator+=(Int n)
{
(*mI) += n;
IB::mPos = mI->pos();
return *this;
}
template <class Index, typename Meta>
XIndex<Index,Meta>& XIndex<Index,Meta>::operator-=(Int n)
{
(*mI) -= n;
IB::mPos = mI->pos();
return *this;
}
template <class Index, typename Meta>
DType XIndex<Index,Meta>::operator*() const
{
return DType(*(*mI));
}
template <class Index, typename Meta>
DType XIndex<Index,Meta>::operator->() const
{
return DType(*(*mI));
}
template <class Index, typename Meta>
Int XIndex<Index,Meta>::pp(PtrId idxPtrNum)
{
return mI->pp(idxPtrNum);
Int out = mI->pp(idxPtrNum);
IB::mPos = mI->pos();
return out;
}
template <class Index, typename Meta>
Int XIndex<Index,Meta>::mm(PtrId idxPtrNum)
{
return mI->mm(idxPtrNum);
Int out = mI->mm(idxPtrNum);
IB::mPos = mI->pos();
return out;
}
template <class Index, typename Meta>
@ -56,6 +107,12 @@ namespace CNORXZ
return mI->dim();
}
template <class Index, typename Meta>
RangePtr XIndex<Index,Meta>::range() const
{
return mI->range();
}
template <class Index, typename Meta>
SizeT XIndex<Index,Meta>::getStepSize(SizeT n) const
{
@ -79,6 +136,7 @@ namespace CNORXZ
{
// check!!!
mI->at(std::any_cast<const Meta&>(meta.get()));
IB::mPos = mI->pos();
return *this;
}
/*

View file

@ -12,48 +12,82 @@ namespace CNORXZ
class XIndexBase : public IndexInterface<XIndexBase,DType>
{
public:
typedef IndexInterface<XIndexBase,DType> IB;
DEFAULT_MEMBERS(XIndexBase);
XIndexBase(SizeT pos) : IndexInterface<XIndexBase,DType>(pos) {}
virtual XIndexBase& operator=(SizeT pos) = 0;
virtual XIndexBase& operator++() = 0;
virtual XIndexBase& operator--() = 0;
virtual Sptr<XIndexBase> operator+(Int n) const = 0;
virtual Sptr<XIndexBase> operator-(Int n) const = 0;
virtual XIndexBase& operator+=(Int n) = 0;
virtual XIndexBase& operator-=(Int n) = 0;
virtual DType operator*() const = 0;
virtual DType operator->() const = 0;
virtual Int pp(PtrId idxPtrNum) = 0;
virtual Int mm(PtrId idxPtrNum) = 0;
virtual size_t dim() const = 0;
virtual size_t getStepSize(SizeT n) const = 0;
virtual SizeT dim() const = 0;
virtual RangePtr range() const = 0;
virtual SizeT getStepSize(SizeT n) const = 0;
virtual String stringMeta() const = 0;
virtual DType meta() const = 0;
virtual XIndexBase& at(const DType& meta) = 0;
//virtual DExpr ifor(SizeT step, DExpr ex) const = 0;
//virtual DExpr iforh(SizeT step, DExpr ex) const = 0;
// ...!!!
};
//Sptr<XIndexBase>& operator++(Sptr<XIndexBase>& i);
//Sptr<XIndexBase>& operator--(Sptr<XIndexBase>& i);
// MultiIndex Wrapper:
template <class Index, typename Meta>
class XIndex : public XIndexBase
{
private:
IndexPtr<Index,Meta> mI;
public:
typedef XIndexBase XIB;
typedef XIB::IB IB;
DEFAULT_MEMBERS(XIndex);
XIndex(const IndexPtr<Index,Meta>& i);
XIndex(const IndexInterface<Index,Meta>& i);
virtual XIndex& operator=(SizeT pos) override;
virtual XIndex& operator++() override;
virtual XIndex& operator--() override;
virtual Int pp(PtrId idxPtrNum) override;
virtual Int mm(PtrId idxPtrNum) override;
virtual SizeT dim() const override;
virtual SizeT getStepSize(SizeT n) const override;
virtual String stringMeta() const override;
virtual DType meta() const override;
virtual XIndexBase& at(const DType& meta) override;
//virtual DExpr ifor(SizeT step, DExpr ex) const override;
//virtual DExpr iforh(SizeT step, DExpr ex) const override;
virtual XIndex& operator=(SizeT pos) override final;
virtual XIndex& operator++() override final;
virtual XIndex& operator--() override final;
virtual Sptr<XIndexBase> operator+(Int n) const override final;
virtual Sptr<XIndexBase> operator-(Int n) const override final;
virtual XIndex& operator+=(Int n) override final;
virtual XIndex& operator-=(Int n) override final;
virtual DType operator*() const override final;
virtual DType operator->() const override final;
virtual Int pp(PtrId idxPtrNum) override final;
virtual Int mm(PtrId idxPtrNum) override final;
virtual SizeT dim() const override final;
virtual RangePtr range() const override final;
virtual SizeT getStepSize(SizeT n) const override final;
virtual String stringMeta() const override final;
virtual DType meta() const override final;
virtual XIndexBase& at(const DType& meta) override final;
//virtual DExpr ifor(SizeT step, DExpr ex) const override final;
//virtual DExpr iforh(SizeT step, DExpr ex) const override final;
// ....!!!!
private:
IndexPtr<Index,Meta> mI;
};
}

View file

@ -27,9 +27,40 @@ namespace {
TEST_F(CR_Test, Basics)
{
auto cr = CRangeFactory(mSize).create();
auto crx = std::dynamic_pointer_cast<CRange>(cr);
EXPECT_EQ(cr->size(), mSize);
EXPECT_EQ(crx->size(), mSize);
EXPECT_EQ(crx->begin() != crx->end(), true);
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);
SizeT cnt = 0;
auto endxi = cr->end();
for(auto xi = cr->begin(); *xi != *endxi; ++(*xi)){
EXPECT_EQ(xi->pos(), cnt);
EXPECT_EQ(*(*xi), cnt);
++cnt;
}
SizeT cnt2 = 0;
for(auto x: *crx){
EXPECT_EQ(x, cnt2++);
}
}
// UR_Test
// RCast_Test
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}