diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index c95912c..ff92fe2 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -13,16 +13,31 @@ namespace MultiArrayTools // purely virtual at the moment + /*************************** + * OperationTemplate * + ***************************/ + + template + template + Operation OperationTemplate::operator+(Second&& in) const + { + return Operation,OperationClass,Second>(*this, in); + } + /************************* * OperationMaster * *************************/ template - OperationMaster::OperationMaster(MutableMultiArrayBase& ma, - const ContainerRange::IndexType& index) : + OperationMaster:: + OperationMaster(MutableMultiArrayBase& ma, + std::shared_ptr::IndexType>& index) : mArrayRef(ma), mIndex() { - (*mIndex) = index; // implement corresp member fucntion in MultiIndex + (*mIndex) = *index; + for(*mIndex = 0; mIndex->pos() != mIndex->max(); ++(*mIndex)){ + get() = mSecond.get(); + } } template @@ -69,6 +84,12 @@ namespace MultiArrayTools mIndex(indices...); } + template + OperationMaster OperationRoot::operator=(const OperationBase& in) + { + return OperationMaster(mArrayRef, in, mIndex); + } + template const T& OperationRoot::get() const { diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index f288a63..05cf68b 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -51,6 +51,15 @@ namespace MultiArrayTools MutableOperationBase() = default; virtual T& get() = 0; }; + + template + class OperationTemplate + { + public: + + template + Operation operator+(const Second& in) const; + }; template class OperationMaster : public MutableOperationBase @@ -60,17 +69,18 @@ namespace MultiArrayTools typedef OperationBase OB; typedef typename MultiRange::IndexType IndexType; - OperationMaster(OperationRoot&& root); - + OperationMaster(MutableMultiArrayBase& ma, OperationBase& second, + const ContainerRange::IndexType& index); + virtual T& get() override; virtual const T& get() const override; protected: - void performAssignment(const MultiArrayOperationBase& in); - + //void performAssignment(const OperationBase& in); + OperationBase const& mSecond; MutableMultiArrayBase& mArrayRef; - mutable IndexType mIndex; + std::shared_ptr mIndex; }; @@ -109,7 +119,9 @@ namespace MultiArrayTools OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); - + + OperationMaster operator=(const OperationBase& in); + virtual const T& get() const override; virtual T& get() override; diff --git a/src/multi_range.cc b/src/multi_range.cc index 55cf433..c82024f 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -30,6 +30,15 @@ namespace MultiArrayTools IB::mPos = PackNum::makePos(mIPack); return *this; } + + template + MultiIndex& MultiIndex::operator=(ContainerIndex& ci) + { + IndexI::operator=(in); + PackNum::copyInst(mIPack, ci); + IB::mPos = PackNum::makePos(mIPack); + return *this; + } template template diff --git a/src/multi_range.h b/src/multi_range.h index ea8b1a5..cf48383 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -33,6 +33,7 @@ namespace MultiArrayTools // ( have to assign sub-indices (ptr!) correctly ) MultiIndex(const MultiIndex& in); MultiIndex& operator=(const MultiIndex& in); + MultiIndex& operator=(const ContainerIndex& ci); template MultiIndex(const std::shared_ptr& range); diff --git a/src/op_unit_test.cc b/src/op_unit_test.cc new file mode 100644 index 0000000..401d7a6 --- /dev/null +++ b/src/op_unit_test.cc @@ -0,0 +1,78 @@ +// -*- C++ -*- + +#include +#include "gtest/gtest.h" +#include + +#include "multi_array_header.h" + +namespace MAT = MultiArrayTools; + +namespace { + + using namespace MAT; + + template + void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) + { + std::vector tmp = ilist; + auto nptr = std::make_shared( tmp ); + fptr = nptr; + } + + template + void swapMFactory(std::shared_ptr& fptr, const Rs&... rs) + { + auto nptr = std::make_shared( rs... ); + fptr = nptr; + } + + template + auto mkt(Ts&&... ts) -> decltype(std::make_tuple(ts...)) + { + return std::make_tuple(ts...); + } + + class OpTest_1Dim : public ::testing::Test + { + protected: + + typedef SingleRangeFactory SRF; + typedef SRF::oType SRange; + + typedef ContainerRangeFactory CRF; + typedef CRF::oType CRange; + + OpTest_1Dim() + { + swapFactory(rfbptr, {'a', 'l', 'f', 'g'} ); + srptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(srptr); + crptr = std::dynamic_pointer_cast( rfbptr->create() ); + } + + std::shared_ptr rfbptr; + std::shared_ptr srptr; + std::shared_ptr crptr; + std::vector v1 = { 2.917, 9.436, 0.373, 7.192 }; + std::vector v2 = { 8.870, 4.790, 8.215, 5.063 }; + }; + + TEST_F(OpTest_1Dim, ExecOp) + { + MultiArray ma1(crptr, v1); + MultiArray ma2(crptr, v2); + MultiArray res(crptr); + + std::map > m; + res(m["mu"]) = ma1(m["mu"]) + ma2(m["mu"]); + } + +} // anonymous namspace + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/src/pack_num.h b/src/pack_num.h index 2ba40ef..15080e0 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -118,6 +118,14 @@ namespace MultiArrayHelper std::get(ip) = std::shared_ptr( new SubIndexType( ind.template get() ) ); PackNum::copy(ip, ind); } + + template class IndexType, class... Indices> + static void copyInst(std::tuple...>& ip, + const IndexType& ind) + { + std::get(ip) = ind.template getPtr() ; + PackNum::copy(ip, ind); + } template static size_t makePos(const std::tuple...>& iPtrTup) @@ -219,6 +227,13 @@ namespace MultiArrayHelper typedef typename std::remove_reference())>::type SubIndexType; std::get<0>(ip) = std::shared_ptr( new SubIndexType( ind.template get<0>() ) ); } + + template class IndexType, class... Indices> + static void copyInst(std::tuple...>& ip, + const IndexType& ind) + { + std::get<0>(ip) = ind.template getPtr<0>(); + } template static size_t makePos(const std::tuple...>& iPtrTup)