restructure MultiArray inheritance

This commit is contained in:
Christian Zimmermann 2017-02-26 18:14:58 +01:00
parent d2d038fd5b
commit d1363c3c3d
7 changed files with 91 additions and 57 deletions

View file

@ -69,6 +69,10 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
class MultiRange; class MultiRange;
// multi_array.h
template <typename T, class Range>
class MultiArrayBase;
// multi_array.h // multi_array.h
template <typename T, class Range> template <typename T, class Range>
class MultiArray; class MultiArray;

View file

@ -1,34 +1,66 @@
// -*- C++ -*-
#include "multi_array.h" #include "multi_array.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
/**********************
* MultiArrayBase *
**********************/
template <typename T, class Range>
MultiArrayBase<T,Range>::MultiArrayBase(const Range& range) : mRange(new Range(range)) {}
template <typename T, class Range>
size_t MultiArrayBase<T,Range>::size() const
{
return mRange->size();
}
template <typename T, class Range>
auto MultiArrayBase<T,Range>::begin() -> decltype(Range().begin())
{
return mRange->begin();
}
template <typename T, class Range>
auto MultiArrayBase<T,Range>::end() -> decltype(Range().end())
{
return mRange->end();
}
template <typename T, class Range>
const Range& MultiArrayBase<T,Range>::range() const
{
return *mRange;
}
/******************* /*******************
* MultiArray * * MultiArray *
*******************/ *******************/
template <typename T, class Range> template <typename T, class Range>
MultiArray<T,Range>::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange->size()) MultiArray<T,Range>::MultiArray(const Range& range) :
{ MultiArrayBase<T,Range>(range),
mInit = true; mCont(MAB::mRange->size()) {}
}
template <typename T, class Range> template <typename T, class Range>
MultiArray<T,Range>::MultiArray(const Range& range, const std::vector<T>& vec) : mRange(new Range(range)), MultiArray<T,Range>::MultiArray(const Range& range, const std::vector<T>& vec) :
mCont(vec) MultiArrayBase<T,Range>(range),
mCont(vec)
{ {
mInit = true; if(mCont.size() > MAB::mRange->size()){
if(mCont.size() > mRange->size()){ mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end());
mCont.erase(mCont.begin() + mRange->size(), mCont.end());
} }
} }
template <typename T, class Range> template <typename T, class Range>
MultiArray<T,Range>::MultiArray(const Range& range, std::vector<T>&& vec) : mRange(new Range(range)), mCont(vec) MultiArray<T,Range>::MultiArray(const Range& range, std::vector<T>&& vec) :
MultiArrayBase<T,Range>(range),
mCont(vec)
{ {
mInit = true; if(mCont.size() > MAB::mRange->size()){
if(mCont.size() > mRange->size()){ mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end());
mCont.erase(mCont.begin() + mRange->size(), mCont.end());
} }
} }
@ -51,24 +83,6 @@ namespace MultiArrayTools
return MultiArrayOperationRoot<T,Range>(*this, Name("master", str...)); return MultiArrayOperationRoot<T,Range>(*this, Name("master", str...));
} }
template <typename T, class Range>
size_t MultiArray<T,Range>::size() const
{
return mRange->size();
}
template <typename T, class Range>
auto MultiArray<T,Range>::begin() -> decltype(Range().begin())
{
return mRange->begin();
}
template <typename T, class Range>
auto MultiArray<T,Range>::end() -> decltype(Range().end())
{
return mRange->end();
}
template <typename T, class Range> template <typename T, class Range>
bool MultiArray<T,Range>::isSlice() const bool MultiArray<T,Range>::isSlice() const
{ {

View file

@ -1,4 +1,3 @@
// -*- C++ -*- // -*- C++ -*-
#ifndef __multi_array_h__ #ifndef __multi_array_h__
@ -16,10 +15,36 @@ namespace MultiArrayTools
{ {
template <typename T, class Range> template <typename T, class Range>
class MultiArray class MultiArrayBase
{ {
public: 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<Range> mRange;
};
template <typename T, class Range>
class MultiArray : public MultiArrayBase<T,Range>
{
public:
typedef MultiArrayBase<T,Range> MAB;
DEFAULT_MEMBERS(MultiArray); DEFAULT_MEMBERS(MultiArray);
MultiArray(const Range& range); MultiArray(const Range& range);
MultiArray(const Range& range, const std::vector<T>& vec); MultiArray(const Range& range, const std::vector<T>& vec);
@ -28,22 +53,12 @@ namespace MultiArrayTools
template <class... NameTypes> template <class... NameTypes>
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str); MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
T& operator[](const typename Range::IndexType& i); T& operator[](const typename Range::IndexType& i) override;
const T& operator[](const typename Range::IndexType& i) const; const T& operator[](const typename Range::IndexType& i) const override;
size_t size() const; virtual bool isSlice() const override;
virtual bool isSlice() const;
auto begin() -> decltype(Range().begin());
auto end() -> decltype(Range().end());
protected:
std::shared_ptr<Range> mRange;
private: private:
bool mInit = false;
std::vector<T> mCont; std::vector<T> mCont;
}; };

View file

@ -27,7 +27,7 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
MultiArrayOperationRoot<T,Range>:: MultiArrayOperationRoot<T,Range>::
MultiArrayOperationRoot(MultiArray<T,Range>& ma, MultiArrayOperationRoot(MultiArrayBase<T,Range>& ma,
const Name& nm) : const Name& nm) :
MultiArrayOperationBase<T>(), MultiArrayOperationBase<T>(),
mArrayRef(ma), mArrayRef(ma),

View file

@ -39,7 +39,7 @@ namespace MultiArrayTools
typedef MultiArrayOperationBase<T> MAOB; typedef MultiArrayOperationBase<T> MAOB;
typedef decltype(MultiArray<T,Range>().begin()) IndexType; typedef decltype(MultiArray<T,Range>().begin()) IndexType;
MultiArrayOperationRoot(MultiArray<T,Range>& ma, const Name& nm); MultiArrayOperationRoot(MultiArrayBase<T,Range>& ma, const Name& nm);
MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in); MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in);
template <class Operation, class... MAOps> template <class Operation, class... MAOps>
@ -84,7 +84,7 @@ namespace MultiArrayTools
protected: protected:
MultiArray<T,Range>& mArrayRef; MultiArrayBase<T,Range>& mArrayRef;
mutable IndexType mIndex; mutable IndexType mIndex;
Name mNm; Name mNm;
}; };

View file

@ -7,8 +7,9 @@ namespace MultiArrayTools
template <typename T, class Range, class MARange, class Index> template <typename T, class Range, class MARange, class Index>
Slice<T,Range,MARange,Index>:: Slice<T,Range,MARange,Index>::
Slice(const Range& range, const Index& slicePos) : Slice(MultiArrayBase<T,MARange>& ma, const Index& slicePos) :
MA::mRange(new Range(range)), MultiArrayBase<T,Range>(ma.range()),
multiArrayRef(ma),
mSlicePos(slicePos) {} mSlicePos(slicePos) {}
template <typename T, class Range, class MARange, class Index> template <typename T, class Range, class MARange, class Index>

View file

@ -15,14 +15,14 @@ namespace MultiArrayTools
// MARange = original range of multi array of which this is the slice // 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) // Index = index which determines the slice position (remnant of MARange w.o. Range)
template <typename T, class Range, class MARange, class Index> template <typename T, class Range, class MARange, class Index>
class Slice : public MultiArray<T,Range> // yes, Range is correct !!! class Slice : public MultiArrayBase<T,Range> // yes, 'Range' is correct !!!
{ {
//MA::mCont has to be empty; only make use of the provided functions //MA::mCont has to be empty; only make use of the provided functions
public: public:
typedef MultiArray<T,MARange> MA; typedef MultiArrayBase<T,MARange> MAB;
Slice(const Range& range, const Index& slicePos); Slice(MultiArray<T,MARange>& ma, const Index& slicePos);
T& operator[](const typename Range::IndexType& i) override; T& operator[](const typename Range::IndexType& i) override;
const T& operator[](const typename Range::IndexType& i) const override; const T& operator[](const typename Range::IndexType& i) const override;
@ -40,7 +40,7 @@ namespace MultiArrayTools
private: private:
MultiArray<T,MARange>& multiArrayRef; MultiArrayBase<T,MARange>& multiArrayRef;
IndefinitIndexBase* mMAPtr; // idx ptr for original MA Range IndefinitIndexBase* mMAPtr; // idx ptr for original MA Range
IndefinitIndexBase* mOwnPtr; // idx ptr for own Range IndefinitIndexBase* mOwnPtr; // idx ptr for own Range
Index mSlicePos; Index mSlicePos;