#include "helper_tools.h" namespace MultiArrayTools { template std::ostream& operator<<(std::ostream& out, const std::tuple& tp) { PackNum::printTuple(out, tp); return out; } template auto getIndex(std::shared_ptr range) -> std::shared_ptr { return std::make_shared(range); } template auto getIndex() -> std::shared_ptr { static_assert( RangeType::defaultable, /*typeid(typename RangeType).name() + */" is not defaultable" ); static auto f = RangeType::factory(); static auto r = std::dynamic_pointer_cast( f.create() ); return std::make_shared(r); } template auto mkMulti(std::shared_ptr... ranges) -> std::shared_ptr > { MultiRangeFactory mrf( ranges... ); return std::dynamic_pointer_cast >( mrf.create() ); } template auto mkMIndex(std::shared_ptr... indices) -> decltype( getIndex( mkMulti( indices->range()... ) ) ) { auto mi = getIndex( mkMulti( indices->range()... ) ); (*mi)( indices... ); return mi; } template auto mkGenMapR(const std::tuple& f, std::shared_ptr... ranges) -> std::shared_ptr > { GenMapRangeFactory mrf(f, ranges... ); return createExplicit( mrf ); } template auto mkGenMapI(const std::tuple& f, std::shared_ptr... indices) -> decltype( getIndex( mkGenMapR( f, indices->range()... ) ) ) { auto mi = getIndex( mkGenMapR( f, indices->range()... ) ); (*mi)(indices...); return mi; } template auto mkMapR(const std::tuple& f, std::shared_ptr... ranges) -> decltype( mkGenMapR(f, ranges... ) ) { return mkGenMapR(f, ranges... ); } template auto mkMapI(const std::tuple& f, std::shared_ptr... indices) -> decltype( mkGenMapI(f, indices... ) ) { return mkGenMapI(f, indices... ); } template auto mkMulti(std::tuple...> rangesTuple) -> MultiRange { MultiRangeFactory mrf( rangesTuple ); return std::dynamic_pointer_cast >( mrf.create() ); } template auto createExplicit(RangeFactory& rf) -> std::shared_ptr { return std::dynamic_pointer_cast( rf.create() ); } template auto createExplicit(std::shared_ptr rfp) -> std::shared_ptr { return std::dynamic_pointer_cast( rfp->create() ); } template auto createRange(const vector& cvec) -> std::shared_ptr { const char* dp = cvec.data(); auto ff = createRangeFactory(&dp); auto rbptr = ff->create(); assert(rbptr->spaceType() == Range::STYPE); // CATCH CAST ERROR HERE !!! return std::dynamic_pointer_cast( rbptr ); } inline auto createRange(const vector* cvec, int metaType, size_t size) -> std::shared_ptr { auto f = createSingleRangeFactory(cvec, metaType, size); return f->create(); } inline auto createRangeA(const vector* cvec, int metaType, size_t size) -> std::shared_ptr { AnonymousRangeFactory arf(createRange(cvec, metaType, size)); return createExplicit(arf); } inline auto cvecMetaCast(const std::shared_ptr,SpaceType::ANY>>& r, int metaType) -> std::shared_ptr { return createRange(&r->get(0), metaType, r->size()); } inline auto cvecMetaCastA(const std::shared_ptr,SpaceType::ANY>>& r, int metaType) -> std::shared_ptr { return createRangeA(&r->get(0), metaType, r->size()); } template auto createRangeE(Args&&... args) -> std::shared_ptr { typename Range::FType f(args...); return createExplicit(f); } template auto rptr(const MArray& ma) -> decltype(ma.template getRangePtr()) { return ma.template getRangePtr(); } template auto dynamic(const MArray& ma, bool slice) -> std::shared_ptr>> { DynamicRangeFactory drf(ma.range()->space()); if(slice){ return std::make_shared>> ( std::dynamic_pointer_cast>( drf.create() ), ma.data() ); } else { return std::make_shared>> ( std::dynamic_pointer_cast>( drf.create() ), ma.vdata() ); } } namespace { template struct CopyRanges { template static inline void exec(const Space1& space1, Space2& space2) { std::get(space2) = std::get(space1); CopyRanges::exec(space1,space2); } }; template <> struct CopyRanges<0> { template static inline void exec(const Space1& space1, Space2& space2) { std::get<0>(space2) = std::get<0>(space1); } }; } template auto anonToDynView(const MultiArray& ma) -> ConstSlice> { constexpr size_t LAST = sizeof...(RangeTypes)+1; DynamicRangeFactory drf(rptr(ma)->orig()); std::tuple,std::shared_ptr..., std::shared_ptr>> mNSpace; CopyRanges::exec(ma.range()->space(),mNSpace); std::get(mNSpace) = createExplicit( drf ); return ConstSlice>(mNSpace, ma.data()); } template auto dynToAnonMove(MultiArray>&& ma) -> MultiArray { constexpr size_t LAST = sizeof...(RangeTypes)+1; AnonymousRangeFactory arf(rptr(ma)->orig()); std::tuple,std::shared_ptr..., std::shared_ptr> mNSpace; CopyRanges::exec(ma.range()->space(),mNSpace); std::get(mNSpace) = createExplicit( arf ); return ma.format(mNSpace); } template auto anonToDynView(const MultiArray& ma) -> ConstSlice> { DynamicRangeFactory drf(rptr<0>(ma)->orig()); auto mNSpace = std::make_tuple( createExplicit( drf ) ); return ConstSlice>(mNSpace, ma.data()); } template auto dynToAnonMove(MultiArray>&& ma) -> MultiArray { AnonymousRangeFactory arf(rptr<0>(ma)->orig()); auto mNSpace = std::make_tuple( createExplicit( arf ) ); return ma.format(mNSpace); } template auto metaSlice(const std::shared_ptr& r) -> ConstSlice { ClassicRF crf(r->size()); return ConstSlice( createExplicit(crf), &r->get(0) ); } template auto metaSlice(const std::shared_ptr& r, const std::shared_ptr& ro) -> ConstSlice { return ConstSlice( ro, &r->get(0) ); } template auto mkArray(const std::shared_ptr&... rs) -> MultiArray { return MultiArray(rs...); } template auto mkArray(const std::shared_ptr&... rs, const T& val) -> MultiArray { return MultiArray(rs..., val); } }