two container indices (one const)

This commit is contained in:
Christian Zimmermann 2021-05-29 00:03:48 +02:00
parent 01703dec28
commit 53362be499
6 changed files with 218 additions and 128 deletions

View file

@ -11,8 +11,8 @@ namespace MultiArrayTools
mExternControl(false),
mBlockSizes(in.mBlockSizes),
mData(in.mData),
mCPos(in.mCPos),
mObjPtrNum(in.mObjPtrNum)
mObjPtrNum(in.mObjPtrNum),
mCPos(in.mCPos)
{
sfor_pn<0,sizeof...(Indices)>
( [&](auto i)
@ -32,8 +32,8 @@ namespace MultiArrayTools
mExternControl = false;
mBlockSizes = in.mBlockSizes;
mData = in.mData;
mCPos = in.mCPos;
mObjPtrNum = in.mObjPtrNum;
mCPos = in.mCPos;
sfor_pn<0,sizeof...(Indices)>
( [&](auto i)
{
@ -117,14 +117,14 @@ namespace MultiArrayTools
template <typename T, class... Indices>
template <size_t N>
auto ConstContainerIndex<T,Indices...>::get() const -> decltype( *std::get<N>( mIPack ) )&
auto& ConstContainerIndex<T,Indices...>::get() const
{
return *std::get<N>( mIPack );
}
template <typename T, class... Indices>
template <size_t N>
auto ConstContainerIndex<T,Indices...>::getPtr() const -> decltype( std::get<N>( mIPack ) )&
auto ConstContainerIndex<T,Indices...>::getPtr() const
{
return std::get<N>( mIPack );
}
@ -274,13 +274,6 @@ namespace MultiArrayTools
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr );
}
template <typename T, class... Indices>
template <size_t N>
auto ConstContainerIndex<T,Indices...>::getPtr() -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>( mIPack );
}
template <typename T, class... Indices>
size_t ConstContainerIndex<T,Indices...>::getStepSize(size_t n)
{

View file

@ -18,8 +18,8 @@ namespace MultiArrayTools
template <typename T, class... Indices>
class ConstContainerIndex : public IndexInterface<ConstContainerIndex<T,Indices...>,
std::tuple<typename Indices::MetaType...> >,
public std::iterator<std::random_access_iterator_tag,T>
std::tuple<typename Indices::MetaType...> >,
public std::iterator<std::random_access_iterator_tag,T>
{
public:
@ -51,8 +51,10 @@ namespace MultiArrayTools
IndexPack mIPack;
std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
const T* mData = nullptr;
size_t mCPos;
std::intptr_t mObjPtrNum;
protected:
size_t mCPos;
public:
@ -75,11 +77,6 @@ namespace MultiArrayTools
std::intptr_t objPtrNum,
const std::array<size_t,sizeof...(Indices)+1>& blockSizes);
template <size_t N>
auto get() const -> decltype( *std::get<N>( mIPack ) )&;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mIPack ) )&;
template <size_t N>
size_t getBlockSize() const { return std::get<N>(mBlockSizes); }
@ -115,7 +112,10 @@ namespace MultiArrayTools
std::shared_ptr<RangeType> range();
template <size_t N>
auto getPtr() -> decltype( std::get<N>( mIPack ) )&;
auto& get() const;
template <size_t N>
auto getPtr() const;
size_t getStepSize(size_t n);
@ -157,6 +157,151 @@ namespace MultiArrayTools
};
template <typename T, class... Indices>
class ContainerIndex : public ConstContainerIndex<T,Indices...>
{
public:
typedef ConstContainerIndex<T,Indices...> CCI;
typedef CCI IB;
typedef typename CCI::MetaType MetaType;
typedef typename CCI::IndexPack IndexPack;
typedef typename CCI::RangeType RangeType;
static constexpr IndexType sType() { return CCI::sType(); }
static constexpr size_t sDim() { return CCI::sDim(); }
static constexpr size_t totalDim() { return CCI::totalDim(); }
static constexpr SpaceType STYPE = CCI::STYPE;
static constexpr bool PARALLEL = CCI::PARALLEL;
template <typename X>
using CIX = ContainerIndex<X,Indices...>;
template <typename X>
friend class CIX;
private:
ContainerIndex() = default;
T* mMData = nullptr;
public:
ContainerIndex(const ContainerIndex& in) = default;
ContainerIndex& operator=(const ContainerIndex& in) = default;
ContainerIndex(const ContainerIndex& in, bool copy) : CCI(in,copy)
{ mMData = in.mMData; }
ContainerIndex(const ConstContainerIndex<T,Indices...>& in, T* data) : CCI(in)
{ mMData = data; }
ContainerIndex(const ConstContainerIndex<T,Indices...>& in, T* data, bool copy) :
CCI(in,copy)
{ mMData = data; }
ContainerIndex& copy(const ContainerIndex& in)
{ CCI::copy(in); mMData = in.mMData; }
template <typename X>
ContainerIndex& operator=(const ContainerIndex<X,Indices...>& in)
{ CCI::operator=(in); return *this; }
template <class MRange>
ContainerIndex(const std::shared_ptr<MRange>& range,
std::intptr_t objPtrNum) : CCI(range, objPtrNum) {}
template <class MRange>
ContainerIndex(const std::shared_ptr<MRange>& range,
std::intptr_t objPtrNum,
const std::array<size_t,sizeof...(Indices)+1>& blockSizes)
: CCI(range, objPtrNum, blockSizes) {}
template <size_t N>
size_t getBlockSize() const { return CCI::template getBlockSize<N>(); }
const IndexPack& pack() const { CCI::pack(); return *this; }
ContainerIndex& sync() { return CCI::sync(); return *this; }
ContainerIndex& operator()(const std::shared_ptr<Indices>&... inds)
{ CCI::operator()(inds...); return *this; }
ContainerIndex& operator()(const std::tuple<std::shared_ptr<Indices>...>& inds)
{ CCI::operator()(inds); return *this; }
ContainerIndex& operator()() { CCI::operator()(); return *this; }
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
IndexType type() const { return CCI::type(); }
ContainerIndex& operator++() { CCI::operator++(); return *this; }
ContainerIndex& operator--() { CCI::operator--(); return *this; }
ContainerIndex& operator=(size_t pos) { CCI::operator=(pos); return *this; }
int pp(std::intptr_t idxPtrNum) { return CCI::pp(idxPtrNum); }
int mm(std::intptr_t idxPtrNum) { return CCI::mm(idxPtrNum); }
std::string stringMeta() const { return CCI::stringMeta; }
MetaType meta() const { return CCI::meta(); }
ContainerIndex& at(const MetaType& metaPos) { CCI::at(metaPos); return *this; }
size_t dim() const { return CCI::dim(); }
bool first() const { return CCI::first(); }
bool last() const { return CCI::last(); }
bool sliceMode() const { return CCI::sliceMode(); }
std::shared_ptr<RangeType> range() { return CCI::range(); }
template <size_t N>
auto& get() const { return CCI::template get<N>(); }
template <size_t N>
auto getPtr() const { return CCI::template getPtr<N>(); }
size_t getStepSize(size_t n) { return getStepSize(n); }
template <class Exprs>
auto ifor(size_t step, Exprs exs) const { return CCI::ifor(step, exs); }
template <class Exprs>
auto iforh(size_t step, Exprs exs) const { return CCI::iforh(step, exs); }
template <class Exprs>
auto pifor(size_t step, Exprs exs) const { return CCI::pifor(step, exs); }
std::intptr_t container() const { return CCI::container(); }
ContainerIndex& format(const std::array<size_t,sizeof...(Indices)+1>& blocks)
{ CCI::format(blocks); return *this; }
// Iterator Stuff
ContainerIndex& setData(T* data) { CCI::setData(data); mMData = data; return *this; }
const T& operator*() const { return CCI::operator*(); }
const T* operator->() const { return CCI::operator->(); }
T& operator*() { return mMData[CCI::mCPos]; }
T* operator->() { return &mMData[CCI::mCPos]; }
ContainerIndex operator++(int) { auto tmp = *this; ++(*this); return tmp; }
ContainerIndex operator--(int) { auto tmp = *this; --(*this); return tmp; }
ContainerIndex& operator+=(int diff) { CCI::operator+=(diff); return *this; }
ContainerIndex& operator-=(int diff) { CCI::operator-=(diff); return *this; }
ContainerIndex operator+(int num) const { CCI::operator+(num); return *this; }
ContainerIndex operator-(int num) const { CCI::operator-(num); return *this; }
int operator-(const ContainerIndex& it) const { return CCI::operator-(it); }
const T& operator[](int num) const { return CCI::operator[](num); }
bool operator<(const ContainerIndex& it) const { return CCI::operator<(it); }
bool operator>(const ContainerIndex& it) const { return CCI::operator>(it); }
bool operator<=(const ContainerIndex& it) const { return CCI::operator<=(it); }
bool operator>=(const ContainerIndex& it) const { return CCI::operator>=(it); }
};
} // end namespace MultiArrayTools
#endif

View file

@ -95,36 +95,6 @@ namespace MultiArrayTools
MAB::mInit = true;
ama.mInit = false;
}
/*
template <typename T, class... SRanges>
template <class Range2, class Range3>
MultiArray<T,SRanges...>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) :
MutableMultiArrayBase<T,SRanges...>(merge(in.range(), in[ in.beginIndex() ].range()))
// assert that Range2 has always same extension
{
MAB::mInit = true;
mCont.clear();
for(auto i = in.beginIndex(); i != in.endIndex(); ++i){
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
}
assert(mCont.size() == MAB::mRange->size());
}
*/
/*
template <typename T, class... SRanges>
template <class Range2, class Range3>
MultiArray<T,SRanges...>& MultiArray<T,SRanges...>::operator=(const MultiArray<MultiArray<T,Range2>,Range3> in)
{
MAB::mRange.reset(new Range(merge(in.range(), in[ in.beginIndex() ].range())));
// assert that Range2 has always same extension
mCont.clear();
for(auto i = in.beginIndex(); i != in.endIndex(); ++i){
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
}
assert(mCont.size() == MAB::mRange->size());
return *this;
} */
template <typename T, class... SRanges>
T& MultiArray<T,SRanges...>::operator[](const IndexType& i)
@ -141,13 +111,13 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
T& MultiArray<T,SRanges...>::at(const typename IndexType::MetaType& meta)
{
return mCont[ MAB::beginIndex().at(meta).pos() ];
return mCont[ MAB::cbegin().at(meta).pos() ];
}
template <typename T, class... SRanges>
const T& MultiArray<T,SRanges...>::at(const typename IndexType::MetaType& meta) const
{
return mCont[ MAB::beginIndex().at(meta).pos() ];
return mCont[ MAB::cbegin().at(meta).pos() ];
}
template <typename T, class... SRanges>
@ -220,17 +190,6 @@ namespace MultiArrayTools
}
}
/*
template <typename T, class... SRanges>
std::shared_ptr<MultiArrayBase<T,AnonymousRange> > MultiArray<T,SRanges...>::anonymousMove()
{
AnonymousRangeFactory arf(MAB::mRange->space());
MAB::mInit = false;
return std::make_shared<MultiArray<T,AnonymousRange> >
( std::dynamic_pointer_cast<AnonymousRange>( arf.create() ),
std::move(mCont) );
}
*/
template <typename T, class... SRanges>
MultiArray<T,SRanges...>& MultiArray<T,SRanges...>::operator=(const T& in)
{
@ -270,7 +229,6 @@ namespace MultiArrayTools
for(size_t i = 0; i != mCont.size(); ++i){
mCont[i] -= in.mCont[i];
}
//std::transform(mCont.begin(), mCont.end(), in.mCont.begin(), mCont.begin(), std::minus<T>());
}
return *this;
}

View file

@ -91,38 +91,30 @@ namespace MultiArrayTools
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::IndexType MultiArrayBase<T,SRanges...>::begin() const
typename MultiArrayBase<T,SRanges...>::CIndexType MultiArrayBase<T,SRanges...>::begin() const
{
IndexType i(*mProtoI,true);
i = 0;
return i.setData(data());
return cbegin();
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::IndexType MultiArrayBase<T,SRanges...>::end() const
typename MultiArrayBase<T,SRanges...>::CIndexType MultiArrayBase<T,SRanges...>::end() const
{
IndexType i(*mProtoI,true);
i = i.max();
//i = mRange->size();
return i.setData(data());
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::IndexType
MultiArrayBase<T,SRanges...>::beginIndex() const
{
IndexType i(*mProtoI,true);
i = 0;
return i.setData(data());
return end();
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::IndexType
MultiArrayBase<T,SRanges...>::endIndex() const
typename MultiArrayBase<T,SRanges...>::CIndexType MultiArrayBase<T,SRanges...>::cbegin() const
{
IndexType i(*mProtoI,true);
CIndexType i(*mProtoI,true);
i = 0;
return i.setData(data());
}
template <typename T, class... SRanges>
typename MultiArrayBase<T,SRanges...>::CIndexType MultiArrayBase<T,SRanges...>::cend() const
{
CIndexType i(*mProtoI,true);
i = i.max();
//i = mRange->size();
return i.setData(data());
}
@ -148,7 +140,7 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
ConstOperationRoot<T,SRanges...>
MultiArrayBase<T,SRanges...>::op(const std::shared_ptr<IndexType>& ind) const
MultiArrayBase<T,SRanges...>::op(const std::shared_ptr<CIndexType>& ind) const
{
return ConstOperationRoot<T,SRanges...>(data(), *ind);
}
@ -189,21 +181,6 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
MutableMultiArrayBase<T,SRanges...>::MutableMultiArrayBase(const typename CRange::Space& space) :
MultiArrayBase<T,SRanges...>(space) {}
/*
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::IndexType MutableMultiArrayBase<T,SRanges...>::begin()
{
auto i = mRange->begin();
return i.setData(data());
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::IndexType MutableMultiArrayBase<T,SRanges...>::end()
{
auto i = mRange->end();
return i.setData(data());
}
*/
template <typename T, class... SRanges>
template <typename X>
@ -217,11 +194,27 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
T& MutableMultiArrayBase<T,SRanges...>::operator[](const std::tuple<IPTR<typename SRanges::IndexType>...>& is)
{
IndexType ii(*MAB::mProtoI);
IndexType ii(*MAB::mProtoI,this->data());
ii(is);
return (*this)[ii];
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::IndexType MutableMultiArrayBase<T,SRanges...>::begin()
{
IndexType i(*MAB::mProtoI,this->data(),true);
i = 0;
return i.setData(data());
}
template <typename T, class... SRanges>
typename MutableMultiArrayBase<T,SRanges...>::IndexType MutableMultiArrayBase<T,SRanges...>::end()
{
IndexType i(*MAB::mProtoI,this->data(),true);
i = i.max();
return i.setData(data());
}
template <typename T, class... SRanges>
bool MutableMultiArrayBase<T,SRanges...>::isConst() const
@ -238,7 +231,7 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
OperationRoot<T,SRanges...>
MutableMultiArrayBase<T,SRanges...>::op(const std::shared_ptr<IndexType>& ind)
MutableMultiArrayBase<T,SRanges...>::op(const std::shared_ptr<CIndexType>& ind)
{
return OperationRoot<T,SRanges...>(data(), *ind);
}
@ -252,7 +245,7 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
ConstOperationRoot<T,SRanges...>
MutableMultiArrayBase<T,SRanges...>::op(const std::shared_ptr<IndexType>& ind) const
MutableMultiArrayBase<T,SRanges...>::op(const std::shared_ptr<CIndexType>& ind) const
{
return ConstOperationRoot<T,SRanges...>(data(), *ind);
}

View file

@ -57,12 +57,13 @@ namespace MultiArrayTools
typedef T value_type;
typedef ContainerRange<SRanges...> CRange;
typedef ConstContainerIndex<T,typename SRanges::IndexType...> IndexType;
typedef ConstContainerIndex<T,typename SRanges::IndexType...> CIndexType;
typedef ContainerIndex<T,typename SRanges::IndexType...> IndexType;
protected:
bool mInit = false;
std::shared_ptr<CRange> mRange;
std::shared_ptr<IndexType> mProtoI;
std::shared_ptr<CIndexType> mProtoI;
public:
@ -82,7 +83,7 @@ namespace MultiArrayTools
const T& operator[](const ConstContainerIndex<X,typename SRanges::IndexType...>& i);
const T& operator[](const std::tuple<IPTR<typename SRanges::IndexType>...>& is) const;
virtual const T& operator[](const IndexType& i) const = 0;
virtual const T& operator[](const CIndexType& i) const = 0;
virtual const T& at(const typename CRange::IndexType::MetaType& meta) const = 0;
virtual const T* data() const = 0;
@ -90,12 +91,11 @@ namespace MultiArrayTools
virtual size_t size() const;
virtual bool isSlice() const = 0;
virtual IndexType begin() const;
virtual IndexType end() const;
virtual CIndexType begin() const;
virtual CIndexType end() const;
virtual CIndexType cbegin() const;
virtual CIndexType cend() const;
virtual IndexType beginIndex() const;
virtual IndexType endIndex() const;
virtual const std::shared_ptr<CRange>& range() const;
virtual bool isConst() const;
@ -103,7 +103,7 @@ namespace MultiArrayTools
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous(bool slice = false) const = 0;
virtual ConstOperationRoot<T,SRanges...>
op(const std::shared_ptr<IndexType>& ind) const;
op(const std::shared_ptr<CIndexType>& ind) const;
virtual ConstOperationRoot<T,SRanges...>
operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const;
@ -127,13 +127,16 @@ namespace MultiArrayTools
typedef ContainerRange<SRanges...> CRange;
typedef MultiArrayBase<T,SRanges...> MAB;
typedef ConstContainerIndex<T,typename SRanges::IndexType...> IndexType;
typedef ContainerIndex<T,typename SRanges::IndexType...> IndexType;
typedef ConstContainerIndex<T,typename SRanges::IndexType...> CIndexType;
using MultiArrayBase<T,SRanges...>::operator[];
using MultiArrayBase<T,SRanges...>::at;
using MultiArrayBase<T,SRanges...>::data;
using MultiArrayBase<T,SRanges...>::begin;
using MultiArrayBase<T,SRanges...>::end;
using MultiArrayBase<T,SRanges...>::cbegin;
using MultiArrayBase<T,SRanges...>::cend;
DEFAULT_MEMBERS(MutableMultiArrayBase);
MutableMultiArrayBase(const std::shared_ptr<SRanges>&... ranges);
@ -143,26 +146,24 @@ namespace MultiArrayTools
T& operator[](const ConstContainerIndex<X,typename SRanges::IndexType...>& i);
T& operator[](const std::tuple<IPTR<typename SRanges::IndexType>...>& is);
virtual T& operator[](const IndexType& i) = 0;
virtual T& operator[](const CIndexType& i) = 0;
virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0;
virtual T* data() = 0;
//virtual IndexType begin();
//virtual IndexType end();
virtual IndexType begin();
virtual IndexType end();
virtual bool isConst() const override;
//virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymousMove() = 0;
virtual ConstOperationRoot<T,SRanges...>
op(const std::shared_ptr<IndexType>& ind) const override;
op(const std::shared_ptr<CIndexType>& ind) const override;
virtual ConstOperationRoot<T,SRanges...>
operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const override;
virtual OperationRoot<T,SRanges...>
op(const std::shared_ptr<IndexType>& ind);
op(const std::shared_ptr<CIndexType>& ind);
virtual OperationRoot<T,SRanges...> operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds);

View file

@ -103,7 +103,7 @@ namespace {
EXPECT_EQ( ma.isConst(), false);
EXPECT_EQ( ma.isSlice(), false);
auto i = ma.beginIndex();
auto i = ma.cbegin();
EXPECT_EQ( ma[ i.at('x') ], 3.141);
EXPECT_EQ( ma[ i.at('y') ], 2.718);
EXPECT_EQ( ma[ i.at('l') ], 1.618);
@ -143,11 +143,11 @@ namespace {
std::shared_ptr<SRange> sr2 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
MultiArray<double,MATest_1Dim::SRange> ma(srptr, vv);
auto i = ma.beginIndex();
auto i = ma.cbegin();
EXPECT_EQ( ma[ i.at('x') ], 3.141);
auto ma2 = ma.format( sr2 );
auto j = ma2.beginIndex();
auto j = ma2.cbegin();
EXPECT_EQ( ma[ j.at('a') ], 3.141);
EXPECT_EQ( ma[ j.at('c') ], 2.718);
@ -162,7 +162,7 @@ namespace {
EXPECT_EQ( ma.size(), 24u );
EXPECT_EQ( ma.range()->dim(), 2u );
auto i = ma.beginIndex();
auto i = ma.cbegin();
EXPECT_EQ( ma[ i.at( mkt( mkt('x', 'a'), '1' ) ) ], 2.917);
EXPECT_EQ( ma[ i.at( mkt( mkt('x', 'a'), '2' ) ) ], 9.436);
@ -178,7 +178,7 @@ namespace {
MultiArray<double,MATest_MDim::MRange,MATest_MDim::SRange> ma(mrptr, sr3ptr, vv);
auto ma2 = ma.format( sr4ptr );
auto i = ma2.beginIndex();
auto i = ma2.cbegin();
EXPECT_EQ( ma2.at('A') , 2.917 );
EXPECT_EQ( ma2[ i.at('G') ], 4.892 );
EXPECT_EQ( ma2.at('J') , 4.790 );