intermediate commit; start to refactor index linkage routines

This commit is contained in:
Christian Zimmermann 2017-03-13 16:24:00 +01:00
parent 50985caf62
commit 5492521c76
7 changed files with 142 additions and 12 deletions

View file

@ -85,6 +85,7 @@ namespace MultiArrayTools
{ {
//assert(not virt()); //assert(not virt());
mPos = pos; mPos = pos;
//evalMajor();
if(linked()){ if(linked()){
mLinked->setPos(mPos); mLinked->setPos(mPos);
mLinked->evalMajor(); mLinked->evalMajor();
@ -104,12 +105,24 @@ namespace MultiArrayTools
return true; return true;
} }
void IndefinitIndexBase::evalMajor(size_t stepSize, int num) void IndefinitIndexBase::evalMajor()
{ {
//assert(not virt()); //assert(not virt());
if(not master()){ if(not master()){
//mMajor->eval(); //int start = mMajor->pos();
mMajor->setPos(static_cast<int>( mMajor->pos() ) + num, this) mMajor->eval();
//VCHECK(static_cast<int>( mMajor->pos() ) - start);
}
}
void IndefinitIndexBase::evalMajor(size_t stepSize, int num)
{
//assert(not virt());
//CHECK;
if(not master()){
//int start = mMajor->pos();
mMajor->setPos( num * stepSize , this);
//VCHECK(static_cast<int>( mMajor->pos() ) - start);
} }
} }
@ -123,7 +136,12 @@ namespace MultiArrayTools
{ {
//assert(not virt()); //assert(not virt());
mMajor = major; mMajor = major;
mMajorStepSize = mMajor->giveSubStepSize(this); mMajorStep = mMajor->giveSubStepSize(this);
}
size_t IndefinitIndexBase::majorStep() const
{
return mMajorStep;
} }
/************** /**************

View file

@ -43,14 +43,14 @@ namespace MultiArrayTools
virtual void freeLinked(); virtual void freeLinked();
virtual bool linked() const; virtual bool linked() const;
virtual void linkTo(IndefinitIndexBase* target) = 0; virtual void linkTo(IndefinitIndexBase* target) = 0;
virtual IndefinitIndexBase* getLinked(const std::string& name) = 0;
//virtual IndefinitIndexBase* getLinked(const std::string& name) = 0;
// include sub-index update routine // include sub-index update routine
virtual void setPos(size_t pos); virtual void setPos(size_t pos);
// does NOT include sub-index update routine !! // does NOT include sub-index update routine !!
virtual void setPos(size_t pos, IndefinitIndexBase* subIndex) = 0; // relative position to current
virtual void setPos(int relPos, IndefinitIndexBase* subIndex) = 0;
virtual size_t max() const = 0; virtual size_t max() const = 0;
virtual size_t outOfRange() const; virtual size_t outOfRange() const;
@ -58,11 +58,14 @@ namespace MultiArrayTools
virtual bool toNull() const; virtual bool toNull() const;
virtual void eval() = 0; virtual void eval() = 0;
virtual void evalMajor();
virtual void evalMajor(size_t stepSize, int num); virtual void evalMajor(size_t stepSize, int num);
virtual bool master(); virtual bool master();
virtual void subOrd(IndefinitIndexBase* major); virtual void subOrd(IndefinitIndexBase* major);
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0; virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0;
virtual size_t majorStep() const;
//virtual bool virt() const { return true; } //virtual bool virt() const { return true; }

View file

@ -73,10 +73,13 @@ namespace MultiArrayTools
} }
}; };
// rewrite !!
template <class MultiIndex> template <class MultiIndex>
inline void plus(MultiIndex& index, size_t digit, int num) inline void plus(MultiIndex& index, size_t digit, int num)
{ {
IndefinitIndexBase& si = index.get(digit); IndefinitIndexBase& si = index.get(digit);
//si.setPos( num, &index );
//si.setPos( -num, &index );
si.setPos( si.pos() + num ); si.setPos( si.pos() + num );
size_t oor = si.outOfRange(); size_t oor = si.outOfRange();
if(oor and digit != 0){ if(oor and digit != 0){
@ -331,32 +334,36 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator++() MultiIndex<Indices...>& MultiIndex<Indices...>::operator++()
{ {
//CHECK;
plus(*this, sizeof...(Indices)-1, 1); plus(*this, sizeof...(Indices)-1, 1);
IIB::setPos( evaluate(*this) ); setPos(1, this);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator--() MultiIndex<Indices...>& MultiIndex<Indices...>::operator--()
{ {
IIB::setPos( IIB::pos() - 1 ); // !!! >|< (0 does not make sense)
plus(*this, 0, -1); plus(*this, 0, -1);
setPos(-1, this);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator+=(int n) MultiIndex<Indices...>& MultiIndex<Indices...>::operator+=(int n)
{ {
//CHECK;
plus(*this, sizeof...(Indices)-1, 1); plus(*this, sizeof...(Indices)-1, 1);
IIB::setPos( evaluate(*this) ); setPos(n, this);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator-=(int n) MultiIndex<Indices...>& MultiIndex<Indices...>::operator-=(int n)
{ {
IIB::setPos( IIB::pos() - n ); // !!! >|<
plus(*this, 0, 0-n); plus(*this, 0, 0-n);
setPos(-n, this);
return *this; return *this;
} }
@ -549,6 +556,51 @@ namespace MultiArrayTools
PositionCopy<sizeof...(Indices)-1>::copyPos(*this, in); PositionCopy<sizeof...(Indices)-1>::copyPos(*this, in);
} }
template <class... Indices>
IndefinitIndexBase* MultiIndex<Indices...>::getLinked(const std::string& name)
{
if(name == IIB::mName){
return this;
}
for(size_t i = 0; i != sizeof...(Indices); ++i){
IndefinitIndexBase* iibPtr = get(i).getLinked(name);
if(iibPtr != nullptr){
return iibPtr;
}
}
return nullptr;
}
template <class... Indices>
void MultiIndex<Indices...>::setPos(int relPos, IndefinitIndexBase* subIndex)
{
IIB::mPos += relPos;
IIB::evalMajor(IIB::mMajorStep, relPos);
if(IIB::linked()){
IIB::mLinked->setPos(relPos, subIndex);
//IIB::mLinked->evalMajor(IIB::mMajorStep, relPos);
}
}
template <class... Indices>
size_t MultiIndex<Indices...>::giveSubStepSize(IndefinitIndexBase* subIndex)
{
size_t sss = 1;
for(size_t i = sizeof...(Indices)-1; i != 0; --i){
IndefinitIndexBase* iibPtr = &get(i);
if(iibPtr == subIndex){
return sss;
}
sss *= iibPtr->max();
}
if(&get(0) == subIndex){
return sss;
}
else {
return 0;
}
}
/* /*
template <class... Indices> template <class... Indices>
void MultiIndex<Indices...>::eval() void MultiIndex<Indices...>::eval()

View file

@ -84,6 +84,10 @@ namespace MultiArrayTools
virtual void copyPos(const MultiIndex<Indices...>& in) override; virtual void copyPos(const MultiIndex<Indices...>& in) override;
virtual IndefinitIndexBase* getLinked(const std::string& name) override;
virtual void setPos(int relPos, IndefinitIndexBase* subIndex) override;
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) override;
//virtual void eval() override; //virtual void eval() override;
//virtual bool virt() const override { return false; } //virtual bool virt() const override { return false; }
//virtual void assignRange(RangeBase<MultiIndex<Indices...> > const* range) override; //virtual void assignRange(RangeBase<MultiIndex<Indices...> > const* range) override;

View file

@ -162,6 +162,40 @@ namespace MultiArrayTools
IIB::setPos(in.pos()); IIB::setPos(in.pos());
} }
template <typename U, RangeType TYPE>
IndefinitIndexBase* SingleIndex<U,TYPE>::getLinked(const std::string& name)
{
if(name == IIB::mName){
return this;
}
else {
return nullptr;
}
}
template <typename U, RangeType TYPE>
void SingleIndex<U,TYPE>::setPos(int relPos, IndefinitIndexBase* subIndex)
{
IIB::mPos += relPos;
IIB::evalMajor(IIB::mMajorStep, relPos);
if(IIB::linked()){
IIB::mLinked->setPos(relPos, subIndex);
//IIB::mLinked->evalMajor(IIB::mMajorStep, relPos);
}
}
template <typename U, RangeType TYPE>
size_t SingleIndex<U,TYPE>::giveSubStepSize(IndefinitIndexBase* subIndex)
{
if(subIndex == this){
return 1;
}
else {
return 0;
}
}
/* /*
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
void SingleIndex<U,TYPE>::eval() void SingleIndex<U,TYPE>::eval()

View file

@ -51,6 +51,10 @@ namespace MultiArrayTools
virtual void copyPos(const SingleIndex& in) override; virtual void copyPos(const SingleIndex& in) override;
virtual IndefinitIndexBase* getLinked(const std::string& name) override;
virtual void setPos(int relPos, IndefinitIndexBase* subIndex) override;
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) override;
//virtual void eval() override; //virtual void eval() override;
//virtual bool virt() const override { return false; } //virtual bool virt() const override { return false; }

View file

@ -153,7 +153,14 @@ namespace {
TEST_F(TwoDimTest, CorrectExtensions) TEST_F(TwoDimTest, CorrectExtensions)
{ {
auto i = ma.begin();
auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>();
EXPECT_EQ(ma.size(), 12); EXPECT_EQ(ma.size(), 12);
EXPECT_EQ(i1.max(), 3);
EXPECT_EQ(i1.majorStep(), 4);
EXPECT_EQ(i2.max(), 4);
EXPECT_EQ(i2.majorStep(), 1);
} }
TEST_F(TwoDimTest, CorrectAssigned) TEST_F(TwoDimTest, CorrectAssigned)
@ -183,7 +190,7 @@ namespace {
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
ma2("alpha","beta") = ma("beta","alpha"); ma2("alpha","beta") = ma("beta","alpha");
EXPECT_EQ(ma2[i(i1 = 0,i2 = 0)],-5); EXPECT_EQ(ma2[i(i1 = 0,i2 = 0)],-5);
EXPECT_EQ(ma2[i(i1 = 1,i2 = 0)],6); EXPECT_EQ(ma2[i(i1 = 1,i2 = 0)],6);
EXPECT_EQ(ma2[i(i1 = 2,i2 = 0)],2); EXPECT_EQ(ma2[i(i1 = 2,i2 = 0)],2);
@ -207,6 +214,14 @@ namespace {
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
EXPECT_EQ(i1.max(), 3);
EXPECT_EQ(i1.majorStep(), 8);
EXPECT_EQ(i2.max(), 2);
EXPECT_EQ(i2.majorStep(), 4);
EXPECT_EQ(i3.max(), 4);
EXPECT_EQ(i3.majorStep(), 1);
ma3d2("alpha","beta","gamma") = ma3d("alpha","gamma","beta"); ma3d2("alpha","beta","gamma") = ma3d("alpha","gamma","beta");
EXPECT_EQ(ma3d2[i(i1 = 0,i2 = 0,i3 = 0)],-5); EXPECT_EQ(ma3d2[i(i1 = 0,i2 = 0,i3 = 0)],-5);