From fbcdfd75801315e23b522e0727a61d08b5ad7e76 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 28 Aug 2017 18:28:43 +0200 Subject: [PATCH] im com (block type scanning routines...) --- src/container_range.cc | 17 ++++++ src/container_range.h | 4 ++ src/index_base.h | 10 ++++ src/multi_array_operation.cc | 101 +++++++++++++++++++++++++++++++++-- src/multi_array_operation.h | 23 +++++++- src/multi_range.cc | 17 ++++++ src/multi_range.h | 3 ++ src/pack_num.h | 25 +++++++-- src/single_range.cc | 12 +++++ src/single_range.h | 4 ++ 10 files changed, 206 insertions(+), 10 deletions(-) diff --git a/src/container_range.cc b/src/container_range.cc index ffa784d..d499261 100644 --- a/src/container_range.cc +++ b/src/container_range.cc @@ -41,6 +41,12 @@ namespace MultiArrayTools IB::mPos = PackNum::makePos(mIPack); } + template + IndexType ContainerIndex::type() const + { + return IndexType::CONT; + } + template ContainerIndex& ContainerIndex::operator++() { @@ -116,6 +122,17 @@ namespace MultiArrayTools { return std::get( mIPack ); } + + template + std::shared_ptr ContainerIndex::getPtr(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + ContainerIndex const* t = this; + return PackNum::getIndexPtr(*t, n); + } template bool ContainerIndex::first() const diff --git a/src/container_range.h b/src/container_range.h index 51905e0..edbb796 100644 --- a/src/container_range.h +++ b/src/container_range.h @@ -38,6 +38,8 @@ namespace MultiArrayTools template ContainerIndex(const std::shared_ptr& range); + virtual IndexType type() const override; + virtual ContainerIndex& operator++() override; virtual ContainerIndex& operator--() override; virtual ContainerIndex& operator=(size_t pos) override; @@ -57,6 +59,8 @@ namespace MultiArrayTools template auto getPtr() const -> decltype( std::get( mIPack ) )&; + + virtual std::shared_ptr getPtr(size_t n) const override; ContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices diff --git a/src/index_base.h b/src/index_base.h index 7d60a44..a0278f8 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -19,6 +19,12 @@ namespace MultiArrayTools ++id; return id; } + + enum class IndexType{ + SINGLE = 0, + MULTI = 1, + CONT = 2 + }; class IndexBase { @@ -31,6 +37,8 @@ namespace MultiArrayTools IndexBase(const std::shared_ptr& range, size_t pos); virtual ~IndexBase() = default; + + virtual IndexType type() const = 0; virtual IndexBase& operator=(size_t pos) = 0; virtual IndexBase& operator++() = 0; @@ -45,6 +53,8 @@ namespace MultiArrayTools virtual bool last() const = 0; virtual bool first() const = 0; + + virtual std::shared_ptr getPtr(size_t n) const = 0; virtual operator size_t() const; diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index a540870..ea825c1 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -8,7 +8,43 @@ namespace MultiArrayTools { using namespace MultiArrayHelper; } - + + void seekIndexInst(std::shared_ptr i, std::vector >& ivec) + { + for(size_t inum = 0; inum != i->range()->dim(); ++inum){ + auto ii = i->getPtr(inum); + if(ii->type() == IndexType::MULTI){ + seekIndexInst(ii, ivec); + } + ivec.push_back(ii); + } + } + + BlockType getBlockType(std::shared_ptr i, + std::shared_ptr j, bool first) + { + BlockType out = BlockType::VALUE; + for(size_t inum = 0; inum != i->range()->dim(); ++inum){ + if(ii == j){ + if(inum == 0){ + out = BlockType::BLOCK; + } + else { + out = BlockType::SPLIT; + } + continue; + } + + if(ii->type() == IndexType::MULTI){ + BlockType tmp = getBlockType(ii, j, ivec); + if(tmp != BlockType::VALUE){ + out = tmp; + } + } + } + return out; + } + /********************************* * MultiArrayOperationBase * *********************************/ @@ -86,6 +122,20 @@ namespace MultiArrayTools { return mArrayRef.data()[ mIndex->pos() ]; } + + template + std::vector OperationMaster::block(const std::shared_ptr& blockIndex) const + { + // seek index with smallest number of SPLITs !!! + + } + + template + OperationMaster& OperationMaster::block() const + { + mBlockPtr->set( &mArrayRef[ (*mIndex) ] ); + return *this; + } /**************************** * ConstOperationRoot * @@ -104,7 +154,21 @@ namespace MultiArrayTools template const BlockBase& ConstOperationRoot::get() const { - return mArrayRef[ (*mIndex)() ]; + block(); + return *mBlockPtr; + } + + template + std::vector ConstOperationRoot::block(const std::shared_ptr& blockIndex) const + { + // !!! + } + + template + ConstOperationRoot& ConstOperationRoot::block() const + { + mBlockPtr->set( &mArrayRef[ (*mIndex)() ] ); + return *this; } /*********************** @@ -130,15 +194,30 @@ namespace MultiArrayTools template const BlockBase& OperationRoot::get() const { - return mArrayRef[ (*mIndex)() ]; + block(); + return *mBlockPtr; } template BlockBase& OperationRoot::get() { - return mArrayRef[ (*mIndex)() ]; + block(); + return *mBlockPtr; // issue: const !!! } + template + std::vector OperationRoot::block(const std::shared_ptr& blockIndex) const + { + // !!! + } + + template + OperationRoot& OperationRoot::block() const + { + mBlockPtr->set( &mArrayRef[ (*mIndex)() ] ); + return *this; + } + /*********************** * OperationRoot * ***********************/ @@ -154,4 +233,18 @@ namespace MultiArrayTools mRes = PackNum::template unpackArgs(mOps); return mRes; } + + template + std::vector Operation::block(const std::shared_ptr& blockIndex) const + { + // !!! + } + + template + Operation& Operation::block() const + { + mBlockPtr->set( &mArrayRef[ (*mIndex)() ] ); + return *this; + } + } diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index ed0612d..9e8fc69 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -29,6 +29,11 @@ namespace MultiArrayTools * OperationTemplate<...> * */ + + void seekIndexInst(std::shared_ptr i, std::vector >& ivec); + + BlockType getBlockType(std::shared_ptr i, + std::shared_ptr j, bool first); template class OperationBase @@ -40,7 +45,9 @@ namespace MultiArrayTools OperationBase() = default; virtual ~OperationBase() = default; - virtual OperationBase& block(const std::shared_ptr& blockIndex) = 0; + // init block, return resulting type (BLOCK, VALUE, SPLIT) + virtual std::vector block(const std::shared_ptr& blockIndex) const = 0; + virtual OperationBase& block() const = 0; // update block //virtual size_t argNum() const = 0; virtual const BlockBase& get() const = 0; @@ -104,6 +111,9 @@ namespace MultiArrayTools virtual BlockBase& get() override; virtual const BlockBase& get() const override; + virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual OperationMaster& block() const override; + protected: //void performAssignment(const OperationBase& in); @@ -129,6 +139,9 @@ namespace MultiArrayTools const std::shared_ptr&... indices); virtual const BlockBase& get() const override; + + virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual ConstOperationRoot& block() const override; protected: @@ -155,6 +168,9 @@ namespace MultiArrayTools virtual const BlockBase& get() const override; virtual BlockBase& get() override; + + virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual OperationRoot& block() const override; protected: @@ -176,7 +192,10 @@ namespace MultiArrayTools Operation(const Ops&... ops); virtual const BlockBase& get() const override; - + + virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual Operation& block() const override; + protected: std::tuple mOps; mutable BlockResult mRes; diff --git a/src/multi_range.cc b/src/multi_range.cc index 808e771..3fe6107 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -48,6 +48,12 @@ namespace MultiArrayTools PackNum::construct(mIPack, *range); IB::mPos = PackNum::makePos(mIPack); } + + template + IndexType MultiIndex::type() const + { + return IndexType::MULTI; + } template MultiIndex& MultiIndex::operator++() @@ -124,6 +130,17 @@ namespace MultiArrayTools return PackNum::getIndex(*t, n); } + template + std::shared_ptr MultiIndex::getPtr(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + MultiIndex const* t = this; + return PackNum::getIndexPtr(*t, n); + } + template typename MultiIndex::MetaType MultiIndex::meta() const { diff --git a/src/multi_range.h b/src/multi_range.h index 1d708d0..4aca29b 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -38,6 +38,8 @@ namespace MultiArrayTools template MultiIndex(const std::shared_ptr& range); + + virtual IndexType type() const override; virtual MultiIndex& operator++() override; virtual MultiIndex& operator--() override; @@ -56,6 +58,7 @@ namespace MultiArrayTools auto getPtr() const -> decltype( std::get( mIPack ) )&; const IndexBase& get(size_t n) const; + virtual std::shared_ptr getPtr(size_t n) const override; virtual MetaType meta() const override; virtual MultiIndex& at(const MetaType& metaPos) override; diff --git a/src/pack_num.h b/src/pack_num.h index ad0665d..f3d292f 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -21,7 +21,7 @@ namespace MultiArrayHelper static IndexBase& getIndex(IndexType& in, size_t n) { if(n == N){ - return in.getIndex(); + return in.template get(); } else { return PackNum::getIndex(in, n); @@ -32,12 +32,23 @@ namespace MultiArrayHelper static const IndexBase& getIndex(const IndexType& in, size_t n) { if(n == N){ - return in.getIndex(); + return in.template get(); } else { return PackNum::getIndex(in, n); } } + + template + static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) + { + if(n == N){ + return in.template getPtr(); + } + else { + return PackNum::getIndexPtr(in, n); + } + } template static inline void pp(std::tuple...>& ip) @@ -163,13 +174,19 @@ namespace MultiArrayHelper template static IndexBase& getIndex(MultiIndex& in, size_t n) { - return in.getIndex<0>(); + return in.template get<0>(); } template static const IndexBase& getIndex(const MultiIndex& in, size_t n) { - return in.getIndex<0>(); + return in.template get<0>(); + } + + template + static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) + { + return in.template getPtr<0>(); } template diff --git a/src/single_range.cc b/src/single_range.cc index 98cdab6..c494cdc 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -11,6 +11,12 @@ namespace MultiArrayTools SingleIndex::SingleIndex(const std::shared_ptr >& range) : IndexInterface(range, 0) {} + template + IndexType SingleIndex::type() const + { + return IndexType::SINGLE; + } + template SingleIndex& SingleIndex::operator=(size_t pos) { @@ -63,6 +69,12 @@ namespace MultiArrayTools return IB::mPos == 0; } + template + std::shared_ptr SingleIndex::getPtr(size_t n) const + { + return std::shared_ptr(); + } + /******************** * SingleRange * ********************/ diff --git a/src/single_range.h b/src/single_range.h index 577e78c..4ad8b44 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -26,6 +26,8 @@ namespace MultiArrayTools //DEFAULT_MEMBERS_X(SingleIndex); SingleIndex(const std::shared_ptr >& range); + + virtual IndexType type() const override; virtual SingleIndex& operator=(size_t pos) override; virtual SingleIndex& operator++() override; @@ -38,6 +40,8 @@ namespace MultiArrayTools virtual bool last() const override; virtual bool first() const override; + virtual std::shared_ptr getPtr(size_t n) const override; + virtual std::string id() const override { return std::string("sin") + std::to_string(IB::mId); } };