range factory + finishing multi/single range/index changes (except for single range specializations)

This commit is contained in:
Christian Zimmermann 2017-07-26 18:38:11 +02:00
parent 5f71b5055d
commit 8db8a07456
8 changed files with 155 additions and 168 deletions

View file

@ -26,6 +26,11 @@ namespace MultiArrayTools
return mPos;
}
size_t IndexBase::max() const
{
return mRangePtr->size();
}
operator IndexBase::size_t() const
{
return mPos;

View file

@ -18,14 +18,12 @@ namespace MultiArrayTools
{
public:
DEFAULT_MEMBERS(IndexBase);
IndexBase(const RangeBase& range, size_t pos);
IndexBase(const std::shared_ptr<RangeBase>& range, size_t pos);
virtual ~IndexBase() = default;
virtual IndexBase& operator=(size_t pos) = 0;
virtual IndexBase& operator++() = 0;
virtual IndexBase& operator--() = 0;
virtual IndexBase& operator+=(int n) = 0;
virtual IndexBase& operator-=(int n) = 0;
bool operator==(const IndexBase& in) const;
bool operator!=(const IndexBase& in) const;

View file

@ -399,6 +399,29 @@ namespace MultiArrayTools
return *this;
}
/*************************
* MultiRangeFactory *
*************************/
template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const std::shared_ptr<Ranges>&... rs)
{
mProd = std::make_shared< MultiRange<Ranges...> >( rs... );
}
template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const MultiRange<Ranges...>::SpaceType& st)
{
mProd = std::make_shared< MultiRange<Ranges...> >( st );
}
template <class... Ranges>
std::shared_ptr<RangeBase> MultiRangeFactory<Ranges...>::create()
{
setSelf();
return mProd;
}
/******************
* MultiRange *
******************/
@ -423,14 +446,6 @@ namespace MultiArrayTools
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace);
}
template <class... Ranges>
MultiRangeType MultiRange<Ranges...>::type() const
{
std::vector<MultiRangeType> rvec;
PackNum<sizeof...(Ranges)-1>::buildRangeVec(rvec, mSpace);
return MultiRangeType(rvec);
}
template <class... Ranges>
const typename MultiRange<Ranges...>::SpaceType& MultiRange<Ranges...>::space() const
{
@ -440,26 +455,22 @@ namespace MultiArrayTools
template <class... Ranges>
MultiIndex<typename Ranges::IndexType...> MultiRange<Ranges...>::begin() const
{
std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is;
PackNum<sizeof...(Ranges)-1>::setBegin(is,mSpace);
return MultiIndex<typename Ranges::IndexType...>(this, is);
MultiIndex<typename Ranges::IndexType...> i( std::dynamic_pointer_cast<MultiRange<Ranges...> >( mThis ) );
return i = 0;
}
template <class... Ranges>
MultiIndex<typename Ranges::IndexType...> MultiRange<Ranges...>::end() const
{
std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is;
PackNum<sizeof...(Ranges)-1>::setEnd(is,mSpace);
return ++MultiIndex<typename Ranges::IndexType...>(this, is);
MultiIndex<typename Ranges::IndexType...> i( std::dynamic_pointer_cast<MultiRange<Ranges...> >( mThis ) );
return i = size();
}
template <class... Ranges>
std::shared_ptr<IndefinitIndexBase> MultiRange<Ranges...>::indexInstance() const
std::shared_ptr<IndexBase> MultiRange<Ranges...>::index() const
{
std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is;
PackNum<sizeof...(Ranges)-1>::setBegin(is,mSpace);
std::shared_ptr<IndefinitIndexBase> sptr(new MultiIndex<typename Ranges::IndexType...>(this, is));
return sptr;
return std::make_shared<MultiIndex<typename Ranges::IndexType...> >
( std::dynamic_pointer_cast<MultiRange<Ranges...> >( mThis ) );
}
}

View file

@ -60,10 +60,24 @@ namespace MultiArrayTools
};
/*****************************
* IndexGetter Functions *
****************************/
/*************************
* MultiRangeFactory *
*************************/
template <class... Ranges>
class MultiRangeFactory : public RangeFactory
{
public:
MultiRangeFactory() = delete;
MultiRangeFactory(const std::shared_ptr<Ranges>&... rs);
MultiRangeFactory(const MultiRange<Ranges...>::SpaceType& st);
virtual std::shared_ptr<RangeBase> create() override;
};
/******************
* MultiRange *
******************/
template <class... Ranges>
class MultiRange : public RangeInterface<MultiIndex<typename Ranges::IndexType...> >
@ -90,6 +104,8 @@ namespace MultiArrayTools
virtual std::shared_ptr<IndexBase> index() const override;
friend MultiRangeFactory<Ranges...>;
protected:
MultiRange() = delete;

View file

@ -7,5 +7,14 @@ namespace MultiArrayTools
* RangeBase *
******************/
bool RangeBase::operator==(const RangeBase& in) const
{
return this == &in;
}
bool RangeBase::operator!=(const RangeBase& in) const
{
return this != &in;
}
}

View file

@ -25,6 +25,23 @@ namespace MultiArrayTools
DISTANCE = 8
};
class RangeFactoryBase
{
public:
RangeFactoryBase() = default;
virtual ~RangeFactoryBase() = default;
// should return mProd !!
virtual std::shared_ptr<RangeBase> create() = 0;
protected:
// call this function before returning product !!
void setSelf() { mProd->mThis = mProd; }
std::shared_ptr<RangeBase> mProd;
};
class RangeBase
{
public:
@ -36,8 +53,15 @@ namespace MultiArrayTools
virtual std::shared_ptr<IndexBase> index() const = 0;
bool operator==(const RangeBase& in) const;
bool operator!=(const RangeBase& in) const;
friend RangeFactoryBase;
protected:
RangeBase() = default;
std::shared_ptr<RangeBase> mThis;
};
template <class Index>
@ -49,6 +73,9 @@ namespace MultiArrayTools
virtual Index begin() = 0;
virtual Index end() = 0;
protected:
RangeInterface() = default;
};
}

View file

@ -7,136 +7,69 @@ namespace MultiArrayTools
* SingleIndex *
******************/
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>::SingleIndex(const U& upos,
std::shared_ptr<const RangeBase<SingleIndex<U,TYPE> > >& rangePtr) :
IndexBase<SingleIndex<U,TYPE> >(rangePtr)
{
IIB::setPos( dynamic_pointer_cast<const SingleRange<U,TYPE> >( IB::mRangePtr )->get(upos) );
}
template <typename U>
template<RangeType TYPE>
SingleIndex<U>::SingleIndex(const std::shared_ptr<SingleRange<U,TYPE> >& range) :
IndexInterface<U>(range, 0) {}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>::SingleIndex(std::shared_ptr<const RangeBase<SingleIndex<U,TYPE> > >& rangePtr,
size_t pos) : IndexBase<SingleIndex<U,TYPE> >(range)
template <typename U>
SingleIndex<U>& SingleIndex<U>::operator=(size_t pos)
{
IIB::setPos( pos );
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator=(size_t pos)
{
IIB::setPos( pos );
mPos = pos;
return *this;
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator++()
template <typename U>
SingleIndex<U>& SingleIndex<U>::operator++()
{
IIB::setPos( IIB::pos() + 1 );
++mPos;
return *this;
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator--()
template <typename U>
SingleIndex<U>& SingleIndex<U>::operator--()
{
IIB::setPos( IIB::pos() - 1 );
--mPos;
return *this;
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator+=(int n)
{
IIB::setPos( IIB::pos() + n );
return *this;
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator-=(int n)
{
IIB::setPos( IIB::pos() - n );
return *this;
}
template <typename U, RangeType TYPE>
bool SingleIndex<U,TYPE>::operator==(const SingleIndex<U,TYPE>& i)
{
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() != static_cast<size_t>( i.mPos );
}
template <typename U, RangeType TYPE>
MultiRangeType SingleIndex<U,TYPE>::rangeType() const
{
return MultiRangeType(TYPE);
}
template <typename U, RangeType TYPE>
U SingleIndex<U,TYPE>::getMetaPos() const
template <typename U>
U SingleIndex<U>::meta() const
{
return dynamic_cast<SingleRange<U,TYPE> const*>( IB::mRange )->get(IIB::pos());
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::atMeta(const U& metaPos)
template <typename U>
SingleIndex<U>& SingleIndex<U>::at(const U& metaPos)
{
operator=( dynamic_cast<SingleRange<U,TYPE> const*>( IB::mRange )->getMeta(metaPos) );
return *this;
}
template <typename U, RangeType TYPE>
size_t SingleIndex<U,TYPE>::dim() const
template <typename U>
size_t SingleIndex<U>::dim() const
{
return 1;
}
template <typename U, RangeType TYPE>
size_t SingleIndex<U,TYPE>::evaluate(const SingleIndex<U,TYPE>& in) const
template <typename U>
bool SingleIndex<U>::last() const
{
return in.pos();
return mPos == mRangePtr->size() - 1;
}
template <typename U, RangeType TYPE>
void SingleIndex<U,TYPE>::copyPos(const SingleIndex<U,TYPE>& in)
template <typename U>
bool SingleIndex<U>::first() const
{
IIB::setPos(in.pos());
return mPos == 0;
}
template <typename U, RangeType TYPE>
size_t SingleIndex<U,TYPE>::giveSubStepSize(IndefinitIndexBase* subIndex)
{
if(subIndex == this){
return 1;
}
else {
return 0;
}
}
std::ostream& operator<<(std::ostream& os, VET vet)
{
os << ( (vet == VET::VALUE) ? std::string("VALUE") : std::string("ERROR") );
return os;
}
/*
template <typename U, RangeType TYPE>
void SingleIndex<U,TYPE>::eval()
{
IIB::setPos( evaluate( *this ) );
}
*/
/********************
* SingleRange *
********************/
template <typename U, RangeType TYPE>
SingleRange<U,TYPE>::SingleRange(const std::vector<U>& space) : RangeBase<SingleIndex<U,TYPE> >(),
SingleRange<U,TYPE>::SingleRange(const std::vector<U>& space) : RangeBase<SingleIndex<U> >(),
mSpace(space) {}
template <typename U, RangeType TYPE>
@ -165,24 +98,34 @@ namespace MultiArrayTools
}
template <typename U, RangeType TYPE>
MultiRangeType SingleRange<U,TYPE>::type() const
size_t SingleRange<U,TYPE>::dim() const
{
return MultiRangeType(TYPE);
return 1;
}
// put this in the interface class !!!
template <typename U, RangeType TYPE>
std::shared_ptr<IndexBase> SingleRange<U,TYPE>::index() const
{
return std::make_shared<SingleIndex<U> >
( std::dynamic_pointer_cast<SingleRange<U,TYPE> >( mThis ) );
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE> SingleRange<U,TYPE>::begin() const
SingleIndex<U> SingleRange<U,TYPE>::begin() const
{
return SingleIndex<U,TYPE>(this, 0);
SingleIndex<U> i(mThis);
return i = 0;
}
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE> SingleRange<U,TYPE>::end() const
SingleIndex<U> SingleRange<U,TYPE>::end() const
{
return SingleIndex<U,TYPE>(this, mSpace.size());
SingleIndex<U> i(mThis);
return i = size();
}
// specializations
// specializations (not updated!!!)
SingleRange<int,RangeType::SPACE>::SingleRange(size_t ext) :
RangeBase<SingleIndex<int,RangeType::SPACE> >(),

View file

@ -14,70 +14,48 @@
namespace MultiArrayTools
{
template <typename U, RangeType TYPE>
class SingleIndex : public IndexBase<SingleIndex<U,TYPE> >
template <typename U>
class SingleIndex : public IndexInterface<U>
{
public:
typedef IndexBase<SingleIndex<U,TYPE> > IB;
typedef IndefinitIndexBase IIB;
DEFAULT_MEMBERS(SingleIndex);
SingleIndex(const U& upos, std::shared_ptr<const RangeBase<SingleIndex<U,TYPE> > >& rangePtr);
SingleIndex(std::shared_ptr<const RangeBase<SingleIndex<U,TYPE> > >& rangePtr,
size_t pos = 0);
//virtual SingleIndex& operator=(const U& upos);
template <RangeType TYPE>
SingleIndex(const std::shared_ptr<SingleRange<U,TYPE> >& range);
virtual SingleIndex& operator=(size_t pos) override;
virtual SingleIndex& operator++() override;
virtual SingleIndex& operator--() override;
virtual SingleIndex& operator+=(int n) override;
virtual SingleIndex& operator-=(int n) override;
virtual bool operator==(const SingleIndex& i);
virtual bool operator!=(const SingleIndex& i);
virtual MultiRangeType rangeType() const override;
virtual U getMetaPos() const;
virtual SingleIndex& atMeta(const U& metaPos);
virtual U meta() const;
virtual SingleIndex& at(const U& metaPos);
virtual size_t dim() const override; // = 1
virtual void copyPos(const SingleIndex& in) override;
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) override;
//virtual void eval() override;
//virtual bool virt() const override { return false; }
protected:
virtual size_t evaluate(const SingleIndex& in) const override;
virtual bool last() const override;
virtual bool first() const override;
};
template <typename U, RangeType TYPE>
class SingleRange : public RangeBase<SingleIndex<U,TYPE> >
class SingleRange : public RangeInterface<SingleIndex<U> >
{
public:
typedef typename RangeBase<SingleIndex<U,TYPE> >::IndexType IndexType;
static SingleRange<U,TYPE> oType() { return SingleRange<U,TYPE>(); }
typedef typename RangeBase<SingleIndex<U> >::IndexType IndexType;
virtual size_t size() const override;
//U get(size_t pos) const;
const U& get(size_t pos) const;
size_t getMeta(const U& metaPos) const;
virtual MultiRangeType type() const override;
virtual SingleIndex<U> begin() const override;
virtual SingleIndex<U> end() const override;
SingleIndex<U,TYPE> begin() const override;
SingleIndex<U,TYPE> end() const override;
friend SingleRangeFactory<U,TYPE>;
protected:
SingleRange() = delete;
SingleRange(const SingleRange& in) = delete;
SingleRange(const std::vector<U>& space);
std::vector<U> mSpace;