restructure MultiArray inheritance
This commit is contained in:
parent
d2d038fd5b
commit
d1363c3c3d
7 changed files with 91 additions and 57 deletions
|
@ -69,6 +69,10 @@ namespace MultiArrayTools
|
|||
template <class... Ranges>
|
||||
class MultiRange;
|
||||
|
||||
// multi_array.h
|
||||
template <typename T, class Range>
|
||||
class MultiArrayBase;
|
||||
|
||||
// multi_array.h
|
||||
template <typename T, class Range>
|
||||
class MultiArray;
|
||||
|
|
|
@ -1,34 +1,66 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
#include "multi_array.h"
|
||||
|
||||
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 *
|
||||
*******************/
|
||||
|
||||
template <typename T, class Range>
|
||||
MultiArray<T,Range>::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange->size())
|
||||
{
|
||||
mInit = true;
|
||||
}
|
||||
MultiArray<T,Range>::MultiArray(const Range& range) :
|
||||
MultiArrayBase<T,Range>(range),
|
||||
mCont(MAB::mRange->size()) {}
|
||||
|
||||
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) :
|
||||
MultiArrayBase<T,Range>(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 <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() > 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<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>
|
||||
bool MultiArray<T,Range>::isSlice() const
|
||||
{
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
// -*- C++ -*-
|
||||
|
||||
#ifndef __multi_array_h__
|
||||
|
@ -16,10 +15,36 @@ namespace MultiArrayTools
|
|||
{
|
||||
|
||||
template <typename T, class Range>
|
||||
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<Range> mRange;
|
||||
|
||||
};
|
||||
|
||||
template <typename T, class Range>
|
||||
class MultiArray : public MultiArrayBase<T,Range>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef MultiArrayBase<T,Range> MAB;
|
||||
|
||||
DEFAULT_MEMBERS(MultiArray);
|
||||
MultiArray(const Range& range);
|
||||
MultiArray(const Range& range, const std::vector<T>& vec);
|
||||
|
@ -28,22 +53,12 @@ namespace MultiArrayTools
|
|||
template <class... NameTypes>
|
||||
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
|
||||
|
||||
T& operator[](const typename Range::IndexType& i);
|
||||
const T& operator[](const typename Range::IndexType& i) const;
|
||||
T& operator[](const typename Range::IndexType& i) override;
|
||||
const T& operator[](const typename Range::IndexType& i) const override;
|
||||
|
||||
size_t size() const;
|
||||
|
||||
virtual bool isSlice() const;
|
||||
|
||||
auto begin() -> decltype(Range().begin());
|
||||
auto end() -> decltype(Range().end());
|
||||
|
||||
protected:
|
||||
|
||||
std::shared_ptr<Range> mRange;
|
||||
virtual bool isSlice() const override;
|
||||
|
||||
private:
|
||||
bool mInit = false;
|
||||
std::vector<T> mCont;
|
||||
};
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace MultiArrayTools
|
|||
|
||||
template <typename T, class Range>
|
||||
MultiArrayOperationRoot<T,Range>::
|
||||
MultiArrayOperationRoot(MultiArray<T,Range>& ma,
|
||||
MultiArrayOperationRoot(MultiArrayBase<T,Range>& ma,
|
||||
const Name& nm) :
|
||||
MultiArrayOperationBase<T>(),
|
||||
mArrayRef(ma),
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace MultiArrayTools
|
|||
typedef MultiArrayOperationBase<T> MAOB;
|
||||
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);
|
||||
|
||||
template <class Operation, class... MAOps>
|
||||
|
@ -84,7 +84,7 @@ namespace MultiArrayTools
|
|||
|
||||
protected:
|
||||
|
||||
MultiArray<T,Range>& mArrayRef;
|
||||
MultiArrayBase<T,Range>& mArrayRef;
|
||||
mutable IndexType mIndex;
|
||||
Name mNm;
|
||||
};
|
||||
|
|
|
@ -7,8 +7,9 @@ namespace MultiArrayTools
|
|||
|
||||
template <typename T, class Range, class MARange, class Index>
|
||||
Slice<T,Range,MARange,Index>::
|
||||
Slice(const Range& range, const Index& slicePos) :
|
||||
MA::mRange(new Range(range)),
|
||||
Slice(MultiArrayBase<T,MARange>& ma, const Index& slicePos) :
|
||||
MultiArrayBase<T,Range>(ma.range()),
|
||||
multiArrayRef(ma),
|
||||
mSlicePos(slicePos) {}
|
||||
|
||||
template <typename T, class Range, class MARange, class Index>
|
||||
|
|
|
@ -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 <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
|
||||
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;
|
||||
const T& operator[](const typename Range::IndexType& i) const override;
|
||||
|
@ -40,7 +40,7 @@ namespace MultiArrayTools
|
|||
|
||||
private:
|
||||
|
||||
MultiArray<T,MARange>& multiArrayRef;
|
||||
MultiArrayBase<T,MARange>& multiArrayRef;
|
||||
IndefinitIndexBase* mMAPtr; // idx ptr for original MA Range
|
||||
IndefinitIndexBase* mOwnPtr; // idx ptr for own Range
|
||||
Index mSlicePos;
|
||||
|
|
Loading…
Reference in a new issue