mutable vs const multi array stuff
This commit is contained in:
parent
2022d743c6
commit
7a02d0f6b8
8 changed files with 152 additions and 58 deletions
|
@ -85,6 +85,10 @@ namespace MultiArrayTools
|
|||
template <typename T, class Range>
|
||||
class MultiArrayBase;
|
||||
|
||||
// multi_array.h
|
||||
template <typename T, class Range>
|
||||
class MutableMultiArrayBase;
|
||||
|
||||
// multi_array.h
|
||||
template <typename T, class Range>
|
||||
class MultiArray;
|
||||
|
|
|
@ -39,23 +39,9 @@ namespace MultiArrayTools
|
|||
}
|
||||
|
||||
template <typename T, class Range>
|
||||
template <class... NameTypes>
|
||||
MultiArrayOperationRoot<T,Range> MultiArrayBase<T,Range>::operator()(const NameTypes&... str)
|
||||
bool MultiArrayBase<T,Range>::isConst() const
|
||||
{
|
||||
return MultiArrayOperationRoot<T,Range>(*this, Name("master", str...));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, class Range>
|
||||
|
@ -84,13 +70,47 @@ namespace MultiArrayTools
|
|||
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 *
|
||||
*******************/
|
||||
|
||||
template <typename T, class Range>
|
||||
MultiArray<T,Range>::MultiArray(const Range& range) :
|
||||
MultiArrayBase<T,Range>(range),
|
||||
MutableMultiArrayBase<T,Range>(range),
|
||||
mCont(MAB::mRange->size())
|
||||
{
|
||||
MAB::mInit = true;
|
||||
|
@ -98,7 +118,7 @@ namespace MultiArrayTools
|
|||
|
||||
template <typename T, class Range>
|
||||
MultiArray<T,Range>::MultiArray(const Range& range, const std::vector<T>& vec) :
|
||||
MultiArrayBase<T,Range>(range),
|
||||
MutableMultiArrayBase<T,Range>(range),
|
||||
mCont(vec)
|
||||
{
|
||||
MAB::mInit = true;
|
||||
|
@ -109,7 +129,7 @@ namespace MultiArrayTools
|
|||
|
||||
template <typename T, class Range>
|
||||
MultiArray<T,Range>::MultiArray(const Range& range, std::vector<T>&& vec) :
|
||||
MultiArrayBase<T,Range>(range),
|
||||
MutableMultiArrayBase<T,Range>(range),
|
||||
mCont(vec)
|
||||
{
|
||||
MAB::mInit = true;
|
||||
|
@ -121,7 +141,7 @@ namespace MultiArrayTools
|
|||
template <typename T, class Range>
|
||||
template <class Range2, class Range3>
|
||||
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
|
||||
{
|
||||
MAB::mInit = true;
|
||||
|
|
|
@ -27,7 +27,6 @@ namespace MultiArrayTools
|
|||
|
||||
virtual ~MultiArrayBase() = default;
|
||||
|
||||
virtual T& operator[](const typename Range::IndexType& i) = 0;
|
||||
virtual const T& operator[](const typename Range::IndexType& i) const = 0;
|
||||
|
||||
virtual size_t size() const;
|
||||
|
@ -38,13 +37,7 @@ namespace MultiArrayTools
|
|||
|
||||
virtual const Range& range() const;
|
||||
|
||||
virtual bool isConst() const = 0;
|
||||
|
||||
template <class... NameTypes>
|
||||
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
|
||||
|
||||
template <class NameType>
|
||||
MultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master);
|
||||
virtual bool isConst() const;
|
||||
|
||||
template <class... NameTypes>
|
||||
ConstMultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str) const;
|
||||
|
@ -61,7 +54,29 @@ namespace MultiArrayTools
|
|||
};
|
||||
|
||||
template <typename T, class Range>
|
||||
class MultiArray : public MultiArrayBase<T,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>
|
||||
class MultiArray : public MutableMultiArrayBase<T,Range>
|
||||
{
|
||||
public:
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ namespace MultiArrayTools
|
|||
|
||||
template <typename T, class Range>
|
||||
MultiArrayOperationRoot<T,Range>::
|
||||
MultiArrayOperationRoot(MultiArrayBase<T,Range>& ma,
|
||||
MultiArrayOperationRoot(MutableMultiArrayBase<T,Range>& ma,
|
||||
const Name& nm) :
|
||||
MutableMultiArrayOperationBase<T>(),
|
||||
mArrayRef(ma),
|
||||
|
|
|
@ -51,7 +51,7 @@ namespace MultiArrayTools
|
|||
typedef typename Range::IndexType 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=(MultiArrayOperationRoot& in);
|
||||
|
@ -138,7 +138,7 @@ namespace MultiArrayTools
|
|||
template <class RangeX>
|
||||
const MultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in);
|
||||
|
||||
MultiArrayBase<T,Range>& mArrayRef;
|
||||
MutableMultiArrayBase<T,Range>& mArrayRef;
|
||||
mutable IndexType mIndex;
|
||||
Name mNm;
|
||||
};
|
||||
|
|
44
src/slice.cc
44
src/slice.cc
|
@ -7,8 +7,21 @@ namespace MultiArrayTools
|
|||
|
||||
template <typename T, class Range, class MARange>
|
||||
Slice<T,Range,MARange>::
|
||||
Slice(const Range& range) :
|
||||
MultiArrayBase<T,Range>(range) {}
|
||||
Slice(const 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>
|
||||
bool Slice<T,Range,MARange>::isSlice() const
|
||||
|
@ -16,8 +29,9 @@ namespace MultiArrayTools
|
|||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
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 typename MARange::IndexType& MAIdx, // for desired slice position
|
||||
const Name& MANm) // for correct linkage)
|
||||
|
@ -32,7 +46,9 @@ namespace MultiArrayTools
|
|||
mOwnIdx.name(ownNm);
|
||||
mMAIdx.linkTo(&mOwnIdx);
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
template <typename T, class Range, class MARange>
|
||||
void Slice<T,Range,MARange>::setConst(const MultiArrayBase<T,MARange>& multiArrayRef,
|
||||
const Name& ownNm, // for correct linkage
|
||||
|
@ -49,7 +65,7 @@ namespace MultiArrayTools
|
|||
mOwnIdx.name(ownNm);
|
||||
mMAIdx.linkTo(&mOwnIdx);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
template <typename T, class Range, class MARange>
|
||||
auto Slice<T,Range,MARange>::begin() const -> decltype(Range().begin())
|
||||
|
@ -85,10 +101,30 @@ namespace MultiArrayTools
|
|||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
template <typename T, class Range, class MARange>
|
||||
ConstSlice<T,Range,MARange>::
|
||||
ConstSlice(const 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>
|
||||
ConstSlice<T,Range,MARange>::
|
||||
|
|
33
src/slice.h
33
src/slice.h
|
@ -15,13 +15,17 @@ namespace MultiArrayTools
|
|||
// 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)
|
||||
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:
|
||||
|
||||
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 const T& operator[](const typename Range::IndexType& i) const override;
|
||||
|
@ -35,27 +39,27 @@ namespace MultiArrayTools
|
|||
virtual bool isSlice() 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,
|
||||
const Name& ownNm,
|
||||
const typename MARange::IndexType& MAIdx,
|
||||
const Name& MANm);
|
||||
|
||||
*/
|
||||
template <typename U, class RangeX, class MARangeX>
|
||||
friend class ConstSlice;
|
||||
|
||||
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 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*/>
|
||||
|
@ -65,7 +69,12 @@ namespace MultiArrayTools
|
|||
|
||||
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);
|
||||
|
||||
virtual T& operator[](const typename Range::IndexType& i) override;
|
||||
|
|
|
@ -413,12 +413,20 @@ namespace {
|
|||
auto i2 = i.template getIndex<1>();
|
||||
auto i3 = i.template getIndex<2>();
|
||||
|
||||
Slice2d3dAny sl(ra);
|
||||
sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)];
|
||||
Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"),
|
||||
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);
|
||||
|
||||
Slice2d3dAny sl2(rb);
|
||||
sl2("alpha","beta") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 0, i3 = 1)];
|
||||
Slice2d3dAny sl2(rb, ma, MAT::Name("master", "alpha", "beta"),
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -428,8 +436,10 @@ namespace {
|
|||
auto i1 = i.template getIndex<0>();
|
||||
auto i2 = i.template getIndex<1>();
|
||||
auto i3 = i.template getIndex<2>();
|
||||
Slice2d3dAny sl(ra);
|
||||
sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)];
|
||||
Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"),
|
||||
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 j1 = j.template getIndex<0>();
|
||||
|
|
Loading…
Reference in a new issue