From d8e62cc539c93b525330d7039f38aa00d43e08a1 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Fri, 22 Dec 2017 14:01:26 +0100 Subject: [PATCH] T* in BlockResult instead of std::vector --- src/block/block.h | 131 +++++++++++++++++++++++++++++++----- src/multi_array_operation.h | 46 ++++++++----- src/pack_num.h | 10 +-- 3 files changed, 148 insertions(+), 39 deletions(-) diff --git a/src/block/block.h b/src/block/block.h index 032fdf2..74e568f 100644 --- a/src/block/block.h +++ b/src/block/block.h @@ -39,13 +39,16 @@ namespace MultiArrayHelper class BlockBase { public: + DEFAULT_MEMBERS(BlockBase); BlockBase(size_t size); size_t size() const; - + bool init() const; + protected: size_t mSize = 0; + bool mInit = false; }; template @@ -133,16 +136,24 @@ namespace MultiArrayHelper class BlockResult : public MutableBlockBase { public: + typedef BlockBase BB; + using BB::init; - DEFAULT_MEMBERS(BlockResult); - + BlockResult(); + BlockResult(const BlockResult& in); + BlockResult(BlockResult&& in); + BlockResult& operator=(const BlockResult& in); + BlockResult& operator=(BlockResult&& in); + BlockResult(size_t size); + + ~BlockResult(); template BlockResult& operator=(const BlockClass& in); - BlockResult& assign(size_t size, const T& val); + BlockResult& init(size_t size); BlockType type() const; const T& operator[](size_t pos) const; @@ -151,7 +162,7 @@ namespace MultiArrayHelper size_t stepSize() const; protected: - std::vector mRes; + T* mResPtr = nullptr; }; } // end namespace MultiArrayHelper @@ -174,6 +185,7 @@ namespace MultiArrayHelper { static OpFunc f; BlockResult res(arg1.size()); + assert(res.init() and arg1.init() and arg2.init()); assert(arg1.size() == arg2.size()); for(size_t i = 0; i != arg1.size(); ++i){ res[i] = f(arg1[i], arg2[i]); @@ -188,7 +200,7 @@ namespace MultiArrayHelper void BlockBinaryOpSelf::operator()(const BlockClass& arg) { static OpFunc f; - if(mRes.size() == 0) { mRes.assign(arg.size(), static_cast(0)); } + assert(mRes.init() and arg.init()); assert(mRes.size() == arg.size()); for(size_t i = 0; i != arg.size(); ++i){ mRes[i] = f(mRes[i], arg[i]); @@ -201,7 +213,7 @@ namespace MultiArrayHelper *****************/ template - BlockBase::BlockBase(size_t size) : mSize(size) {} + BlockBase::BlockBase(size_t size) : mSize(size), mInit(size != 0) {} template size_t BlockBase::size() const @@ -209,6 +221,12 @@ namespace MultiArrayHelper return mSize; } + template + bool BlockBase::init() const + { + return mInit; + } + /************************ * MutableBlockBase * ************************/ @@ -317,16 +335,86 @@ namespace MultiArrayHelper *******************/ template - BlockResult::BlockResult(size_t size) : - MutableBlockBase(size), - mRes(size) {} + BlockResult::BlockResult() : MutableBlockBase() {} + template + BlockResult::BlockResult(const BlockResult& in) : MutableBlockBase(in.size()) + { + if(BB::mInit){ + mResPtr = new T[BB::mSize]; + } + for(size_t i = 0; i != BB::mSize; ++i){ + mResPtr[i] = in.mResPtr[i]; + } + } + + template + BlockResult::BlockResult(BlockResult&& in) : MutableBlockBase(in.size()) + { + if(BB::mInit){ + mResPtr = in.mResPtr; + } + in.mSize = 0; + in.mInit = false; + in.mResPtr = nullptr; + } + + template + BlockResult& BlockResult::operator=(const BlockResult& in) + { + BB::mSize = in.size(); + BB::mInit = BB::mInit and BB::mSize != 0; + if(BB::mInit){ + mResPtr = new T[BB::mSize]; + } + for(size_t i = 0; i != BB::mSize; ++i){ + mResPtr[i] = in.mResPtr[i]; + } + return *this; + } + + template + BlockResult& BlockResult::operator=(BlockResult&& in) + { + BB::mSize = in.size(); + BB::mInit = BB::mInit and BB::mSize != 0; + if(BB::mInit){ + mResPtr = in.mResPtr; + } + in.mSize = 0; + in.mInit = false; + in.mResPtr = nullptr; + return *this; + } + + template + BlockResult::BlockResult(size_t size) : + MutableBlockBase(size) + { + if(BB::mInit){ + mResPtr = new T[BB::mSize]; + } + for(size_t i = 0; i != BB::mSize; ++i){ + mResPtr[i] = static_cast( 0 ); + } + } + + template + BlockResult::~BlockResult() + { + delete[] mResPtr; + mResPtr = nullptr; + BB::mInit = false; + BB::mSize = 0; + } + template template BlockResult& BlockResult::operator=(const BlockClass& in) { - //CHECK; - for(size_t i = 0; i != BlockBase::mSize; ++i){ + assert(BB::mInit); + assert(BB::mSize == in.size()); + for(size_t i = 0; i != BB::mSize; ++i){ (*this)[i] = in[i]; } return *this; @@ -341,14 +429,13 @@ namespace MultiArrayHelper template const T& BlockResult::operator[](size_t i) const { - return mRes[i]; + return mResPtr[i]; } template T& BlockResult::operator[](size_t i) { - - return mRes[i]; + return mResPtr[i]; } template @@ -364,10 +451,20 @@ namespace MultiArrayHelper } template - BlockResult& BlockResult::assign(size_t size, const T& val) + BlockResult& BlockResult::init(size_t size) { BB::mSize = size; - mRes.assign(BB::mSize, val); + delete[] mResPtr; + if(BB::mSize != 0){ + BB::mInit = true; + mResPtr = new T[BB::mSize]; + } + else { + BB::mInit = false; + } + for(size_t i = 0; i != BB::mSize; ++i){ + mResPtr[i] = static_cast( 0 ); + } return *this; } diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 40dd60c..4dce8f6 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -96,7 +96,7 @@ namespace MultiArrayTools MBlock& get(); const Block& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex, bool init = false) const; const OperationMaster& block() const; protected: @@ -126,7 +126,7 @@ namespace MultiArrayTools const Block& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex, bool init = false) const; const ConstOperationRoot& block() const; protected: @@ -157,7 +157,7 @@ namespace MultiArrayTools const MBlock& get() const; MBlock& get(); - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex, bool init = false) const; const OperationRoot& block() const; protected: @@ -182,7 +182,7 @@ namespace MultiArrayTools const BlockResult& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex, bool init = false) const; const Operation& block() const; protected: @@ -202,7 +202,7 @@ namespace MultiArrayTools const BlockResult& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex, bool init = false) const; const Contraction& block() const; protected: @@ -323,8 +323,8 @@ namespace MultiArrayTools auto blockIndex = seekBlockIndex( make_viwb( mIndex ), second); std::intptr_t blockIndexNum = blockIndex->getPtrNum(); - block(blockIndex); - second.block(blockIndex); + block(blockIndex, true); + second.block(blockIndex, true); //size_t cnt = 0; //std::clock_t cs = clock(); for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndexNum) ){ @@ -354,10 +354,12 @@ namespace MultiArrayTools } template - std::vector OperationMaster::block(const std::shared_ptr blockIndex) const + std::vector OperationMaster::block(const std::shared_ptr blockIndex, bool init) const { std::vector btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) ); - mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); + if(init){ + mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); + } return btv; } @@ -390,10 +392,12 @@ namespace MultiArrayTools } template - std::vector ConstOperationRoot::block(const std::shared_ptr blockIndex) const + std::vector ConstOperationRoot::block(const std::shared_ptr blockIndex, bool init) const { std::vector btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) ); - mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); + if(init){ + mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); + } return btv; } @@ -440,10 +444,12 @@ namespace MultiArrayTools } template - std::vector OperationRoot::block(const std::shared_ptr blockIndex) const + std::vector OperationRoot::block(const std::shared_ptr blockIndex, bool init) const { std::vector btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) ); - mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); + if(init){ + mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); + } return btv; } @@ -471,10 +477,13 @@ namespace MultiArrayTools } template - std::vector Operation::block(const std::shared_ptr blockIndex) const + std::vector Operation::block(const std::shared_ptr blockIndex, bool init) const { std::vector btv; - PackNum::makeBlockTypeVec(btv, mOps, blockIndex); + PackNum::makeBlockTypeVec(btv, mOps, blockIndex, init); + if(init){ + mRes.init(blockIndex->max()); + } return btv; } @@ -506,9 +515,12 @@ namespace MultiArrayTools } template - std::vector Contraction::block(const std::shared_ptr blockIndex) const + std::vector Contraction::block(const std::shared_ptr blockIndex, bool init) const { - return mOp.block(blockIndex); + if(init){ + mRes.init(blockIndex->max()); + } + return mOp.block(blockIndex, init); } template diff --git a/src/pack_num.h b/src/pack_num.h index 7147024..d6978cf 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -20,11 +20,11 @@ namespace MultiArrayHelper template static void makeBlockTypeVec(std::vector >& btv, const std::tuple& ops, - std::shared_ptr idxPtr) + std::shared_ptr idxPtr, bool init) { - auto subvec = std::move( std::get(ops).block(idxPtr) ); + auto subvec = std::move( std::get(ops).block(idxPtr, init) ); btv.insert(btv.end(), subvec.begin(), subvec.end() ); - PackNum::makeBlockTypeVec(btv, ops, idxPtr); + PackNum::makeBlockTypeVec(btv, ops, idxPtr, init); } template @@ -57,9 +57,9 @@ namespace MultiArrayHelper template static void makeBlockTypeVec(std::vector >& btv, const std::tuple& ops, - std::shared_ptr idxPtr) + std::shared_ptr idxPtr, bool init) { - auto subvec = std::move( std::get<0>(ops).block(idxPtr) ); + auto subvec = std::move( std::get<0>(ops).block(idxPtr, init) ); btv.insert(btv.end(), subvec.begin(), subvec.end() ); }