some awkward hacks reg const stuff... should refactor this

This commit is contained in:
Christian Zimmermann 2017-03-16 22:42:55 +01:00
parent ed53e4b3c7
commit 2022d743c6
6 changed files with 149 additions and 23 deletions

View file

@ -62,7 +62,7 @@ namespace MultiArrayTools
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
{ {
return MultiArrayOperationRoot<T,Range>(*this, Name("master", str...)); return ConstMultiArrayOperationRoot<T,Range>(*this, Name("master", str...));
} }
template <typename T, class Range> template <typename T, class Range>
@ -158,6 +158,12 @@ namespace MultiArrayTools
return mCont[ i.pos() ]; return mCont[ i.pos() ];
} }
template <typename T, class Range>
bool MultiArray<T,Range>::isConst() const
{
return false;
}
template <typename T, class Range> template <typename T, class Range>
bool MultiArray<T,Range>::isSlice() const bool MultiArray<T,Range>::isSlice() const
{ {

View file

@ -38,6 +38,8 @@ namespace MultiArrayTools
virtual const Range& range() const; virtual const Range& range() const;
virtual bool isConst() const = 0;
template <class... NameTypes> template <class... NameTypes>
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str); MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
@ -78,7 +80,8 @@ namespace MultiArrayTools
T& operator[](const typename Range::IndexType& i) override; T& operator[](const typename Range::IndexType& i) override;
const T& operator[](const typename Range::IndexType& i) const override; const T& operator[](const typename Range::IndexType& i) const override;
virtual bool isConst() const override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
// virtual void manipulate(ManipulatorBase<T>& mb, // virtual void manipulate(ManipulatorBase<T>& mb,

View file

@ -40,7 +40,7 @@ namespace MultiArrayTools
IndexType& iref = dynamic_cast<IndexType&>(*MAOB::mIibPtr); IndexType& iref = dynamic_cast<IndexType&>(*MAOB::mIibPtr);
//CHECK; //CHECK;
const size_t endPos = mArrayRef.end().pos(); const size_t endPos = mArrayRef.end().pos();
std::cout << "assignment: " << endPos << "elements" << std::endl; std::cout << "assignment: " << endPos << " elements" << std::endl;
for(iref = mArrayRef.begin().pos(); iref != mArrayRef.end(); ++iref){ for(iref = mArrayRef.begin().pos(); iref != mArrayRef.end(); ++iref){
std::cout << iref.pos() << '\r' << std::flush; std::cout << iref.pos() << '\r' << std::flush;
get() = in.get(); get() = in.get();
@ -59,16 +59,17 @@ namespace MultiArrayTools
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class Range>
template <class RangeX> template <class RangeX>
const MultiArrayOperationRoot<T,Range>& const MultiArrayOperationRoot<T,Range>&
MultiArrayOperationRoot<T,Range>::makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in) MultiArrayOperationRoot<T,Range>::makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in)
{ {
ConstSlice<T,Range,RangeX>& sl = dynamic_cast<ConstSlice<T,Range,RangeX>&>( mArrayRef ); Slice<T,Range,RangeX>& sl = dynamic_cast<Slice<T,Range,RangeX>&>( mArrayRef );
sl.set(in.mArrayRef, name(), dynamic_cast<const typename RangeX::IndexType&>( in.index() ), in.name()); sl.setConst(in.mArrayRef, name(), dynamic_cast<const typename RangeX::IndexType&>( in.index() ), in.name());
return *this; return *this;
} }
// CONST SLICE !!!!! // CONST SLICE !!!!!
@ -91,6 +92,7 @@ namespace MultiArrayTools
MultiArrayOperationRoot<T,Range>::operator=(const MultiArrayOperationRoot<T,Range>& in) MultiArrayOperationRoot<T,Range>::operator=(const MultiArrayOperationRoot<T,Range>& in)
{ {
performAssignment(in); performAssignment(in);
freeIndex();
return *this; return *this;
} }
@ -104,6 +106,7 @@ namespace MultiArrayTools
return makeSlice(in); return makeSlice(in);
} }
performAssignment(in); performAssignment(in);
freeIndex();
return *this; return *this;
} }
@ -117,9 +120,11 @@ namespace MultiArrayTools
return makeSlice(in); return makeSlice(in);
} }
performAssignment(in); performAssignment(in);
freeIndex();
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class Range>
template <class Range2> template <class Range2>
const MultiArrayOperationRoot<T,Range>& const MultiArrayOperationRoot<T,Range>&
@ -131,6 +136,7 @@ namespace MultiArrayTools
return makeConstSlice(in); return makeConstSlice(in);
} }
performAssignment(in); performAssignment(in);
freeIndex();
return *this; return *this;
} }
@ -146,6 +152,7 @@ namespace MultiArrayTools
assert(0); assert(0);
} }
performAssignment(in); performAssignment(in);
freeIndex();
return *this; return *this;
} }
@ -301,17 +308,28 @@ namespace MultiArrayTools
/************************************** /**************************************
* ConstMultiArrayOperationBase * * ConstMultiArrayOperationBase *
**************************************/ **************************************/
/*
template <typename T, class Range> template <typename T, class Range>
template <class RangeX> template <class RangeX>
const ConstMultiArrayOperationRoot<T,Range>& const ConstMultiArrayOperationRoot<T,Range>&
ConstMultiArrayOperationRoot<T,Range>::makeConstSlice(const ConstMultiArrayOperationRoot<T,RangeX>& in) const ConstMultiArrayOperationRoot<T,Range>::makeConstSlice(const ConstMultiArrayOperationRoot<T,RangeX>& in)
{ {
ConstSlice<T,Range,RangeX>& sl = dynamic_cast<ConstSlice<T,Range,RangeX>&>( mArrayRef ); ConstSlice<T,Range,RangeX>& sl = dynamic_cast<ConstSlice<T,Range,RangeX>&>( mArrayRef );
sl.set(in.mArrayRef, name(), dynamic_cast<const typename RangeX::IndexType&>( in.index() ), in.name()); sl.set(in.mArrayRef, name(), dynamic_cast<const typename RangeX::IndexType&>( in.index() ), in.name());
return *this; return *this;
} }
template <typename T, class Range>
template <class RangeX>
const ConstMultiArrayOperationRoot<T,Range>&
ConstMultiArrayOperationRoot<T,Range>::makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in)
{
ConstSlice<T,Range,RangeX>& sl = dynamic_cast<ConstSlice<T,Range,RangeX>&>( mArrayRef );
sl.set(in.mArrayRef, name(), dynamic_cast<const typename RangeX::IndexType&>( in.index() ), in.name());
return *this;
}
*/
// CONST SLICE !!!!! // CONST SLICE !!!!!
@ -341,7 +359,8 @@ namespace MultiArrayTools
MAOB::mIibPtr->name(mNm); MAOB::mIibPtr->name(mNm);
//mIndex.name(nm); //mIndex.name(nm);
} }
/*
template <typename T, class Range> template <typename T, class Range>
const ConstMultiArrayOperationRoot<T,Range>& const ConstMultiArrayOperationRoot<T,Range>&
ConstMultiArrayOperationRoot<T,Range>::operator=(const ConstMultiArrayOperationRoot<T,Range>& in) ConstMultiArrayOperationRoot<T,Range>::operator=(const ConstMultiArrayOperationRoot<T,Range>& in)
@ -368,6 +387,20 @@ namespace MultiArrayTools
return *this; return *this;
} }
template <typename T, class Range>
template <class Range2>
const ConstMultiArrayOperationRoot<T,Range>&
ConstMultiArrayOperationRoot<T,Range>::operator=(const MultiArrayOperationRoot<T,Range2>& in)
{
//CHECK;
if(mArrayRef.isSlice() and not mArrayRef.isInit()){
//CHECK;
return makeConstSlice(in);
}
assert(0);
return *this;
}
*/
template <typename T, class Range> template <typename T, class Range>
template <class Operation, class... MAOps> template <class Operation, class... MAOps>
MultiArrayOperation<T,Operation,ConstMultiArrayOperationRoot<T,Range>, MAOps...> MultiArrayOperation<T,Operation,ConstMultiArrayOperationRoot<T,Range>, MAOps...>
@ -444,7 +477,6 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
const T& ConstMultiArrayOperationRoot<T,Range>::get() const const T& ConstMultiArrayOperationRoot<T,Range>::get() const
{ {
//return mArrayRef[mIndex];
return mArrayRef[*dynamic_cast<IndexType*>(MAOB::mIibPtr)]; return mArrayRef[*dynamic_cast<IndexType*>(MAOB::mIibPtr)];
} }

View file

@ -59,6 +59,7 @@ namespace MultiArrayTools
template <class Range2> template <class Range2>
MultiArrayOperationRoot& operator=(MultiArrayOperationRoot<T,Range2>& in); MultiArrayOperationRoot& operator=(MultiArrayOperationRoot<T,Range2>& in);
template <class Range2> template <class Range2>
const MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot<T,Range2>& in); const MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot<T,Range2>& in);
@ -122,6 +123,9 @@ namespace MultiArrayTools
template <typename U, class RangeX> template <typename U, class RangeX>
friend class MultiArrayOperationRoot; friend class MultiArrayOperationRoot;
template <typename U, class RangeX>
friend class ConstMultiArrayOperationRoot;
protected: protected:
@ -130,6 +134,7 @@ namespace MultiArrayTools
template <class RangeX> template <class RangeX>
MultiArrayOperationRoot& makeSlice(MultiArrayOperationRoot<T,RangeX>& in); MultiArrayOperationRoot& makeSlice(MultiArrayOperationRoot<T,RangeX>& in);
template <class RangeX> template <class RangeX>
const MultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in); const MultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in);
@ -149,12 +154,17 @@ namespace MultiArrayTools
ConstMultiArrayOperationRoot(const MultiArrayBase<T,Range>& ma, const Name& nm); ConstMultiArrayOperationRoot(const MultiArrayBase<T,Range>& ma, const Name& nm);
ConstMultiArrayOperationRoot(const MultiArrayOperationRoot<T,Range>& in); ConstMultiArrayOperationRoot(const MultiArrayOperationRoot<T,Range>& in);
/*
const ConstMultiArrayOperationRoot& operator=(const ConstMultiArrayOperationRoot& in); const ConstMultiArrayOperationRoot& operator=(const ConstMultiArrayOperationRoot& in);
template <class Range2> template <class Range2>
const ConstMultiArrayOperationRoot& operator=(const ConstMultiArrayOperationRoot<T,Range2>& in); const ConstMultiArrayOperationRoot& operator=(const ConstMultiArrayOperationRoot<T,Range2>& in);
template <class Range2>
const ConstMultiArrayOperationRoot& operator=(const MultiArrayOperationRoot<T,Range2>& in);
*/
//template <class Operation, class... MAOps> //template <class Operation, class... MAOps>
//MultiArrayOperation<T,Operation,MultiArrayOperationRoot<T,Range>, MAOps...> //MultiArrayOperation<T,Operation,MultiArrayOperationRoot<T,Range>, MAOps...>
//operator()(Operation& op, const MAOps&... secs) const; //operator()(Operation& op, const MAOps&... secs) const;
@ -196,14 +206,22 @@ namespace MultiArrayTools
const MultiArrayBase<T,Range>& getCont() const { return mArrayRef; } const MultiArrayBase<T,Range>& getCont() const { return mArrayRef; }
template <typename U, class RangeX>
friend class ConstMultiArrayOperationRoot;
template <typename U, class RangeX> template <typename U, class RangeX>
friend class MultiArrayOperationRoot; friend class MultiArrayOperationRoot;
protected: protected:
/*
template <class RangeX> template <class RangeX>
const ConstMultiArrayOperationRoot& makeConstSlice(const ConstMultiArrayOperationRoot<T,RangeX>& in) const; const ConstMultiArrayOperationRoot& makeConstSlice(const ConstMultiArrayOperationRoot<T,RangeX>& in);
template <class RangeX>
const ConstMultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot<T,RangeX>& in);
*/
// const
MultiArrayBase<T,Range> const& mArrayRef; MultiArrayBase<T,Range> const& mArrayRef;
mutable IndexType mIndex; mutable IndexType mIndex;
Name mNm; Name mNm;

View file

@ -23,6 +23,8 @@ namespace MultiArrayTools
const Name& MANm) // for correct linkage) const Name& MANm) // for correct linkage)
{ {
MAB::mInit = true; MAB::mInit = true;
mOwnName = ownNm;
mMAName = MANm;
mMultiArrayPtr = &multiArrayRef; mMultiArrayPtr = &multiArrayRef;
mMAIdx = MAIdx; mMAIdx = MAIdx;
mOwnIdx = MAB::mRange->begin(); mOwnIdx = MAB::mRange->begin();
@ -31,6 +33,24 @@ namespace MultiArrayTools
mMAIdx.linkTo(&mOwnIdx); 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
const typename MARange::IndexType& MAIdx, // for desired slice position
const Name& MANm) // for correct linkage)
{
MAB::mInit = true;
mOwnName = ownNm;
mMAName = MANm;
mConstMultiArrayPtr = &multiArrayRef;
mMAIdx = MAIdx;
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>
auto Slice<T,Range,MARange>::begin() const -> decltype(Range().begin()) auto Slice<T,Range,MARange>::begin() const -> decltype(Range().begin())
{ {
@ -58,23 +78,55 @@ namespace MultiArrayTools
//mOwnIdx = i.pos(); //mOwnIdx = i.pos();
return (*mMultiArrayPtr)[ mMAIdx ]; return (*mMultiArrayPtr)[ mMAIdx ];
} }
template <typename T, class Range, class MARange>
bool Slice<T,Range,MARange>::isConst() const
{
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 Slice<T,Range,MARange>& slice) :
MultiArrayBase<T,Range>(slice.range()),
mOwnIdx(slice.mOwnIdx),
mMAIdx(slice.mMAIdx)
{
if(slice.mInit){
MAB::mInit = true;
mMultiArrayPtr = slice.mConstMultiArrayPtr;
mMAIdx = slice.mMAIdx;
mOwnIdx = MAB::mRange->begin();
mMAIdx.name(slice.mMAName);
mOwnIdx.name(slice.mOwnName);
mMAIdx.linkTo(&mOwnIdx);
}
}
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
bool ConstSlice<T,Range,MARange>::isSlice() const bool ConstSlice<T,Range,MARange>::isSlice() const
{ {
return true; return true;
} }
template <typename T, class Range, class MARange>
bool ConstSlice<T,Range,MARange>::isConst() const
{
return true;
}
/*
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
void ConstSlice<T,Range,MARange>::set(const MultiArrayBase<T,MARange>& multiArrayRef, void ConstSlice<T,Range,MARange>::set(const MultiArrayBase<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) const // for correct linkage)
{ {
MAB::mInit = true; MAB::mInit = true;
mMultiArrayPtr = &multiArrayRef; mMultiArrayPtr = &multiArrayRef;
@ -84,7 +136,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 ConstSlice<T,Range,MARange>::begin() const -> decltype(Range().begin()) auto ConstSlice<T,Range,MARange>::begin() const -> decltype(Range().begin())
{ {

View file

@ -33,17 +33,29 @@ namespace MultiArrayTools
virtual auto end() const -> decltype(Range().end()) override; virtual auto end() const -> decltype(Range().end()) override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
virtual bool isConst() const override;
void set(MultiArrayBase<T,MARange>& multiArrayRef, void set(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);
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: private:
MultiArrayBase<T,MARange> const* mConstMultiArrayPtr = nullptr;
MultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr; MultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr;
mutable typename Range::IndexType mOwnIdx; mutable typename Range::IndexType mOwnIdx;
mutable typename MARange::IndexType mMAIdx; mutable typename MARange::IndexType mMAIdx;
Name mOwnName;
Name mMAName;
}; };
template <typename T, class Range, class MARange/*, class Index*/> template <typename T, class Range, class MARange/*, class Index*/>
@ -54,6 +66,7 @@ namespace MultiArrayTools
typedef MultiArrayBase<T,Range> MAB; typedef MultiArrayBase<T,Range> MAB;
ConstSlice(const Range& range); ConstSlice(const Range& range);
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;
virtual const T& operator[](const typename Range::IndexType& i) const override; virtual const T& operator[](const typename Range::IndexType& i) const override;
@ -65,12 +78,14 @@ namespace MultiArrayTools
virtual auto end() const -> decltype(Range().end()) override; virtual auto end() const -> decltype(Range().end()) override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
virtual bool isConst() const override;
/*
void set(const MultiArrayBase<T,MARange>& multiArrayRef, void set(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) const;
*/
private: private:
// !!! // !!!