put ma iterator stuff completely in ContainerRange (<T> dependence)

This commit is contained in:
Christian Zimmermann 2018-02-15 20:06:14 +01:00
parent d7e5098639
commit 901fae0735
9 changed files with 323 additions and 529 deletions

View file

@ -24,52 +24,9 @@ namespace MultiArrayTools
public:
typedef T value_type;
typedef ContainerRange<SRanges...> CRange;
typedef ContainerRange<T,SRanges...> CRange;
typedef typename CRange::IndexType IndexType;
class const_iterator : public std::iterator<std::random_access_iterator_tag,T>
{
public:
DEFAULT_MEMBERS(const_iterator);
const_iterator(const MultiArrayBase& ma);
const_iterator(const MultiArrayBase& ma, const typename CRange::IndexType& index);
// Requirements:
bool operator==(const const_iterator& it) const;
bool operator!=(const const_iterator& it) const;
const T& operator*() const;
T const* operator->() const;
const_iterator& operator++();
const_iterator operator++(int);
const_iterator& operator--();
const_iterator operator--(int);
const_iterator& operator+=(int diff);
const_iterator& operator-=(int diff);
const_iterator operator+(int num) const;
const_iterator operator-(int num) const;
int operator-(const const_iterator& it) const;
const T& operator[](int num) const;
bool operator<(const const_iterator& it) const;
bool operator>(const const_iterator& it) const;
bool operator<=(const const_iterator& it) const;
bool operator>=(const const_iterator& it) const;
// Multi Array specific:
typename ContainerRange<SRanges...>::IndexType index() const;
protected:
MultiArrayBase const* mMAPtr = nullptr;
size_t mPos;
};
DEFAULT_MEMBERS(MultiArrayBase);
MultiArrayBase(const std::shared_ptr<SRanges>&... ranges);
@ -85,8 +42,8 @@ namespace MultiArrayTools
virtual size_t size() const;
virtual bool isSlice() const = 0;
virtual const_iterator begin() const;
virtual const_iterator end() const;
virtual IndexType begin() const;
virtual IndexType end() const;
virtual IndexType beginIndex() const;
virtual IndexType endIndex() const;
@ -111,58 +68,11 @@ namespace MultiArrayTools
{
public:
typedef ContainerRange<SRanges...> CRange;
typedef typename MultiArrayBase<T,SRanges...>::const_iterator const_iterator;
typedef ContainerRange<T,SRanges...> CRange;
//typedef typename MultiArrayBase<T,SRanges...>::const_iterator const_iterator;
typedef MultiArrayBase<T,SRanges...> MAB;
typedef typename CRange::IndexType IndexType;
class iterator : public std::iterator<std::random_access_iterator_tag,T>,
public std::iterator<std::output_iterator_tag,T>
{
public:
DEFAULT_MEMBERS(iterator);
iterator(MutableMultiArrayBase& ma);
iterator(MutableMultiArrayBase& ma, const IndexType& index);
// Requirements:
bool operator==(const iterator& it) const;
bool operator!=(const iterator& it) const;
const T& operator*() const;
T const* operator->() const;
T& operator*();
T* operator->();
iterator& operator++();
iterator operator++(int);
iterator& operator--();
iterator operator--(int);
iterator& operator+=(int diff);
iterator& operator-=(int diff);
iterator operator+(int num) const;
iterator operator-(int num) const;
int operator-(const iterator& it) const;
const T& operator[](int num) const;
T& operator[](int num);
bool operator<(const iterator& it) const;
bool operator>(const iterator& it) const;
bool operator<=(const iterator& it) const;
bool operator>=(const iterator& it) const;
// Multi Array specific:
typename CRange::IndexType index() const;
protected:
MutableMultiArrayBase* mMAPtr = nullptr;
size_t mPos;
};
using MultiArrayBase<T,SRanges...>::operator[];
using MultiArrayBase<T,SRanges...>::at;
using MultiArrayBase<T,SRanges...>::data;
@ -179,8 +89,8 @@ namespace MultiArrayTools
virtual T* data() = 0;
virtual std::vector<T>& datav() = 0;
virtual iterator begin();
virtual iterator end();
//virtual IndexType begin();
//virtual IndexType end();
virtual bool isConst() const override;
@ -194,10 +104,10 @@ namespace MultiArrayTools
{
public:
typedef ContainerRange<SRanges...> CRange;
typedef ContainerRange<T,SRanges...> CRange;
typedef MultiArrayBase<T,SRanges...> MAB;
typedef typename MultiArrayBase<T,SRanges...>::const_iterator const_iterator;
typedef typename MutableMultiArrayBase<T,SRanges...>::iterator iterator;
//typedef typename MultiArrayBase<T,SRanges...>::const_iterator const_iterator;
//typedef typename MutableMultiArrayBase<T,SRanges...>::iterator iterator;
typedef typename CRange::IndexType IndexType;
DEFAULT_MEMBERS(MultiArray);
@ -251,7 +161,7 @@ namespace MultiArrayTools
{
public:
typedef ContainerRange<SRanges...> CRange;
typedef ContainerRange<T,SRanges...> CRange;
typedef MultiArrayBase<T,CRange> MAB;
typedef typename MultiArrayBase<T,CRange>::const_iterator const_iterator;
typedef typename CRange::IndexType IndexType;
@ -278,146 +188,6 @@ namespace MultiArrayTools
namespace MultiArrayTools
{
/**************************************
* MultiArrayBase::const_iterator *
**************************************/
template <typename T, class... SRanges>
MultiArrayBase<T,SRanges...>::const_iterator::const_iterator(const MultiArrayBase<T,SRanges...>& ma):
mMAPtr(&ma), mPos(0) { }
template <typename T, class... SRanges>
MultiArrayBase<T,SRanges...>::const_iterator::const_iterator(const MultiArrayBase<T,SRanges...>& ma,
const typename CRange::IndexType& index):
mMAPtr(&ma), mPos(index.pos()) { }
template <typename T, class... SRanges>
bool MultiArrayBase<T,SRanges...>::const_iterator::operator==(const const_iterator& it) const
{
return mMAPtr == it.mMAPtr and mPos == it.mPos;
}
template <typename T, class... SRanges>
bool MultiArrayBase<T,SRanges...>::const_iterator::operator!=(const const_iterator& it) const
{
return mMAPtr != it.mMAPtr or mPos != it.mPos;
}
template <typename T, class... SRanges>
const T& MultiArrayBase<T,SRanges...>::const_iterator::operator*() const
{
return mMAPtr->data()[mPos];
}
template <typename T, class... SRanges>
T const* MultiArrayBase<T,SRanges...>::const_iterator::operator->() const
{
return &mMAPtr->data()[mPos];
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator++()
{
++mPos;
return *this;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator++(int)
{
const_iterator tmp(*this);
++mPos;
return tmp;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator--()
{
--mPos;
return *this;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator--(int)
{
const_iterator tmp(*this);
--mPos;
return tmp;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator+=(int diff)
{
mPos += diff;
return *this;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator-=(int diff)
{
mPos -= diff;
return *this;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator+(int num) const
{
const_iterator tmp(*this);
tmp += num;
return tmp;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator-(int num) const
{
const_iterator tmp(*this);
tmp -= num;
}
template <typename T, class... SRanges>
int MultiArrayBase<T,SRanges...>::const_iterator::operator-(const const_iterator& it) const
{
return mPos - it.mPos;
}
template <typename T, class... SRanges>
const T& MultiArrayBase<T,SRanges...>::const_iterator::operator[](int num) const
{
return *(operator+(num));
}
template <typename T, class... SRanges>
bool MultiArrayBase<T,SRanges...>::const_iterator::operator<(const const_iterator& it) const
{
return mPos < it.mPos;
}
template <typename T, class... SRanges>
bool MultiArrayBase<T,SRanges...>::const_iterator::operator>(const const_iterator& it) const
{
return mPos > it.mPos;
}
template <typename T, class... SRanges>
bool MultiArrayBase<T,SRanges...>::const_iterator::operator<=(const const_iterator& it) const
{
return mPos <= it.mPos;
}
template <typename T, class... SRanges>
bool MultiArrayBase<T,SRanges...>::const_iterator::operator>=(const const_iterator& it) const
{
return mPos >= it.mPos;
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::IndexType
MultiArrayBase<T,SRanges...>::const_iterator::index() const
{
auto i = mMAPtr->beginIndex();
i = mPos;
return i;
}
/**********************
* MultiArrayBase *
@ -426,8 +196,8 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
MultiArrayBase<T,SRanges...>::MultiArrayBase(const std::shared_ptr<SRanges>&... ranges)
{
ContainerRangeFactory<SRanges...> crf(ranges...);
mRange = std::dynamic_pointer_cast<ContainerRange<SRanges...> >( crf.create() );
ContainerRangeFactory<T,SRanges...> crf(ranges...);
mRange = std::dynamic_pointer_cast<ContainerRange<T,SRanges...> >( crf.create() );
}
template <typename T, class... SRanges>
@ -437,29 +207,33 @@ namespace MultiArrayTools
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::begin() const
typename MultiArrayBase<T,SRanges...>::IndexType MultiArrayBase<T,SRanges...>::begin() const
{
return const_iterator(*this, beginIndex());
auto i = mRange->begin();
return i.setData(data());
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::end() const
typename MultiArrayBase<T,SRanges...>::IndexType MultiArrayBase<T,SRanges...>::end() const
{
return const_iterator(*this, endIndex());
auto i = mRange->end();
return i.setData(data());
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::IndexType
MultiArrayBase<T,SRanges...>::beginIndex() const
{
return mRange->begin();
auto i = mRange->begin();
return i.setData(data());
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::IndexType
MultiArrayBase<T,SRanges...>::endIndex() const
{
return mRange->end();
auto i = mRange->end();
return i.setData(data());
}
template <typename T, class... SRanges>
@ -488,167 +262,6 @@ namespace MultiArrayTools
return mInit;
}
/****************************************
* MutableMultiArrayBase::iterator *
****************************************/
template <typename T, class... SRanges>
MutableMultiArrayBase<T,SRanges...>::iterator::iterator(MutableMultiArrayBase<T,SRanges...>& ma):
mMAPtr(&ma), mPos(0)
{ }
template <typename T, class... SRanges>
MutableMultiArrayBase<T,SRanges...>::iterator::iterator(MutableMultiArrayBase<T,SRanges...>& ma,
const typename CRange::IndexType& index):
mMAPtr(&ma), mPos(index.pos())
{ }
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator==(const iterator& it) const
{
return mMAPtr == it.mMAPtr and mPos == it.mPos;
}
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator!=(const iterator& it) const
{
return mMAPtr != it.mMAPtr or mPos != it.mPos;
}
template <typename T, class... SRanges>
const T& MutableMultiArrayBase<T,SRanges...>::iterator::operator*() const
{
return mMAPtr->data()[mPos];
}
template <typename T, class... SRanges>
T const* MutableMultiArrayBase<T,SRanges...>::iterator::operator->() const
{
return &mMAPtr->data()[mPos];
}
template <typename T, class... SRanges>
T& MutableMultiArrayBase<T,SRanges...>::iterator::operator*()
{
return mMAPtr->data()[mPos];
}
template <typename T, class... SRanges>
T* MutableMultiArrayBase<T,SRanges...>::iterator::operator->()
{
return &mMAPtr->data()[mPos];
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator++()
{
++mPos;
return *this;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator++(int)
{
iterator tmp(*this);
++mPos;
return tmp;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator--()
{
--mPos;
return *this;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator--(int)
{
iterator tmp(*this);
--mPos;
return tmp;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator+=(int diff)
{
mPos += diff;
return *this;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator-=(int diff)
{
mPos -= diff;
return *this;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator+(int num) const
{
iterator tmp(*this);
tmp += num;
return tmp;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator-(int num) const
{
iterator tmp(*this);
tmp -= num;
}
template <typename T, class... SRanges>
int MutableMultiArrayBase<T,SRanges...>::iterator::operator-(const iterator& it) const
{
return mPos - it.mPos;
}
template <typename T, class... SRanges>
const T& MutableMultiArrayBase<T,SRanges...>::iterator::operator[](int num) const
{
return *(operator+(num));
}
template <typename T, class... SRanges>
T& MutableMultiArrayBase<T,SRanges...>::iterator::operator[](int num)
{
return *(operator+(num));
}
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator<(const iterator& it) const
{
return mPos < it.mPos;
}
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator>(const iterator& it) const
{
return mPos > it.mPos;
}
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator<=(const iterator& it) const
{
return mPos <= it.mPos;
}
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator>=(const iterator& it) const
{
return mPos >= it.mPos;
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::IndexType
MutableMultiArrayBase<T,SRanges...>::iterator::index() const
{
auto i = mMAPtr->beginIndex();
i = mPos;
return i;
}
/******************************
* MutableMultiArrayBase *
******************************/
@ -656,19 +269,21 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
MutableMultiArrayBase<T,SRanges...>::MutableMultiArrayBase(const std::shared_ptr<SRanges>&... ranges) :
MultiArrayBase<T,SRanges...>(ranges...) {}
/*
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::begin()
typename MutableMultiArrayBase<T,SRanges...>::IndexType MutableMultiArrayBase<T,SRanges...>::begin()
{
return iterator(*this, MAB::beginIndex());
auto i = mRange->begin();
return i.setData(data());
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::end()
typename MutableMultiArrayBase<T,SRanges...>::IndexType MutableMultiArrayBase<T,SRanges...>::end()
{
return iterator(*this, MAB::endIndex());
auto i = mRange->end();
return i.setData(data());
}
*/
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::isConst() const
{

View file

@ -94,7 +94,7 @@ namespace MultiArrayTools
typedef T value_type;
typedef OperationBase<T> OB;
typedef ContainerRange<Ranges...> CRange;
typedef ContainerRange<T,Ranges...> CRange;
typedef typename MultiRange<Ranges...>::IndexType IndexType;
OperationMaster(MutableMultiArrayBase<T,Ranges...>& ma, const OpClass& second,
@ -129,7 +129,7 @@ namespace MultiArrayTools
typedef T value_type;
typedef OperationBase<T> OB;
typedef OperationTemplate<T,ConstOperationRoot<T,Ranges...> > OT;
typedef ContainerRange<Ranges...> CRange;
typedef ContainerRange<T,Ranges...> CRange;
typedef typename CRange::IndexType IndexType;
static constexpr size_t SIZE = 1;
@ -165,7 +165,7 @@ namespace MultiArrayTools
typedef T value_type;
typedef OperationBase<T> OB;
typedef OperationTemplate<T,OperationRoot<T,Ranges...> > OT;
typedef ContainerRange<Ranges...> CRange;
typedef ContainerRange<T,Ranges...> CRange;
typedef typename CRange::IndexType IndexType;
static constexpr size_t SIZE = 1;

View file

@ -16,17 +16,18 @@
namespace MultiArrayTools
{
template <class... Indices>
class ContainerIndex : public IndexInterface<ContainerIndex<Indices...>,
std::tuple<typename Indices::MetaType...> >
template <typename T, class... Indices>
class ContainerIndex : public IndexInterface<ContainerIndex<T,Indices...>,
std::tuple<typename Indices::MetaType...> >,
public std::iterator<std::random_access_iterator_tag,T>
{
public:
typedef IndexInterface<ContainerIndex<Indices...>,
typedef IndexInterface<ContainerIndex<T,Indices...>,
std::tuple<typename Indices::MetaType...> > IB;
typedef std::tuple<typename Indices::MetaType...> MetaType;
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
typedef ContainerRange<typename Indices::RangeType...> RangeType;
typedef ContainerRange<T,typename Indices::RangeType...> RangeType;
static IndexType sType() { return IndexType::CONT; }
static size_t sDim() { return sizeof...(Indices); }
@ -37,6 +38,7 @@ namespace MultiArrayTools
bool mExternControl = false;
IndexPack mIPack;
std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
const T* mData;
public:
@ -96,19 +98,41 @@ namespace MultiArrayTools
auto iforh(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs));
// Iterator Stuff
ContainerIndex& setData(const T* data);
const T& operator*() const;
const T* operator->() const;
ContainerIndex operator++(int);
ContainerIndex operator--(int);
ContainerIndex& operator+=(int diff);
ContainerIndex& operator-=(int diff);
ContainerIndex operator+(int num) const;
ContainerIndex operator-(int num) const;
int operator-(const ContainerIndex& it) const;
const T& operator[](int num) const;
bool operator<(const ContainerIndex& it) const;
bool operator>(const ContainerIndex& it) const;
bool operator<=(const ContainerIndex& it) const;
bool operator>=(const ContainerIndex& it) const;
};
template <class... Ranges>
template <typename T, class... Ranges>
class ContainerRangeFactory : public RangeFactoryBase
{
public:
typedef ContainerRange<Ranges...> oType;
typedef ContainerRange<T,Ranges...> oType;
ContainerRangeFactory();
ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs);
ContainerRangeFactory(const typename ContainerRange<Ranges...>::SpaceType& space);
ContainerRangeFactory(const typename ContainerRange<T,Ranges...>::SpaceType& space);
virtual std::shared_ptr<RangeBase> create() override;
@ -116,14 +140,14 @@ namespace MultiArrayTools
};
template <class... Ranges>
class ContainerRange : public RangeInterface<ContainerIndex<typename Ranges::IndexType...> >
template <typename T, class... Ranges>
class ContainerRange : public RangeInterface<ContainerIndex<T,typename Ranges::IndexType...> >
{
public:
typedef RangeBase RB;
typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType;
typedef ContainerIndex<typename Ranges::IndexType...> IndexType;
typedef ContainerIndex<T,typename Ranges::IndexType...> IndexType;
//typedef typename RangeInterface<ContainerIndex<typename Ranges::IndexType...> >::IndexType IndexType;
protected:
@ -153,7 +177,7 @@ namespace MultiArrayTools
virtual IndexType begin() const override;
virtual IndexType end() const override;
friend ContainerRangeFactory<Ranges...>;
friend ContainerRangeFactory<T,Ranges...>;
static constexpr bool defaultable = false;
static constexpr size_t ISSTATIC = SubProp<Ranges...>::ISSTATIC;
@ -177,10 +201,10 @@ namespace MultiArrayTools
* ContainerIndex *
**********************/
template <class... Indices>
template <typename T, class... Indices>
template <class MRange>
ContainerIndex<Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<ContainerIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
ContainerIndex<T,Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<ContainerIndex<T,Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
{
RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
@ -188,8 +212,8 @@ namespace MultiArrayTools
RPackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack);
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::sync()
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::sync()
{
if(mExternControl){
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
@ -200,39 +224,39 @@ namespace MultiArrayTools
return *this;
}
template <class... Indices>
template <typename T, class... Indices>
template <size_t N>
auto ContainerIndex<Indices...>::get() const -> decltype( *std::get<N>( mIPack ) )&
auto ContainerIndex<T,Indices...>::get() const -> decltype( *std::get<N>( mIPack ) )&
{
return *std::get<N>( mIPack );
}
template <class... Indices>
template <typename T, class... Indices>
template <size_t N>
auto ContainerIndex<Indices...>::getPtr() const -> decltype( std::get<N>( mIPack ) )&
auto ContainerIndex<T,Indices...>::getPtr() const -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>( mIPack );
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()(const std::shared_ptr<Indices>&... inds)
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::operator()(const std::shared_ptr<Indices>&... inds)
{
RPackNum<sizeof...(Indices)-1>::swapIndices(mIPack, inds...);
mExternControl = true;
return sync();
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()()
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::operator()()
{
return sync();
}
template <class... Indices>
IndexType ContainerIndex<Indices...>::type() const { return IndexType::CONT; }
template <typename T, class... Indices>
IndexType ContainerIndex<T,Indices...>::type() const { return IndexType::CONT; }
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator++()
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::operator++()
{
if(mExternControl){
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
@ -242,8 +266,8 @@ namespace MultiArrayTools
return *this;
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator--()
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::operator--()
{
if(mExternControl){
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
@ -254,80 +278,80 @@ namespace MultiArrayTools
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator=(size_t pos)
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::operator=(size_t pos)
{
IB::mPos = pos;
RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos);
return *this;
}
template <class... Indices>
int ContainerIndex<Indices...>::pp(std::intptr_t idxPtrNum)
template <typename T, class... Indices>
int ContainerIndex<T,Indices...>::pp(std::intptr_t idxPtrNum)
{
int tmp = RPackNum<sizeof...(Indices)-1>::pp(mIPack, mBlockSizes, idxPtrNum);
IB::mPos += tmp;
return tmp;
}
template <class... Indices>
int ContainerIndex<Indices...>::mm(std::intptr_t idxPtrNum)
template <typename T, class... Indices>
int ContainerIndex<T,Indices...>::mm(std::intptr_t idxPtrNum)
{
int tmp = RPackNum<sizeof...(Indices)-1>::mm(mIPack, mBlockSizes, idxPtrNum);
IB::mPos -= tmp;
return tmp;
}
template <class... Indices>
typename ContainerIndex<Indices...>::MetaType ContainerIndex<Indices...>::meta()
template <typename T, class... Indices>
typename ContainerIndex<T,Indices...>::MetaType ContainerIndex<T,Indices...>::meta()
{
MetaType metaTuple;
RPackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack);
return metaTuple;
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::at(const MetaType& metaPos)
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::at(const MetaType& metaPos)
{
RPackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this;
}
template <class... Indices>
size_t ContainerIndex<Indices...>::dim()
template <typename T, class... Indices>
size_t ContainerIndex<T,Indices...>::dim()
{
return sizeof...(Indices);
}
template <class... Indices>
bool ContainerIndex<Indices...>::first()
template <typename T, class... Indices>
bool ContainerIndex<T,Indices...>::first()
{
return IB::pos() == 0;
}
template <class... Indices>
bool ContainerIndex<Indices...>::last()
template <typename T, class... Indices>
bool ContainerIndex<T,Indices...>::last()
{
return IB::pos() == IB::mMax - 1;
}
template <class... Indices>
std::shared_ptr<typename ContainerIndex<Indices...>::RangeType>
ContainerIndex<Indices...>::range()
template <typename T, class... Indices>
std::shared_ptr<typename ContainerIndex<T,Indices...>::RangeType>
ContainerIndex<T,Indices...>::range()
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr );
}
template <class... Indices>
template <typename T, class... Indices>
template <size_t N>
auto ContainerIndex<Indices...>::getPtr() -> decltype( std::get<N>( mIPack ) )&
auto ContainerIndex<T,Indices...>::getPtr() -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>( mIPack );
}
template <class... Indices>
size_t ContainerIndex<Indices...>::getStepSize(size_t n)
template <typename T, class... Indices>
size_t ContainerIndex<T,Indices...>::getStepSize(size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
@ -336,8 +360,8 @@ namespace MultiArrayTools
return mBlockSizes[n+1];
}
template <class... Indices>
std::vector<IndexInfo> ContainerIndex<Indices...>::infoVec() const
template <typename T, class... Indices>
std::vector<IndexInfo> ContainerIndex<T,Indices...>::infoVec() const
{
std::vector<IndexInfo> out;
out.reserve(sizeof...(Indices));
@ -345,14 +369,14 @@ namespace MultiArrayTools
return std::move( out );
}
template <class... Indices>
std::string ContainerIndex<Indices...>::id() const
template <typename T, class... Indices>
std::string ContainerIndex<T,Indices...>::id() const
{
return std::string("con") + std::to_string(IB::mId);
}
template <class... Indices>
void ContainerIndex<Indices...>::print(size_t offset)
template <typename T, class... Indices>
void ContainerIndex<T,Indices...>::print(size_t offset)
{
if(offset == 0){
std::cout << " === " << std::endl;
@ -363,42 +387,156 @@ namespace MultiArrayTools
RPackNum<sizeof...(Indices)-1>::printIndex(mIPack, offset+1);
}
template <class... Indices>
template <typename T, class... Indices>
template <class Exprs>
auto ContainerIndex<Indices...>::ifor(Exprs exs) const
auto ContainerIndex<T,Indices...>::ifor(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs))
{
return RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs);
}
template <class... Indices>
template <typename T, class... Indices>
template <class Exprs>
auto ContainerIndex<Indices...>::iforh(Exprs exs) const
auto ContainerIndex<T,Indices...>::iforh(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs))
{
return RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs);
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::setData(const T* data)
{
mData = data;
return *this;
}
template <typename T, class... Indices>
const T& ContainerIndex<T,Indices...>::operator*() const
{
return mData[IB::mPos];
}
template <typename T, class... Indices>
const T* ContainerIndex<T,Indices...>::operator->() const
{
return &mData[IB::mPos];
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...> ContainerIndex<T,Indices...>::operator++(int)
{
auto tmp = *this;
++(*this);
return tmp;
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...> ContainerIndex<T,Indices...>::operator--(int)
{
auto tmp = *this;
--(*this);
return tmp;
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::operator+=(int diff)
{
if(diff < 0){
for(int i = 0; i != diff; ++i){
(*this)--;
}
}
else {
for(int i = 0; i != diff; ++i){
(*this)++;
}
}
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::operator-=(int diff)
{
if(diff < 0){
for(int i = 0; i != diff; ++i){
(*this)++;
}
}
else {
for(int i = 0; i != diff; ++i){
(*this)--;
}
}
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...> ContainerIndex<T,Indices...>::operator+(int num) const
{
auto tmp = *this;
return tmp += num;
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...> ContainerIndex<T,Indices...>::operator-(int num) const
{
auto tmp = *this;
return tmp -= num;
}
template <typename T, class... Indices>
int ContainerIndex<T,Indices...>::operator-(const ContainerIndex<T,Indices...>& it) const
{
return static_cast<int>( IB::mPos ) - static_cast<int>( it.pos() );
}
template <typename T, class... Indices>
const T& ContainerIndex<T,Indices...>::operator[](int num) const
{
return mData[IB::mPos + num];
}
template <typename T, class... Indices>
bool ContainerIndex<T,Indices...>::operator<(const ContainerIndex<T,Indices...>& it) const
{
return IB::mPos < it.pos();
}
template <typename T, class... Indices>
bool ContainerIndex<T,Indices...>::operator>(const ContainerIndex<T,Indices...>& it) const
{
return IB::mPos > it.pos();
}
template <typename T, class... Indices>
bool ContainerIndex<T,Indices...>::operator<=(const ContainerIndex<T,Indices...>& it) const
{
return IB::mPos <= it.pos();
}
template <typename T, class... Indices>
bool ContainerIndex<T,Indices...>::operator>=(const ContainerIndex<T,Indices...>& it) const
{
return IB::mPos >= it.pos();
}
/*****************************
* ContainerRangeFactory *
*****************************/
template <class... Ranges>
ContainerRangeFactory<Ranges...>::ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs)
template <typename T, class... Ranges>
ContainerRangeFactory<T,Ranges...>::ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs)
{
mProd = std::shared_ptr<ContainerRange<Ranges...> >( new ContainerRange<Ranges...>( rs... ) );
mProd = std::shared_ptr<ContainerRange<T,Ranges...> >( new ContainerRange<T,Ranges...>( rs... ) );
}
template <class... Ranges>
ContainerRangeFactory<Ranges...>::
ContainerRangeFactory(const typename ContainerRange<Ranges...>::SpaceType& space)
template <typename T, class... Ranges>
ContainerRangeFactory<T,Ranges...>::
ContainerRangeFactory(const typename ContainerRange<T,Ranges...>::SpaceType& space)
{
mProd = std::shared_ptr<ContainerRange<Ranges...> >( new ContainerRange<Ranges...>( space ) );
mProd = std::shared_ptr<ContainerRange<T,Ranges...> >( new ContainerRange<T,Ranges...>( space ) );
}
template <class... Ranges>
std::shared_ptr<RangeBase> ContainerRangeFactory<Ranges...>::create()
template <typename T, class... Ranges>
std::shared_ptr<RangeBase> ContainerRangeFactory<T,Ranges...>::create()
{
setSelf();
return mProd;
@ -408,64 +546,65 @@ namespace MultiArrayTools
* ContainerRange *
**********************/
template <class... Ranges>
ContainerRange<Ranges...>::ContainerRange(const std::shared_ptr<Ranges>&... rs) :
template <typename T, class... Ranges>
ContainerRange<T,Ranges...>::ContainerRange(const std::shared_ptr<Ranges>&... rs) :
mSpace( std::make_tuple( rs... ) ) {}
template <class... Ranges>
ContainerRange<Ranges...>::ContainerRange(const SpaceType& space) : mSpace( space ) {}
template <typename T, class... Ranges>
ContainerRange<T,Ranges...>::ContainerRange(const SpaceType& space) : mSpace( space ) {}
template <class... Ranges>
size_t ContainerRange<Ranges...>::dim() const
template <typename T, class... Ranges>
size_t ContainerRange<T,Ranges...>::dim() const
{
return sizeof...(Ranges);
}
template <class... Ranges>
size_t ContainerRange<Ranges...>::size() const
template <typename T, class... Ranges>
size_t ContainerRange<T,Ranges...>::size() const
{
return RPackNum<sizeof...(Ranges)-1>::getSize(mSpace);
}
template <class... Ranges>
template <typename T, class... Ranges>
template <size_t N>
auto ContainerRange<Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )&
auto ContainerRange<T,Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )&
{
return *std::get<N>( mSpace );
}
template <class... Ranges>
template <typename T, class... Ranges>
template <size_t N>
auto ContainerRange<Ranges...>::getPtr() const -> decltype( std::get<N>( mSpace ) )&
auto ContainerRange<T,Ranges...>::getPtr() const -> decltype( std::get<N>( mSpace ) )&
{
return std::get<N>( mSpace );
}
template <class... Ranges>
const typename ContainerRange<Ranges...>::SpaceType& ContainerRange<Ranges...>::space() const
template <typename T, class... Ranges>
const typename ContainerRange<T,Ranges...>::SpaceType& ContainerRange<T,Ranges...>::space() const
{
return mSpace;
}
template <class... Ranges>
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::begin() const
template <typename T, class... Ranges>
typename ContainerRange<T,Ranges...>::IndexType ContainerRange<T,Ranges...>::begin() const
{
ContainerIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
ContainerIndex<T,typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<T,Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0;
return i;
}
template <class... Ranges>
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::end() const
template <typename T, class... Ranges>
typename ContainerRange<T,Ranges...>::IndexType ContainerRange<T,Ranges...>::end() const
{
ContainerIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
ContainerIndex<T,typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<T,Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = size();
return i;
}
} // end namespace MultiArrayTools

View file

@ -51,7 +51,8 @@ namespace MultiArrayTools
// ( have to assign sub-indices (ptr!) correctly )
//MultiIndex(const MultiIndex& in);
//MultiIndex& operator=(const MultiIndex& in);
MultiIndex& operator=(ContainerIndex<Indices...>& ci);
template <typename T>
MultiIndex& operator=(ContainerIndex<T,Indices...>& ci);
template <class MRange>
MultiIndex(const std::shared_ptr<MRange>& range);
@ -127,7 +128,9 @@ namespace MultiArrayTools
MultiRangeFactory() = delete;
MultiRangeFactory(const std::shared_ptr<Ranges>&... rs);
MultiRangeFactory(const typename MultiRange<Ranges...>::SpaceType& space);
MultiRangeFactory(const std::shared_ptr<ContainerRange<Ranges...> >& cr);
template <typename T>
MultiRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr);
virtual std::shared_ptr<RangeBase> create() override;
};
@ -217,7 +220,8 @@ namespace MultiArrayTools
}
*/
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(ContainerIndex<Indices...>& ci)
template <typename T>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(ContainerIndex<T,Indices...>& ci)
{
RPackNum<sizeof...(Indices)-1>::copyInst(mIPack, ci);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
@ -441,7 +445,8 @@ namespace MultiArrayTools
}
template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const std::shared_ptr<ContainerRange<Ranges...> >& cr)
template <typename T>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr)
{
mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( cr->space() ) );
}

View file

@ -51,15 +51,15 @@ namespace MultiArrayTools
class MultiIndex;
// container_range.h
template <class... Ranges>
template <typename T, class... Ranges>
class ContainerRangeFactory;
// container_range.h
template <class... Ranges>
template <typename T, class... Ranges>
class ContainerRange;
// container_range.h
template <class... Indices>
template <typename T, class... Indices>
class ContainerIndex;
// anonymous_range.h

View file

@ -162,9 +162,9 @@ namespace MultiArrayHelper
RPackNum<N-1>::construct(ip, range);
}
template <template<class...> class IndexType, class... Indices>
template <class IndexType, class... Indices>
static void copyInst(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
const IndexType& ind)
{
std::get<N>(ip) = ind.template getPtr<N>() ;
RPackNum<N-1>::copyInst(ip, ind);
@ -341,9 +341,9 @@ namespace MultiArrayHelper
std::get<0>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( range.template getPtr<0>() ) );
}
template <template<class...> class IndexType, class... Indices>
template <class IndexType, class... Indices>
static void copyInst(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
const IndexType& ind)
{
std::get<0>(ip) = ind.template getPtr<0>();
}

View file

@ -92,6 +92,9 @@ namespace {
typedef MultiRangeFactory<SRange,SRange> MRF;
typedef MRF::oType MRange;
typedef AnonymousRange ANO;
typedef MultiArray<double,ANO> AMA;
OpTest_MDim()
{
swapFactory<SRF>(rfbptr, {'x', 'l', 'f', 'g'} );
@ -420,6 +423,38 @@ namespace {
EXPECT_EQ( xround( res.at(mkt(mkt('3','b'),'B')) ), xround(2.911 + 0.373 + 2.210) );
}
TEST_F(OpTest_MDim, ExecAnonOp1)
{
MultiArray<double,MRange,SRange> res(mr1ptr,sr4ptr);
MultiArray<double,MRange> ma1(mr1ptr, v3);
MultiArray<double,SRange> ma2(sr2ptr, v1);
MultiArray<double,SRange> ma3(sr4ptr, v4);
auto si1 = MAT::getIndex( sr2ptr );
auto si2 = MAT::getIndex( sr3ptr );
auto si3 = MAT::getIndex( sr4ptr );
auto mi = MAT::getIndex( mr1ptr );
mi->operator()(si1,si2);
res(mi,si3) = ma1(mi) + ma2(si1) + ma3(si3);
EXPECT_EQ( xround( res.at(mkt(mkt('1','a'),'A')) ), xround(0.353 + 2.917 + 1.470) );
EXPECT_EQ( xround( res.at(mkt(mkt('1','a'),'B')) ), xround(0.353 + 2.917 + 2.210) );
EXPECT_EQ( xround( res.at(mkt(mkt('1','b'),'A')) ), xround(4.005 + 2.917 + 1.470) );
EXPECT_EQ( xround( res.at(mkt(mkt('1','b'),'B')) ), xround(4.005 + 2.917 + 2.210) );
EXPECT_EQ( xround( res.at(mkt(mkt('2','a'),'A')) ), xround(1.070 + 9.436 + 1.470) );
EXPECT_EQ( xround( res.at(mkt(mkt('2','a'),'B')) ), xround(1.070 + 9.436 + 2.210) );
EXPECT_EQ( xround( res.at(mkt(mkt('2','b'),'A')) ), xround(2.310 + 9.436 + 1.470) );
EXPECT_EQ( xround( res.at(mkt(mkt('2','b'),'B')) ), xround(2.310 + 9.436 + 2.210) );
EXPECT_EQ( xround( res.at(mkt(mkt('3','a'),'A')) ), xround(9.243 + 0.373 + 1.470) );
EXPECT_EQ( xround( res.at(mkt(mkt('3','a'),'B')) ), xround(9.243 + 0.373 + 2.210) );
EXPECT_EQ( xround( res.at(mkt(mkt('3','b'),'A')) ), xround(2.911 + 0.373 + 1.470) );
EXPECT_EQ( xround( res.at(mkt(mkt('3','b'),'B')) ), xround(2.911 + 0.373 + 2.210) );
}
} // anonymous namspace
int main(int argc, char** argv)

View file

@ -48,7 +48,7 @@ namespace {
typedef MultiRangeFactory<SRange,M3Range,SRange> MasterRF;
typedef MasterRF::oType MasterRange;
typedef ContainerRangeFactory<M3Range,SRange> CRF;
typedef ContainerRangeFactory<double,M3Range,SRange> CRF;
typedef CRF::oType CRange;
IndexTest()