continuing...

This commit is contained in:
Christian Zimmermann 2017-05-22 18:21:14 +02:00
parent 030d26a3bb
commit 3fde2b9f66
5 changed files with 120 additions and 209 deletions

View file

@ -9,10 +9,7 @@ namespace MultiArrayTools
IndefinitIndexBase::~IndefinitIndexBase()
{
//freeLinked();
mLinked = nullptr;
mMajor = nullptr;
mSoftLinked = nullptr;
}
size_t IndefinitIndexBase::pos() const
@ -21,42 +18,26 @@ namespace MultiArrayTools
return static_cast<size_t>( mPos );
}
const std::string& IndefinitIndexBase::name() const
{
//assert(not virt());
return mName;
}
void IndefinitIndexBase::name(const std::string& str)
{
//assert(not virt());
mName = str;
}
void IndefinitIndexBase::name(const Name& nm)
{
//assert(not virt());
mName = nm.own();
}
void IndefinitIndexBase::setPos(size_t pos)
{
//CHECK;
//assert(not virt());
mPos = pos;
if(linked()){
mLinked->setPos(mPos);
mLinked->evalMajor();
}
}
void IndefinitIndexBase::setPosRel(int relPos)
{
mPos += relPos;
if(linked()){
mLinked->setPosRel(relPos);
mLinked->evalMajor(relPos);
}
// MAJOR INDEX UPDATE !!!!!
void IndefinitIndexBase::toFirst()
{
mPos = 0;
}
void IndefinitIndexBase::toLast()
{
mPos = max() - 1;
}
int IndefinitIndexBase::outOfRange() const
@ -72,6 +53,11 @@ namespace MultiArrayTools
}
}
bool atEdge() const
{
return mPos == max();
}
bool IndefinitIndexBase::toNull() const
{
//assert(not virt());

View file

@ -33,18 +33,17 @@ namespace MultiArrayTools
virtual size_t dim() const = 0;
virtual size_t pos() const;
virtual const std::string& name() const;
virtual void name(const std::string& str);
virtual void name(const Name& nm);
virtual MultiRangeType rangeType() const = 0;
virtual void setPos(size_t pos);
virtual void setPosRel(int relPos);
virtual IndefinitIndexBase& toFirst();
virtual IndefinitIndexBase& toLast();
virtual size_t max() const = 0;
virtual int outOfRange() const;
virtual bool atEdge() const;
virtual bool toNull() const;
virtual void eval() = 0;
@ -55,17 +54,10 @@ namespace MultiArrayTools
virtual void subOrd(IndefinitIndexBase* major);
virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0;
virtual size_t majorStep() const;
//virtual bool virt() const { return true; }
protected:
std::string mName;
int mPos;
size_t mMajorStep;
IndefinitIndexBase* mMajor = nullptr;
std::map<IndefinitIndexBase*,size_t> mMajor;
};
template <class Index>

View file

@ -10,6 +10,7 @@
#include <algorithm>
#include "base_def.h"
#include "multi_range.h"
#include "multi_array_operation.h"
#include "manipulator.h"
#include "name.h"
@ -17,11 +18,15 @@
namespace MultiArrayTools
{
template <typename T, class Range>
template <class... Ranges>
using TopRange = MultiRange<Ranges...>;
template <typename T, class... Ranges>
class MultiArrayBase
{
public:
typedef TopRange<Ranges...> TopRangeType
typedef T value_type;
class const_iterator : public std::iterator<std::random_access_iterator_tag,T>
@ -31,7 +36,7 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(const_iterator);
const_iterator(const MultiArrayBase& ma);
const_iterator(const MultiArrayBase& ma, const typename Range::IndexType& index);
const_iterator(const MultiArrayBase& ma, const typename TopRangeType::IndexType& index);
virtual ~const_iterator() = default;
// Requirements:
@ -62,23 +67,23 @@ namespace MultiArrayTools
// Multi Array specific:
const typename Range::IndexType& index() const;
typename Range::IndexType& index();
const typename TopRangeType::IndexType& index() const;
typename TopRangeType::IndexType& index();
protected:
MultiArrayBase const* mMAPtr = nullptr;
typename Range::IndexType mIndex;
typename TopRangeType::IndexType mIndex;
};
DEFAULT_MEMBERS(MultiArrayBase);
MultiArrayBase(const Range& range);
MultiArrayBase(const TopRangeType& range);
virtual ~MultiArrayBase() = default;
// only relevant for slices... has no effect for usual multiarrays
virtual void link(IndefinitIndexBase* iibPtr) const;
virtual const T& operator[](const typename Range::IndexType& i) const = 0;
virtual const T& operator[](const typename TopRangeType::IndexType& i) const = 0;
virtual size_t size() const;
virtual bool isSlice() const = 0;
@ -89,31 +94,33 @@ namespace MultiArrayTools
virtual auto beginIndex() const -> decltype(Range().begin());
virtual auto endIndex() const -> decltype(Range().end());
virtual const Range& range() const;
virtual const TopRangeType& range() const;
virtual bool isConst() const;
template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str) const;
ConstMultiArrayOperationRoot<T,TopRangeType> operator()(const NameTypes&... str) const;
template <class NameType>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master) const;
ConstMultiArrayOperationRoot<T,TopRangeType> operator()(const NameType& name, bool master) const;
virtual bool isInit() const;
protected:
bool mInit = false;
std::shared_ptr<Range> mRange;
std::shared_ptr<TopRangeType> mRange;
};
template <typename T, class Range>
class MutableMultiArrayBase : public MultiArrayBase<T,Range>
template <typename T, class... Ranges>
class MutableMultiArrayBase : public MultiArrayBase<T,Ranges...>
{
public:
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
typedef MultiArrayBase<T,Range> MAB;
typedef typename MultiArrayBase<T,Ranges...>::const_iterator const_iterator;
typedef MultiArrayBase<T,Ranges...> MAB;
typedef typename MAB::TopRangeType TopRangeType;
typedef typename TopRangeType::IndexType IndexType;
class iterator : public std::iterator<std::random_access_iterator_tag,T>,
public std::iterator<std::output_iterator_tag,T>
@ -123,7 +130,7 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(iterator);
iterator(MutableMultiArrayBase& ma);
iterator(MutableMultiArrayBase& ma, const typename Range::IndexType& index);
iterator(MutableMultiArrayBase& ma, const IndexType& index);
virtual ~iterator() = default;
// Requirements:
@ -157,19 +164,19 @@ namespace MultiArrayTools
// Multi Array specific:
const typename Range::IndexType& index() const;
typename Range::IndexType& index();
const IndexType& index() const;
IndexType& index();
protected:
MutableMultiArrayBase* mMAPtr = nullptr;
typename Range::IndexType mIndex;
IndexType mIndex;
};
DEFAULT_MEMBERS(MutableMultiArrayBase);
MutableMultiArrayBase(const Range& range);
MutableMultiArrayBase(const TopRangeType& range);
virtual T& operator[](const typename Range::IndexType& i) = 0;
virtual T& operator[](const IndexType& i) = 0;
virtual iterator begin();
virtual iterator end();
@ -177,38 +184,40 @@ namespace MultiArrayTools
virtual bool isConst() const override;
template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> operator()(bool x, const NameTypes&... str) const
ConstMultiArrayOperationRoot<T,Ranges...> operator()(bool x, const NameTypes&... str) const
{
return MAB::operator()(str...);
}
template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str) const;
ConstMultiArrayOperationRoot<T,Ranges...> operator()(const NameTypes&... str) const;
template <class NameType>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master) const;
ConstMultiArrayOperationRoot<T,Ranges...> operator()(const NameType& name, bool master) const;
template <class... NameTypes>
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
MultiArrayOperationRoot<T,Ranges...> operator()(const NameTypes&... str);
template <class NameType>
MultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master);
MultiArrayOperationRoot<T,Ranges...> operator()(const NameType& name, bool master);
};
template <typename T, class Range>
class MultiArray : public MutableMultiArrayBase<T,Range>
template <typename T, class Ranges...>
class MultiArray : public MutableMultiArrayBase<T,Ranges...>
{
public:
typedef MultiArrayBase<T,Range> MAB;
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
typedef typename MutableMultiArrayBase<T,Range>::iterator iterator;
typedef MultiArrayBase<T,Ranges...> MAB;
typedef typename MultiArrayBase<T,Ranges...>::const_iterator const_iterator;
typedef typename MutableMultiArrayBase<T,Ranges...>::iterator iterator;
typedef typename MAB::TopRangeType TopRangeType;
typedef typename TopRangeType::IndexType IndexType;
DEFAULT_MEMBERS(MultiArray);
MultiArray(const Range& range);
MultiArray(const Range& range, const std::vector<T>& vec);
MultiArray(const Range& range, std::vector<T>&& vec);
MultiArray(const TopRangeType& range);
MultiArray(const TopRangeType& range, const std::vector<T>& vec);
MultiArray(const TopRangeType& range, std::vector<T>&& vec);
template <class Range2, class Range3>
MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in);
@ -218,8 +227,8 @@ namespace MultiArrayTools
template <class Range2, class Range3>
MultiArray& operator=(const MultiArray<MultiArray<T,Range2>,Range3> in);
T& operator[](const typename Range::IndexType& i) override;
const T& operator[](const typename Range::IndexType& i) const override;
T& operator[](const IndexType& i) override;
const T& operator[](const IndexType& i) const override;
virtual bool isConst() const override;
virtual bool isSlice() const override;
@ -227,8 +236,8 @@ namespace MultiArrayTools
const T* data() const;
// virtual void manipulate(ManipulatorBase<T>& mb,
// const typename Range::IndexType& manBegin,
// const typename Range::IndexType& manEnd);
// const typename Ranges...::IndexType& manBegin,
// const typename Ranges...::IndexType& manEnd);
template <typename U, class RangeX>
friend class MultiArray;
@ -237,18 +246,20 @@ namespace MultiArrayTools
std::vector<T> mCont;
};
template <typename T, class Range, class Function>
class FunctionalMultiArray : public MultiArrayBase<T,Range>
template <typename T, class Ranges..., class Function>
class FunctionalMultiArray : public MultiArrayBase<T,Ranges...>
{
public:
typedef MultiArrayBase<T,Range> MAB;
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
typedef MultiArrayBase<T,Ranges...> MAB;
typedef typename MultiArrayBase<T,Ranges...>::const_iterator const_iterator;
typedef typename MAB::TopRangeType TopRangeType;
typedef typename TopRangeType::IndexType IndexType;
DEFAULT_MEMBERS(FunctionalMultiArray);
//FunctionalMultiArray(const Range& range);
FunctionalMultiArray(const Range& range, const Function& func);
//FunctionalMultiArray(const Ranges...& range);
FunctionalMultiArray(const TopRangeType& range, const Function& func);
virtual const T& operator[](const typename Range::IndexType& i) const override;
virtual const T& operator[](const IndexType& i) const override;
virtual bool isConst() const override;
virtual bool isSlice() const override;

View file

@ -5,45 +5,30 @@
namespace MultiArrayTools
{
/*********************************
* MultiArrayOperationRoot *
*********************************/
template <typename T>
MultiArrayOperationBase<T>::~MultiArrayOperationBase()
{
//delete mIibPtr;
}
template <typename T>
const IndefinitIndexBase& MultiArrayOperationBase<T>::index() const
{
return *mIibPtr;
}
template <typename T>
void MultiArrayOperationBase<T>::freeIndex() const
{
mIibPtr->freeLinked();
}
/*********************************
* MultiArrayOperationBase *
*********************************/
// purely virtual at the moment
/*********************************
* MultiArrayOperationRoot *
*********************************/
template <typename T, class Range>
void MultiArrayOperationRoot<T,Range>::performAssignment(const MultiArrayOperationBase<T>& in)
{
//#error "WRITE MAOR INTRINSIC CONTRACT FUNCTION"
//CHECK;
IndexList il = in.getIndices();
setInternalIndex(il);
in.setInternalIndex(il);
//CHECK;
IndexType& iref = dynamic_cast<IndexType&>(*MAOB::mIibPtr);
//CHECK;
const size_t endPos = mArrayRef.endIndex().pos();
const size_t endPos = mIndex.max();
std::cout << "assignment: " << endPos << " elements" << std::endl;
// assignment loop
for(iref = mArrayRef.beginIndex().pos(); iref != mArrayRef.endIndex(); ++iref){
for(mIndex.toFirst(); not mIndex.atEdge(); ++mIndex){
//std::cout << get() << " / " << in.get() << std::endl;
//std::cout << iref.pos() << '\r' << std::flush;
get() = in.get();
@ -51,7 +36,6 @@ namespace MultiArrayTools
//assert(not std::isnan( get() ));
}
//CHECK;
}
template <typename T, class Range>
@ -60,37 +44,20 @@ namespace MultiArrayTools
const Name& nm) :
MutableMultiArrayOperationBase<T>(),
mArrayRef(ma),
mIndex(mArrayRef.beginIndex()),
mNm(nm)
{
//CHECK;
MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(nm);
//CHECK;
//mIndex.name(nm);
}
mNm(nm) {}
template <typename T, class Range>
MultiArrayOperationRoot<T,Range>::
MultiArrayOperationRoot(const MultiArrayOperationRoot& in) :
MutableMultiArrayOperationBase<T>(),
mArrayRef(in.mArrayRef),
mIndex(mArrayRef.beginIndex()),
mNm(in.mNm)
{
//CHECK;
MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(mNm);
//CHECK;
//mIndex.name(nm);
}
mNm(in.mNm) {}
template <typename T, class Range>
MultiArrayOperationRoot<T,Range>&
MultiArrayOperationRoot<T,Range>::operator=(const MultiArrayOperationRoot<T,Range>& in)
{
performAssignment(in);
return *this;
}
@ -309,20 +276,14 @@ namespace MultiArrayTools
T& MultiArrayOperationRoot<T,Range>::get()
{
//CHECK;
//return mArrayRef[mIndex];
//assert(MAOB::mIibPtr == &mIndex);
//VCHECK(mArrayRef[*dynamic_cast<IndexType*>(MAOB::mIibPtr)]);
return mArrayRef[*dynamic_cast<IndexType*>(MAOB::mIibPtr)];
return mArrayRef[mIndex];
}
template <typename T, class Range>
const T& MultiArrayOperationRoot<T,Range>::get() const
{
//CHECK;
//return mArrayRef[mIndex];
//assert(MAOB::mIibPtr == &mIndex);
//VCHECK(mArrayRef[*dynamic_cast<IndexType*>(MAOB::mIibPtr)]);
return mArrayRef[*dynamic_cast<IndexType*>(MAOB::mIibPtr)];
return mArrayRef[mIndex];
}
template <typename T, class Range>
@ -345,14 +306,6 @@ namespace MultiArrayTools
return *this;
}
template <typename T, class Range>
void MultiArrayOperationRoot<T,Range>::freeIndex() const
{
mIndex = mArrayRef.beginIndex();
MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(mNm);
}
/**************************************
* ConstMultiArrayOperationBase *
**************************************/
@ -364,39 +317,21 @@ namespace MultiArrayTools
const Name& nm) :
MultiArrayOperationBase<T>(),
mArrayRef(ma),
mIndex(mArrayRef.beginIndex()),
mNm(nm)
{
MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(nm);
//mIndex.name(nm);
}
mNm(nm) {}
template <typename T, class Range>
ConstMultiArrayOperationRoot<T,Range>::
ConstMultiArrayOperationRoot(const MultiArrayOperationRoot<T,Range>& in) :
MultiArrayOperationBase<T>(),
mArrayRef(in.getCont()),
mIndex(mArrayRef.beginIndex()),
mNm(in.name())
{
MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(mNm);
//mIndex.name(nm);
}
mNm(in.name()) {}
template <typename T, class Range>
ConstMultiArrayOperationRoot<T,Range>::
ConstMultiArrayOperationRoot(const ConstMultiArrayOperationRoot& in) :
MultiArrayOperationBase<T>(),
mArrayRef(in.mArrayRef),
mIndex(mArrayRef.beginIndex()),
mNm(in.mNm)
{
MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(mNm);
//mIndex.name(nm);
}
mNm(in.mNm) {}
template <typename T, class Range>
template <class Operation, class... MAOps>
@ -498,8 +433,7 @@ namespace MultiArrayTools
const T& ConstMultiArrayOperationRoot<T,Range>::get() const
{
//CHECK;
//assert(MAOB::mIibPtr == &mIndex);
return mArrayRef[*dynamic_cast<IndexType*>(MAOB::mIibPtr)];
return mArrayRef[mIndex];
}
template <typename T, class Range>
@ -525,16 +459,7 @@ namespace MultiArrayTools
const IndexMapFunction<InRange,OutRange>& mf) :
MutableMultiArrayOperationBase<T>(),
mMF(mf),
mRoot(root)
//mIndex(mArrayRef.beginIndex()),
//mNm(nm)
{
//CHECK;
MAOB::mIibPtr = &mIndex;
//CHECK;
//MAOB::mIibPtr->name(nm);
//mIndex.name(nm);
}
mRoot(root) {}
template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>&
@ -543,7 +468,6 @@ namespace MultiArrayTools
{
mIndex = dynamic_cast<typename TotalInRange::IndexType const&>( in.index() );
MAOB::mIibPtr = &mIndex;
mNm = in.name();
mIndex.name(mNm); // to be sure...
@ -557,7 +481,7 @@ namespace MultiArrayTools
MultiArray<T,TotalRange> cnt(mRoot->range());
MultiArrayOperationRoot<T,TotalRange> cnto(cnt, mRoot.name());
for(mIndex.setPos(0), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){
for(mIndex.toFirst(), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){
get() += in.get();
cnto.get() += 1.;
}
@ -574,19 +498,18 @@ namespace MultiArrayTools
{
mIndex = dynamic_cast<typename TotalInRange::IndexType const&>( in.index() );
MAOB::mIibPtr = &mIndex;
mNm = in.name();
mIndex.name(mNm); // to be sure...
typename TotalInRange::IndexType endIndex = mIndex;
endIndex.setPos( mIndex.max() );
++endIndex.toLast();
std::cout << "map assignment: " << endIndex.pos() << " elements" << std::endl;
//!!!!!!!
MultiArray<T,TotalRange> cnt(mRoot->range());
MultiArrayOperationRoot<T,TotalRange> cnto(cnt, mRoot.name());
for(mIndex.setPos(0), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){
for(mIndex.toFirst(), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){
get() += in.get();
cnto.get() += 1.;
}
@ -765,8 +688,8 @@ namespace MultiArrayTools
mBeginIndex(runIndex), mEndIndex(runIndex),
mRunIndex(runIndex)
{
mBeginIndex.setPos(0);
mEndIndex.setPos(mRunIndex.max());
mBeginIndex.toFirst();
++mEndIndex.toLast();
// DON'T link here !!
//linkIndicesTo(&mRunIndex);
}
@ -817,7 +740,7 @@ namespace MultiArrayTools
const std::string& indexName,
const MAOps2&... mao) const
{
typename Range2::IndexType* ind = dynamic_cast<typename Range2::IndexType*>( getLinked(indexName) );
//
return MultiArrayContraction<T,ContractOperation2,Range2,MultiArrayContraction<T,ContractOperation,Range,MAOps...>,
MAOps2...>(cop, *ind, *this, mao...);
}
@ -865,7 +788,7 @@ namespace MultiArrayTools
{
//CHECK;
mOp.reset();
for(mRunIndex.copyPos( mBeginIndex ); mRunIndex.pos() != mEndIndex.pos(); ++mRunIndex){
for(mRunIndex = mBeginIndex ; mRunIndex != mEndIndex; ++mRunIndex){
OperationCall<sizeof...(MAOps)-1>::
template callOperation(mOp, mArgs);
}

View file

@ -13,6 +13,8 @@
namespace MultiArrayTools
{
typedef std::vector<std::shared_ptr<IndefinitIndexBase> > IndexList;
template <typename T>
class MultiArrayOperationBase
{
@ -21,18 +23,15 @@ namespace MultiArrayTools
typedef T value_type;
MultiArrayOperationBase() /*{ CHECK; }*/ = default;
//MultiArrayOperationBase(const MultiArrayOperationBase& in) = default;
virtual ~MultiArrayOperationBase();
virtual size_t argNum() const = 0;
const IndefinitIndexBase& index() const;
const IndefinitIndexBase& index() const = 0;
virtual const T& get() const = 0;
protected:
mutable IndefinitIndexBase* mIibPtr = nullptr;
virtual IndexList getIndices() const = 0;
virtual void setInternalIndex(const IndexList& il) = 0;
};
@ -42,7 +41,6 @@ namespace MultiArrayTools
public:
MutableMultiArrayOperationBase() /*{ CHECK; }*/ = default;
//MutableMultiArrayOperationBase(const MutableMultiArrayOperationBase& in) = default;
virtual T& get() = 0;
};
@ -316,13 +314,14 @@ namespace MultiArrayTools
typedef MultiArrayOperationBase<T> MAOB;
typedef std::tuple<MAOps...> OBT;
typedef typename Range::IndexType RunIndexType;
MultiArrayContraction(const ContractOperation& cop,
const typename Range::IndexType& runIndex,
const RunIndexType& runIndex,
const MAOps&... mao);
MultiArrayContraction(const ContractOperation& cop,
const typename Range::IndexType& runIndex,
const RunIndexType& runIndex,
size_t begin,
size_t end,
const MAOps&... mao);
@ -364,9 +363,9 @@ namespace MultiArrayTools
mutable T mVal;
ContractOperation mOp;
OBT mArgs; // include first arg also here !!!
typename Range::IndexType mBeginIndex;
typename Range::IndexType mEndIndex;
mutable typename Range::IndexType mRunIndex;
RunIndexType mBeginIndex;
RunIndexType mEndIndex;
mutable RunIndexType mRunIndex;
};
}