From 7a17aba37dee31eb4fbc16577bbf837cdbe6b248 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 11 Sep 2017 12:54:24 +0200 Subject: [PATCH] OpTest_MDim.ExecOp1 works... --- src/block.cc | 63 ++++++++++++++++++++++++++++++------ src/block.h | 22 +++++++++++-- src/multi_array.cc | 8 +++++ src/multi_array.h | 7 ++-- src/multi_array_header.h | 7 ++++ src/multi_array_operation.cc | 28 ++++++++-------- src/multi_array_operation.h | 4 +-- src/op_unit_test.cc | 4 +-- src/pack_num.h | 13 +++++++- 9 files changed, 123 insertions(+), 33 deletions(-) diff --git a/src/block.cc b/src/block.cc index ba255b7..930205d 100644 --- a/src/block.cc +++ b/src/block.cc @@ -120,6 +120,12 @@ namespace MultiArrayHelper return *this; } + template + size_t Block::stepSize() const + { + return 1; + } + /************** * MBlock * **************/ @@ -157,6 +163,12 @@ namespace MultiArrayHelper mBegPtr = &(*mData)[npos]; return *this; } + + template + size_t MBlock::stepSize() const + { + return 1; + } /****************** * BlockValue * @@ -167,7 +179,7 @@ namespace MultiArrayHelper size_t pos, size_t size) : BlockBase(size), mData(&data), - mVal(data[pos]) {} + mVal(&data[pos]) {} template BlockType BlockValue::type() const @@ -179,16 +191,22 @@ namespace MultiArrayHelper const T& BlockValue::operator[](size_t i) const { - return mVal; + return *mVal; } template BlockValue& BlockValue::set(size_t npos) { - mVal = (*mData)[npos]; + mVal = &(*mData)[npos]; return *this; } + template + size_t BlockValue::stepSize() const + { + return 0; + } + /******************* * MBlockValue * *******************/ @@ -198,7 +216,7 @@ namespace MultiArrayHelper size_t pos, size_t size) : MutableBlockBase(size), mData(&data), - mVal(data[pos]) {} + mVal(&data[pos]) {} template BlockType MBlockValue::type() const @@ -210,23 +228,29 @@ namespace MultiArrayHelper const T& MBlockValue::operator[](size_t i) const { - return mVal; + return *mVal; } template T& MBlockValue::operator[](size_t i) { - return mVal; + return *mVal; } template MBlockValue& MBlockValue::set(size_t npos) { - mVal = (*mData)[npos]; + mVal = &(*mData)[npos]; return *this; } + template + size_t MBlockValue::stepSize() const + { + return 0; + } + /****************** * SplitBlock * ******************/ @@ -235,9 +259,9 @@ namespace MultiArrayHelper SplitBlock::SplitBlock(const std::vector& data, size_t begPos, size_t stepSize, size_t size) : BlockBase(size), - mData(data), + mData(&data), mStepSize(stepSize), - mBegPtr(data.data() + begPos) {} + mBegPtr(&data[begPos]) {} template BlockType SplitBlock::type() const @@ -259,6 +283,12 @@ namespace MultiArrayHelper return *this; } + template + size_t SplitBlock::stepSize() const + { + return mStepSize; + } + /******************* * MSplitBlock * *******************/ @@ -267,8 +297,9 @@ namespace MultiArrayHelper MSplitBlock::MSplitBlock(std::vector& data, size_t begPos, size_t stepSize, size_t size) : MutableBlockBase(size), + mData(&data), mStepSize(stepSize), - mBegPtr(data.data() + begPos) {} + mBegPtr(&data[begPos]) {} template BlockType MSplitBlock::type() const @@ -296,6 +327,12 @@ namespace MultiArrayHelper mBegPtr = &(*mData)[npos]; return *this; } + + template + size_t MSplitBlock::stepSize() const + { + return mStepSize; + } /******************* * BlockResult * @@ -331,5 +368,11 @@ namespace MultiArrayHelper { return *this; } + + template + size_t BlockResult::stepSize() const + { + return 1; + } } // end namespace MultiArrayHelper diff --git a/src/block.h b/src/block.h index c129402..ee6f7a7 100644 --- a/src/block.h +++ b/src/block.h @@ -40,6 +40,7 @@ namespace MultiArrayHelper BlockBase& operator=(BlockBase&& res) = default; 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; @@ -58,6 +59,16 @@ namespace MultiArrayHelper size_t mSize; }; + template + std::ostream& operator<<(std::ostream& out, const BlockBase& block) + { + out << block[0]; + for(size_t i = 1; i != block.size(); ++i){ + out << ", " << block[i]; + } + return out; + } + template class MutableBlockBase : public BlockBase { @@ -85,6 +96,7 @@ namespace MultiArrayHelper 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; protected: const std::vector* mData; @@ -102,6 +114,7 @@ namespace MultiArrayHelper 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; protected: std::vector* mData; @@ -119,10 +132,11 @@ namespace MultiArrayHelper 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; - T& mVal; + const T* mVal; }; template @@ -137,10 +151,11 @@ namespace MultiArrayHelper 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; + T* mVal; }; template @@ -155,6 +170,7 @@ namespace MultiArrayHelper 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; @@ -175,6 +191,7 @@ namespace MultiArrayHelper 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; @@ -197,6 +214,7 @@ namespace MultiArrayHelper 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; protected: std::vector mRes; diff --git a/src/multi_array.cc b/src/multi_array.cc index 8da67ff..58df5ec 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -408,6 +408,14 @@ namespace MultiArrayTools return OperationRoot(*this, inds...); } + template + ConstOperationRoot + MutableMultiArrayBase::operator()(std::shared_ptr&... inds) const + { + return ConstOperationRoot(*this, inds...); + } + + /******************* * MultiArray * *******************/ diff --git a/src/multi_array.h b/src/multi_array.h index 973ff12..07d6564 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -96,7 +96,8 @@ namespace MultiArrayTools virtual bool isConst() const; - ConstOperationRoot operator()(std::shared_ptr&... inds) const; + virtual ConstOperationRoot + operator()(std::shared_ptr&... inds) const; virtual bool isInit() const; @@ -179,7 +180,9 @@ namespace MultiArrayTools virtual bool isConst() const override; - OperationRoot operator()(std::shared_ptr&... inds); + virtual ConstOperationRoot + operator()(std::shared_ptr&... inds) const override; + virtual OperationRoot operator()(std::shared_ptr&... inds); }; template diff --git a/src/multi_array_header.h b/src/multi_array_header.h index 5fa3f05..6e37477 100644 --- a/src/multi_array_header.h +++ b/src/multi_array_header.h @@ -53,6 +53,13 @@ namespace MultiArrayTools typedef SingleRange, RangeType::MOMENTUM> LinMom4dR; // ... */ + + template + std::ostream& operator<<(std::ostream& out, const std::tuple& tp) + { + PackNum::printTuple(out, tp); + return out; + } } #endif diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index d8f8525..a6b5cc1 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -28,10 +28,11 @@ namespace MultiArrayTools // stepSize == 1 => BLOCK // stepSize > 1 => SPLIT :) BTSS out(BlockType::VALUE, 0); - for(size_t inum = 0; inum != i->rangePtr()->dim(); ++inum){ + size_t lastNum = i->rangePtr()->dim(); + for(size_t inum = 0; inum != lastNum; ++inum){ auto ii = i->getPtr(inum); if(ii == j){ - if(inum == 0 and first){ + if(inum == lastNum - 1 and first){ out = BTSS(BlockType::BLOCK, 1); } else { @@ -42,7 +43,7 @@ namespace MultiArrayTools if(ii->type() == IndexType::MULTI or ii->type() == IndexType::CONT){ - BTSS tmp = getBlockType(ii, j, inum == 0); + BTSS tmp = getBlockType(ii, j, inum == lastNum - 1); if(tmp.first != BlockType::VALUE){ out = tmp; } @@ -54,7 +55,6 @@ namespace MultiArrayTools template std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize) { - VCHECK(stepSize); if(stepSize == 0){ return std::make_shared >(vec, 0, blockSize); } @@ -69,7 +69,6 @@ namespace MultiArrayTools template std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize) { - VCHECK(stepSize); if(stepSize == 0){ return std::make_shared >(vec, 0, blockSize); } @@ -202,6 +201,7 @@ namespace MultiArrayTools auto blockIndex = seekBlockIndex( mIndex, second); block(blockIndex); second.block(blockIndex); + for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndex) ){ get() = mSecond.get(); } @@ -232,7 +232,7 @@ namespace MultiArrayTools template const OperationMaster& OperationMaster::block() const { - mBlockPtr->set( mIndex->pos() ); // containerRange <-> multiRange !!! + mBlockPtr->set( mIndex->pos() ); return *this; } @@ -247,7 +247,7 @@ namespace MultiArrayTools OperationBase(), OperationTemplate >(this), mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) { - mIndex(indices...); + (*mIndex)(indices...); } template @@ -268,14 +268,14 @@ namespace MultiArrayTools template const ConstOperationRoot& ConstOperationRoot::block() const { - mBlockPtr->set( mIndex->pos() ); + mBlockPtr->set( (*mIndex)().pos() ); return *this; } /*********************** * OperationRoot * ***********************/ - + template OperationRoot:: OperationRoot(MutableMultiArrayBase& ma, @@ -298,7 +298,7 @@ namespace MultiArrayTools block(); return *mBlockPtr; } - + template MutableBlockBase& OperationRoot::get() { @@ -317,13 +317,13 @@ namespace MultiArrayTools template const OperationRoot& OperationRoot::block() const { - mBlockPtr->set( mIndex->pos() ); + mBlockPtr->set( (*mIndex)().pos() ); return *this; } - /*********************** - * OperationRoot * - ***********************/ + /******************* + * Operation * + *******************/ template Operation::Operation(const Ops&... ops) : diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 276a755..8e2f6a0 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -167,7 +167,7 @@ namespace MultiArrayTools std::shared_ptr mIndex; mutable std::shared_ptr > mBlockPtr; }; - + template class OperationRoot : public MutableOperationBase, public OperationTemplate > @@ -197,7 +197,7 @@ namespace MultiArrayTools std::shared_ptr mIndex; mutable std::shared_ptr > mBlockPtr; }; - + template class Operation : public OperationBase, public OperationTemplate > diff --git a/src/op_unit_test.cc b/src/op_unit_test.cc index d743049..804347c 100644 --- a/src/op_unit_test.cc +++ b/src/op_unit_test.cc @@ -114,8 +114,8 @@ namespace { TEST_F(OpTest_MDim, ExecOp1) { MultiArray res(sr2ptr,sr4ptr); - MultiArray ma1(sr2ptr, v1); - MultiArray ma2(sr4ptr, v2); + const MultiArray ma1(sr2ptr, v1); + const MultiArray ma2(sr4ptr, v2); auto i1 = std::dynamic_pointer_cast( sr2ptr->index() ); auto i2 = std::dynamic_pointer_cast( sr4ptr->index() ); diff --git a/src/pack_num.h b/src/pack_num.h index ba91b6b..c5279b2 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -231,6 +231,12 @@ namespace MultiArrayHelper btv.insert(btv.end(), subvec.begin(), subvec.end() ); PackNum::makeBlockTypeVec(btv, ops, idxPtr); } + + template + static void printTuple(std::ostream& out, const std::tuple& tp){ + out << std::get(tp) << ", "; + PackNum::printTuple(out, tp); + } }; @@ -403,7 +409,12 @@ namespace MultiArrayHelper auto subvec = std::move( std::get<0>(ops).block(idxPtr) ); btv.insert(btv.end(), subvec.begin(), subvec.end() ); } - + + template + static void printTuple(std::ostream& out, const std::tuple& tp){ + out << std::get(tp); + } + }; } // end namespace MultiArrayHelper