again devel of map range + usage of std::map single index

This commit is contained in:
Christian Zimmermann 2018-08-08 18:20:08 +02:00
parent 89eb03ab7f
commit bcd875f7d2
3 changed files with 141 additions and 32 deletions

View file

@ -26,6 +26,52 @@ namespace MultiArrayTools
using namespace MultiArrayHelper; using namespace MultiArrayHelper;
} }
template <class MA, class... Indices>
auto mkMapOp(const MA& ma,
const std::tuple<std::shared_ptr<Indices>...>& itp)
-> ConstOperation<T,typename Indices::RangeType...>
{
return PackNum<sizeof...(Indices)-1>::mkMapOp(ma, itp);
}
template <class MapF, class Expr>
class OpExpr
{
public:
typedef SingleIndex<MapF::value_type,SpaceType::ANY> OIType;
typedef MapF::IndexPack IndexPack;
static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE;
private:
OpExpr() = default;
const IndexClass* mIndPtr;
size_t mSPos;
size_t mMax;
Expr mExpr;
typedef decltype(mOp.rootSteps(iPtrNum).extend( mExpr.rootSteps(iPtrNum) )) ExtType;
ExtType mExt;
typedef decltype(mkMapOp(std::declval<MapF>(), std::declval<IndexPack>())) Op;
Op mOp;
public:
OpExpr(const OpExpr& in) = default;
OpExpr(OpExpr&& in) = default;
OpExpr& operator=(const OpExpr& in) = default;
OpExpr& operator=(OpExpr&& in) = default;
OpExpr(const MapF& mapf, const IndexPack& ipack, const std::shared_ptr<OIType> oind, Expr ex);
inline void operator()(size_t mlast, ExtType last) const;
inline void operator()(size_t mlast = 0) const;
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
};
template <class MapF, class... Indices> template <class MapF, class... Indices>
class MapIndex : public IndexInterface<MapIndex<MapF,Indices...>, class MapIndex : public IndexInterface<MapIndex<MapF,Indices...>,
std::tuple<typename Indices::MetaType...> > std::tuple<typename Indices::MetaType...> >
@ -117,7 +163,7 @@ namespace MultiArrayTools
template <class Exprs> template <class Exprs>
auto ifor(Exprs exs) const auto ifor(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, SinleExpression( mOutIndex, exs ) )); -> decltype(RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, OpExpr( range()->map() , mOutIndex, exs ) ));
/* /*
template <class Exprs> template <class Exprs>
@ -145,11 +191,8 @@ namespace MultiArrayTools
typedef MapRange<MapF,Ranges...> oType; typedef MapRange<MapF,Ranges...> oType;
MapRangeFactory() = delete; MapRangeFactory() = delete;
MapRangeFactory(const std::shared_ptr<Ranges>&... rs); MapRangeFactory(const MapF& mapf, const std::shared_ptr<Ranges>&... rs);
MapRangeFactory(const typename MapRange<MapF,Ranges...>::Space& space); MapRangeFactory(const MapF& mapf, const typename MapRange<MapF,Ranges...>::Space& space);
template <typename T>
MapRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr);
virtual std::shared_ptr<RangeBase> create() override; virtual std::shared_ptr<RangeBase> create() override;
@ -184,6 +227,7 @@ namespace MultiArrayTools
MapRange(const MapF& mapf, const Space& space); MapRange(const MapF& mapf, const Space& space);
Space mSpace; Space mSpace;
const MapF& mMapf;
shred_ptr<ORType> mOutRange; shred_ptr<ORType> mOutRange;
public: public:
@ -197,6 +241,7 @@ namespace MultiArrayTools
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&; auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
shred_ptr<ORType> outRange() const; shred_ptr<ORType> outRange() const;
const MapF& map() const;
virtual size_t dim() const final; virtual size_t dim() const final;
virtual size_t size() const final; virtual size_t size() const final;
@ -237,6 +282,52 @@ namespace MultiArrayTools
using namespace MultiArrayHelper; using namespace MultiArrayHelper;
} }
/**************
* OpExpr *
**************/
template <class MapF, class Expr>
OpExpr<MapF,Expr>::OpExpr(const MapF& mapf, const IndexPack& ipack,
const std::shared_ptr<OIType> oind, Expr ex) :
mIndPtr(oind.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mExpr(expr),
mExt(expr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr.get() ))),
mOp(mapf, ipack)
{
assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id());
//VCHECK(mIndPtr->max());
}
template <class MapF, class Expr>
inline void OpExpr<MapF,Expr>::operator()(size_t mlast,
ExtType last) const
{
const ExtType npos = last + mExt*pos;
constexpr size_t NEXT = Op::SIZE;
const size_t pos = mIndPtr->getMeta( mOp.get( npos ) );
const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos);
mExpr(mnpos, Getter<NEXT>::template getX<ExtType>( npos ) );
}
template <class MapF, class Expr>
inline void OpExpr<MapF,Expr>::operator()(size_t mlast) const
{
const ExtType last;
const ExtType npos = last + mExt*pos;
constexpr size_t NEXT = Op::SIZE;
const size_t pos = mIndPtr->at( mOp.get( npos ) ).pos();
const size_t mnpos = PosForward<ForType::DEFAULT>::value(mlast, mMax, pos);
mExpr(mnpos, Getter<NEXT>::template getX<ExtType>( npos ));
}
template <class MapF, class Expr>
auto OpExpr<MapF,Expr>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
{
return mOp.rootSteps(iPtrNum).extend( mExpr.rootSteps(iPtrNum) );
}
// -> define in range_base.cc // -> define in range_base.cc
//std::shared_ptr<RangeFactoryBase> mkMULTI(const char** dp); //std::shared_ptr<RangeFactoryBase> mkMULTI(const char** dp);
@ -454,11 +545,12 @@ namespace MultiArrayTools
template <class MapF, class... Indices> template <class MapF, class... Indices>
template <class Exprs> template <class Exprs>
auto MapIndex<MapF,Indices...>::ifor(Exprs exs) const auto MapIndex<MapF,Indices...>::ifor(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs)) -> decltype(RPackNum<sizeof...(Indices)-1>::mkFor
(mIPack, OpExpr( range()->map(), mIPack, mOutIndex, exs ) ) )
{ {
return RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs); return RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, OpExpr( range()->map(), mIPack, mOutIndex, exs ) );
} }
/*
template <class MapF, class... Indices> template <class MapF, class... Indices>
template <class Exprs> template <class Exprs>
auto MapIndex<MapF,Indices...>::iforh(Exprs exs) const auto MapIndex<MapF,Indices...>::iforh(Exprs exs) const
@ -466,28 +558,21 @@ namespace MultiArrayTools
{ {
return RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs); return RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs);
} }
*/
/************************* /*************************
* MapRangeFactory * * MapRangeFactory *
*************************/ *************************/
template <class MapF, class... Ranges> template <class MapF, class... Ranges>
MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const std::shared_ptr<Ranges>&... rs) MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const MapF& mapf, const std::shared_ptr<Ranges>&... rs)
{ {
mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( rs... ) ); mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( mapf, rs... ) );
} }
template <class MapF, class... Ranges> template <class MapF, class... Ranges>
MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const typename MapRange<MapF,Ranges...>::Space& st) MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const MapF& mapf, const typename MapRange<MapF,Ranges...>::Space& st)
{ {
mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( st ) ); mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( mapf, st ) );
}
template <class MapF, class... Ranges>
template <typename T>
MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr)
{
mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( cr->space() ) );
} }
template <class MapF, class... Ranges> template <class MapF, class... Ranges>
@ -526,7 +611,9 @@ namespace MultiArrayTools
******************/ ******************/
template <class MapF, class... Ranges> template <class MapF, class... Ranges>
MapRange<MapF,Ranges...>::MapRange(const std::shared_ptr<Ranges>&... rs) : mSpace(std::make_tuple(rs...)) MapRange<MapF,Ranges...>::MapRange(const MapF& mapf, const std::shared_ptr<Ranges>&... rs) :
mSpace(std::make_tuple(rs...)),
mMapf(mapf)
{ {
std::vector<MapF::value_type> outmeta; std::vector<MapF::value_type> outmeta;
// set !!!! // set !!!!
@ -535,7 +622,9 @@ namespace MultiArrayTools
} }
template <class MapF, class... Ranges> template <class MapF, class... Ranges>
MapRange<MapF,Ranges...>::MapRange(const Space& space) : mSpace( space ) MapRange<MapF,Ranges...>::MapRange(const MapF& mapf, const Space& space) :
mSpace( space ),
mMapf(mapf)
{ {
std::vector<MapF::value_type> outmeta; std::vector<MapF::value_type> outmeta;
// set !!!! // set !!!!
@ -563,6 +652,12 @@ namespace MultiArrayTools
return mOutRange; return mOutRange;
} }
template <class MapF, class... Ranges>
const MapF& map() const
{
return mMapf;
}
template <class MapF, class... Ranges> template <class MapF, class... Ranges>
size_t MapRange<MapF,Ranges...>::dim() const size_t MapRange<MapF,Ranges...>::dim() const
{ {

View file

@ -107,6 +107,13 @@ namespace MultiArrayHelper
{ {
return std::get<N>(rtp1).get() == std::get<N>(rtp2).get() and PackNum<N-1>::checkIfSameInstance(rtp1,rtp2); return std::get<N>(rtp1).get() == std::get<N>(rtp2).get() and PackNum<N-1>::checkIfSameInstance(rtp1,rtp2);
} }
template <class MA, class ITuple, class... Indices>
static inline auto mkMapOp(const MA& ma, const ITuple& itp, const std::shared_ptr<Indices>&... inds)
-> decltype(PackNum<N-1>::mkMapOp(ma, itp, std::get<N>(itp), inds...))
{
return PackNum<N-1>::mkMapOp(ma, itp, std::get<N>(itp), inds...);
}
}; };
template<> template<>
@ -178,6 +185,13 @@ namespace MultiArrayHelper
return std::get<0>(rtp1).get() == std::get<0>(rtp2).get(); return std::get<0>(rtp1).get() == std::get<0>(rtp2).get();
} }
template <class MA, class ITuple, class... Indices>
static inline auto mkMapOp(const MA& ma, const ITuple& itp, const std::shared_ptr<Indices>&... inds)
-> decltype(ma.exec(std::get<0>(itp), inds...))
{
return ma.exec(std::get<0>(itp), inds...);
}
}; };

View file

@ -6,6 +6,7 @@
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <map>
//#include "base_def.h" //#include "base_def.h"
//#include "ranges/rpack_num.h" //#include "ranges/rpack_num.h"
@ -136,6 +137,7 @@ namespace MultiArrayTools
SingleRange(const std::vector<U>& space); SingleRange(const std::vector<U>& space);
std::vector<U> mSpace; std::vector<U> mSpace;
std::map<U,size_t> mMSpace;
}; };
} }
@ -349,7 +351,12 @@ namespace MultiArrayTools
template <typename U, SpaceType TYPE> template <typename U, SpaceType TYPE>
SingleRange<U,TYPE>::SingleRange(const std::vector<U>& space) : RangeInterface<SingleIndex<U,TYPE> >(), SingleRange<U,TYPE>::SingleRange(const std::vector<U>& space) : RangeInterface<SingleIndex<U,TYPE> >(),
mSpace(space) {} mSpace(space)
{
for(size_t i = 0; i != mSpace.size(); ++i){
mMSpace[mSpace[i]] = i;
}
}
template <typename U, SpaceType TYPE> template <typename U, SpaceType TYPE>
const U& SingleRange<U,TYPE>::get(size_t pos) const const U& SingleRange<U,TYPE>::get(size_t pos) const
@ -360,14 +367,7 @@ namespace MultiArrayTools
template <typename U, SpaceType TYPE> template <typename U, SpaceType TYPE>
size_t SingleRange<U,TYPE>::getMeta(const U& metaPos) const size_t SingleRange<U,TYPE>::getMeta(const U& metaPos) const
{ {
size_t cnt = 0; return mMSpace.at(metaPos);
for(auto& x: mSpace){
if(x == metaPos){
return cnt;
}
++cnt;
}
return cnt;
} }
template <typename U, SpaceType TYPE> template <typename U, SpaceType TYPE>