From 3e502b24962b35c81cd2eb2723c96c0b75f2838a Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 31 Oct 2017 19:22:34 +0100 Subject: [PATCH] remove virtual stuff in block and operation classes --- src/base_def.h | 4 +- src/block.cc | 222 ++++++----------------------------- src/block.h | 143 +++++----------------- src/multi_array_operation.cc | 71 +++++------ src/multi_array_operation.h | 69 +++++------ src/pack_num.h | 2 +- 6 files changed, 133 insertions(+), 378 deletions(-) diff --git a/src/base_def.h b/src/base_def.h index e1d8d57..0e9723c 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -131,7 +131,7 @@ namespace MultiArrayTools class OperationTemplate; // multi_array_operation.h - template + template class OperationMaster; // multi_array_operation.h @@ -189,7 +189,7 @@ namespace MultiArrayTools namespace MultiArrayHelper { // block.h - template + template class BlockBinaryOp; // block.h diff --git a/src/block.cc b/src/block.cc index 930205d..ea91bc2 100644 --- a/src/block.cc +++ b/src/block.cc @@ -9,9 +9,10 @@ namespace MultiArrayHelper * BlockBinaryOp * *********************/ - template - BlockResult BlockBinaryOp::operator()(const BlockBase& arg1, - const BlockBase& arg2) + template + BlockResult + BlockBinaryOp::operator()(const BlockClass1& arg1, + const BlockClass2& arg2) { static OpFunc f; BlockResult res(arg1.size()); @@ -80,14 +81,6 @@ 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 * @@ -95,22 +88,24 @@ namespace MultiArrayHelper template Block::Block(const std::vector& data, - size_t begPos, size_t size) : + size_t begPos, size_t size, size_t stepSize) : BlockBase(size), mData(&data), - mBegPtr(data.data() + begPos) {} + mBegPtr(data.data() + begPos), + mStepSize(stepSize) {} template BlockType Block::type() const { - return BlockType::BLOCK; + return mStepSize == 0 ? BlockType::VALUE : + ( mStepSize == 1 ? BlockType::BLOCK : BlockType::SPLIT ); } template const T& Block::operator[](size_t i) const { - return *(mBegPtr + i); + return *(mBegPtr + i * mStepSize); } template @@ -132,29 +127,41 @@ namespace MultiArrayHelper template MBlock::MBlock(std::vector& data, - size_t begPos, size_t size) : + size_t begPos, size_t size, size_t stepSize) : MutableBlockBase(size), mData(&data), - mBegPtr(data.data() + begPos) {} + mBegPtr(data.data() + begPos), + mStepSize(stepSize) {} + + template + template + MBlock& MBlock::operator=(const BlockClass& in) + { + for(size_t i = 0; i != BlockBase::mSize; ++i){ + (*this)[i] = in[i]; + } + return *this; + } template BlockType MBlock::type() const { - return BlockType::BLOCK; + return mStepSize == 0 ? BlockType::VALUE : + ( mStepSize == 1 ? BlockType::BLOCK : BlockType::SPLIT ); } template const T& MBlock::operator[](size_t i) const { - return *(mBegPtr + i); + return *(mBegPtr + i * mStepSize); } template T& MBlock::operator[](size_t i) { - return *(mBegPtr + i); + return *(mBegPtr + i * mStepSize); } template @@ -170,170 +177,6 @@ namespace MultiArrayHelper return 1; } - /****************** - * BlockValue * - ******************/ - - template - BlockValue::BlockValue(const std::vector& data, - size_t pos, size_t size) : - BlockBase(size), - mData(&data), - mVal(&data[pos]) {} - - template - BlockType BlockValue::type() const - { - return BlockType::VALUE; - } - - template - const T& BlockValue::operator[](size_t i) const - { - - return *mVal; - } - - template - BlockValue& BlockValue::set(size_t npos) - { - mVal = &(*mData)[npos]; - return *this; - } - - template - size_t BlockValue::stepSize() const - { - return 0; - } - - /******************* - * MBlockValue * - *******************/ - - template - MBlockValue::MBlockValue(std::vector& data, - size_t pos, size_t size) : - MutableBlockBase(size), - mData(&data), - mVal(&data[pos]) {} - - template - BlockType MBlockValue::type() const - { - return BlockType::VALUE; - } - - template - const T& MBlockValue::operator[](size_t i) const - { - - return *mVal; - } - - template - T& MBlockValue::operator[](size_t i) - { - - return *mVal; - } - - template - MBlockValue& MBlockValue::set(size_t npos) - { - mVal = &(*mData)[npos]; - return *this; - } - - template - size_t MBlockValue::stepSize() const - { - return 0; - } - - /****************** - * SplitBlock * - ******************/ - - template - SplitBlock::SplitBlock(const std::vector& data, size_t begPos, - size_t stepSize, size_t size) : - BlockBase(size), - mData(&data), - mStepSize(stepSize), - mBegPtr(&data[begPos]) {} - - template - BlockType SplitBlock::type() const - { - return BlockType::SPLIT; - } - - template - const T& SplitBlock::operator[](size_t pos) const - { - - return *(mBegPtr + pos*mStepSize); - } - - template - SplitBlock& SplitBlock::set(size_t npos) - { - mBegPtr = &(*mData)[npos]; - return *this; - } - - template - size_t SplitBlock::stepSize() const - { - return mStepSize; - } - - /******************* - * MSplitBlock * - *******************/ - - template - MSplitBlock::MSplitBlock(std::vector& data, size_t begPos, - size_t stepSize, size_t size) : - MutableBlockBase(size), - mData(&data), - mStepSize(stepSize), - mBegPtr(&data[begPos]) {} - - template - BlockType MSplitBlock::type() const - { - return BlockType::SPLIT; - } - - template - const T& MSplitBlock::operator[](size_t pos) const - { - - return *(mBegPtr + pos*mStepSize); - } - - template - T& MSplitBlock::operator[](size_t pos) - { - - return *(mBegPtr + pos*mStepSize); - } - - template - MSplitBlock& MSplitBlock::set(size_t npos) - { - mBegPtr = &(*mData)[npos]; - return *this; - } - - template - size_t MSplitBlock::stepSize() const - { - return mStepSize; - } - /******************* * BlockResult * *******************/ @@ -343,6 +186,16 @@ namespace MultiArrayHelper MutableBlockBase(size), mRes(size) {} + template + template + BlockResult& BlockResult::operator=(const BlockClass& in) + { + for(size_t i = 0; i != BlockBase::mSize; ++i){ + (*this)[i] = in[i]; + } + return *this; + } + template BlockType BlockResult::type() const { @@ -352,7 +205,6 @@ namespace MultiArrayHelper template const T& BlockResult::operator[](size_t i) const { - return mRes[i]; } diff --git a/src/block.h b/src/block.h index 3348aa3..8efdfe0 100644 --- a/src/block.h +++ b/src/block.h @@ -20,14 +20,16 @@ namespace MultiArrayHelper }; // manage vectorization in the future !! - - template + + template class BlockBinaryOp { public: BlockBinaryOp() = default; - BlockResult operator()(const BlockBase& arg1, const BlockBase& arg2); + BlockResult operator()(const BlockClass1& arg1, const BlockClass2& arg2); }; + + // EVERYTHING IN HERE MUST N O T BE VITUAL !! template class BlockBase @@ -35,14 +37,8 @@ namespace MultiArrayHelper public: DEFAULT_MEMBERS(BlockBase); BlockBase(size_t size); - - virtual BlockType type() const = 0; - virtual size_t stepSize() const = 0; - - virtual size_t size() const; - virtual const T& operator[](size_t pos) const = 0; - virtual BlockBase& set(size_t npos) = 0; + size_t size() const; template BlockResult operate(const BlockBase& in); @@ -74,10 +70,6 @@ namespace MultiArrayHelper DEFAULT_MEMBERS(MutableBlockBase); MutableBlockBase(size_t size); - MutableBlockBase& operator=(const BlockBase& in); - - virtual T& operator[](size_t pos) = 0; - }; template @@ -85,16 +77,17 @@ namespace MultiArrayHelper { public: DEFAULT_MEMBERS(Block); - Block(const std::vector& data, size_t begPos, size_t size); + Block(const std::vector& data, size_t begPos, size_t size, size_t stepSize); - virtual BlockType type() const override; - virtual const T& operator[](size_t pos) const override; - virtual Block& set(size_t npos) override; - virtual size_t stepSize() const override; + BlockType type() const; + const T& operator[](size_t pos) const; + Block& set(size_t npos); + size_t stepSize() const; protected: const std::vector* mData; const T* mBegPtr; + size_t mStepSize; }; template @@ -102,102 +95,23 @@ namespace MultiArrayHelper { public: DEFAULT_MEMBERS(MBlock); - MBlock(std::vector& data, size_t begPos, size_t size); + MBlock(std::vector& data, size_t begPos, size_t size, size_t stepSize); - virtual BlockType type() const override; - virtual const T& operator[](size_t pos) const override; - virtual T& operator[](size_t pos) override; - virtual MBlock& set(size_t npos) override; - virtual size_t stepSize() const override; + template + MBlock& operator=(const BlockClass& in); + + BlockType type() const; + const T& operator[](size_t pos) const; + T& operator[](size_t pos); + MBlock& set(size_t npos); + size_t stepSize() const; protected: std::vector* mData; T* mBegPtr; + size_t mStepSize; }; - template - class BlockValue : public BlockBase - { - public: - DEFAULT_MEMBERS(BlockValue); - - BlockValue(const std::vector& data, - size_t pos, size_t size); - - virtual BlockType type() const override; - virtual const T& operator[](size_t pos) const override; - virtual BlockValue& set(size_t npos) override; - virtual size_t stepSize() const override; - - protected: - const std::vector* mData; - const T* mVal; - }; - - template - class MBlockValue : public MutableBlockBase - { - public: - DEFAULT_MEMBERS(MBlockValue); - - MBlockValue(std::vector& data, - size_t pos, size_t size); - - virtual BlockType type() const override; - virtual const T& operator[](size_t pos) const override; - virtual T& operator[](size_t pos) override; - virtual MBlockValue& set(size_t npos) override; - virtual size_t stepSize() const override; - - protected: - std::vector* mData; - T* mVal; - }; - - template - class SplitBlock : public BlockBase - { - public: - - DEFAULT_MEMBERS(SplitBlock); - - SplitBlock(const std::vector& data, size_t begPos, - size_t stepSize, size_t size); - - virtual BlockType type() const override; - virtual const T& operator[](size_t pos) const override; - virtual SplitBlock& set(size_t npos) override; - virtual size_t stepSize() const override; - - protected: - const std::vector* mData; - size_t mStepSize; - const T* mBegPtr; - }; - - template - class MSplitBlock : public MutableBlockBase - { - public: - - DEFAULT_MEMBERS(MSplitBlock); - - MSplitBlock(std::vector& data, size_t begPos, - size_t stepSize, size_t size); - - virtual BlockType type() const override; - virtual const T& operator[](size_t pos) const override; - virtual T& operator[](size_t pos) override; - virtual MSplitBlock& set(size_t npos) override; - virtual size_t stepSize() const override; - - protected: - std::vector* mData; - size_t mStepSize; - T* mBegPtr; - }; - - template class BlockResult : public MutableBlockBase { @@ -206,11 +120,14 @@ namespace MultiArrayHelper BlockResult(size_t size); - virtual BlockType type() const override; - virtual const T& operator[](size_t pos) const override; - virtual T& operator[](size_t i) override; - virtual BlockResult& set(size_t npos) override; - virtual size_t stepSize() const override; + template + BlockResult& operator=(const BlockClass& in); + + BlockType type() const; + const T& operator[](size_t pos) const; + T& operator[](size_t i); + BlockResult& set(size_t npos); + size_t stepSize() const; protected: std::vector mRes; diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 85eccc5..dd8ac64 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -58,31 +58,15 @@ namespace MultiArrayTools } template - std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize) + std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize) { - if(stepSize == 0){ - return std::make_shared >(vec, 0, blockSize); - } - else if(stepSize == 1){ - return std::make_shared >(vec, 0, blockSize); - } - else { - return std::make_shared >(vec, 0, stepSize, blockSize); - } + return std::make_shared >(vec, 0, blockSize, stepSize); } template - std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize) + std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize) { - if(stepSize == 0){ - return std::make_shared >(vec, 0, blockSize); - } - else if(stepSize == 1){ - return std::make_shared >(vec, 0, blockSize); - } - else { - return std::make_shared >(vec, 0, stepSize, blockSize); - } + return std::make_shared >(vec, 0, blockSize, stepSize); } size_t getBTNum(const std::vector& mp, BlockType bt) @@ -119,9 +103,9 @@ namespace MultiArrayTools } - template + template std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, - const OperationBase& second) + const OpClass& second) { std::vector > ivec; seekIndexInst(ownIdx, ivec); @@ -189,9 +173,9 @@ namespace MultiArrayTools * OperationMaster * *************************/ - template - OperationMaster:: - OperationMaster(MutableMultiArrayBase& ma, const OperationBase& second, + template + OperationMaster:: + OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index) : mSecond(second), mArrayRef(ma), mIndex() { @@ -211,30 +195,30 @@ namespace MultiArrayTools } } - template - MutableBlockBase& OperationMaster::get() + template + MBlock& OperationMaster::get() { block(); return *mBlockPtr; } - template - const BlockBase& OperationMaster::get() const + template + const Block& OperationMaster::get() const { block(); return *mBlockPtr; } - template - std::vector OperationMaster::block(const std::shared_ptr blockIndex) const + template + std::vector OperationMaster::block(const std::shared_ptr blockIndex) const { std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); return btv; } - template - const OperationMaster& OperationMaster::block() const + template + const OperationMaster& OperationMaster::block() const { mBlockPtr->set( mIndex->pos() ); return *this; @@ -248,14 +232,14 @@ namespace MultiArrayTools ConstOperationRoot:: ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices) : - OperationBase(), OperationTemplate >(this), + OperationTemplate >(this), mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) { (*mIndex)(indices...); } template - const BlockBase& ConstOperationRoot::get() const + const Block& ConstOperationRoot::get() const { block(); return *mBlockPtr; @@ -284,27 +268,28 @@ namespace MultiArrayTools OperationRoot:: OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices) : - MutableOperationBase(), OperationTemplate >(this), + OperationTemplate >(this), mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) { (*mIndex)(indices...); } template - OperationMaster OperationRoot::operator=(const OperationBase& in) + template + OperationMaster OperationRoot::operator=(const OpClass& in) { - return OperationMaster(mArrayRef, in, mIndex); + return OperationMaster(mArrayRef, in, mIndex); } - + template - const BlockBase& OperationRoot::get() const + const MBlock& OperationRoot::get() const { block(); return *mBlockPtr; } template - MutableBlockBase& OperationRoot::get() + MBlock& OperationRoot::get() { block(); return *mBlockPtr; @@ -331,11 +316,11 @@ namespace MultiArrayTools template Operation::Operation(const Ops&... ops) : - OperationBase(), OperationTemplate >(this), + OperationTemplate >(this), mOps(ops...) {} template - const BlockBase& Operation::get() const + const BlockResult& Operation::get() const { mRes = std::move( PackNum::template unpackArgs(mOps) ); return mRes; diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index b48b1f8..b5b883d 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -43,20 +43,20 @@ namespace MultiArrayTools bool first, size_t higherStepSize = 1); template - std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize); + std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize); template - std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize); + std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize); size_t getBTNum(const std::vector& mp, BlockType bt); void minimizeAppearanceOfType(std::map, std::vector >& mp, BlockType bt); - template + template std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, - const OperationBase& second); - + const OpClass& second); + /* template class OperationBase { @@ -71,7 +71,7 @@ namespace MultiArrayTools virtual const OperationBase& block() const = 0; // update block //virtual size_t argNum() const = 0; - virtual const BlockBase& get() const = 0; + virtual const Block& get() const = 0; }; template @@ -82,10 +82,10 @@ namespace MultiArrayTools MutableOperationBase() = default; - virtual MutableBlockBase& get() = 0; + virtual MBlock& get() = 0; }; - + */ template class OperationTemplate { @@ -113,8 +113,8 @@ namespace MultiArrayTools OperationClass* mOc; }; - template - class OperationMaster : public MutableOperationBase + template + class OperationMaster/* : public MutableOperationBase*/ { public: @@ -123,27 +123,27 @@ namespace MultiArrayTools typedef ContainerRange CRange; typedef typename MultiRange::IndexType IndexType; - OperationMaster(MutableMultiArrayBase& ma, const OperationBase& second, + OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index); - - virtual MutableBlockBase& get() override; - virtual const BlockBase& get() const override; + + MBlock& get(); + const Block& get() const; - virtual std::vector block(const std::shared_ptr blockIndex) const override; - virtual const OperationMaster& block() const override; + std::vector block(const std::shared_ptr blockIndex) const; + const OperationMaster& block() const; protected: //void performAssignment(const OperationBase& in); - OperationBase const& mSecond; + OpClass const& mSecond; MutableMultiArrayBase& mArrayRef; std::shared_ptr mIndex; - mutable std::shared_ptr > mBlockPtr; + mutable std::shared_ptr > mBlockPtr; }; template - class ConstOperationRoot : public OperationBase, + class ConstOperationRoot : /*public OperationBase,*/ public OperationTemplate > { public: @@ -157,20 +157,20 @@ namespace MultiArrayTools ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices); - virtual const BlockBase& get() const override; + const Block& get() const; - virtual std::vector block(const std::shared_ptr blockIndex) const override; - virtual const ConstOperationRoot& block() const override; + std::vector block(const std::shared_ptr blockIndex) const; + const ConstOperationRoot& block() const; protected: MultiArrayBase const& mArrayRef; std::shared_ptr mIndex; - mutable std::shared_ptr > mBlockPtr; + mutable std::shared_ptr > mBlockPtr; }; template - class OperationRoot : public MutableOperationBase, + class OperationRoot : /*public MutableOperationBase,*/ public OperationTemplate > { public: @@ -184,23 +184,24 @@ namespace MultiArrayTools OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); - OperationMaster operator=(const OperationBase& in); + template + OperationMaster operator=(const OpClass& in); - virtual const BlockBase& get() const override; - virtual MutableBlockBase& get() override; + const MBlock& get() const; + MBlock& get(); - virtual std::vector block(const std::shared_ptr blockIndex) const override; - virtual const OperationRoot& block() const override; + std::vector block(const std::shared_ptr blockIndex) const; + const OperationRoot& block() const; protected: MutableMultiArrayBase& mArrayRef; std::shared_ptr mIndex; - mutable std::shared_ptr > mBlockPtr; + mutable std::shared_ptr > mBlockPtr; }; template - class Operation : public OperationBase, + class Operation : /*public OperationBase,*/ public OperationTemplate > { public: @@ -212,10 +213,10 @@ namespace MultiArrayTools Operation(const Ops&... ops); - virtual const BlockBase& get() const override; + const BlockResult& get() const; - virtual std::vector block(const std::shared_ptr blockIndex) const override; - virtual const Operation& block() const override; + std::vector block(const std::shared_ptr blockIndex) const; + const Operation& block() const; protected: std::tuple mOps; diff --git a/src/pack_num.h b/src/pack_num.h index 2c05c32..bdd3175 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -408,7 +408,7 @@ namespace MultiArrayHelper { static_assert(sizeof...(Args) == std::tuple_size::value-1, "inconsistent number of arguments"); - static BlockBinaryOp f; + static BlockBinaryOp(tp).get()), decltype(args)...> f; return f(std::get<0>(tp).get(), args...); }