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());
mPos = pos;
//evalMajor();
if(linked()){
mLinked->setPos(mPos);
mLinked->evalMajor();
@ -104,12 +105,24 @@ namespace MultiArrayTools
return true;
}
void IndefinitIndexBase::evalMajor(size_t stepSize, int num)
void IndefinitIndexBase::evalMajor()
{
//assert(not virt());
if(not master()){
//mMajor->eval();
mMajor->setPos(static_cast<int>( mMajor->pos() ) + num, this)
//int start = mMajor->pos();
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());
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 bool linked() const;
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
virtual void setPos(size_t pos);
// 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 outOfRange() const;
@ -58,12 +58,15 @@ namespace MultiArrayTools
virtual bool toNull() const;
virtual void eval() = 0;
virtual void evalMajor();
virtual void evalMajor(size_t stepSize, int num);
virtual bool master();
virtual void subOrd(IndefinitIndexBase* major);
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0;
virtual size_t majorStep() const;
//virtual bool virt() const { return true; }
protected:

View file

@ -73,10 +73,13 @@ namespace MultiArrayTools
}
};
// rewrite !!
template <class MultiIndex>
inline void plus(MultiIndex& index, size_t digit, int num)
{
IndefinitIndexBase& si = index.get(digit);
//si.setPos( num, &index );
//si.setPos( -num, &index );
si.setPos( si.pos() + num );
size_t oor = si.outOfRange();
if(oor and digit != 0){
@ -331,32 +334,36 @@ namespace MultiArrayTools
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator++()
{
//CHECK;
plus(*this, sizeof...(Indices)-1, 1);
IIB::setPos( evaluate(*this) );
setPos(1, this);
return *this;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator--()
{
IIB::setPos( IIB::pos() - 1 );
// !!! >|< (0 does not make sense)
plus(*this, 0, -1);
setPos(-1, this);
return *this;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator+=(int n)
{
//CHECK;
plus(*this, sizeof...(Indices)-1, 1);
IIB::setPos( evaluate(*this) );
setPos(n, this);
return *this;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator-=(int n)
{
IIB::setPos( IIB::pos() - n );
// !!! >|<
plus(*this, 0, 0-n);
setPos(-n, this);
return *this;
}
@ -549,6 +556,51 @@ namespace MultiArrayTools
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>
void MultiIndex<Indices...>::eval()

View file

@ -84,6 +84,10 @@ namespace MultiArrayTools
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 bool virt() const override { return false; }
//virtual void assignRange(RangeBase<MultiIndex<Indices...> > const* range) override;

View file

@ -162,6 +162,40 @@ namespace MultiArrayTools
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>
void SingleIndex<U,TYPE>::eval()

View file

@ -51,6 +51,10 @@ namespace MultiArrayTools
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 bool virt() const override { return false; }

View file

@ -153,7 +153,14 @@ namespace {
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(i1.max(), 3);
EXPECT_EQ(i1.majorStep(), 4);
EXPECT_EQ(i2.max(), 4);
EXPECT_EQ(i2.majorStep(), 1);
}
TEST_F(TwoDimTest, CorrectAssigned)
@ -207,6 +214,14 @@ namespace {
auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>();
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");
EXPECT_EQ(ma3d2[i(i1 = 0,i2 = 0,i3 = 0)],-5);