dynamic index compiles

This commit is contained in:
Christian Zimmermann 2018-10-27 14:58:34 +02:00
parent 1b8c2edf45
commit 9cf294f485
4 changed files with 184 additions and 120 deletions

View file

@ -55,26 +55,28 @@ namespace MultiArrayTools
virtual std::string stringMeta() const = 0;
virtual DynamicMetaT meta() const = 0;
virtual const DynamicMetaT* metaPtr() const = 0;
virtual IndexWrapperBase& at(const U& metaPos) = 0;
virtual size_t posAt(const U& metaPos) const = 0;
//virtual IndexWrapperBase& at(const U& metaPos) = 0;
//virtual size_t posAt(const U& metaPos) const = 0;
//virtual bool isMeta(const U& metaPos) const = 0;
virtual size_t dim() = 0;
virtual bool last() = 0;
virtual bool first() = 0;
virtual size_t dim() const = 0;
virtual bool last() const = 0;
virtual bool first() const = 0;
virtual std::shared_ptr<RangeBase> range() = 0;
virtual std::shared_ptr<RangeBase> range() const = 0;
virtual size_t getStepSize(size_t n) = 0;
virtual size_t getStepSize(size_t n) const = 0;
virtual std::intptr_t get() const = 0;
virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const = 0;
virtual DynamicalExpression iforh(size_t step, DynamicalExpression ex) const = 0;
};
typedef IndexWrapperBase IndexW;
template <class Index>
template <class Index, class Expr>
class IndexWrapper : public IndexWrapperBase
{
protected:
@ -84,8 +86,6 @@ namespace MultiArrayTools
std::shared_ptr<Index> mI;
public:
static constexpr size_t EXPRMAX = IndexWrapperBase::EXPRMAX;
IndexWrapper(const IndexWrapper& in) = default;
IndexWrapper(IndexWrapper&& in) = default;
IndexWrapper& operator=(const IndexWrapper& in) = default;
@ -97,7 +97,7 @@ namespace MultiArrayTools
virtual IndexWrapperBase& operator=(size_t pos) final { (*mI) = pos; return *this; }
virtual IndexWrapperBase& operator++() final { ++(*mI); return *this; }
virtual IndexWrapperBase& operator--() final { --(*mi); return *this; }
virtual IndexWrapperBase& operator--() final { --(*mI); return *this; }
virtual int pp(std::intptr_t idxPtrNum) final { return mI->pp(idxPtrNum); }
virtual int mm(std::intptr_t idxPtrNum) final { return mI->mm(idxPtrNum); }
@ -105,27 +105,36 @@ namespace MultiArrayTools
virtual std::string stringMeta() const final { return mI->stringMeta(); }
virtual DynamicMetaT meta() const final { return DynamicMetaT(mI->meta()); }
virtual const DynamicMetaT* metaPtr() const final { return nullptr; }
virtual IndexWrapperBase& at(const U& metaPos) final { mI->at(metaPos); return *this; }
virtual size_t posAt(const U& metaPos) const final { return mI->posAt(metaPos); }
IndexWrapperBase& at(const typename Index::MetaType& metaPos) { mI->at(metaPos); return *this; }
size_t posAt(const typename Index::MetaType& metaPos) const { return mI->posAt(metaPos); }
//virtual bool isMeta(const U& metaPos) const final { return mI->isMeta(); }
virtual size_t dim() final { return mI->dim(); }
virtual bool last() final { return mI->last(); }
virtual bool first() final { return mI->first(); }
virtual size_t dim() const final { return mI->dim(); }
virtual bool last() const final { return mI->last(); }
virtual bool first() const final { return mI->first(); }
virtual std::shared_ptr<RangeBase> range() final { return mI->range(); }
virtual std::shared_ptr<RangeBase> range() const final { return mI->range(); }
virtual size_t getStepSize(size_t n) final { return mI->getStepSize(n); }
virtual size_t getStepSize(size_t n) const final { return mI->getStepSize(n); }
virtual std::intptr_t get() const final { return reinterpret_cast<std::intptr_t>(mI.get()); }
virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const final { return mI->ifor(step, ex); }
virtual DynamicalExpression iforh(size_t step, DynamicalExpression ex) const final { return mI->iforh(step, ex); }
};
typedef SingleRange<size_t,SpaceType::DYN> DynamicRange;
class DynamicIndex : public IndexInterface<DynamicIndex,DynamicMetaT>
{
private:
std::vector<std::pair<std::shared_ptr<IndexWrapperBase>,size_t>> mIVec;
std::vector<std::pair<std::shared_ptr<IndexW>,size_t>> mIVec;
inline DynamicalExpression mkFor(size_t i, size_t step,
DynamicalExpression ex, bool hidden = false) const;
public:
typedef IndexInterface<DynamicIndex,DynamicMetaT> IB;
typedef DynamicMetaT MetaType;
@ -173,14 +182,15 @@ namespace MultiArrayTools
template <class Expr>
auto ifor(size_t step, Expr ex) const
-> DynamicalExpression<Expr>;
-> DynamicalExpression;
template <class Expr>
auto iforh(size_t step, Expr ex) const
-> DynamicalExpression<Expr>;
-> DynamicalExpression;
};
// NOT THREAD SAVE!!
class DynamicRangeFactory : public RangeFactoryBase
{
@ -210,7 +220,8 @@ namespace MultiArrayTools
bool mProductCreated = false;
};
class DynamicRange : public RangeInterface<DynamicIndex>
template <>
class SingleRange<size_t,SpaceType::DYN> : public RangeInterface<DynamicIndex>
{
public:
static constexpr bool defaultable = true;
@ -219,19 +230,19 @@ namespace MultiArrayTools
static constexpr bool HASMETACONT = false;
typedef RangeBase RB;
typedef typename RangeInterface<DynamicIndex>::IndexType IndexType;
typedef DynamicIndex IndexType;
typedef DynamicRange RangeType;
typedef DynamicMetaT MetaType;
private:
DynamicRange() = default;
DynamicRange(const DynamicRange& in) = default;
SingleRange() = default;
SingleRange(const SingleRange& in) = default;
template <class... RangeTypes>
DynamicRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
SingleRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
template <class... RangeTypes>
DynamicRange(std::shared_ptr<RangeTypes>... origs);
SingleRange(std::shared_ptr<RangeTypes>... origs);
size_t mSize = 1;
bool mEmpty = true;
@ -281,24 +292,6 @@ namespace MultiArrayTools
namespace MultiArrayTools
{
/*************************
* IndexForWrapper *
*************************/
template <class Index, class Expr>
auto IndexForWrapper<Index,Expr>::ifor(size_t step, Expr ex)
-> DynamicalExpression<Expr>
{
return DynamicalExpression<Expr>(std::make_shared<For<Index,Expr>>(mI,step,ex));
}
template <class Index, class Expr>
auto IndexForWrapper<Index,Expr>::iforh(size_t step, Expr ex)
-> DynamicalExpression<Expr>
{
return DynamicalExpression<Expr>(std::make_shared<For<Index,Expr,FT::HIDDEN>>(mI,step,ex));
}
/***********************
* DynamicRange *
@ -333,7 +326,7 @@ namespace MultiArrayTools
* Functions *
*****************/
std::shared_ptr<DynamicRange> defaultRange(size_t size = 0);
//std::shared_ptr<DynamicRange> defaultRange(size_t size = 0);
}
namespace MultiArrayHelper
@ -358,7 +351,7 @@ namespace MultiArrayHelper
std::shared_ptr<DynamicRange> r)
{
if(not r->isEmpty()){
for(size_t i = r->anonymousDim(); i != 0; --i){
for(size_t i = r->dim(); i != 0; --i){
v.insert(v.begin(), r->sub(i-1));
}
}
@ -410,23 +403,33 @@ namespace MultiArrayTools
return std::dynamic_pointer_cast<MultiRange<Ranges...> >( mrf.create() );
}
inline DynamicalExpression DynamicIndex::mkFor(size_t i, size_t step,
DynamicalExpression ex, bool hidden) const
{
if(i != 0){
auto& ii = *mIVec[i].first;
return mkFor(i-1, step, hidden ? ii.iforh(step, ex) : ii.ifor(step, ex));
}
else {
auto& ii = *mIVec[0].first;
return hidden ? ii.iforh(step, ex) : ii.ifor(step, ex);
}
}
template <class Expr>
auto DynamicIndex::ifor(size_t step, Expr ex) const
-> DynamicalExpression
{
//for...
ExpressionHolder<Expr> epxr(ex); // derived from ExpressionBase; just to resolve underlying types!
IVec[0].first->ifor( ... mIVec[i].first->ifor(expr) );
// ...
DynamicalExpression expr(std::make_shared<Expr>(ex));
return mkFor(mIVec.size()-1, step, expr);
}
template <class Expr>
auto DynamicIndex::iforh(size_t step, Expr ex) const
-> DynamicalExpression
{
//for...
// ...
DynamicalExpression expr(std::make_shared<Expr>(ex));
return mkFor(mIVec.size()-1, step, expr, true);
}
}

View file

@ -27,7 +27,7 @@ namespace MultiArrayTools
#include "range_types/header.h"
#undef include_range_type
ANON = -1, // anonymous content
DYN = -2 // dynamic content
DYN = -3 // dynamic content
};
struct DataHeader

View file

@ -79,7 +79,7 @@ namespace MultiArrayTools
class DynamicRangeFactory;
// dynamic_range.h
class DynamicRange;
//class DynamicRange;
// value_range.h
template <typename U>

View file

@ -15,7 +15,7 @@ namespace MultiArrayHelper
// 'HIDDEN FOR' CLASS for nested for loops in contractions a.s.o.
// (NO COUNTING OF MASTER POSITION !!!!!)
typedef std::pair<size_t*,size_t> DExt;
typedef std::pair<size_t const*,size_t> DExt;
class ExpressionBase
{
@ -84,6 +84,43 @@ namespace MultiArrayHelper
}
};
template <class Expr>
class ExpressionHolder : public ExpressionBase
{
private:
ExpressionHolder() = default;
Expr mExpr;
typedef decltype(mExpr.rootSteps()) ExtType;
ExtType mExt;
mutable ExtType mRootSteps;
public:
typedef ExpressionBase EB;
static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE;
ExpressionHolder(const ExpressionHolder& in) = default;
ExpressionHolder(ExpressionHolder&& in) = default;
ExpressionHolder& operator=(const ExpressionHolder& in) = default;
ExpressionHolder& operator=(ExpressionHolder&& in) = default;
ExpressionHolder(Expr expr);
inline void operator()(size_t mlast, DExt last) const override final;
inline void operator()(size_t mlast, ExtType last) const;
inline void operator()(size_t mlast = 0) const override final;
DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final;
DExt dExtension() const override final;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
auto extension() const -> ExtType;
};
template <class IndexClass, class Expr>
class SingleExpression : public ExpressionBase
{
@ -98,6 +135,8 @@ namespace MultiArrayHelper
typedef decltype(mExpr.rootSteps()) ExtType;
ExtType mExt;
mutable ExtType mRootSteps;
public:
typedef ExpressionBase EB;
@ -187,25 +226,21 @@ namespace MultiArrayHelper
std::shared_ptr<ExpressionBase> mNext;
public:
typedef ExpressionBase EB;
using EB::rootSteps;
typedef typename ExpressionBase::ExtType ExtType;
static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE;
DynamicalExpression(const DynamicalExpression& in) = default;
DynamicalExpression(DynamicalExpression&& in) = default;
DynamicalExpression& operator=(const DynamicalExpression& in) = default;
DynamicalExpression& operator=(DynamicalExpression&& in) = default;
DynamicalExpression(const std::shared_ptr<ExpressionBase>& next);
DynamicalExpression(const std::shared_ptr<ExpressionBase>& next) :
mNext(next)
{}
inline void operator()(size_t mlast, DExt last) const override final;
inline void operator()(size_t mlast = 0) const override final;
DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final;
DExt dExtension() const override final;
inline DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final;
inline DExt dExtension() const override final;
};
@ -220,22 +255,6 @@ namespace MultiArrayHelper
namespace MultiArrayHelper
{
/**********************
* ExpressionBase *
**********************/
template <class Expr>
ExpressionBase::ExpressionBase(ExtType ext, Expr&& expr) :
mExpr(std::forward<Expr>(expr)),
mExt(ext)
{}
template <class Expr>
ExpressionBase::ExpressionBase(ExtType ext) :
mExt(ext)
{}
/*****************
* F o r *
*****************/
@ -243,7 +262,7 @@ namespace MultiArrayHelper
template <class IndexClass, class Expr, ForType FT>
For<IndexClass,Expr,FT>::For(const std::shared_ptr<IndexClass>& indPtr,
size_t step, Expr expr) :
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step),
mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step),
mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr )))
{
assert(mIndPtr != nullptr);
@ -254,7 +273,7 @@ namespace MultiArrayHelper
template <class IndexClass, class Expr, ForType FT>
For<IndexClass,Expr,FT>::For(const IndexClass* indPtr,
size_t step, Expr expr) :
mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step),
mIndPtr(indPtr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step),
mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr )))
{
assert(mIndPtr != nullptr);
@ -265,7 +284,7 @@ namespace MultiArrayHelper
template <class IndexClass, class Expr, ForType FT>
inline void For<IndexClass,Expr,FT>::operator()(size_t mlast, DExt last) const
{
operator()(mlast, *reinterpret_cast<ExtType*>(last));
operator()(mlast, *reinterpret_cast<ExtType const*>(last.first));
}
template <class IndexClass, class Expr, ForType FT>
@ -277,8 +296,8 @@ namespace MultiArrayHelper
//for(size_t pos = mSPos; pos != mMax; ++pos){
//const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos);
const size_t mnpos = PosForward<FT>::valuex(mlast, mStep, pos);
const ExtType npos = last + EB::mExt*pos;
EB::mExpr(mnpos, npos);
const ExtType npos = last + mExt*pos;
mExpr(mnpos, npos);
}
}
@ -291,8 +310,8 @@ namespace MultiArrayHelper
//for(size_t pos = mSPos; pos != mMax; ++pos){
//const size_t mnpos = PosForward<FT>::value(mlast, mMax, pos);
const size_t mnpos = PosForward<FT>::valuex(mlast, mStep, pos);
const ExtType npos = last + EB::mExt*pos;
EB::mExpr(mnpos, npos);
const ExtType npos = last + mExt*pos;
mExpr(mnpos, npos);
}
}
@ -312,17 +331,17 @@ namespace MultiArrayHelper
}
template <class IndexClass, class Expr, ForType FT>
DExt For<IndexClass,Expr,FT>::dRootSteps(std::intptr_t iPtrNum = 0) const
DExt For<IndexClass,Expr,FT>::dRootSteps(std::intptr_t iPtrNum) const
{
mRootSteps = rootSteps(iPtrNum);
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mRootSteps),
return std::make_pair<size_t const*,size_t>(reinterpret_cast<size_t const*>(&mRootSteps),
sizeof(ExtType)/sizeof(size_t));
}
template <class IndexClass, class Expr, ForType FT>
DExt For<IndexClass,Expr,FT>::dExtension() const
{
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mExt),
return std::make_pair<size_t const*,size_t>(reinterpret_cast<size_t const*>(&mExt),
sizeof(ExtType)/sizeof(size_t));
}
@ -355,7 +374,7 @@ namespace MultiArrayHelper
template <class IndexClass, class Expr>
inline void SingleExpression<IndexClass,Expr>::operator()(size_t mlast, DExt last) const
{
operator()(mlast, *reinterpret_cast<ExtType*>(last));
operator()(mlast, *reinterpret_cast<ExtType const*>(last.first));
}
template <class IndexClass, class Expr>
@ -365,8 +384,8 @@ namespace MultiArrayHelper
//typedef typename IndexClass::RangeType RangeType;
const size_t pos = mIndPtr->pos();
const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos);
const ExtType npos = last + EB::mExt*pos;
EB::mExpr(mnpos, npos);
const ExtType npos = last + mExt*pos;
mExpr(mnpos, npos);
}
template <class IndexClass, class Expr>
@ -376,8 +395,8 @@ namespace MultiArrayHelper
const ExtType last;
const size_t pos = mIndPtr->pos();
const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos);
const ExtType npos = last + EB::mExt*pos;
EB::mExpr(mlast, last);
const ExtType npos = last + mExt*pos;
mExpr(mlast, last);
}
template <class IndexClass, class Expr>
@ -395,17 +414,17 @@ namespace MultiArrayHelper
}
template <class IndexClass, class Expr>
DExt SingleExpression<IndexClass,Expr>::dRootSteps(std::intptr_t iPtrNum = 0) const
DExt SingleExpression<IndexClass,Expr>::dRootSteps(std::intptr_t iPtrNum) const
{
mRootSteps = rootSteps(iPtrNum);
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mRootSteps),
return std::make_pair<size_t const*,size_t>(reinterpret_cast<size_t const*>(&mRootSteps),
sizeof(ExtType)/sizeof(size_t));
}
template <class IndexClass, class Expr>
DExt SingleExpression<IndexClass,Expr>::dExtension() const
{
return std::make_pair<size_t*,size_t>(reinterpret_cast<size_t*>(&mExt),
return std::make_pair<size_t const*,size_t>(reinterpret_cast<size_t const*>(&mExt),
sizeof(ExtType)/sizeof(size_t));
}
@ -413,36 +432,78 @@ namespace MultiArrayHelper
* DynamicalExpression *
***************************/
template <class Expr>
DynamicalExpression<Expr>::
DynamicalExpression(const std::shared_ptr<ExpressionBase>& next) :
mNext(next)
{}
template <class Expr>
inline void DynamicalExpression<Expr>::operator()(size_t mlast, ExtType last) const
inline void DynamicalExpression::operator()(size_t mlast, DExt last) const
{
(*mNext)(mlast,last);
}
template <class Expr>
inline void DynamicalExpression<Expr>::operator()(size_t mlast) const
inline void DynamicalExpression::operator()(size_t mlast) const
{
(*mNext)(mlast);
}
template <class Expr>
DExt DynamicalExpression<Expr>::dRootSteps(std::intptr_t iPtrNum = 0) const
inline DExt DynamicalExpression::dRootSteps(std::intptr_t iPtrNum) const
{
return mNext->dRootSteps(iPtrNum);
}
template <class Expr>
DExt DynamicalExpression<Expr>::dExtension() const
inline DExt DynamicalExpression::dExtension() const
{
return mNext->dExtension();
}
/************************
* ExpressionHolder *
************************/
template <class Expr>
ExpressionHolder<Expr>::ExpressionHolder(Expr expr) : mExpr(expr) {}
template <class Expr>
inline void ExpressionHolder<Expr>::operator()(size_t mlast, DExt last) const
{
mExpr(mlast,last);
}
template <class Expr>
inline void ExpressionHolder<Expr>::operator()(size_t mlast, ExtType last) const
{
mExpr(mlast,last);
}
template <class Expr>
inline void ExpressionHolder<Expr>::operator()(size_t mlast) const
{
mExpr(mlast);
}
template <class Expr>
DExt ExpressionHolder<Expr>::dRootSteps(std::intptr_t iPtrNum) const
{
return mExpr.dRootSteps(iPtrNum);
}
template <class Expr>
DExt ExpressionHolder<Expr>::dExtension() const
{
return mExpr.dExtension();
}
template <class Expr>
auto ExpressionHolder<Expr>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
{
return mExpr.rootSteps(iPtrNum);
}
template <class Expr>
auto ExpressionHolder<Expr>::extension() const
-> ExtType
{
return mExpr.extension();
}
} // namespace MultiArrayHelper