From 3ccc5277e397591fa518e30153fad00384cf408b Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 6 Feb 2019 12:21:42 +0100 Subject: [PATCH] generalize map range: specification of out range type --- src/include/helper_tools.h | 1 + src/include/map_range.cc.h | 325 +++++++++++--------- src/include/map_range.h | 111 ++++--- src/include/map_range_factory_product_map.h | 4 +- src/include/mbase_def.h | 14 +- 5 files changed, 258 insertions(+), 197 deletions(-) diff --git a/src/include/helper_tools.h b/src/include/helper_tools.h index c428adb..b4bd316 100644 --- a/src/include/helper_tools.h +++ b/src/include/helper_tools.h @@ -6,6 +6,7 @@ #include "slice.h" #include #include "pack_num.h" +#include "map_range.h" #include namespace MultiArrayTools diff --git a/src/include/map_range.cc.h b/src/include/map_range.cc.h index 847b091..8f6699d 100644 --- a/src/include/map_range.cc.h +++ b/src/include/map_range.cc.h @@ -13,8 +13,8 @@ namespace MultiArrayTools * OpExpr * **************/ - template - OpExpr::OpExpr(const MapF& mapf, const IndexPack& ipack, + template + OpExpr::OpExpr(const MapF& mapf, const IndexPack& ipack, const std::shared_ptr& oind, size_t step, Expr ex) : mIndPtr(oind.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr( std::forward(ex) ), @@ -26,8 +26,8 @@ namespace MultiArrayTools assert(mIndPtr != nullptr); } - template - inline void OpExpr::operator()(size_t mlast, + template + inline void OpExpr::operator()(size_t mlast, ExtType last) const { constexpr size_t NEXT = Op::SIZE; @@ -40,8 +40,8 @@ namespace MultiArrayTools mExpr(mnpos, Getter::template getX( npos ) ); } - template - inline void OpExpr::operator()(size_t mlast) const + template + inline void OpExpr::operator()(size_t mlast) const { const ExtType last; constexpr size_t NEXT = Op::SIZE; @@ -52,8 +52,8 @@ namespace MultiArrayTools mExpr(mnpos, Getter::template getX( npos )); } - template - auto OpExpr::rootSteps(std::intptr_t iPtrNum) const + template + auto OpExpr::rootSteps(std::intptr_t iPtrNum) const -> ExtType { return mOp.rootSteps(iPtrNum).extend( mExpr.rootSteps(iPtrNum) ); @@ -87,10 +87,10 @@ namespace MultiArrayTools } */ - template + template template - MapIndex::MapIndex(const std::shared_ptr& range) : - IndexInterface,std::tuple >(range, 0) + GenMapIndex::GenMapIndex(const std::shared_ptr& range) : + IndexInterface,std::tuple >(range, 0) { RPackNum::construct(mIPack, *range); IB::mPos = RPackNum::makePos(mIPack); @@ -100,9 +100,9 @@ namespace MultiArrayTools ( std::dynamic_pointer_cast( IB::mRangePtr )->outRange()->begin() ); } - template + template template - MapIndex& MapIndex::up() + GenMapIndex& GenMapIndex::up() { static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); IB::mPos += RPackNum::blockSize( mIPack ); @@ -110,9 +110,9 @@ namespace MultiArrayTools return *this; } - template + template template - MapIndex& MapIndex::down() + GenMapIndex& GenMapIndex::down() { static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); IB::mPos -= RPackNum::blockSize( mIPack ); @@ -120,136 +120,136 @@ namespace MultiArrayTools return *this; } - template + template template - auto MapIndex::get() const -> decltype( *std::get( mIPack ) )& + auto GenMapIndex::get() const -> decltype( *std::get( mIPack ) )& { return *std::get(mIPack); } - template + template template - auto MapIndex::getPtr() const -> decltype( std::get( mIPack ) )& + auto GenMapIndex::getPtr() const -> decltype( std::get( mIPack ) )& { return std::get(mIPack); } - template - auto MapIndex::outIndex() const -> std::shared_ptr + template + auto GenMapIndex::outIndex() const -> std::shared_ptr { return mOutIndex; } - template - MapIndex& MapIndex::operator()(std::shared_ptr&... indices) + template + GenMapIndex& GenMapIndex::operator()(std::shared_ptr&... indices) { RPackNum::swapIndices(mIPack, indices...); RPackNum::setIndexPack(mIPack, IB::mPos); return *this; } - template - IndexType MapIndex::type() const + template + IndexType GenMapIndex::type() const { return IndexType::MULTI; } - template - MapIndex& MapIndex::operator=(size_t pos) + template + GenMapIndex& GenMapIndex::operator=(size_t pos) { IB::mPos = pos; RPackNum::setIndexPack(mIPack, pos); return *this; } - template - MapIndex& MapIndex::operator++() + template + GenMapIndex& GenMapIndex::operator++() { RPackNum::pp( mIPack ); ++IB::mPos; return *this; } - template - MapIndex& MapIndex::operator--() + template + GenMapIndex& GenMapIndex::operator--() { RPackNum::mm( mIPack ); --IB::mPos; return *this; } - template - int MapIndex::pp(std::intptr_t idxPtrNum) + template + int GenMapIndex::pp(std::intptr_t idxPtrNum) { int tmp = RPackNum::pp(mIPack, mBlockSizes, idxPtrNum); IB::mPos += tmp; return tmp; } - template - int MapIndex::mm(std::intptr_t idxPtrNum) + template + int GenMapIndex::mm(std::intptr_t idxPtrNum) { int tmp = RPackNum::mm(mIPack, mBlockSizes, idxPtrNum); IB::mPos -= tmp; return tmp; } - template - std::string MapIndex::stringMeta() const + template + std::string GenMapIndex::stringMeta() const { return std::dynamic_pointer_cast( IB::mRangePtr )->stringMeta(IB::mPos); } - template - typename MapIndex::MetaType MapIndex::meta() const + template + typename GenMapIndex::MetaType GenMapIndex::meta() const { MetaType metaTuple; RPackNum::getMetaPos(metaTuple, mIPack); return metaTuple; } - template - MapIndex& MapIndex::at(const MetaType& metaPos) + template + GenMapIndex& GenMapIndex::at(const MetaType& metaPos) { RPackNum::setMeta(mIPack, metaPos); IB::mPos = RPackNum::makePos(mIPack); return *this; } - template - size_t MapIndex::dim() const + template + size_t GenMapIndex::dim() const { return sizeof...(Indices); } - template - bool MapIndex::first() const + template + bool GenMapIndex::first() const { return IB::mPos == 0; } - template - bool MapIndex::last() const + template + bool GenMapIndex::last() const { return IB::mPos == IB::mMax - 1; } - template - std::shared_ptr::RangeType> - MapIndex::range() const + template + std::shared_ptr::RangeType> + GenMapIndex::range() const { return std::dynamic_pointer_cast( IB::mRangePtr ); } - template + template template - auto MapIndex::getPtr() -> decltype( std::get( mIPack ) )& + auto GenMapIndex::getPtr() -> decltype( std::get( mIPack ) )& { return std::get(mIPack); } - template - size_t MapIndex::getStepSize(size_t n) const + template + size_t GenMapIndex::getStepSize(size_t n) const { if(n >= sizeof...(Indices)){ assert(0); @@ -258,14 +258,14 @@ namespace MultiArrayTools return mBlockSizes[n+1]; } - template - std::string MapIndex::id() const + template + std::string GenMapIndex::id() const { return std::string("mul") + std::to_string(IB::mId); } - template - void MapIndex::print(size_t offset) const + template + void GenMapIndex::print(size_t offset) const { if(offset == 0){ std::cout << " === " << std::endl; @@ -276,30 +276,30 @@ namespace MultiArrayTools RPackNum::printIndex(mIPack, offset+1); } - template + template template - auto MapIndex::ifor(size_t step, Exprs exs) const + auto GenMapIndex::ifor(size_t step, Exprs exs) const -> decltype(RPackNum::mkForh - (step, mIPack, mBlockSizes, OpExpr + (step, mIPack, mBlockSizes, OpExpr ( range()->map(), mIPack, mOutIndex, step, exs ) ) ) { return RPackNum::mkForh - (step, mIPack, mBlockSizes, OpExpr + (step, mIPack, mBlockSizes, OpExpr ( range()->map(), mIPack, mOutIndex, step, exs ) ); } - template + template template - auto MapIndex::pifor(size_t step, Exprs exs) const + auto GenMapIndex::pifor(size_t step, Exprs exs) const -> decltype(ifor(step, exs)) { return ifor(step, exs); } /* - template + template template - auto MapIndex::iforh(Exprs exs) const + auto GenMapIndex::iforh(Exprs exs) const -> decltype(RPackNum::mkForh(mIPack, exs)) { return RPackNum::mkForh(mIPack, exs); @@ -309,28 +309,30 @@ namespace MultiArrayTools * MapRangeFactory * *************************/ - template - MapRangeFactory::MapRangeFactory(const MapF& mapf, const std::shared_ptr&... rs) + template + GenMapRangeFactory::GenMapRangeFactory(const MapF& mapf, const std::shared_ptr&... rs) { - mProd = std::shared_ptr< MapRange >( new MapRange( mapf, rs... ) ); + mProd = std::shared_ptr< GenMapRange > + ( new GenMapRange( mapf, rs... ) ); } - template - MapRangeFactory::MapRangeFactory(const MapF& mapf, const typename MapRange::Space& st) + template + GenMapRangeFactory::GenMapRangeFactory(const MapF& mapf, const typename GenMapRange::Space& st) { - mProd = std::shared_ptr< MapRange >( new MapRange( mapf, st ) ); + mProd = std::shared_ptr< GenMapRange > + ( new GenMapRange( mapf, st ) ); } - template - std::shared_ptr MapRangeFactory::create() + template + std::shared_ptr GenMapRangeFactory::create() { mProd = checkIfCreated( std::dynamic_pointer_cast( mProd )->mSpace ); setSelf(); return mProd; } - template - std::shared_ptr MapRangeFactory::checkIfCreated(const std::tuple...>& ptp) + template + std::shared_ptr GenMapRangeFactory::checkIfCreated(const std::tuple...>& ptp) { std::shared_ptr out; bool check = false; @@ -358,107 +360,151 @@ namespace MultiArrayTools * MapRange * ******************/ - template - void MapRange::mkOutRange() + template + struct OutRangeMaker + {}; + + template <> + struct OutRangeMaker + { + template + static void mk(std::shared_ptr& outRange, MultiArray& mapMult, const MapF& mapf) + { + std::map mult; + for(auto ii = mapf.begin(); ii.max() != ii.pos(); ++ii) { + mult[mapf[ii]]++; + } + + std::vector outmeta(mult.size()); + std::vector outmult(mult.size()); + + size_t cnt = 0; + for(auto& x: mult){ + outmeta[cnt] = x.first; + outmult[cnt] = x.second; + ++cnt; + } + + typename ORType::FType orf(outmeta); + outRange = std::dynamic_pointer_cast( orf.create() ); + mapMult = MultiArray( outRange, outmult ); + } + }; + + template <> + struct OutRangeMaker + { + template + static void mk(std::shared_ptr& outRange, MultiArray& mapMult, const MapF& mapf) + { + static_assert( std::is_same::value, + "out range value type for NONE must be size_t" ); + size_t max = 0; + for(auto ii = mapf.begin(); ii.max() != ii.pos(); ++ii) { + max = mapf[ii] > max ? mapf[ii]+1 : max; + } + std::vector mult(max,0); + for(auto ii = mapf.begin(); ii.max() != ii.pos(); ++ii) { + mult[mapf[ii]]++; + } + + std::vector outmult(mult.size()); + + size_t cnt = 0; + for(auto& x: mult){ + outmult[cnt++] = x; + } + + typename ORType::FType orf(max); + outRange = std::dynamic_pointer_cast( orf.create() ); + mapMult = MultiArray( outRange, outmult ); + } + }; + + template + void GenMapRange::mkOutRange() { //FunctionalMultiArray fma(mSpace, mMapf); - std::map mult; - for(auto ii = mMapf.begin(); ii.max() != ii.pos(); ++ii) { - mult[mMapf[ii]]++; - } - - std::vector outmeta(mult.size()); - std::vector outmult(mult.size()); - - size_t cnt = 0; - for(auto& x: mult){ - outmeta[cnt] = x.first; - outmult[cnt] = x.second; - ++cnt; - } - - ORFType orf(outmeta); - mOutRange = std::dynamic_pointer_cast( orf.create() ); - mMapMult = MultiArray( mOutRange, outmult ); + OutRangeMaker::mk(mOutRange,mMapMult,mMapf); } - template - MapRange::MapRange(const MapF& mapf, const std::shared_ptr&... rs) : + template + GenMapRange::GenMapRange(const MapF& mapf, const std::shared_ptr&... rs) : mSpace(std::make_tuple(rs...)), mMapf(mapf) { mkOutRange(); } - template - MapRange::MapRange(const MapF& mapf, const Space& space) : + template + GenMapRange::GenMapRange(const MapF& mapf, const Space& space) : mSpace( space ), mMapf(mapf) { mkOutRange(); } - template + template template - auto MapRange::get() const -> decltype( *std::get( mSpace ) )& + auto GenMapRange::get() const -> decltype( *std::get( mSpace ) )& { return *std::get(mSpace); } - template + template template - auto MapRange::getPtr() const -> decltype( std::get( mSpace ) )& + auto GenMapRange::getPtr() const -> decltype( std::get( mSpace ) )& { return std::get(mSpace); } - template - auto MapRange::outRange() const -> std::shared_ptr + template + auto GenMapRange::outRange() const -> std::shared_ptr { return mOutRange; } - template - const MapF& MapRange::map() const + template + const MapF& GenMapRange::map() const { return mMapf; } - template - size_t MapRange::dim() const + template + size_t GenMapRange::dim() const { return sdim; } - template - size_t MapRange::size() const + template + size_t GenMapRange::size() const { return mOutRange->size(); //return RPackNum::getSize(mSpace); } - template - SpaceType MapRange::spaceType() const + template + SpaceType GenMapRange::spaceType() const { return SpaceType::ANY; } - template - const typename MapRange::Space& MapRange::space() const + template + const typename GenMapRange::Space& GenMapRange::space() const { return mSpace; } - template - std::string MapRange::stringMeta(size_t pos) const + template + std::string GenMapRange::stringMeta(size_t pos) const { auto i = begin(); i = pos; return "[ " + RPackNum::metaTupleToString(i.meta()) + " ]"; } - template - std::vector MapRange::data() const + template + std::vector GenMapRange::data() const { DataHeader h = dataHeader(); std::vector out; @@ -469,8 +515,8 @@ namespace MultiArrayTools return out; } - template - DataHeader MapRange::dataHeader() const + template + DataHeader GenMapRange::dataHeader() const { DataHeader h; h.spaceType = static_cast( SpaceType::ANY ); @@ -479,50 +525,51 @@ namespace MultiArrayTools return h; } - template - typename MapRange::IndexType MapRange::begin() const + template + typename GenMapRange::IndexType GenMapRange::begin() const { MapIndex - i( std::dynamic_pointer_cast > + i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - template - typename MapRange::IndexType MapRange::end() const + template + typename GenMapRange::IndexType GenMapRange::end() const { MapIndex - i( std::dynamic_pointer_cast > + i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis )) ); i = size(); return i; } - template - auto MapRange::mapMultiplicity() const + template + auto GenMapRange::mapMultiplicity() const -> const MultiArray& { return mMapMult; } - template - auto MapRange::explMapMultiplicity() const - -> MultiArray + template + auto GenMapRange::explMapMultiplicity() const + -> MultiArray { auto tmp = mMapMult; - return tmp.format( std::dynamic_pointer_cast > + return tmp.format( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis )) ); } - - template + + /* + template template - auto MapRange::cat(const std::shared_ptr >& erange) - -> std::shared_ptr > + auto GenMapRange::cat(const std::shared_ptr >& erange) + -> std::shared_ptr > { auto crange = std::tuple_cat(mSpace, erange->space()); MapRangeFactory rf(crange); return std::dynamic_pointer_cast >(rf.create()); } - + */ } diff --git a/src/include/map_range.h b/src/include/map_range.h index 9ca7087..61c7d43 100644 --- a/src/include/map_range.h +++ b/src/include/map_range.h @@ -34,11 +34,11 @@ namespace MultiArrayTools return PackNum::mkMapOp(ma, itp); } - template + template class OpExpr { public: - typedef SingleIndex OIType; + typedef SingleIndex OIType; //typedef typename MapF::IndexPack IndexPack; static constexpr size_t LAYER = Expr::LAYER + 1; static constexpr size_t SIZE = Expr::SIZE; @@ -73,25 +73,25 @@ namespace MultiArrayTools }; - template - class MapIndex : public IndexInterface, - std::tuple > + template + class GenMapIndex : public IndexInterface, + std::tuple > { public: - typedef IndexInterface, + typedef IndexInterface, std::tuple > IB; typedef std::tuple...> IndexPack; typedef std::tuple MetaType; - typedef MapRange RangeType; - typedef MapIndex IType; - typedef SingleIndex OIType; + typedef GenMapRange RangeType; + typedef GenMapIndex IType; + typedef SingleIndex OIType; static constexpr IndexType sType() { return IndexType::MULTI; } static constexpr size_t sDim() { return sizeof...(Indices); } static constexpr size_t totalDim() { return mkTotalDim(); } - static constexpr SpaceType STYPE = SpaceType::ANY; + static constexpr SpaceType STYPE = XSTYPE; static constexpr bool PARALLEL = false; private: @@ -104,7 +104,7 @@ namespace MultiArrayTools const IndexPack& pack() const { return mIPack; } - MapIndex() = delete; + GenMapIndex() = delete; // NO DEFAULT HERE !!! // ( have to assign sub-indices (ptr!) correctly ) @@ -112,13 +112,13 @@ namespace MultiArrayTools //MapIndex& operator=(const MapIndex& in); template - MapIndex(const std::shared_ptr& range); + GenMapIndex(const std::shared_ptr& range); template - MapIndex& up(); + GenMapIndex& up(); template - MapIndex& down(); + GenMapIndex& down(); template auto get() const -> decltype( *std::get( mIPack ) )&; @@ -135,23 +135,23 @@ 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 - MapIndex& operator()(std::shared_ptr&... indices); + GenMapIndex& operator()(std::shared_ptr&... indices); // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== IndexType type() const; - MapIndex& operator=(size_t pos); + GenMapIndex& operator=(size_t pos); - MapIndex& operator++(); - MapIndex& operator--(); + GenMapIndex& operator++(); + GenMapIndex& operator--(); int pp(std::intptr_t idxPtrNum); int mm(std::intptr_t idxPtrNum); std::string stringMeta() const; MetaType meta() const; - MapIndex& at(const MetaType& metaPos); + GenMapIndex& at(const MetaType& metaPos); size_t dim() const; bool first() const; @@ -169,7 +169,7 @@ namespace MultiArrayTools template auto ifor(size_t step, Exprs exs) const -> decltype(RPackNum::mkForh - (step, mIPack, mBlockSizes, OpExpr( range()->map(), mIPack, mOutIndex, step, exs ) ) ); + (step, mIPack, mBlockSizes, OpExpr( range()->map(), mIPack, mOutIndex, step, exs ) ) ); // first step arg not used! template @@ -183,27 +183,21 @@ namespace MultiArrayTools */ }; - template - auto mapResult/* >*/(const std::shared_ptr >& ind) - -> decltype(ind->outIndex()) - { - return ind->outIndex(); - } /************************* * MapRangeFactory * *************************/ // NOT THREAD SAVE - template - class MapRangeFactory : public RangeFactoryBase + template + class GenMapRangeFactory : public RangeFactoryBase { public: - typedef MapRange oType; + typedef GenMapRange oType; - MapRangeFactory() = delete; - MapRangeFactory(const MapF& mapf, const std::shared_ptr&... rs); - MapRangeFactory(const MapF& mapf, const typename MapRange::Space& space); + GenMapRangeFactory() = delete; + GenMapRangeFactory(const MapF& mapf, const std::shared_ptr&... rs); + GenMapRangeFactory(const MapF& mapf, const typename GenMapRange::Space& space); virtual std::shared_ptr create() override; @@ -217,25 +211,25 @@ namespace MultiArrayTools * MapRange * ******************/ - template - class MapRange : public RangeInterface > + template + class GenMapRange : public RangeInterface > { public: typedef RangeBase RB; typedef std::tuple...> Space; - typedef MapIndex IndexType; - typedef MapRange RangeType; - typedef SingleRange ORType; - typedef SingleRangeFactory ORFType; + typedef GenMapIndex IndexType; + typedef GenMapRange RangeType; + typedef SingleRange ORType; + typedef SingleRangeFactory ORFType; //typedef typename RangeInterface >::IndexType IndexType; protected: - MapRange() = delete; - MapRange(const MapRange& in) = delete; - MapRange& operator=(const MapRange& in) = delete; + GenMapRange() = delete; + GenMapRange(const GenMapRange& in) = delete; + GenMapRange& operator=(const GenMapRange& in) = delete; - MapRange(const MapF& mapf, const std::shared_ptr&... rs); - MapRange(const MapF& mapf, const Space& space); + GenMapRange(const MapF& mapf, const std::shared_ptr&... rs); + GenMapRange(const MapF& mapf, const Space& space); Space mSpace; const MapF& mMapf; @@ -273,21 +267,38 @@ namespace MultiArrayTools virtual IndexType end() const final; const MultiArray& mapMultiplicity() const; - MultiArray explMapMultiplicity() const; + MultiArray explMapMultiplicity() const; - + /* template - auto cat(const std::shared_ptr >& erange) - -> std::shared_ptr >; - - friend MapRangeFactory; + auto cat(const std::shared_ptr >& erange) + -> std::shared_ptr >; + */ + friend GenMapRangeFactory; static constexpr bool HASMETACONT = false; static constexpr bool defaultable = false; static constexpr size_t ISSTATIC = SubProp::ISSTATIC; static constexpr size_t SIZE = SubProp::SIZE; }; - + + // for legacy + template + using MapIndex = GenMapIndex; + + template + using MapRangeFactory = GenMapRangeFactory; + + template + using MapRange = GenMapRange; + + template + auto mapResult/* >*/(const std::shared_ptr >& ind) + -> decltype(ind->outIndex()) + { + return ind->outIndex(); + } + } diff --git a/src/include/map_range_factory_product_map.h b/src/include/map_range_factory_product_map.h index fea4cba..992a822 100644 --- a/src/include/map_range_factory_product_map.h +++ b/src/include/map_range_factory_product_map.h @@ -14,8 +14,8 @@ namespace MultiArrayTools { public: - template - friend class MapRangeFactory; + template + friend class GenMapRangeFactory; private: static std::map,std::vector > mAleadyCreated; diff --git a/src/include/mbase_def.h b/src/include/mbase_def.h index 1f91e6a..72a8b7f 100644 --- a/src/include/mbase_def.h +++ b/src/include/mbase_def.h @@ -2,6 +2,8 @@ #ifndef __mbase_def_h__ #define __mbase_def_h__ +#include "ranges/rbase_def.h" + namespace MultiArrayTools { /*********************** @@ -76,16 +78,16 @@ namespace MultiArrayTools class ConstSliceDef; // map_range.h - template - class MapIndex; + template + class GenMapIndex; // map_range.h - template - class MapRangeFactory; + template + class GenMapRangeFactory; // map_range.h - template - class MapRange; + template + class GenMapRange; } #endif