#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::shared_ptr& f, std::shared_ptr... ranges) -> std::shared_ptr, STYPE,RangeTypes...> > { typedef FunctionalMultiArray FMA; std::shared_ptr> mrfptr; if(Func::FISSTATIC){ FMA fma(ranges...); mrfptr = std::make_shared >( fma, ranges... ); } else { FMA fma(ranges...,f); mrfptr = std::make_shared >( fma, ranges... ); } return std::dynamic_pointer_cast >( mrfptr->create() ); } template auto mkGenMapI(const std::shared_ptr& 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 Func& f, std::shared_ptr... ranges) -> decltype( mkGenMapR(f, ranges... ) ) { return mkGenMapR(f, ranges... ); } template auto mkMapI(const Func& 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 std::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 ); } 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); } }