From 07febad83a70dcd36023564db200c4b8d6aa9023 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 21 Feb 2017 21:47:40 +0100 Subject: [PATCH] start testing operation routines: some open issues... --- src/base_def.h | 2 +- src/index_base.h | 2 ++ src/multi_array.cc | 6 ++---- src/multi_array_operation.cc | 34 ++++++++++++++++++++++++++++-- src/multi_array_operation.h | 11 ++++++++-- src/multi_range.cc | 4 ++-- src/name.cc | 30 +++++++++++++++++---------- src/name.h | 3 +++ src/unit_test.cc | 40 +++++++++++++++++++++++++++++++++++- 9 files changed, 109 insertions(+), 23 deletions(-) diff --git a/src/base_def.h b/src/base_def.h index b503e87..b79e4d2 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -9,7 +9,7 @@ __class_name__(__class_name__&& in) = default; \ __class_name__& operator=(__class_name__&& in) = default -//#define DEBUG_MODE_X +#define DEBUG_MODE_X #ifdef DEBUG_MODE_X diff --git a/src/index_base.h b/src/index_base.h index c930051..4f38595 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -17,6 +17,8 @@ namespace MultiArrayTools class IndefinitIndexBase { public: + + virtual ~IndefinitIndexBase() {} virtual IndefinitIndexBase& operator=(size_t pos) = 0; virtual IndefinitIndexBase& operator++() = 0; diff --git a/src/multi_array.cc b/src/multi_array.cc index 37db470..2088d9e 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -8,7 +8,7 @@ namespace MultiArrayTools *******************/ template - MultiArray::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange.size()) + MultiArray::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange->size()) { mInit = true; } @@ -48,9 +48,7 @@ namespace MultiArrayTools template MultiArrayOperationBase MultiArray::operator()(const NameTypes&... str) { - auto index = mRange->begin(); - index.name(Name("master", str...)); - return MultiArrayOperationBase(*this, index); + return MultiArrayOperationBase(*this, Name("master", str...)); } template diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 34cc536..402c31e 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -6,17 +6,47 @@ namespace MultiArrayTools /********************************* * MultiArrayOperationBase * *********************************/ + + template + MultiArrayOperationBase:: + MultiArrayOperationBase(MultiArray& ma, + const Name& nm) : mArrayRef(ma), + mIibPtr(new IndexType(mArrayRef.begin())) + { + mIibPtr->name(nm); + } + + template + MultiArrayOperationBase::~MultiArrayOperationBase() + { + delete mIibPtr; + } + + template + MultiArrayOperationBase& + MultiArrayOperationBase::operator=(const MultiArrayOperationBase& in) + { + CHECK; + in.linkIndicesTo(mIibPtr); + for(*mIibPtr = mArrayRef.begin(); *mIibPtr != mArrayRef.end(); ++(*mIibPtr)){ + // build in vectorization later + get() = in.get(); + } + return *this; + } template template MultiArrayOperationBase& MultiArrayOperationBase::operator=(const MultiArrayOperationBase& in) { + CHECK; in.linkIndicesTo(mIibPtr); for(*mIibPtr = mArrayRef.begin(); *mIibPtr != mArrayRef.end(); ++(*mIibPtr)){ // build in vectorization later get() = in.get(); } + return *this; } template @@ -48,13 +78,13 @@ namespace MultiArrayTools template T& MultiArrayOperationBase::get() { - return mArrayRef[*mIibPtr]; + return mArrayRef[*dynamic_cast(mIibPtr)]; } template const T& MultiArrayOperationBase::get() const { - return mArrayRef[*mIibPtr]; + return mArrayRef[*dynamic_cast(mIibPtr)]; } /***************************** diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 66b0a00..fb2651d 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -16,8 +16,13 @@ namespace MultiArrayTools class MultiArrayOperationBase { public: + + typedef decltype(MultiArray().begin()) IndexType; - MultiArrayOperationBase(MultiArray& ma, const IndefinitIndexBase& iib); + MultiArrayOperationBase(MultiArray& ma, const Name& nm); + MultiArrayOperationBase& operator=(const MultiArrayOperationBase& in); + + virtual ~MultiArrayOperationBase(); // execute AnyOperation // exception if range types are inconsitent with names @@ -52,8 +57,10 @@ namespace MultiArrayTools protected: + // HERE !!!!!! + MultiArray& mArrayRef; - IndefinitIndexBase* mIibPtr; + IndefinitIndexBase* mIibPtr = nullptr; }; diff --git a/src/multi_range.cc b/src/multi_range.cc index f93d9e3..f55eee8 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -179,14 +179,14 @@ namespace MultiArrayTools template void MultiIndex::name(const Name& nm) { - name(nm.own()); + IIB::mName = nm.own(); if(nm.size() >= sizeof...(Indices)){ TupleNamer::nameTuple(mIPack, nm); } else { Name nm2 = nm; nm2.autoName(sizeof...(Indices)); - TupleNamer::nameTuple(mIPack, nm); + TupleNamer::nameTuple(mIPack, nm2); } } diff --git a/src/name.cc b/src/name.cc index 3898263..8ba778d 100644 --- a/src/name.cc +++ b/src/name.cc @@ -6,7 +6,17 @@ namespace MultiArrayTools namespace { - /* + + void giveNames(std::vector& nvec) + { + nvec.clear(); + } + + void giveNames(std::vector& nvec, const Name& name) + { + nvec.push_back(name); + } + template void giveNames(std::vector& nvec, const Name& name1, const Name& name2, const NameTypes&... names) { @@ -14,15 +24,6 @@ namespace MultiArrayTools giveNames(nvec, name2, names...); } - void giveNames(std::vector& nvec, const Name& name) - { - nvec.push_back(name); - }*/ - - void giveNames(std::vector& nvec) - { - nvec.clear(); - } } @@ -33,6 +34,13 @@ namespace MultiArrayTools giveNames(mSub, names...); } + template + Name::Name(char const* mainName, const NameTypes&... names) : mMain(mainName) + { + mSub.reserve(sizeof...(NameTypes)); + giveNames(mSub, names...); + } + const std::string& Name::own() const { return mMain; @@ -49,7 +57,7 @@ namespace MultiArrayTools mSub.resize(newSize); if(oldSize < newSize){ for(size_t i = oldSize; i != newSize; ++i){ - mSub[i] = mMain + std::to_string( i ); + mSub[i] = Name( mMain + std::to_string( i ) ); } } } diff --git a/src/name.h b/src/name.h index 853d8d9..c3eb9ce 100644 --- a/src/name.h +++ b/src/name.h @@ -18,6 +18,9 @@ namespace MultiArrayTools template Name(const std::string& mainName, const NameTypes&... names); + + template + Name(char const* mainName, const NameTypes&... names); const std::string& own() const; const Name& get(size_t n) const; diff --git a/src/unit_test.cc b/src/unit_test.cc index e94e054..b72da12 100644 --- a/src/unit_test.cc +++ b/src/unit_test.cc @@ -47,7 +47,27 @@ namespace { MultiArray2dAny ma; }; - + + class ReorderTest : public ::testing::Test + { + protected: + + typedef MAT::SingleRange Range1dAny; + typedef MAT::MultiRange Range2dAny; + typedef MAT::MultiArray MultiArray2dAny; + + ReorderTest() : r1({'a','b','c'}), r2({'a','b'}), + ra(r1,r2), rb(r2,r1), + ma(ra, {-5,6,2,1,9,54}) {} + + Range1dAny r1; + Range1dAny r2; + Range2dAny ra; + Range2dAny rb; + MultiArray2dAny ma; + + }; + TEST_F(OneDimTest, CorrectExtensions) { EXPECT_EQ(ma.size(), 5); @@ -88,6 +108,24 @@ namespace { EXPECT_EQ(ma[i(i1 = 2, i2 = 2)], 90); EXPECT_EQ(ma[i(i1 = 2, i2 = 3)], -67); } + + TEST_F(ReorderTest, ReorderingWorks) + { + MultiArray2dAny ma2(rb); + auto i = ma2.begin(); + auto i1 = i.template getIndex<0>(); + auto i2 = i.template getIndex<1>(); + //ma2("alpha","beta"); + //ma("beta","alpha"); + CHECK; + ma2("alpha","beta") = ma("beta","alpha"); + EXPECT_EQ(ma2[i(i1 = 0,i2 = 0)],-5); + EXPECT_EQ(ma2[i(i1 = 1,i2 = 0)],6); + EXPECT_EQ(ma2[i(i1 = 0,i2 = 1)],2); + EXPECT_EQ(ma2[i(i1 = 1,i2 = 1)],1); + EXPECT_EQ(ma2[i(i1 = 0,i2 = 2)],9); + EXPECT_EQ(ma2[i(i1 = 1,i2 = 2)],54); + } } // end namespace