From d1363c3c3d840ec2937c40d478e462a884c230d3 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 26 Feb 2017 18:14:58 +0100 Subject: [PATCH] restructure MultiArray inheritance --- src/base_def.h | 4 ++ src/multi_array.cc | 76 +++++++++++++++++++++--------------- src/multi_array.h | 49 +++++++++++++++-------- src/multi_array_operation.cc | 2 +- src/multi_array_operation.h | 4 +- src/slice.cc | 5 ++- src/slice.h | 8 ++-- 7 files changed, 91 insertions(+), 57 deletions(-) diff --git a/src/base_def.h b/src/base_def.h index c8ce733..00a77f4 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -68,6 +68,10 @@ namespace MultiArrayTools // multi_range.h template class MultiRange; + + // multi_array.h + template + class MultiArrayBase; // multi_array.h template diff --git a/src/multi_array.cc b/src/multi_array.cc index cb51356..f48fe92 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -1,34 +1,66 @@ +// -*- C++ -*- #include "multi_array.h" namespace MultiArrayTools { + /********************** + * MultiArrayBase * + **********************/ + + template + MultiArrayBase::MultiArrayBase(const Range& range) : mRange(new Range(range)) {} + + template + size_t MultiArrayBase::size() const + { + return mRange->size(); + } + + template + auto MultiArrayBase::begin() -> decltype(Range().begin()) + { + return mRange->begin(); + } + + template + auto MultiArrayBase::end() -> decltype(Range().end()) + { + return mRange->end(); + } + + template + const Range& MultiArrayBase::range() const + { + return *mRange; + } + /******************* * MultiArray * *******************/ template - MultiArray::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange->size()) - { - mInit = true; - } + MultiArray::MultiArray(const Range& range) : + MultiArrayBase(range), + mCont(MAB::mRange->size()) {} template - MultiArray::MultiArray(const Range& range, const std::vector& vec) : mRange(new Range(range)), - mCont(vec) + MultiArray::MultiArray(const Range& range, const std::vector& vec) : + MultiArrayBase(range), + mCont(vec) { - mInit = true; - if(mCont.size() > mRange->size()){ - mCont.erase(mCont.begin() + mRange->size(), mCont.end()); + if(mCont.size() > MAB::mRange->size()){ + mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); } } template - MultiArray::MultiArray(const Range& range, std::vector&& vec) : mRange(new Range(range)), mCont(vec) + MultiArray::MultiArray(const Range& range, std::vector&& vec) : + MultiArrayBase(range), + mCont(vec) { - mInit = true; - if(mCont.size() > mRange->size()){ - mCont.erase(mCont.begin() + mRange->size(), mCont.end()); + if(mCont.size() > MAB::mRange->size()){ + mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); } } @@ -51,24 +83,6 @@ namespace MultiArrayTools return MultiArrayOperationRoot(*this, Name("master", str...)); } - template - size_t MultiArray::size() const - { - return mRange->size(); - } - - template - auto MultiArray::begin() -> decltype(Range().begin()) - { - return mRange->begin(); - } - - template - auto MultiArray::end() -> decltype(Range().end()) - { - return mRange->end(); - } - template bool MultiArray::isSlice() const { diff --git a/src/multi_array.h b/src/multi_array.h index 3f6fd66..a4de6af 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -1,4 +1,3 @@ - // -*- C++ -*- #ifndef __multi_array_h__ @@ -14,12 +13,38 @@ namespace MultiArrayTools { - + template - class MultiArray + class MultiArrayBase { public: + DEFAULT_MEMBERS(MultiArrayBase); + MultiArrayBase(const Range& range); + + virtual T& operator[](const typename Range::IndexType& i) = 0; + virtual const T& operator[](const typename Range::IndexType& i) const = 0; + + virtual size_t size() const; + virtual bool isSlice() const = 0; + + virtual auto begin() -> decltype(Range().begin()); + virtual auto end() -> decltype(Range().end()); + + virtual const Range& range() const; + + protected: + std::shared_ptr mRange; + + }; + + template + class MultiArray : public MultiArrayBase + { + public: + + typedef MultiArrayBase MAB; + DEFAULT_MEMBERS(MultiArray); MultiArray(const Range& range); MultiArray(const Range& range, const std::vector& vec); @@ -27,23 +52,13 @@ namespace MultiArrayTools template MultiArrayOperationRoot operator()(const NameTypes&... str); + + T& operator[](const typename Range::IndexType& i) override; + const T& operator[](const typename Range::IndexType& i) const override; - T& operator[](const typename Range::IndexType& i); - const T& operator[](const typename Range::IndexType& i) const; - - size_t size() const; - - virtual bool isSlice() const; - - auto begin() -> decltype(Range().begin()); - auto end() -> decltype(Range().end()); - - protected: - - std::shared_ptr mRange; + virtual bool isSlice() const override; private: - bool mInit = false; std::vector mCont; }; diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 10615a5..ecde2e2 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -27,7 +27,7 @@ namespace MultiArrayTools template MultiArrayOperationRoot:: - MultiArrayOperationRoot(MultiArray& ma, + MultiArrayOperationRoot(MultiArrayBase& ma, const Name& nm) : MultiArrayOperationBase(), mArrayRef(ma), diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index e77b6b1..e44a9a7 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -39,7 +39,7 @@ namespace MultiArrayTools typedef MultiArrayOperationBase MAOB; typedef decltype(MultiArray().begin()) IndexType; - MultiArrayOperationRoot(MultiArray& ma, const Name& nm); + MultiArrayOperationRoot(MultiArrayBase& ma, const Name& nm); MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in); template @@ -84,7 +84,7 @@ namespace MultiArrayTools protected: - MultiArray& mArrayRef; + MultiArrayBase& mArrayRef; mutable IndexType mIndex; Name mNm; }; diff --git a/src/slice.cc b/src/slice.cc index f8b3d46..1aa1554 100644 --- a/src/slice.cc +++ b/src/slice.cc @@ -7,8 +7,9 @@ namespace MultiArrayTools template Slice:: - Slice(const Range& range, const Index& slicePos) : - MA::mRange(new Range(range)), + Slice(MultiArrayBase& ma, const Index& slicePos) : + MultiArrayBase(ma.range()), + multiArrayRef(ma), mSlicePos(slicePos) {} template diff --git a/src/slice.h b/src/slice.h index 0331118..7c295af 100644 --- a/src/slice.h +++ b/src/slice.h @@ -15,14 +15,14 @@ namespace MultiArrayTools // MARange = original range of multi array of which this is the slice // Index = index which determines the slice position (remnant of MARange w.o. Range) template - class Slice : public MultiArray // yes, Range is correct !!! + class Slice : public MultiArrayBase // yes, 'Range' is correct !!! { //MA::mCont has to be empty; only make use of the provided functions public: - typedef MultiArray MA; + typedef MultiArrayBase MAB; - Slice(const Range& range, const Index& slicePos); + Slice(MultiArray& ma, const Index& slicePos); T& operator[](const typename Range::IndexType& i) override; const T& operator[](const typename Range::IndexType& i) const override; @@ -40,7 +40,7 @@ namespace MultiArrayTools private: - MultiArray& multiArrayRef; + MultiArrayBase& multiArrayRef; IndefinitIndexBase* mMAPtr; // idx ptr for original MA Range IndefinitIndexBase* mOwnPtr; // idx ptr for own Range Index mSlicePos;