some improvements... certainly further ones are possible

This commit is contained in:
Christian Zimmermann 2017-03-13 19:04:24 +01:00
parent 5492521c76
commit 1c55afd55f
6 changed files with 76 additions and 105 deletions

View file

@ -18,7 +18,7 @@ namespace MultiArrayTools
size_t IndefinitIndexBase::pos() const
{
//assert(not virt());
return mPos;
return static_cast<size_t>( mPos );
}
const std::string& IndefinitIndexBase::name() const
@ -85,18 +85,32 @@ namespace MultiArrayTools
{
//assert(not virt());
mPos = pos;
//evalMajor();
if(linked()){
mLinked->setPos(mPos);
mLinked->evalMajor();
}
}
size_t IndefinitIndexBase::outOfRange() const
void IndefinitIndexBase::setPosRel(int relPos)
{
//assert(not virt());
int res = pos() - max() + 1;
return res > 0 ? static_cast<size_t>(res) : 0;
mPos += relPos;
if(linked()){
mLinked->setPosRel(relPos);
mLinked->evalMajor(relPos);
}
}
int IndefinitIndexBase::outOfRange() const
{
if(mPos < 0){
return mPos;
}
else if(mPos >= static_cast<int>( max() ) ){
return mPos - max() + 1;
}
else {
return 0;
}
}
bool IndefinitIndexBase::toNull() const
@ -115,13 +129,13 @@ namespace MultiArrayTools
}
}
void IndefinitIndexBase::evalMajor(size_t stepSize, int num)
void IndefinitIndexBase::evalMajor(int num)
{
//assert(not virt());
//CHECK;
if(not master()){
//int start = mMajor->pos();
mMajor->setPos( num * stepSize , this);
mMajor->setPosRel( num * mMajorStep);
//VCHECK(static_cast<int>( mMajor->pos() ) - start);
}
}

View file

@ -50,16 +50,16 @@ namespace MultiArrayTools
// does NOT include sub-index update routine !!
// relative position to current
virtual void setPos(int relPos, IndefinitIndexBase* subIndex) = 0;
virtual void setPosRel(int relPos);
virtual size_t max() const = 0;
virtual size_t outOfRange() const;
virtual int outOfRange() const;
virtual bool toNull() const;
virtual void eval() = 0;
virtual void evalMajor();
virtual void evalMajor(size_t stepSize, int num);
virtual void evalMajor(int num);
virtual bool master();
virtual void subOrd(IndefinitIndexBase* major);
@ -72,7 +72,7 @@ namespace MultiArrayTools
protected:
std::string mName;
size_t mPos;
int mPos;
size_t mMajorStep;
IndefinitIndexBase* mLinked = nullptr;

View file

@ -78,13 +78,18 @@ namespace MultiArrayTools
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 );
si.setPosRel(num);
//si.setPos( si.pos() + num );
size_t oor = si.outOfRange();
if(oor and digit != 0){
plus(index, digit - 1, 1);
plus(index, digit, -si.max());
if(digit){
if(oor > 0){
plus(index, digit - 1, 1);
plus(index, digit, -si.max());
}
else if(oor < 0){
plus(index, digit - 1, -1);
plus(index, digit, si.max());
}
}
}
@ -272,6 +277,35 @@ namespace MultiArrayTools
std::get<0>(target).atMeta( std::get<0>(source) );
}
};
template <size_t N>
struct IndexPackSetter
{
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto i = std::get<N>(iPack);
const size_t ownPos = pos % i.max();
i = ownPos;
if(ownPos == pos){
IndexPackSetter<N-1>::setIndexPack(iPack, (pos - ownPos) / i.max() );
}
}
};
template <>
struct IndexPackSetter<0>
{
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto i = std::get<0>(iPack);
const size_t ownPos = pos % i.max();
i = ownPos;
}
};
}
@ -324,46 +358,35 @@ namespace MultiArrayTools
IIB::mPos = evaluate(*this);
}
/*
template <class... Indices>
MultiIndex<Indices...>::~MultiIndex()
{
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, nullptr);
}*/
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator++()
{
//CHECK;
plus(*this, sizeof...(Indices)-1, 1);
setPos(1, this);
IIB::setPosRel(1);
return *this;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator--()
{
// !!! >|< (0 does not make sense)
plus(*this, 0, -1);
setPos(-1, this);
plus(*this, sizeof...(Indices)-1, -1);
IIB::setPosRel(-1);
return *this;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator+=(int n)
{
//CHECK;
plus(*this, sizeof...(Indices)-1, 1);
setPos(n, this);
plus(*this, sizeof...(Indices)-1, n);
IIB::setPosRel(n);
return *this;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator-=(int n)
{
// !!! >|<
plus(*this, 0, 0-n);
setPos(-n, this);
plus(*this, sizeof...(Indices)-1, -n);
IIB::setPosRel(-n);
return *this;
}
@ -383,7 +406,7 @@ namespace MultiArrayTools
IndefinitIndexBase& MultiIndex<Indices...>::operator=(size_t pos)
{
IIB::setPos( pos );
//setIndexPack(mIPack, pos); // -> implement !!!
IndexPackSetter<sizeof...(Indices)-1>::setIndexPack(mIPack, pos);
return *this;
}
@ -571,17 +594,6 @@ namespace MultiArrayTools
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)
{
@ -601,48 +613,6 @@ namespace MultiArrayTools
}
}
/*
template <class... Indices>
void MultiIndex<Indices...>::eval()
{
IIB::setPos( evaluate( *this ) );
}
*/
/*
template <size_t N>
struct RangeAssigner
{
template <class... Indices>
static void assignRange(const RangeBase<MultiIndex<Indices...> >& multiRange,
MultiIndex<Indices...>& multiIndex)
{
multiIndex.template getIndex<N>().assignRange(nullptr &multiRange.template getRange<N>());
RangeAssigner<N-1>::assignRange(multiRange, multiIndex);
}
};
template <>
struct RangeAssigner<0>
{
template <class... Indices>
static void assignRange(const RangeBase<MultiIndex<Indices...> >& multiRange,
MultiIndex<Indices...>& multiIndex)
{
multiIndex.template getIndex<0>().assignRange(nullptr &multiRange.template getRange<0>());
}
};
template <class... Indices>
void MultiIndex<Indices...>::assignRange(RangeBase<MultiIndex<Indices...> > const* range)
{
if(IB::toNull()){
IB::mRange = range;
}
MultiIndex<Indices...>& thisRef = *this;
RangeAssigner<sizeof...(Indices)-1>::assignRange(*range, thisRef);
}*/
/******************
* MultiRange *
******************/

View file

@ -85,7 +85,6 @@ 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;

View file

@ -110,13 +110,13 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE>
bool SingleIndex<U,TYPE>::operator==(const SingleIndex<U,TYPE>& i)
{
return IB::mRange == i.mRange and IIB::pos() == i.mPos;
return IB::mRange == i.mRange and IIB::pos() == static_cast<size_t>( i.mPos );
}
template <typename U, RangeType TYPE>
bool SingleIndex<U,TYPE>::operator!=(const SingleIndex<U,TYPE>& i)
{
return IB::mRange != i.mRange or IIB::pos() != i.mPos;
return IB::mRange != i.mRange or IIB::pos() != static_cast<size_t>( i.mPos );
}
template <typename U, RangeType TYPE>
@ -173,17 +173,6 @@ namespace MultiArrayTools
}
}
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)
{

View file

@ -52,7 +52,6 @@ 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;