diff --git a/src/base_def.h b/src/base_def.h index 1aefc47..e5cdf67 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -117,43 +117,7 @@ namespace MultiArrayTools // multi_array.h template class MultiArray; - - // block.h - template - class BlockBase; - - // block.h - template - class MutableBlockBase; - - // block.h - template - class Block; - - // block.h - template - class MBlock; - - // block.h - template - class BlockValue; - - // block.h - template - class MBlockValue; - - // block.h - template - class SplitBlock; - - // block.h - template - class MSplitBlock; - - // block.h - template - class BlockResult; - + // multi_array_operation.h template class OperationBase; @@ -163,7 +127,7 @@ namespace MultiArrayTools class MutableOperationBase; // multi_array_operation.h - template + template class OperationTemplate; // multi_array_operation.h @@ -222,4 +186,47 @@ namespace MultiArrayTools */ } +namespace MultiArrayHelper +{ + // block.h + enum class BlockType; + + // block.h + template + class BlockBase; + + // block.h + template + class MutableBlockBase; + + // block.h + template + class Block; + + // block.h + template + class MBlock; + + // block.h + template + class BlockValue; + + // block.h + template + class MBlockValue; + + // block.h + template + class SplitBlock; + + // block.h + template + class MSplitBlock; + + // block.h + template + class BlockResult; + +} + #endif diff --git a/src/block.cc b/src/block.cc index 0eb0b52..7d8dfc9 100644 --- a/src/block.cc +++ b/src/block.cc @@ -20,7 +20,7 @@ namespace MultiArrayHelper template template - BlockResult BlockBase::operate(const BlockBase& in) + BlockResult BlockBase::operate(const BlockBase& in) { assert(mSize == in.size()); OpFunction f; @@ -30,27 +30,28 @@ namespace MultiArrayHelper } return res; } - + + template - BlockResult BlockBase::operator+(const BlockBase& in) + BlockResult BlockBase::operator+(const BlockBase& in) { return operate >(in); } template - BlockResult BlockBase::operator-(const BlockBase& in) + BlockResult BlockBase::operator-(const BlockBase& in) { return operate >(in); } template - BlockResult BlockBase::operator*(const BlockBase& in) + BlockResult BlockBase::operator*(const BlockBase& in) { return operate >(in); } template - BlockResult BlockBase::operator/(const BlockBase& in) + BlockResult BlockBase::operator/(const BlockBase& in) { return operate >(in); } @@ -61,6 +62,15 @@ namespace MultiArrayHelper template MutableBlockBase::MutableBlockBase(size_t size) : BlockBase(size) {} + + template + MutableBlockBase& MutableBlockBase::operator=(const BlockBase& in) + { + for(size_t i = 0; i != BlockBase::mSize; ++i){ + (*this)[i] = in[i]; + } + return *this; + } /************* * Block * @@ -96,11 +106,11 @@ namespace MultiArrayHelper **************/ template - MBlock::MBlock(const std::vector& data, - size_t begPos, size_t size) : + MBlock::MBlock(std::vector& data, + size_t begPos, size_t size) : MutableBlockBase(size), mBegPtr(data.data() + begPos) {} - + template BlockType MBlock::type() const { @@ -159,7 +169,7 @@ namespace MultiArrayHelper *******************/ template - MBlockValue::MBlockValue(const T& val, size_t size) : + MBlockValue::MBlockValue(T& val, size_t size) : BlockBase(size), mVal(val) {} @@ -223,8 +233,8 @@ namespace MultiArrayHelper *******************/ template - MSplitBlock::MSplitBlock(const std::vector& data, size_t begPos, - size_t stepSize, size_t size) : + MSplitBlock::MSplitBlock(std::vector& data, size_t begPos, + size_t stepSize, size_t size) : BlockBase(size), mStepSize(stepSize), mBegPtr(data.data() + begPos) {} diff --git a/src/block.h b/src/block.h index e5d13a0..174b009 100644 --- a/src/block.h +++ b/src/block.h @@ -1,7 +1,7 @@ // -*- C++ -*- -#ifndef __block_h__ -#define __block_h__ +#ifndef __ma_block_h__ +#define __ma_block_h__ #include #include @@ -16,7 +16,7 @@ namespace MultiArrayHelper BLOCK = 1, VALUE = 2, SPLIT = 3, - RESULT = 4, + RESULT = 4 }; // manage vectorization in the future !! @@ -28,6 +28,9 @@ namespace MultiArrayHelper BlockBase() = default; BlockBase(size_t size); + BlockBase(BlockBase&& res) = default; + BlockBase& operator=(BlockBase&& res) = default; + virtual BlockType type() const = 0; virtual size_t size() const; @@ -37,7 +40,7 @@ namespace MultiArrayHelper template BlockResult operate(const BlockBase& in); - + BlockResult operator+(const BlockBase& in); BlockResult operator-(const BlockBase& in); BlockResult operator*(const BlockBase& in); @@ -54,8 +57,13 @@ namespace MultiArrayHelper MutableBlockBase() = default; MutableBlockBase(size_t size); + + MutableBlockBase& operator=(const BlockBase& in); - virtual T& operator[](size_t pos) override; + MutableBlockBase(MutableBlockBase&& res) = default; + MutableBlockBase& operator=(MutableBlockBase&& res) = default; + + virtual T& operator[](size_t pos) = 0; }; @@ -84,7 +92,7 @@ namespace MultiArrayHelper virtual BlockType type() const override; virtual const T& operator[](size_t pos) const override; virtual T& operator[](size_t pos) override; - virtual Block& set(const T* nbeg) override; + virtual MBlock& set(const T* nbeg) override; protected: T* mBegPtr; @@ -115,7 +123,7 @@ namespace MultiArrayHelper virtual BlockType type() const override; virtual const T& operator[](size_t pos) const override; virtual T& operator[](size_t pos) override; - virtual BlockValue& set(const T* nbeg) override; + virtual MBlockValue& set(const T* nbeg) override; protected: T& mVal; @@ -151,7 +159,7 @@ namespace MultiArrayHelper virtual BlockType type() const override; virtual const T& operator[](size_t pos) const override; virtual T& operator[](size_t pos) override; - virtual SplitBlock& set(const T* nbeg) override; + virtual MSplitBlock& set(const T* nbeg) override; protected: size_t mStepSize; @@ -166,6 +174,9 @@ namespace MultiArrayHelper BlockResult() = default; BlockResult(size_t size); + BlockResult(BlockResult&& res) = default; + BlockResult& operator=(BlockResult&& res) = default; + virtual BlockType type() const override; virtual const T& operator[](size_t pos) const override; virtual T& operator[](size_t i) override; diff --git a/src/container_range.cc b/src/container_range.cc index ac0698f..b660671 100644 --- a/src/container_range.cc +++ b/src/container_range.cc @@ -79,17 +79,17 @@ namespace MultiArrayTools } template - size_t ContainerIndex::pp(std::shared_ptr& idxPtr) + size_t ContainerIndex::pp(std::shared_ptr& idxPtr) { - size_t tmp = pp(mIPack, mBlockSizes, idxPtr); + size_t tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); IB::mPos += tmp; return tmp; } template - size_t ContainerIndex::mm(std::shared_ptr& idxPtr) + size_t ContainerIndex::mm(std::shared_ptr& idxPtr) { - size_t tmp = mm(mIPack, mBlockSizes, idxPtr); + size_t tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); IB::mPos -= tmp; return tmp; } @@ -140,7 +140,7 @@ namespace MultiArrayTools } template - std::shared_ptr ContainerIndex::getPtr(size_t n) const + std::shared_ptr ContainerIndex::getPtr(size_t n) const { if(n >= sizeof...(Indices)){ assert(0); @@ -149,7 +149,18 @@ namespace MultiArrayTools ContainerIndex const* t = this; return PackNum::getIndexPtr(*t, n); } + + template + size_t ContainerIndex::getStepSize(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return mBlockSizes[n+1]; + } + template bool ContainerIndex::first() const { diff --git a/src/container_range.h b/src/container_range.h index c5bc32a..87c5753 100644 --- a/src/container_range.h +++ b/src/container_range.h @@ -28,7 +28,8 @@ namespace MultiArrayTools protected: bool mExternControl = false; IndexPack mIPack; - + std::array mBlockSizes; + public: ContainerIndex() = delete; @@ -44,8 +45,8 @@ namespace MultiArrayTools virtual ContainerIndex& operator--() override; virtual ContainerIndex& operator=(size_t pos) override; - virtual size_t pp(std::shared_ptr& idxPtr) override; - virtual size_t mm(std::shared_ptr& idxPtr) override; + virtual size_t pp(std::shared_ptr& idxPtr) override; + virtual size_t mm(std::shared_ptr& idxPtr) override; virtual MetaType meta() const override; virtual ContainerIndex& at(const MetaType& metaPos) override; @@ -63,7 +64,8 @@ namespace MultiArrayTools template auto getPtr() const -> decltype( std::get( mIPack ) )&; - virtual std::shared_ptr getPtr(size_t n) const override; + virtual std::shared_ptr getPtr(size_t n) const override; + virtual size_t getStepSize(size_t n) const override; ContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices diff --git a/src/index_base.cc b/src/index_base.cc index 545d0b5..6d31d79 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -39,12 +39,17 @@ namespace MultiArrayTools return mLocked; } - IndexBase& IndexBase::lock(std::shared_ptr& idx) + IndexBase& IndexBase::lock(std::shared_ptr& idx) { mLocked = (idx.get() == this); return *this; } */ + + std::shared_ptr IndexBase::rangePtr() const + { + return mRangePtr; + } IndexBase::operator size_t() const { diff --git a/src/index_base.h b/src/index_base.h index f84a47c..a0d0f7b 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -44,8 +44,8 @@ namespace MultiArrayTools virtual IndexBase& operator++() = 0; virtual IndexBase& operator--() = 0; - virtual size_t pp(std::shared_ptr& idxPtr) = 0; - virtual size_t mm(std::shared_ptr& idxPtr) = 0; + virtual size_t pp(std::shared_ptr& idxPtr) = 0; + virtual size_t mm(std::shared_ptr& idxPtr) = 0; bool operator==(const IndexBase& in) const; bool operator!=(const IndexBase& in) const; @@ -58,9 +58,12 @@ namespace MultiArrayTools virtual bool first() const = 0; //virtual bool locked() const; - //virtual IndexBase& lock(std::shared_ptr& idx); - - virtual std::shared_ptr getPtr(size_t n) const = 0; + //virtual IndexBase& lock(std::shared_ptr& idx); + + virtual std::shared_ptr rangePtr() const; + virtual std::shared_ptr getPtr(size_t n) const = 0; + + virtual size_t getStepSize(size_t n) const = 0; virtual operator size_t() const; diff --git a/src/multi_array_header.h b/src/multi_array_header.h index dbfd3e5..5fa3f05 100644 --- a/src/multi_array_header.h +++ b/src/multi_array_header.h @@ -10,6 +10,7 @@ #include "single_range.h" #include "multi_range.h" #include "container_range.h" +#include "block.h" #include "multi_array_operation.h" #include "multi_array.h" //#include "slice.h" diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 973990d..a48568b 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -9,9 +9,9 @@ namespace MultiArrayTools using namespace MultiArrayHelper; } - void seekIndexInst(std::shared_ptr i, std::vector >& ivec) + void seekIndexInst(std::shared_ptr i, std::vector >& ivec) { - for(size_t inum = 0; inum != i->range()->dim(); ++inum){ + for(size_t inum = 0; inum != i->rangePtr()->dim(); ++inum){ auto ii = i->getPtr(inum); if(ii->type() == IndexType::MULTI){ seekIndexInst(ii, ivec); @@ -20,16 +20,15 @@ namespace MultiArrayTools } } - // !!! - BTSS getBlockType(std::shared_ptr i, - std::shared_ptr j, bool first) + BTSS getBlockType(std::shared_ptr i, + std::shared_ptr j, bool first) { // returning BlockType and step size is redundant (change in the future) // stepSize == 0 => VALUE // stepSize == 1 => BLOCK // stepSize > 1 => SPLIT :) BTSS out(BlockType::VALUE, 0); - for(size_t inum = 0; inum != i->range()->dim(); ++inum){ + for(size_t inum = 0; inum != i->rangePtr()->dim(); ++inum){ auto ii = i->getPtr(inum); if(ii == j){ if(inum == 0 and first){ @@ -79,6 +78,62 @@ namespace MultiArrayTools return std::make_shared >(vec, 0, stepSize, blockSize); } } + + size_t getBTNum(const std::vector& mp, BlockType bt) + { + size_t out = 0; + for(auto& xx: mp){ + if(xx.first == bt){ + ++out; + } + } + return out; + } + + void minimizeAppearanceOfType(std::map, std::vector > mp, + BlockType bt) + { + size_t minNum = getBTNum( mp.begin()->second, bt ); + for(auto& mm: mp){ + size_t tmp = getBTNum( mm.second, bt ); + if(tmp < minNum){ + minNum = tmp; + } + } + + for(auto mit = mp.begin(); mit != mp.end(); ){ + size_t tmp = getBTNum( mit->second, bt ); + if(tmp > minNum){ + mit = mp.erase(mit); + } + else { + ++mit; + } + } + + } + + template + std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, + const OperationBase& second) + { + std::vector > ivec; + seekIndexInst(ownIdx, ivec); + std::map, std::vector > mp; + + for(auto& xx: ivec){ + mp[xx] = second.block(xx); + } + + // seek minimal number of VALUEs => guarantees absence of conflicting blocks + minimizeAppearanceOfType(mp, BlockType::VALUE); + + // seek mininmal number of SPLITs => maximal vectorization possible + minimizeAppearanceOfType(mp, BlockType::SPLIT); + + return mp.begin()->first; + } + /********************************* * MultiArrayOperationBase * @@ -91,39 +146,39 @@ namespace MultiArrayTools * OperationTemplate * ***************************/ - template - OperationTemplate::OperationTemplate(OperationClass* oc) : mOc(oc) {} + template + OperationTemplate::OperationTemplate(OperationClass* oc) : mOc(oc) {} - template + template template - auto OperationTemplate::operator+(const Second& in) const - -> Operation,OperationClass,Second> + auto OperationTemplate::operator+(const Second& in) const + -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(*mOc, in); } - template + template template - auto OperationTemplate::operator-(const Second& in) const - -> Operation,OperationClass,Second> + auto OperationTemplate::operator-(const Second& in) const + -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(*mOc, in); } - template + template template - auto OperationTemplate::operator*(const Second& in) const - -> Operation,OperationClass,Second> + auto OperationTemplate::operator*(const Second& in) const + -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(*mOc, in); } - template + template template - auto OperationTemplate::operator/(const Second& in) const - -> Operation,OperationClass,Second> + auto OperationTemplate::operator/(const Second& in) const + -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(*mOc, in); } /************************* @@ -142,16 +197,16 @@ namespace MultiArrayTools mIndex = std::make_shared( mr->begin() ); (*mIndex) = *index; - auto blockIndex = seekBlockIndex(mIndex, second); + auto blockIndex = seekBlockIndex( mIndex, second); block(blockIndex); second.block(blockIndex); - for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndex) )){ + for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndex) ){ get() = mSecond.get(); } } template - BlockBase& OperationMaster::get() + MutableBlockBase& OperationMaster::get() { block(); return *mBlockPtr; @@ -165,7 +220,7 @@ namespace MultiArrayTools } template - std::vector OperationMaster::block(const std::shared_ptr& blockIndex) const + std::vector OperationMaster::block(const std::shared_ptr blockIndex) const { std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); mBlockPtr = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); @@ -187,7 +242,7 @@ namespace MultiArrayTools ConstOperationRoot:: ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices) : - OperationBase(), OperationTemplate >(this), + OperationBase(), OperationTemplate >(this), mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) { mIndex(indices...); @@ -201,7 +256,7 @@ namespace MultiArrayTools } template - std::vector ConstOperationRoot::block(const std::shared_ptr& blockIndex) const + std::vector ConstOperationRoot::block(const std::shared_ptr blockIndex) const { std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); mBlockPtr = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); @@ -223,7 +278,7 @@ namespace MultiArrayTools OperationRoot:: OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices) : - MutableOperationBase(), OperationTemplate >(this), + MutableOperationBase(), OperationTemplate >(this), mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) { (*mIndex)(indices...); @@ -243,14 +298,14 @@ namespace MultiArrayTools } template - BlockBase& OperationRoot::get() + MutableBlockBase& OperationRoot::get() { block(); return *mBlockPtr; } template - std::vector OperationRoot::block(const std::shared_ptr& blockIndex) const + std::vector OperationRoot::block(const std::shared_ptr blockIndex) const { std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); mBlockPtr = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); @@ -270,7 +325,7 @@ namespace MultiArrayTools template Operation::Operation(const Ops&... ops) : - OperationBase(), OperationTemplate >(this), + OperationBase(), OperationTemplate >(this), mOps(ops...) {} template @@ -280,18 +335,18 @@ namespace MultiArrayTools return mRes; } - template - std::vector Operation::block(const std::shared_ptr& blockIndex) const + template + std::vector Operation::block(const std::shared_ptr blockIndex) const { std::vector btv; - PackNum::makeBlockTypeVec(btv, mOps, blockIndex); + PackNum::makeBlockTypeVec(btv, mOps, blockIndex); return btv; } - template - Operation& Operation::block() const + template + Operation& Operation::block() const { - mBlockPtr->set( &mArrayRef[ (*mIndex)() ] ); + //mBlockPtr->set( &mArrayRef[ (*mIndex)() ] ); return *this; } diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 6e6703e..0c50527 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -32,14 +32,14 @@ namespace MultiArrayTools * */ - void seekIndexInst(std::shared_ptr i, std::vector >& ivec); + void seekIndexInst(std::shared_ptr i, std::vector >& ivec); // typedef std::pair BTSS; - BTSS getBlockType(std::shared_ptr i, - std::shared_ptr j, bool first); + BTSS getBlockType(std::shared_ptr i, + std::shared_ptr j, bool first); template std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize); @@ -47,73 +47,26 @@ namespace MultiArrayTools template std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize); - size_t getBTNum(const std::vector& mp, BlockType bt) - { - size_t out = 0; - for(auto& xx: mp){ - if(xx.first == bt){ - ++out; - } - } - return out; - } + size_t getBTNum(const std::vector& mp, BlockType bt); - void minimizeAppearanceOfType(std::map, std::vector > mp, - BlockType bt) - { - size_t minNum = getBTNum( *mp.begin(), bt ); - for(auto& mm: mp){ - size_t tmp = getBTNum( mm.second, bt ); - if(tmp < minNum){ - minNum = tmp; - } - } + void minimizeAppearanceOfType(std::map, std::vector > mp, + BlockType bt); - for(auto mit = mp.begin(); mit != mp.end(); ){ - size_t tmp = getBTNum( mit->second, bt ); - if(tmp > minNum){ - mit = mp.erase(mit); - } - else { - ++mit; - } - } - - } - template - std::shared_ptr seekBlockIndex(std::shared_ptr& ownIdx, - const OperationBase& second) - { - std::vector > ivec; - seekIndexInst(ownIdx, ivec); - std::map, std::vector > mp; - - for(auto& xx: ivec){ - mp[xx] = second.block(xx); - } - - // seek minimal number of VALUEs => guarantees absence of conflicting blocks - minimizeAppearanceOfType(mp, BlockType::VALUE); - - // seek mininmal number of SPLITs => maximal vectorization possible - minimizeAppearanceOfType(mp, BlockType::SPLIT); - - return *mp.begin(); - } + std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, + const OperationBase& second); template class OperationBase { public: - typedef T value_type; - + OperationBase() = default; virtual ~OperationBase() = default; // init block, return resulting type (BLOCK, VALUE, SPLIT) - virtual std::vector block(const std::shared_ptr& blockIndex) const = 0; + virtual std::vector block(const std::shared_ptr blockIndex) const = 0; virtual OperationBase& block() const = 0; // update block //virtual size_t argNum() const = 0; @@ -127,33 +80,33 @@ namespace MultiArrayTools typedef T value_type; MutableOperationBase() = default; - virtual BlockBase& get() = 0; + + virtual MutableBlockBase& get() = 0; + }; - template + template class OperationTemplate { public: - - typedef typename OperationClass::value_type value_type; OperationTemplate(OperationClass* oc); template auto operator+(const Second& in) const - -> Operation,OperationClass,Second>; + -> Operation,OperationClass,Second>; template auto operator-(const Second& in) const - -> Operation,OperationClass,Second>; + -> Operation,OperationClass,Second>; template auto operator*(const Second& in) const - -> Operation,OperationClass,Second>; + -> Operation,OperationClass,Second>; template auto operator/(const Second& in) const - -> Operation,OperationClass,Second>; + -> Operation,OperationClass,Second>; private: OperationClass* mOc; @@ -175,7 +128,7 @@ namespace MultiArrayTools virtual MutableBlockBase& get() override; virtual const BlockBase& get() const override; - virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual std::vector block(const std::shared_ptr blockIndex) const override; virtual OperationMaster& block() const override; protected: @@ -184,19 +137,19 @@ namespace MultiArrayTools OperationBase const& mSecond; MutableMultiArrayBase& mArrayRef; std::shared_ptr mIndex; - mutable std::shared_ptr mBlockPtr; + mutable std::shared_ptr > mBlockPtr; }; template class ConstOperationRoot : public OperationBase, - public OperationTemplate > + public OperationTemplate > { public: typedef T value_type; typedef OperationBase OB; - typedef OperationTemplate > OT; + typedef OperationTemplate > OT; typedef ContainerRange CRange; typedef typename CRange::IndexType IndexType; @@ -205,25 +158,25 @@ namespace MultiArrayTools virtual const BlockBase& get() const override; - virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual std::vector block(const std::shared_ptr blockIndex) const override; virtual ConstOperationRoot& block() const override; protected: MultiArrayBase const& mArrayRef; std::shared_ptr mIndex; - mutable std::shared_ptr mBlockPtr; + mutable std::shared_ptr > mBlockPtr; }; template class OperationRoot : public MutableOperationBase, - public OperationTemplate > + public OperationTemplate > { public: typedef T value_type; typedef OperationBase OB; - typedef OperationTemplate > OT; + typedef OperationTemplate > OT; typedef ContainerRange CRange; typedef typename CRange::IndexType IndexType; @@ -235,32 +188,32 @@ namespace MultiArrayTools virtual const BlockBase& get() const override; virtual MutableBlockBase& get() override; - virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual std::vector block(const std::shared_ptr blockIndex) const override; virtual OperationRoot& block() const override; protected: MutableMultiArrayBase& mArrayRef; std::shared_ptr mIndex; - mutable std::shared_ptr mBlockPtr; + mutable std::shared_ptr > mBlockPtr; }; template class Operation : public OperationBase, - public OperationTemplate > + public OperationTemplate > { public: typedef T value_type; typedef OperationBase OB; - typedef OperationTemplate > OT; + typedef OperationTemplate > OT; typedef OpFunction F; Operation(const Ops&... ops); virtual const BlockBase& get() const override; - virtual std::vector block(const std::shared_ptr& blockIndex) const override; + virtual std::vector block(const std::shared_ptr blockIndex) const override; virtual Operation& block() const override; protected: diff --git a/src/multi_range.cc b/src/multi_range.cc index ef7f994..13aa158 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -81,17 +81,17 @@ namespace MultiArrayTools } template - size_t MultiIndex::pp(std::shared_ptr& idxPtr) + size_t MultiIndex::pp(std::shared_ptr& idxPtr) { - size_t tmp = pp(mIPack, mBlockSizes, idxPtr); + size_t tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); IB::mPos += tmp; return tmp; } template - size_t MultiIndex::mm(std::shared_ptr& idxPtr) + size_t MultiIndex::mm(std::shared_ptr& idxPtr) { - size_t tmp = mm(mIPack, mBlockSizes, idxPtr); + size_t tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); IB::mPos -= tmp; return tmp; } @@ -148,7 +148,7 @@ namespace MultiArrayTools } template - std::shared_ptr MultiIndex::getPtr(size_t n) const + std::shared_ptr MultiIndex::getPtr(size_t n) const { if(n >= sizeof...(Indices)){ assert(0); @@ -158,7 +158,8 @@ namespace MultiArrayTools return PackNum::getIndexPtr(*t, n); } - size_t getStepSize(size_t n) const + template + size_t MultiIndex::getStepSize(size_t n) const { if(n >= sizeof...(Indices)){ assert(0); @@ -202,7 +203,7 @@ namespace MultiArrayTools } /* template - MultiIndex& MultiIndex::lock(std::shared_ptr& idx) + MultiIndex& MultiIndex::lock(std::shared_ptr& idx) { IB::mLocked = (idx.get() == this); PackNum::lock(mIPack, idx); diff --git a/src/multi_range.h b/src/multi_range.h index 7783966..5cd348f 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -46,8 +46,8 @@ namespace MultiArrayTools virtual MultiIndex& operator--() override; virtual MultiIndex& operator=(size_t pos) override; - virtual size_t pp(std::shared_ptr& idxPtr) override; - virtual size_t mm(std::shared_ptr& idxPtr) override; + virtual size_t pp(std::shared_ptr& idxPtr) override; + virtual size_t mm(std::shared_ptr& idxPtr) override; template MultiIndex& up(); @@ -62,7 +62,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 std::shared_ptr getPtr(size_t n) const override; virtual size_t getStepSize(size_t n) const override; virtual MetaType meta() const override; @@ -75,7 +75,7 @@ namespace MultiArrayTools std::shared_ptr range() const; - //virtual MultiIndex& lock(std::shared_ptr& idx) override; + //virtual MultiIndex& lock(std::shared_ptr& idx) override; // raplace instances (in contrast to its analogon in ContainerIndex // MultiIndices CANNOT be influences be its subindices, so there is diff --git a/src/pack_num.h b/src/pack_num.h index 63d6018..4d888fb 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -52,7 +52,7 @@ namespace MultiArrayHelper template static void lock(std::tuple...>& ip, - std::shared_ptr& toLock) + std::shared_ptr toLock) { std::get(ip)->lock(toLock); PackNum::lock(ip, toLock); @@ -87,7 +87,7 @@ namespace MultiArrayHelper template static inline size_t pp(std::tuple...>& ip, std::array& bs, - std::shared_ptr& idxPtr) + std::shared_ptr idxPtr) { auto siPtr = std::get(ip); if(siPtr.get() == idxPtr.get()){ @@ -109,7 +109,7 @@ namespace MultiArrayHelper { auto& si = *std::get(ip); if(si.first()){ - si = si.max(); + si = si.max() - 1; PackNum::mm(ip); } else { @@ -120,7 +120,7 @@ namespace MultiArrayHelper template static inline size_t mm(std::tuple...>& ip, std::array& bs, - std::shared_ptr& idx) + std::shared_ptr idxPtr) { auto siPtr = std::get(ip); if(siPtr.get() == idxPtr.get()){ @@ -128,10 +128,11 @@ namespace MultiArrayHelper } else { if(siPtr->first()){ + (*siPtr) = siPtr->max() - 1; return PackNum::mm(ip, bs) - siPtr->max() + 1; } else { - return siPtr->pp(idx); + return siPtr->mm(idxPtr); } } } @@ -229,7 +230,7 @@ namespace MultiArrayHelper template static void makeBlockTypeVec(std::vector >& btv, const std::tuple& ops, - std::shared_ptr& idxPtr) + std::shared_ptr idxPtr) { auto& subvec = std::get(ops).block(idxPtr); btv.insert(btv.end(), subvec.begin(), subvec.end() ); @@ -261,7 +262,7 @@ namespace MultiArrayHelper template static void lock(std::tuple...>& ip, - std::shared_ptr& toLock) + std::shared_ptr toLock) { std::get<0>(ip)->lock(toLock); } @@ -282,21 +283,20 @@ namespace MultiArrayHelper template static inline size_t pp(std::tuple...>& ip, - std::array& bs) + std::array& bs, + std::shared_ptr idxPtr) { - auto& si = *std::get<0>(ip); - if(si.locked()){ - si = si.max()+1; - return std::get<0>(bs) - std::get<1>(bs) + 1; + auto siPtr = std::get<0>(ip); + if(siPtr.get() == idxPtr.get()){ + return std::get<0>(bs); } - else { - ++si; - return 1; + else { + return siPtr->pp(idxPtr); } } template - static inline size_t mm(std::tuple...>& ip) + static inline void mm(std::tuple...>& ip) { auto& si = *std::get<0>(ip); --si; @@ -304,17 +304,15 @@ namespace MultiArrayHelper template static inline size_t mm(std::tuple...>& ip, - std::array& bs) + std::array& bs, + std::shared_ptr idxPtr) { - auto& si = *std::get<0>(ip); - if(si.locked()){ - si = 0; - --si; - return std::get<0>(bs) - std::get<1>(bs) + 1; + auto siPtr = std::get<0>(ip); + if(siPtr.get() == idxPtr.get()){ + return std::get<0>(bs); } - else { - --si; - return 1; + else { + return siPtr->mm(idxPtr); } } @@ -405,7 +403,7 @@ namespace MultiArrayHelper template static void makeBlockTypeVec(std::vector >& btv, const std::tuple& ops, - std::shared_ptr& idxPtr) + std::shared_ptr idxPtr) { auto& subvec = std::get<0>(ops).block(idxPtr); btv.insert(btv.end(), subvec.begin(), subvec.end() ); diff --git a/src/single_range.cc b/src/single_range.cc index a230918..7009cc0 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -39,14 +39,14 @@ namespace MultiArrayTools } template - size_t SingleIndex::pp(std::shared_ptr& idxPtr) + size_t SingleIndex::pp(std::shared_ptr& idxPtr) { ++(*this); return 1; } template - size_t SingleIndex::mm(std::shared_ptr& idxPtr) + size_t SingleIndex::mm(std::shared_ptr& idxPtr) { --(*this); return 1; @@ -84,9 +84,15 @@ namespace MultiArrayTools } template - std::shared_ptr SingleIndex::getPtr(size_t n) const + std::shared_ptr SingleIndex::getPtr(size_t n) const { - return std::shared_ptr(); + return std::shared_ptr(); + } + + template + size_t SingleIndex::getStepSize(size_t n) const + { + return 1; } /******************** diff --git a/src/single_range.h b/src/single_range.h index e481668..b9d86c0 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -33,8 +33,8 @@ namespace MultiArrayTools virtual SingleIndex& operator++() override; virtual SingleIndex& operator--() override; - virtual size_t pp(std::shared_ptr& idxPtr) override; - virtual size_t mm(std::shared_ptr& idxPtr) override; + virtual size_t pp(std::shared_ptr& idxPtr) override; + virtual size_t mm(std::shared_ptr& idxPtr) override; virtual U meta() const override; virtual SingleIndex& at(const U& metaPos) override; @@ -43,7 +43,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::shared_ptr getPtr(size_t n) const override; + virtual size_t getStepSize(size_t n) const override; virtual std::string id() const override { return std::string("sin") + std::to_string(IB::mId); } };