mutable vs const multi array stuff

This commit is contained in:
Christian Zimmermann 2017-03-20 11:21:33 +01:00
parent 2022d743c6
commit 7a02d0f6b8
8 changed files with 152 additions and 58 deletions

View file

@ -84,6 +84,10 @@ namespace MultiArrayTools
// multi_array.h // multi_array.h
template <typename T, class Range> template <typename T, class Range>
class MultiArrayBase; class MultiArrayBase;
// multi_array.h
template <typename T, class Range>
class MutableMultiArrayBase;
// multi_array.h // multi_array.h
template <typename T, class Range> template <typename T, class Range>

View file

@ -39,25 +39,11 @@ namespace MultiArrayTools
} }
template <typename T, class Range> template <typename T, class Range>
template <class... NameTypes> bool MultiArrayBase<T,Range>::isConst() const
MultiArrayOperationRoot<T,Range> MultiArrayBase<T,Range>::operator()(const NameTypes&... str)
{ {
return MultiArrayOperationRoot<T,Range>(*this, Name("master", str...)); return true;
} }
template <typename T, class Range>
template <class NameType>
MultiArrayOperationRoot<T,Range> MultiArrayBase<T,Range>::operator()(const NameType& name, bool master)
{
//CHECK;
if(master){
return MultiArrayOperationRoot<T,Range>(*this, name);
}
else {
return operator()(name);
}
}
template <typename T, class Range> template <typename T, class Range>
template <class... NameTypes> template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> MultiArrayBase<T,Range>::operator()(const NameTypes&... str) const ConstMultiArrayOperationRoot<T,Range> MultiArrayBase<T,Range>::operator()(const NameTypes&... str) const
@ -83,6 +69,40 @@ namespace MultiArrayTools
{ {
return mInit; return mInit;
} }
/******************************
* MutableMultiArrayBase *
******************************/
template <typename T, class Range>
MutableMultiArrayBase<T,Range>::MutableMultiArrayBase(const Range& range) : MultiArrayBase<T,Range>(range) {}
template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::isConst() const
{
return false;
}
template <typename T, class Range>
template <class... NameTypes>
MultiArrayOperationRoot<T,Range> MutableMultiArrayBase<T,Range>::operator()(const NameTypes&... str)
{
return MultiArrayOperationRoot<T,Range>(*this, Name("master", str...));
}
template <typename T, class Range>
template <class NameType>
MultiArrayOperationRoot<T,Range> MutableMultiArrayBase<T,Range>::operator()(const NameType& name, bool master)
{
//CHECK;
if(master){
return MultiArrayOperationRoot<T,Range>(*this, name);
}
else {
return operator()(name);
}
}
/******************* /*******************
* MultiArray * * MultiArray *
@ -90,7 +110,7 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
MultiArray<T,Range>::MultiArray(const Range& range) : MultiArray<T,Range>::MultiArray(const Range& range) :
MultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,Range>(range),
mCont(MAB::mRange->size()) mCont(MAB::mRange->size())
{ {
MAB::mInit = true; MAB::mInit = true;
@ -98,7 +118,7 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
MultiArray<T,Range>::MultiArray(const Range& range, const std::vector<T>& vec) : MultiArray<T,Range>::MultiArray(const Range& range, const std::vector<T>& vec) :
MultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,Range>(range),
mCont(vec) mCont(vec)
{ {
MAB::mInit = true; MAB::mInit = true;
@ -109,7 +129,7 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
MultiArray<T,Range>::MultiArray(const Range& range, std::vector<T>&& vec) : MultiArray<T,Range>::MultiArray(const Range& range, std::vector<T>&& vec) :
MultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,Range>(range),
mCont(vec) mCont(vec)
{ {
MAB::mInit = true; MAB::mInit = true;
@ -121,7 +141,7 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
template <class Range2, class Range3> template <class Range2, class Range3>
MultiArray<T,Range>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) : MultiArray<T,Range>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) :
MultiArrayBase<T,Range>(merge(in.range(), in[ in.begin() ].range())) MutableMultiArrayBase<T,Range>(merge(in.range(), in[ in.begin() ].range()))
// assert that Range2 has always same extension // assert that Range2 has always same extension
{ {
MAB::mInit = true; MAB::mInit = true;

View file

@ -27,7 +27,6 @@ namespace MultiArrayTools
virtual ~MultiArrayBase() = default; virtual ~MultiArrayBase() = default;
virtual T& operator[](const typename Range::IndexType& i) = 0;
virtual const T& operator[](const typename Range::IndexType& i) const = 0; virtual const T& operator[](const typename Range::IndexType& i) const = 0;
virtual size_t size() const; virtual size_t size() const;
@ -38,14 +37,8 @@ namespace MultiArrayTools
virtual const Range& range() const; virtual const Range& range() const;
virtual bool isConst() const = 0; virtual bool isConst() const;
template <class... NameTypes>
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
template <class NameType>
MultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master);
template <class... NameTypes> template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str) const; ConstMultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str) const;
@ -59,9 +52,31 @@ namespace MultiArrayTools
std::shared_ptr<Range> mRange; std::shared_ptr<Range> mRange;
}; };
template <typename T, class Range>
class MutableMultiArrayBase : public MultiArrayBase<T,Range>
{
public:
// iterator ( containing idx of Range )
DEFAULT_MEMBERS(MutableMultiArrayBase);
MutableMultiArrayBase(const Range& range);
virtual T& operator[](const typename Range::IndexType& i) = 0;
virtual bool isConst() const override;
template <class... NameTypes>
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
template <class NameType>
MultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master);
};
template <typename T, class Range> template <typename T, class Range>
class MultiArray : public MultiArrayBase<T,Range> class MultiArray : public MutableMultiArrayBase<T,Range>
{ {
public: public:

View file

@ -75,7 +75,7 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
MultiArrayOperationRoot<T,Range>:: MultiArrayOperationRoot<T,Range>::
MultiArrayOperationRoot(MultiArrayBase<T,Range>& ma, MultiArrayOperationRoot(MutableMultiArrayBase<T,Range>& ma,
const Name& nm) : const Name& nm) :
MutableMultiArrayOperationBase<T>(), MutableMultiArrayOperationBase<T>(),
mArrayRef(ma), mArrayRef(ma),

View file

@ -51,7 +51,7 @@ namespace MultiArrayTools
typedef typename Range::IndexType IndexType; typedef typename Range::IndexType IndexType;
//typedef decltype(MultiArray<T,Range>().begin()) IndexType; //typedef decltype(MultiArray<T,Range>().begin()) IndexType;
MultiArrayOperationRoot(MultiArrayBase<T,Range>& ma, const Name& nm); MultiArrayOperationRoot(MutableMultiArrayBase<T,Range>& ma, const Name& nm);
MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in); MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in);
MultiArrayOperationRoot& operator=(MultiArrayOperationRoot& in); MultiArrayOperationRoot& operator=(MultiArrayOperationRoot& in);
@ -138,7 +138,7 @@ namespace MultiArrayTools
template <class RangeX> template <class RangeX>
const MultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in); const MultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in);
MultiArrayBase<T,Range>& mArrayRef; MutableMultiArrayBase<T,Range>& mArrayRef;
mutable IndexType mIndex; mutable IndexType mIndex;
Name mNm; Name mNm;
}; };

View file

@ -7,8 +7,21 @@ namespace MultiArrayTools
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
Slice<T,Range,MARange>:: Slice<T,Range,MARange>::
Slice(const Range& range) : Slice(const Range& range,
MultiArrayBase<T,Range>(range) {} MutableMultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm, // for correct linkage
const typename MARange::IndexType& MAIdx, // for desired slice position
const Name& MANm) :
MutableMultiArrayBase<T,Range>(range),
mOwnName(ownNm), mMAName(MANm), mMultiArrayPtr(&multiArrayRef),
mMAIdx(MAIdx)
{
MAB::mInit = true;
mOwnIdx = MAB::mRange->begin();
mMAIdx.name(MANm);
mOwnIdx.name(ownNm);
mMAIdx.linkTo(&mOwnIdx);
}
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
bool Slice<T,Range,MARange>::isSlice() const bool Slice<T,Range,MARange>::isSlice() const
@ -16,8 +29,9 @@ namespace MultiArrayTools
return true; return true;
} }
/*
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
void Slice<T,Range,MARange>::set(MultiArrayBase<T,MARange>& multiArrayRef, void Slice<T,Range,MARange>::set(MutableMultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm, // for correct linkage const Name& ownNm, // for correct linkage
const typename MARange::IndexType& MAIdx, // for desired slice position const typename MARange::IndexType& MAIdx, // for desired slice position
const Name& MANm) // for correct linkage) const Name& MANm) // for correct linkage)
@ -32,7 +46,9 @@ namespace MultiArrayTools
mOwnIdx.name(ownNm); mOwnIdx.name(ownNm);
mMAIdx.linkTo(&mOwnIdx); mMAIdx.linkTo(&mOwnIdx);
} }
*/
/*
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
void Slice<T,Range,MARange>::setConst(const MultiArrayBase<T,MARange>& multiArrayRef, void Slice<T,Range,MARange>::setConst(const MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm, // for correct linkage const Name& ownNm, // for correct linkage
@ -49,7 +65,7 @@ namespace MultiArrayTools
mOwnIdx.name(ownNm); mOwnIdx.name(ownNm);
mMAIdx.linkTo(&mOwnIdx); mMAIdx.linkTo(&mOwnIdx);
} }
*/
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
auto Slice<T,Range,MARange>::begin() const -> decltype(Range().begin()) auto Slice<T,Range,MARange>::begin() const -> decltype(Range().begin())
@ -84,12 +100,32 @@ namespace MultiArrayTools
{ {
return false; return false;
} }
/*
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
ConstSlice<T,Range,MARange>:: ConstSlice<T,Range,MARange>::
ConstSlice(const Range& range) : ConstSlice(const Range& range) :
MultiArrayBase<T,Range>(range) {} MultiArrayBase<T,Range>(range) {}
*/
template <typename T, class Range, class MARange>
ConstSlice<T,Range,MARange>::
ConstSlice(const Range& range,
const MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm, // for correct linkage
const typename MARange::IndexType& MAIdx, // for desired slice position
const Name& MANm) :
MutableMultiArrayBase<T,Range>(range),
mMultiArrayPtr(&multiArrayRef),
mMAIdx(MAIdx)
{
MAB::mInit = true;
mOwnIdx = MAB::mRange->begin();
mMAIdx.name(MANm);
mOwnIdx.name(ownNm);
mMAIdx.linkTo(&mOwnIdx);
}
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
ConstSlice<T,Range,MARange>:: ConstSlice<T,Range,MARange>::
ConstSlice(const Slice<T,Range,MARange>& slice) : ConstSlice(const Slice<T,Range,MARange>& slice) :

View file

@ -15,14 +15,18 @@ namespace MultiArrayTools
// MARange = original range of multi array of which this is the slice // MARange = original range of multi array of which this is the slice
// Index = index which determines the slice position (remnant of MARange w.o. Range) // Index = index which determines the slice position (remnant of MARange w.o. Range)
template <typename T, class Range, class MARange/*, class Index*/> template <typename T, class Range, class MARange/*, class Index*/>
class Slice : public MultiArrayBase<T,Range> // yes, 'Range' is correct !!! class Slice : public MutableMultiArrayBase<T,Range> // yes, 'Range' is correct !!!
{ {
public: public:
typedef MultiArrayBase<T,Range> MAB; typedef MultiArrayBase<T,Range> MAB;
Slice(const Range& range); Slice(const Range& range,
MutableMultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm,
const typename MARange::IndexType& MAIdx,
const Name& MANm);
virtual T& operator[](const typename Range::IndexType& i) override; virtual T& operator[](const typename Range::IndexType& i) override;
virtual const T& operator[](const typename Range::IndexType& i) const override; virtual const T& operator[](const typename Range::IndexType& i) const override;
@ -34,28 +38,28 @@ namespace MultiArrayTools
virtual bool isSlice() const override; virtual bool isSlice() const override;
virtual bool isConst() const override; virtual bool isConst() const override;
void set(MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm,
const typename MARange::IndexType& MAIdx,
const Name& MANm);
/*
void set();
*/
/*
void setConst(const MultiArrayBase<T,MARange>& multiArrayRef, void setConst(const MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm, const Name& ownNm,
const typename MARange::IndexType& MAIdx, const typename MARange::IndexType& MAIdx,
const Name& MANm); const Name& MANm);
*/
template <typename U, class RangeX, class MARangeX> template <typename U, class RangeX, class MARangeX>
friend class ConstSlice; friend class ConstSlice;
private: private:
MultiArrayBase<T,MARange> const* mConstMultiArrayPtr = nullptr;
MultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr;
mutable typename Range::IndexType mOwnIdx;
mutable typename MARange::IndexType mMAIdx;
Name mOwnName; Name mOwnName;
Name mMAName; Name mMAName;
MutableMultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr;
mutable typename Range::IndexType mOwnIdx;
mutable typename MARange::IndexType mMAIdx;
}; };
template <typename T, class Range, class MARange/*, class Index*/> template <typename T, class Range, class MARange/*, class Index*/>
@ -65,7 +69,12 @@ namespace MultiArrayTools
typedef MultiArrayBase<T,Range> MAB; typedef MultiArrayBase<T,Range> MAB;
ConstSlice(const Range& range); //ConstSlice(const Range& range);
ConstSlice(const Range& range,
const MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm,
const typename MARange::IndexType& MAIdx,
const Name& MANm);
ConstSlice(const Slice<T,Range,MARange>& slice); ConstSlice(const Slice<T,Range,MARange>& slice);
virtual T& operator[](const typename Range::IndexType& i) override; virtual T& operator[](const typename Range::IndexType& i) override;

View file

@ -413,12 +413,20 @@ namespace {
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
Slice2d3dAny sl(ra); Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"),
sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; i(i1 = 0, i2 = 2, i3 = 0),
MAT::Name("master", "alpha","beta","gamma"));
//Slice2d3dAny sl(ra);
//sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)];
EXPECT_EQ(sl.size(), 6); EXPECT_EQ(sl.size(), 6);
Slice2d3dAny sl2(rb); Slice2d3dAny sl2(rb, ma, MAT::Name("master", "alpha", "beta"),
sl2("alpha","beta") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 0, i3 = 1)]; i(i1 = 0, i2 = 0, i3 = 1),
MAT::Name("master", "alpha","beta","gamma"));
//Slice2d3dAny sl2(rb);
//sl2("alpha","beta") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 0, i3 = 1)];
EXPECT_EQ(sl2.size(), 12); EXPECT_EQ(sl2.size(), 12);
} }
@ -428,8 +436,10 @@ namespace {
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
Slice2d3dAny sl(ra); Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"),
sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; i(i1 = 0, i2 = 2, i3 = 0),
MAT::Name("master", "alpha","beta","gamma"));
//sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)];
auto j = sl.begin(); auto j = sl.begin();
auto j1 = j.template getIndex<0>(); auto j1 = j.template getIndex<0>();