generalize map range

This commit is contained in:
Christian Zimmermann 2019-04-03 16:26:53 +02:00
parent 505237c958
commit 6326b32d6a
3 changed files with 45 additions and 35 deletions

View file

@ -14,10 +14,10 @@ namespace MultiArrayTools
* OpExpr *
**************/
template <class Op, class Expr, SpaceType STYPE>
OpExpr<Op,Expr,STYPE>::OpExpr(const Op& mapf, const std::shared_ptr<OIType>& oind,
template <class Op, class Index, class Expr, SpaceType STYPE>
OpExpr<Op,Index,Expr,STYPE>::OpExpr(const Op& mapf, const Index* ind,
size_t step, Expr ex) :
mIndPtr(oind.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()),
mIndPtr(ind), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()),
mStep(step), mExpr( ex ),
mOp(mapf),
//mExt(ex.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr )))
@ -27,22 +27,20 @@ namespace MultiArrayTools
assert(mIndPtr != nullptr);
}
template <class Op, class Expr, SpaceType STYPE>
inline void OpExpr<Op,Expr,STYPE>::operator()(size_t mlast,
template <class Op, class Index, class Expr, SpaceType STYPE>
inline void OpExpr<Op,Index,Expr,STYPE>::operator()(size_t mlast,
ExtType last)
{
constexpr size_t NEXT = Op::SIZE;
const ExtType nxpos = last;
const size_t pos = mIndPtr->posAt( mOp.get( nxpos ) );
//VCHECK(pos);
const ExtType npos = last + mExt*pos;
//VCHECK(npos.next().next().val());
const size_t mnpos = PosForward<ForType::DEFAULT>::valuex(mlast, mStep, pos);
mExpr(mnpos, Getter<NEXT>::template getX<ExtType>( npos ) );
}
template <class Op, class Expr, SpaceType STYPE>
inline void OpExpr<Op,Expr,STYPE>::operator()(size_t mlast)
template <class Op, class Index, class Expr, SpaceType STYPE>
inline void OpExpr<Op,Index,Expr,STYPE>::operator()(size_t mlast)
{
const ExtType last;
constexpr size_t NEXT = Op::SIZE;
@ -53,8 +51,8 @@ namespace MultiArrayTools
mExpr(mnpos, Getter<NEXT>::template getX<ExtType>( npos ));
}
template <class Op, class Expr, SpaceType STYPE>
auto OpExpr<Op,Expr,STYPE>::rootSteps(std::intptr_t iPtrNum) const
template <class Op, class Index, class Expr, SpaceType STYPE>
auto OpExpr<Op,Index,Expr,STYPE>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
{
return mOp.rootSteps(iPtrNum).extend( mExpr.rootSteps(iPtrNum) );
@ -142,7 +140,7 @@ namespace MultiArrayTools
}
template <class Op, SpaceType XSTYPE, class... Indices>
GenMapIndex<Op,XSTYPE,Indices...>& GenMapIndex<Op,XSTYPE,Indices...>::operator()(std::shared_ptr<Indices>&... indices)
GenMapIndex<Op,XSTYPE,Indices...>& GenMapIndex<Op,XSTYPE,Indices...>::operator()(const std::shared_ptr<Indices>&... indices)
{
RPackNum<sizeof...(Indices)-1>::swapIndices(mIPack, indices...);
RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, IB::mPos);
@ -227,6 +225,12 @@ namespace MultiArrayTools
return *this;
}
template <class Op, SpaceType XSTYPE, class... Indices>
size_t GenMapIndex<Op,XSTYPE,Indices...>::posAt(const MetaType& metaPos) const
{
return range()->outRange()->getMeta(metaPos);
}
template <class Op, SpaceType XSTYPE, class... Indices>
size_t GenMapIndex<Op,XSTYPE,Indices...>::dim() const
{
@ -291,12 +295,12 @@ namespace MultiArrayTools
template <class Exprs>
auto GenMapIndex<Op,XSTYPE,Indices...>::ifor(size_t step, Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh
(step, mIPack, mBlockSizes, OpExpr<Op,Exprs,XSTYPE>
( range()->map(), mOutIndex, step, exs ) ) )
(step, mIPack, mBlockSizes, OpExpr<Op,GenMapIndex<Op,XSTYPE,Indices...>,Exprs,XSTYPE>
( range()->map(), this, step, exs ) ) )
{
return RPackNum<sizeof...(Indices)-1>::mkForh
(step, mIPack, mBlockSizes, OpExpr<Op,Exprs,XSTYPE>
( range()->map(), mOutIndex, step, exs ) );
(0, mIPack, mBlockSizes, OpExpr<Op,GenMapIndex<Op,XSTYPE,Indices...>,Exprs,XSTYPE>
( range()->map(), this, step, exs ) );
}
template <class Op, SpaceType XSTYPE, class... Indices>
@ -598,11 +602,15 @@ namespace MultiArrayTools
template <class Op, SpaceType XSTYPE, class... Ranges>
auto GenMapRange<Op,XSTYPE,Ranges...>::explMapMultiplicity() const
-> MultiArray<size_t,GenMapRange>
-> ConstSlice<size_t,GenMapRange>
{
/*
auto tmp = mMapMult;
return tmp.format( std::dynamic_pointer_cast<GenMapRange<Op,XSTYPE,Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis )) );
*/
return mMapMult.slformat(std::dynamic_pointer_cast<GenMapRange<Op,XSTYPE,Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis )));
}
template <class Op, SpaceType XSTYPE, class... Ranges>

View file

@ -48,19 +48,21 @@ namespace MultiArrayTools
template <class Op, class Expr, SpaceType STYPE = SpaceType::ANY>
template <class Op, class Index, class Expr, SpaceType STYPE = SpaceType::ANY>
//template <class MapF, class IndexPack, class Expr, SpaceType STYPE = SpaceType::ANY>
class OpExpr
{
public:
typedef SingleIndex<typename Op::value_type,STYPE> OIType;
typedef typename Index::OIType OIType;
//typedef SingleIndex<typename Op::value_type,STYPE> OIType;
static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE + Op::SIZE;
private:
OpExpr() = default;
const OIType* mIndPtr;
const Index* mIndPtr;
//const OIType* mIndPtr;
size_t mSPos;
size_t mMax;
size_t mStep;
@ -76,7 +78,7 @@ namespace MultiArrayTools
OpExpr& operator=(const OpExpr& in) = default;
OpExpr& operator=(OpExpr&& in) = default;
OpExpr(const Op& mapf, const std::shared_ptr<OIType>& oind, size_t step, Expr ex);
OpExpr(const Op& mapf, const Index* ind, size_t step, Expr ex);
inline void operator()(size_t mlast, ExtType last);
inline void operator()(size_t mlast = 0);
@ -102,7 +104,7 @@ namespace MultiArrayTools
typedef GenMapIndex IType;
typedef SingleIndex<typename Op::value_type,XSTYPE> OIType;
static constexpr IndexType sType() { return IndexType::MULTI; }
static constexpr IndexType sType() { return IndexType::SINGLE; }
static constexpr size_t sDim() { return sizeof...(Indices); }
static constexpr size_t totalDim() { return mkTotalDim<Indices...>(); }
@ -150,7 +152,7 @@ namespace MultiArrayTools
// MultiIndices CANNOT be influences be its subindices, so there is
// NO foreign/external controll)
// Do NOT share index instances between two or more MapIndex instances
GenMapIndex& operator()(std::shared_ptr<Indices>&... indices);
GenMapIndex& operator()(const std::shared_ptr<Indices>&... indices);
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
@ -167,6 +169,7 @@ namespace MultiArrayTools
std::string stringMeta() const;
MetaType meta() const;
GenMapIndex& at(const MetaType& metaPos);
size_t posAt(const MetaType& metaPos) const;
size_t dim() const;
bool first() const;
@ -184,7 +187,7 @@ namespace MultiArrayTools
template <class Exprs>
auto ifor(size_t step, Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh
(step, mIPack, mBlockSizes, OpExpr<Op,Exprs,XSTYPE>( range()->map(), mOutIndex, step, exs ) ) );
(step, mIPack, mBlockSizes, OpExpr<Op,GenMapIndex,Exprs,XSTYPE>( range()->map(), this, step, exs ) ) );
// first step arg not used!
template <class Exprs>
@ -297,7 +300,7 @@ namespace MultiArrayTools
virtual IndexType end() const final;
const MultiArray<size_t,ORType>& mapMultiplicity() const;
MultiArray<size_t,GenMapRange> explMapMultiplicity() const;
ConstSlice<size_t,GenMapRange> explMapMultiplicity() const;
vector<size_t> mapPos() const;

View file

@ -376,9 +376,8 @@ namespace {
///auto jj = mkMapI( std::make_shared<plus<size_t> >(), ii1, ii1 );
res(jj) += ma1(ii1,ii2);
auto mult = mr->mapMultiplicity();
auto jjx = jj->outIndex();
res2(jj) += ma1(ii1,ii2) / staticcast<double>( mult(jjx) );
auto mult = mr->explMapMultiplicity();
res2(jj) += ma1(ii1,ii2) / staticcast<double>( mult(jj) );
MultiArray<double,TRange> form = res.format(mpr1ptr->outRange());
MultiArray<double,TRange> form2 = res2.format(mpr1ptr->outRange());