simplify index/range framework -> no native major index system (planned to implement separate class)

This commit is contained in:
Christian Zimmermann 2017-07-25 17:46:59 +02:00
parent 30d8e5e5b2
commit c13c51bd51
6 changed files with 175 additions and 418 deletions

View file

@ -3,114 +3,40 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
/************************ /*****************
* IndefinitIndexBase * * IndexBase *
************************/ *****************/
size_t IndefinitIndexBase::pos() const IndexBase::IndexBase(const std::shared_ptr<RangeBase>& range,
size_t pos) : mRangePtr(range),
mPos(pos) {}
bool IndexBase::operator==(const IndexBase& in) const
{ {
//assert(not virt()); return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get();
return static_cast<size_t>( mPos );
} }
bool IndefinitIndexBase::operator==(const IndefinitIndexBase& in) const bool IndexBase::operator!=(const IndexBase& in) const
{ {
return rangeType() == in.rangeType() and pos() == in.pos(); return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get();
} }
bool IndefinitIndexBase::operator!=(const IndefinitIndexBase& in) const size_t IndexBase::pos() const
{ {
return rangeType() != in.rangeType() or pos() != in.pos(); return mPos;
} }
IndefinitIndexBase& IndefinitIndexBase::setPos(size_t pos, IndefinitIndexBase* ctrlPtr) operator IndexBase::size_t() const
{ {
mPos = pos; return mPos;
for(auto mm: mMajor){
if(mm.first == ctrlPtr){
continue;
}
mm.first->setPos(mm.first->pos() % mm.second + mm.second * pos, this);
}
return *this;
} }
IndefinitIndexBase& IndefinitIndexBase::setPosRel(int relPos, IndefinitIndexBase* ctrlPtr) /**********************
{ * IndexInterface *
mPos += relPos; **********************/
for(auto mm: mMajor){
if(mm.first == ctrlPtr){
continue;
}
mm.first->setPosRel(mm.second * relPos, this);
}
return *this;
}
IndefinitIndexBase& IndefinitIndexBase::toFirst(IndefinitIndexBase* ctrlPtr) template <typename MetaType>
{ IndexInterface::IndexInterface(const RangeBase& range, size_t pos) :
mPos = 0; IndexBase(range, pos) {}
for(auto mm: mMajor){
if(mm.first == ctrlPtr){
continue;
}
mm.first->setPos(mm.first->pos() % mm.second, this);
}
return *this;
}
IndefinitIndexBase& IndefinitIndexBase::toLast(IndefinitIndexBase* ctrlPtr)
{
mPos = max() - 1;
for(auto mm: mMajor){
if(mm.first == ctrlPtr){
continue;
}
mm.first->setPos(mm.first->pos() % mm.second + mm.second * mPos, this);
}
return *this;
}
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::atEdge() const
{
return static_cast<size_t>( mPos ) == max();
}
bool IndefinitIndexBase::master() const
{
return mMajor.size() == 0;
}
IndefinitIndexBase& IndefinitIndexBase::subOrd(IndefinitIndexBase* major)
{
mMajor[major] = major->giveSubStepSize(this);
return *this;
}
/**************
* IndexBase *
**************/
template <class Index>
IndexBase<Index>::IndexBase(std::shared_ptr<const RangeBase<Index> >& rangePtr) : mRangePtr(rangePtr) {}
template <class Index>
size_t IndexBase<Index>::max() const
{
//assert(not virt());
return mRange->size();
}
} }

View file

@ -14,65 +14,46 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
class IndefinitIndexBase class IndexBase
{ {
public: public:
DEFAULT_MEMBERS(IndefinitIndexBase); DEFAULT_MEMBERS(IndexBase);
virtual ~IndefinitIndexBase() = default; IndexBase(const RangeBase& range, size_t pos);
virtual ~IndexBase() = default;
virtual IndefinitIndexBase& operator=(size_t pos) = 0; virtual IndexBase& operator=(size_t pos) = 0;
virtual IndefinitIndexBase& operator++() = 0; virtual IndexBase& operator++() = 0;
virtual IndefinitIndexBase& operator--() = 0; virtual IndexBase& operator--() = 0;
virtual IndefinitIndexBase& operator+=(int n) = 0; virtual IndexBase& operator+=(int n) = 0;
virtual IndefinitIndexBase& operator-=(int n) = 0; virtual IndexBase& operator-=(int n) = 0;
bool operator==(const IndefinitIndexBase& in) const; bool operator==(const IndexBase& in) const;
bool operator!=(const IndefinitIndexBase& in) const; bool operator!=(const IndexBase& in) const;
virtual size_t dim() const = 0; virtual size_t dim() const = 0;
virtual size_t pos() const; virtual size_t pos() const;
virtual size_t max() const;
virtual MultiRangeType rangeType() const = 0; virtual bool last() const = 0;
virtual bool first() const = 0;
virtual IndefinitIndexBase& setPos(size_t pos, IndefinitIndexBase* ctrlPtr = nullptr); virtual operator size_t() const;
virtual IndefinitIndexBase& setPosRel(int relPos, IndefinitIndexBase* ctrlPtr = nullptr);
virtual IndefinitIndexBase& toFirst(IndefinitIndexBase* ctrlPtr = nullptr);
virtual IndefinitIndexBase& toLast(IndefinitIndexBase* ctrlPtr = nullptr);
virtual size_t max() const = 0;
virtual int outOfRange() const;
virtual bool atEdge() const;
virtual bool master() const;
virtual IndefinitIndexBase& subOrd(IndefinitIndexBase* major);
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0;
protected: protected:
int mPos; std::shared_ptr<RangeBase> mRangePtr;
std::map<IndefinitIndexBase*,size_t> mMajor; size_t mPos;
}; };
template <class Index> template <typename MetaType>
class IndexBase : public IndefinitIndexBase class IndexInterface : public IndexBase
{ {
public: public:
typedef IndefinitIndexBase IIB; IndexInterface(const RangeBase& range, size_t pos);
DEFAULT_MEMBERS(IndexBase); MetaType& meta() = 0;
IndexBase(std::shared_ptr<const RangeBase<Index> >& rangePtr); const MetaType& meta() const = 0;
//virtual size_t pos() const override; // = mPos; implement !!!
virtual size_t max() const override;
virtual void copyPos(const Index& in) = 0;
protected:
// translate index into position
virtual size_t evaluate(const Index& in) const = 0;
std::shared_ptr<const RangeBase<Index> > mRangePtr;
}; };
} }

View file

@ -362,62 +362,106 @@ namespace MultiArrayTools
}; };
template <size_t N>
struct SubIndexConstruct
{
template <class MRange, class... Indices>
void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef decltype(range.template get<N>()) SubIndexType;
typedef decltype(std::get<N>(ip).get()) wTypeFromIndexPack;
static_assert(is_same<SubIndexType,SubIndexType>::value,
"inconsiśtent types");
std::get<N>(ip).swap( std::make_shared<SubIndexType> ( range.template get<N>() ) );
SubIndexConstruct<N-1>::construct(ip, range);
}
template <class... Indices>
void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const MultiIndex<Indices...>& ind)
{
typedef decltype(ind.template get<N>()) SubIndexType;
std::get<N>(ip).swap( std::make_shared<SubIndexType>( ind.template get<N>() ) );
SubIndexConstruct<N-1>::copy(ip, ind);
}
};
template <>
struct SubIndexConstruct<0>
{
template <class MRange, class... Indices>
void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef decltype(range.template get<0>()) SubIndexType;
typedef decltype(std::get<0>(ip).get()) wTypeFromIndexPack;
static_assert(std::is_same<SubIndexType,SubIndexType>::value,
"inconsiśtent types");
std::get<0>(ip).swap( std::make_shared<SubIndexType> ( range.template get<0>() ) );
}
template <class... Indices>
void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const MultiIndex<Indices...>& ind)
{
typedef decltype(ind.template get<0>()) SubIndexType;
std::get<0>(ip).swap( std::make_shared<SubIndexType>( ind.template get<0>() ) );
}
};
template <size_t N>
struct PosGetter
{
template <class... Indices>
size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
return std::get<N>(iPtrTup)->pos() +
PosGetter<N-1>::makePos(iPtrTup) * std::get<N-1>(iPtrTup)->max();
}
};
template <>
struct PosGetter<0>
{
template <class... Indices>
size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
return std::get<0>(iPtrTup)->pos();
}
};
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) : MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) :
IndexBase<MultiIndex<Indices...> >(in), IndexInterface<std::tuple<decltype(Indices().meta())...> >(in)
mIPack(in.mIPack)
{ {
// THAT's the point: SubIndexConstruct<sizeof...(Indices)-1>::copy(mIPack, in);
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, this); mPos = PosGetter<sizeof...(Indices)-1>::makePos(mIPack);
IIB::mPos = evaluate(*this);
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(const MultiIndex<Indices...>& in) MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(const MultiIndex<Indices...>& in)
{ {
IndexBase<MultiIndex<Indices...> >::operator=(in); IndexI::operator=(in);
mIPack = in.mIPack; SubIndexConstruct<sizeof...(Indices)-1>::copy(mIPack, in);
mPos = PosGetter<sizeof...(Indices)-1>::makePos(mIPack);
// THAT's the point:
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, this);
IIB::mPos = evaluate(*this);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>::MultiIndex(RangeBase<MultiIndex<Indices...> > const* range) : template <class MRange>
IndexBase<MultiIndex<Indices...> >(range), MultiIndex<Indices...>::MultiIndex(const std::shared_ptr<MRange>& range) :
mIPack() IndexInterface<std::tuple<decltype(Indices().meta())...> >(range, 0)
{ {
operator=(IB::mRange->begin()); SubIndexConstruct<sizeof...(Indices)-1>::construct(mIPack, *range);
IIB::mPos = evaluate(*this); mPos = PosGetter<sizeof...(Indices)-1>::makePos(mIPack);
}
template <class... Indices>
MultiIndex<Indices...>::MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
Indices&&... inds) : IndexBase<MultiIndex<Indices...> >(range),
mIPack(std::make_tuple(inds...))
{
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, this);
IIB::mPos = evaluate(*this);
}
template <class... Indices>
MultiIndex<Indices...>::MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
const IndexPack& ipack) : IndexBase<MultiIndex<Indices...> >(range),
mIPack(ipack)
{
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, this);
IIB::mPos = evaluate(*this);
}
template <class... Indices>
MultiIndex<Indices...>::MultiIndex(std::vector<std::shared_ptr<IndefinitIndexBase> >& indexList)
{
mIPack = IndexPackSetter<sizeof...(Indices)-1>::setFromPointerList(mIPack, indexList);
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, this);
} }
template <class... Indices> template <class... Indices>

View file

@ -15,20 +15,14 @@ namespace MultiArrayTools
{ {
template <class... Indices> template <class... Indices>
class MultiIndex : public IndexBase<MultiIndex<Indices...> > class MultiIndex : public IndexInterface<std::tuple<decltype(Indices().meta())...> >
{ {
public: public:
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack; typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
typedef IndefinitIndexBase IIB; typedef std::tuple<decltype(Indices().meta())...> MetaType;
typedef IndexBase<MultiIndex<std::shared_ptr<Indices>...> > IB; typedef IndexInterface<MetaType> IndexI;
typedef std::tuple<decltype(Indices().getMetaPos())...> MetaType;
protected:
virtual bool linkLower(IndefinitIndexBase* toLink);
virtual size_t evaluate(const MultiIndex& in) const override;
IndexPack mIPack;
public: public:
@ -39,39 +33,23 @@ namespace MultiArrayTools
MultiIndex(const MultiIndex& in); MultiIndex(const MultiIndex& in);
MultiIndex& operator=(const MultiIndex& in); MultiIndex& operator=(const MultiIndex& in);
MultiIndex(RangeBase<MultiIndex<std::shared_ptr<Indices>...> > const* range); template <class MRange>
MultiIndex(const std::shared_ptr<MRange>& range);
MultiIndex(RangeBase<MultiIndex<std::shared_ptr<Indices>...> > const* range,
Indices&&... inds);
MultiIndex(RangeBase<MultiIndex<std::shared_ptr<Indices>...> > const* range,
const IndexPack& ipack);
MultiIndex(std::vector<std::shared_ptr<IndefinitIndexBase> >& indexList);
virtual MultiIndex& operator++() override; virtual MultiIndex& operator++() override;
virtual MultiIndex& operator--() override; virtual MultiIndex& operator--() override;
virtual MultiIndex& operator+=(int n) override; virtual MultiIndex& operator+=(int n) override;
virtual MultiIndex& operator-=(int n) override; virtual MultiIndex& operator-=(int n) override;
bool operator==(const MultiIndex& in);
bool operator!=(const MultiIndex& in);
virtual MultiIndex& operator=(size_t pos) override; virtual MultiIndex& operator=(size_t pos) override;
virtual MultiRangeType rangeType() const override;
template <size_t N> template <size_t N>
auto getIndex() -> decltype(*std::get<N>(mIPack))&; auto get() const -> decltype(*std::get<N>(mIPack))&;
//typename std::tuple_element<N, std::tuple<std::shared_ptr<Indices>...> >::type& getIndex();
template <size_t N>
auto getIndex() const -> decltype(*std::get<N>(mIPack))&;
//typename std::tuple_element<N, std::tuple<std::shared_ptr<Indices>...> >::type const& getIndex() const; //typename std::tuple_element<N, std::tuple<std::shared_ptr<Indices>...> >::type const& getIndex() const;
IndefinitIndexBase& get(size_t n); const IndexBase& get(size_t n) const;
const IndefinitIndexBase& get(size_t n) const;
MetaType getMetaPos() const; virtual MetaType meta() const override;
MultiIndex& atMeta(const MetaType& metaPos); MultiIndex& atMeta(const MetaType& metaPos);
MultiIndex& operator()(Indices&&... inds); MultiIndex& operator()(Indices&&... inds);
@ -79,7 +57,11 @@ namespace MultiArrayTools
// dimension of MultiRange; includes ALL degrees of freedom // dimension of MultiRange; includes ALL degrees of freedom
virtual size_t dim() const override; virtual size_t dim() const override;
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) override;
protected:
IndexPack mIPack;
}; };
/***************************** /*****************************
@ -88,33 +70,29 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
class MultiRange : public RangeBase<MultiIndex<typename Ranges::IndexType...> > class MultiRange : public RangeInterface<MultiIndex<typename Ranges::IndexType...> >
{ {
public: public:
typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType; typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType;
typedef typename RangeBase<MultiIndex<typename Ranges::IndexType...> >::IndexType typedef typename RangeInterface<MultiIndex<typename Ranges::IndexType...> >::IndexType
IndexType; IndexType;
static const size_t dim = sizeof...(Ranges); static const size_t dim = sizeof...(Ranges);
template <size_t N> // !!! return ref to range, not the corresp. shared ptr
auto getRange() -> ;
//typename std::tuple_element<N, std::tuple<std::shared_ptr<Ranges>...> >::type& getRange();
template <size_t N> // !!! template <size_t N> // !!!
auto getRange() const ->; auto get() const ->;
//typename std::tuple_element<N, std::tuple<std::shared_ptr<Ranges>...> >::type const& getRange() const; //typename std::tuple_element<N, std::tuple<std::shared_ptr<Ranges>...> >::type const& getRange() const;
size_t dim() const override;
size_t size() const override; size_t size() const override;
const SpaceType& space() const; const SpaceType& space() const;
virtual MultiRangeType type() const override; virtual typename IndexType begin() const override;
virtual typename IndexType end() const override;
virtual MultiIndex<typename Ranges::IndexType...> begin() const override; virtual std::shared_ptr<IndexBase> index() const override;
virtual MultiIndex<typename Ranges::IndexType...> end() const override;
virtual std::shared_ptr<IndefinitIndexBase> indexInstance() const override;
protected: protected:

View file

@ -3,121 +3,9 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
/*********************
* MultiRangeType *
*********************/
MultiRangeType::MultiRangeType(const RangeType& type) : mType(type), mMultiType(nullptr) {}
MultiRangeType::MultiRangeType(const std::vector<MultiRangeType>& multiType) :
mType(RangeType::NIL),
mMultiType(new std::vector<MultiRangeType>(multiType)) {}
MultiRangeType::~MultiRangeType()
{
delete mMultiType;
}
MultiRangeType& MultiRangeType::operator=(const RangeType& type)
{
setType(type);
return *this;
}
MultiRangeType& MultiRangeType::operator=(const std::vector<MultiRangeType>& multiType)
{
setMultiType(multiType);
return *this;
}
MultiRangeType& MultiRangeType::operator[](size_t num)
{
return mMultiType->at(num);
}
const MultiRangeType& MultiRangeType::operator[](size_t num) const
{
return mMultiType->at(num);
}
bool MultiRangeType::multi() const
{
return mMultiType != nullptr;
}
bool MultiRangeType::operator==(const MultiRangeType& in) const
{
if(multi() xor in.multi()){
return false;
}
if(multi()){
return *mMultiType == *in.mMultiType;
}
else {
return mType == in.mType;
}
}
bool MultiRangeType::operator!=(const MultiRangeType& in) const
{
if(multi() xor in.multi()){
return true;
}
if(multi()){
return *mMultiType != *in.mMultiType;
}
else {
return mType != in.mType;
}
}
void MultiRangeType::setType(RangeType type)
{
mType = type;
if(mMultiType != nullptr){
delete mMultiType;
}
mMultiType = nullptr;
}
void MultiRangeType::setMultiType(const std::vector<MultiRangeType>& multiType)
{
mMultiType = new std::vector<MultiRangeType>( multiType );
mType = RangeType::NIL;
}
/****************** /******************
* RangeBase * * RangeBase *
******************/ ******************/
template <class Index>
bool RangeBase<Index>::isSubRange() const
{
return false;
}
template <class Index>
RangeBase<Index>* RangeBase<Index>::base()
{
return nullptr;
}
/*********************
* SubRangeBase *
*********************/
template <class Index>
bool SubRangeBase<Index>::isSubRange() const
{
return true;
}
template <class Index>
RangeBase<Index>* SubRangeBase<Index>::base()
{
return mBase;
}
} }

View file

@ -25,90 +25,30 @@ namespace MultiArrayTools
DISTANCE = 8 DISTANCE = 8
}; };
class MultiRangeType class RangeBase
{ {
public: public:
DEFAULT_MEMBERS(MultiRangeType); virtual ~RangeBase() = default;
MultiRangeType(const RangeType& type);
MultiRangeType(const std::vector<MultiRangeType>& multiType);
~MultiRangeType();
MultiRangeType& operator=(const RangeType& type);
MultiRangeType& operator=(const std::vector<MultiRangeType>& multiType);
MultiRangeType& operator[](size_t num);
const MultiRangeType& operator[](size_t num) const;
bool multi() const;
bool operator==(const MultiRangeType& in) const;
bool operator!=(const MultiRangeType& in) const;
private:
void setType(RangeType type);
void setMultiType(const std::vector<MultiRangeType>& multiType);
RangeType mType;
std::vector<MultiRangeType>* mMultiType;
};
class IndefinitRangeBase
{
public:
virtual ~IndefinitRangeBase() = default;
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual bool isSubRange() const = 0; virtual size_t dim() const = 0;
virtual MultiRangeType type() const = 0;
virtual std::shared_ptr<IndefinitIndexBase> indexInstance() const = 0; virtual std::shared_ptr<IndexBase> index() const = 0;
protected: protected:
// only constructable via Factory
IndefinitRangeBase() = default;
IndefinitRangeBase(const IndefinitRangeBase& in) = delete;
IndefinitRangeBase& operator=(const IndefinitRangeBase& in) = delete;
}; };
template <class Index> template <class Index>
class RangeBase : public IndefinitRangeBase class RangeInterface : public RangeBase
{ {
public: public:
typedef Index IndexType; typedef Index IndexType;
virtual IndexType begin() const = 0; virtual Index begin() = 0;
virtual IndexType end() const = 0; virtual Index end() = 0;
virtual RangeBase<Index>* base();
virtual bool isSubRange() const override;
virtual std::shared_ptr<IndefinitIndexBase> indexInstance() const override;
protected:
RangeBase() = default;
RangeBase(const RangeBase& in) = delete;
RangeBase& operator=(const RangeBase& in) = delete;
};
//template <class Range>
//auto cross(const Range& r1, const Range& r2) -> /**/;
//template <class Range1, class Range2>
//auto cross(const Range1& r1, const Range2& r2) -> /**/;
template <class Index>
class SubRangeBase : public RangeBase<Index>
{
public:
virtual bool isSubRange() const override;
virtual RangeBase<Index>* base() override;
protected:
DEFAULT_MEMBERS(SubRangeBase);
RangeBase<Index>* mBase;
std::vector<bool> mOccupation;
}; };
} }