From 3b0f67eb4751544c129c265f92d4d7a274b6bc26 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 9 Aug 2017 16:58:38 +0200 Subject: [PATCH] im com --- src/ma_unit_test.cc | 79 +++++++++++++++++++++++++++++++------ src/multi_array.cc | 12 ++++++ src/multi_array.h | 12 ++++-- src/multi_array_operation.h | 6 +-- 4 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/ma_unit_test.cc b/src/ma_unit_test.cc index e08d4bc..b7bac2e 100644 --- a/src/ma_unit_test.cc +++ b/src/ma_unit_test.cc @@ -59,31 +59,58 @@ namespace { std::vector vv = { 3.141, 2.718, 1.618, 0.693, 0.577 }; }; - /* - class MATest_1Dim : public ::testing::Test + + class MATest_MDim : public ::testing::Test { protected: typedef SingleRangeFactory SRF; typedef SRF::oType SRange; - typedef ContainerRangeFactory CRF; + typedef MultiRangeFactory MRF; + typedef MRF::oType MRange; + + typedef ContainerRangeFactory CRF; typedef CRF::oType CRange; - MATest_1Dim() - { - swapFactory(rfbptr, {'x', 'y', 'l', 'f', 'g'} ); - srptr = std::dynamic_pointer_cast( rfbptr->create() ); + typedef ContainerRangeFactory CRF2; + typedef CRF2::oType CRange2; - swapMFactory(rfbptr, srptr); + + MATest_MDim() + { + swapFactory(rfbptr, {'x', 'y'} ); + sr1ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapFactory(rfbptr, {'a', 'l', 'f', 'g'} ); + sr2ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapFactory(rfbptr, {'1', '2', '3'} ); + sr3ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapFactory(rfbptr, { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X' } ); + sr4ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, sr1ptr, sr2ptr); + mrptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, mrptr, sr3ptr); crptr = std::dynamic_pointer_cast( rfbptr->create() ); } std::shared_ptr rfbptr; - std::shared_ptr srptr; + std::shared_ptr sr1ptr; + std::shared_ptr sr2ptr; + std::shared_ptr sr3ptr; + std::shared_ptr sr4ptr; + std::shared_ptr mrptr; std::shared_ptr crptr; - std::vector vv = { 3.141, 2.718, 1.618, 0.693, 0.577 }; - };*/ + std::vector vv = { 2.917, 9.436, 0.373, 7.192, 7.315, 1.536, 4.892, 0.280, + 8.870, 4.790, 8.215, 5.063, 1.530, 3.084, 1.609, 4.847, + 8.175, 0.112, 6.712, 6.408, 1.959, 0.331, 4.209, 2.951 }; + }; TEST_F(MATest_1Dim, SimpleCall) { @@ -146,6 +173,36 @@ namespace { EXPECT_EQ( ma[ j.at('g') ], 0.693); EXPECT_EQ( ma[ j.at('i') ], 0.577); } + + TEST_F(MATest_MDim, SimpleCall) + { + MultiArray ma(crptr, vv); + EXPECT_EQ( ma.size(), 24 ); + EXPECT_EQ( ma.range().dim(), 2 ); + + auto i = ma.beginIndex(); + EXPECT_EQ( ma[ i.at( mkt( mkt('x', 'a'), '1' ) ) ], 2.917); + EXPECT_EQ( ma[ i.at( mkt( mkt('x', 'a'), '2' ) ) ], 9.436); + + EXPECT_EQ( ma.at( mkt( mkt('x', 'a'), '1' ) ), 2.917); + EXPECT_EQ( ma.at( mkt( mkt('x', 'a'), '2' ) ), 9.436); + + ma.at( mkt( mkt('x', 'a'), '2' ) ) = 4.444; + EXPECT_EQ( ma[ i.at( mkt( mkt('x', 'a'), '2' ) ) ], 4.444 ); + } + + TEST_F(MATest_MDim, ReFormat) + { + MultiArray ma(crptr, vv); + + auto ma2 = ma.format( sr4ptr ); + auto i = ma2.beginIndex(); + EXPECT_EQ( ma2.at('A') , 2.917 ); + EXPECT_EQ( ma2[ i.at('G') ], 4.892 ); + EXPECT_EQ( ma2.at('J') , 4.790 ); + EXPECT_EQ( ma2[ i.at('M') ], 1.530 ); + EXPECT_EQ( ma2.at('W') , 4.209 ); + } } // end namespace diff --git a/src/multi_array.cc b/src/multi_array.cc index 7fae5de..ccf7cc9 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -519,6 +519,18 @@ namespace MultiArrayTools return mCont[ i.pos() ]; } + template + T& MultiArray::at(const typename CRange::IndexType::MetaType& meta) + { + return mCont[ MAB::beginIndex().at(meta).pos() ]; + } + + template + const T& MultiArray::at(const typename CRange::IndexType::MetaType& meta) const + { + return mCont[ MAB::beginIndex().at(meta).pos() ]; + } + template bool MultiArray::isConst() const { diff --git a/src/multi_array.h b/src/multi_array.h index 2180c62..f55398a 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -75,6 +75,7 @@ namespace MultiArrayTools virtual ~MultiArrayBase() = default; virtual const T& operator[](const typename CRange::IndexType& i) const = 0; + virtual const T& at(const typename CRange::IndexType::MetaType& meta) const = 0; virtual const T* data() const = 0; @@ -167,7 +168,8 @@ namespace MultiArrayTools MutableMultiArrayBase(const std::shared_ptr& range); virtual T& operator[](const IndexType& i) = 0; - + virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0; + virtual T* data() = 0; virtual iterator begin(); @@ -218,9 +220,11 @@ namespace MultiArrayTools //template //MultiArray& operator=(const MultiArray,Range3> in); - T& operator[](const IndexType& i) override; - const T& operator[](const IndexType& i) const override; - + virtual T& operator[](const IndexType& i) override; + virtual const T& operator[](const IndexType& i) const override; + virtual T& at(const typename CRange::IndexType::MetaType& meta) override; + virtual const T& at(const typename CRange::IndexType::MetaType& meta) const override; + virtual bool isConst() const override; virtual bool isSlice() const override; diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 0d7d7a3..c335678 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -22,12 +22,10 @@ namespace MultiArrayTools typedef T value_type; - MultiArrayOperationBase() /*{ CHECK; }*/ = default; + MultiArrayOperationBase() = default; virtual ~MultiArrayOperationBase(); virtual size_t argNum() const = 0; - const IndefinitIndexBase& index() const = 0; - virtual const T& get() const = 0; }; @@ -36,7 +34,7 @@ namespace MultiArrayTools { public: - MutableMultiArrayOperationBase() /*{ CHECK; }*/ = default; + MutableMultiArrayOperationBase() = default; virtual T& get() = 0; };