add meta position access (compiles, untested)

This commit is contained in:
Christian Zimmermann 2017-03-03 17:51:42 +01:00
parent d5c8ec7c9d
commit 6164b58cd0
5 changed files with 79 additions and 4 deletions

View file

@ -224,6 +224,52 @@ namespace MultiArrayTools
}
};
template <size_t N>
struct MetaPosGetter
{
template <class... Indices>
static void getMetaPos(typename MultiIndex<Indices...>::MetaType& target,
const typename MultiIndex<Indices...>::IndexPack& source)
{
std::get<N>(target) = std::get<N>(source).getMetaPos();
MetaPosGetter<N-1>::getMetaPos(target, source);
}
};
template <>
struct MetaPosGetter<0>
{
template <class... Indices>
static void getMetaPos(typename MultiIndex<Indices...>::MetaType& target,
const typename MultiIndex<Indices...>::IndexPack& source)
{
std::get<0>(target) = std::get<0>(source).getMetaPos();
}
};
template <size_t N>
struct MetaSetter
{
template <class... Indices>
static void setMeta(typename MultiIndex<Indices...>::IndexPack& target,
const typename MultiIndex<Indices...>::MetaType& source)
{
std::get<N>(target).atMeta( std::get<N>(source) );
MetaSetter<N-1>::setMeta(target, source);
}
};
template <>
struct MetaSetter<0>
{
template <class... Indices>
static void setMeta(typename MultiIndex<Indices...>::IndexPack& target,
const typename MultiIndex<Indices...>::MetaType& source)
{
std::get<0>(target).atMeta( std::get<0>(source) );
}
};
}
template <class... Indices>
@ -445,6 +491,7 @@ namespace MultiArrayTools
IndefinitIndexBase& MultiIndex<Indices...>::get(size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
MultiIndex<Indices...>* t = this;
@ -455,12 +502,28 @@ namespace MultiArrayTools
const IndefinitIndexBase& MultiIndex<Indices...>::get(size_t n) const
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
MultiIndex<Indices...> const* t = this;
return IndexGetter<sizeof...(Indices)-1>::getIndex(*t, n);
}
template <class... Indices>
typename MultiIndex<Indices...>::MetaType MultiIndex<Indices...>::getMetaPos() const
{
MetaType metaTuple;
MetaPosGetter<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack);
return metaTuple;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::atMeta(const MultiIndex<Indices...>::MetaType& metaPos)
{
MetaSetter<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
return *this;
}
template <class... Indices>
bool MultiIndex<Indices...>::linkLower(IndefinitIndexBase* toLink)
{

View file

@ -20,6 +20,7 @@ namespace MultiArrayTools
typedef std::tuple<Indices...> IndexPack;
typedef IndefinitIndexBase IIB;
typedef IndexBase<MultiIndex<Indices...> > IB;
typedef std::tuple<decltype(Indices().getMetaPos())...> MetaType;
protected:
@ -67,6 +68,9 @@ namespace MultiArrayTools
IndefinitIndexBase& get(size_t n);
const IndefinitIndexBase& get(size_t n) const;
MetaType getMetaPos() const;
MultiIndex& atMeta(const MetaType& metaPos);
MultiIndex& operator()(Indices&&... inds);
MultiIndex& operator()(const Indices&... inds);

View file

@ -18,7 +18,7 @@ namespace MultiArrayTools
}
template <typename U, RangeType TYPE>
size_t SingleRange<U,TYPE>::get(const U& metaPos) const
size_t SingleRange<U,TYPE>::getMeta(const U& metaPos) const
{
size_t cnt = 0;
for(auto& x: mSpace){
@ -131,6 +131,13 @@ namespace MultiArrayTools
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)
{
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
{

View file

@ -44,6 +44,7 @@ namespace MultiArrayTools
virtual MultiRangeType rangeType() const override;
virtual const U& getMetaPos() const;
virtual SingleIndex& atMeta(const U& metaPos);
virtual size_t dim() const override; // = 1
virtual void linkTo(IndefinitIndexBase* target) override;
@ -68,7 +69,7 @@ namespace MultiArrayTools
virtual size_t size() const override;
const U& get(size_t pos) const;
size_t get(const U& metaPos) const;
size_t getMeta(const U& metaPos) const;
virtual MultiRangeType type() const override;