start correcting compile errors regarding Slice (still linker error existent)

This commit is contained in:
Christian Zimmermann 2017-02-28 11:27:23 +01:00
parent f489d165ab
commit 2c12332abb
9 changed files with 154 additions and 18 deletions

View file

@ -79,7 +79,7 @@ namespace MultiArrayTools
{ {
mPos = pos; mPos = pos;
if(linked()){ if(linked()){
mLinked->setPos(pos); mLinked->setPos(mPos);
mLinked->evalMajor(); mLinked->evalMajor();
} }
} }
@ -149,4 +149,11 @@ namespace MultiArrayTools
{ {
setPos( evaluate(*dynamic_cast<Index const*>( this )) ); setPos( evaluate(*dynamic_cast<Index const*>( this )) );
} }
template <class Index>
void IndexBase<Index>::copyPos(const Index& in)
{
IIB::setPos(in.pos());
}
} }

View file

@ -72,6 +72,8 @@ namespace MultiArrayTools
{ {
public: public:
typedef IndefinitIndexBase IIB;
DEFAULT_MEMBERS(IndexBase); DEFAULT_MEMBERS(IndexBase);
IndexBase(RangeBase<Index> const* range); IndexBase(RangeBase<Index> const* range);
@ -83,6 +85,8 @@ namespace MultiArrayTools
virtual void eval() override; virtual void eval() override;
virtual void copyPos(const Index& in);
protected: protected:
// translate index into position // translate index into position

View file

@ -10,5 +10,6 @@
#include "multi_range.h" #include "multi_range.h"
#include "multi_array_operation.h" #include "multi_array_operation.h"
#include "multi_array.h" #include "multi_array.h"
#include "slice.h"
#endif #endif

View file

@ -74,7 +74,7 @@ namespace MultiArrayTools
IndexType& index(); IndexType& index();
// set index // set index -> implement !!!!!
MultiArrayOperationRoot<T,Range>& operator[](const IndexType& ind); MultiArrayOperationRoot<T,Range>& operator[](const IndexType& ind);
virtual void linkIndicesTo(IndefinitIndexBase* target) const override; virtual void linkIndicesTo(IndefinitIndexBase* target) const override;
@ -84,6 +84,9 @@ namespace MultiArrayTools
const Name& name() const; const Name& name() const;
template <typename U, class RangeX>
friend class MultiArrayOperationRoot;
protected: protected:
void performAssignment(const MultiArrayOperationBase<T>& in); void performAssignment(const MultiArrayOperationBase<T>& in);

View file

@ -126,6 +126,28 @@ namespace MultiArrayTools
std::get<0>(iPack).subOrd(major); std::get<0>(iPack).subOrd(major);
} }
}; };
template <size_t N>
struct PositionCopy
{
template <class MultiIndex>
static void copyPos(MultiIndex& target, const MultiIndex& source)
{
target.template getIndex<N>().copyPos( source.template getIndex<N>() );
PositionCopy<N-1>::copyPos(target, source);
}
};
template <>
struct PositionCopy<0>
{
template <class MultiIndex>
static void copyPos(MultiIndex& target, const MultiIndex& source)
{
target.template getIndex<0>().copyPos( source.template getIndex<0>() );
}
};
} }
template <class... Indices> template <class... Indices>
@ -349,6 +371,13 @@ namespace MultiArrayTools
get(i).linkTo(target); get(i).linkTo(target);
} }
} }
template <class... Indices>
void MultiIndex<Indices...>::copyPos(const MultiIndex<Indices...>& in)
{
PositionCopy<sizeof...(Indices)-1>::copyPos(*this, in);
}
/* /*
template <size_t N> template <size_t N>
struct RangeAssigner struct RangeAssigner

View file

@ -71,6 +71,8 @@ namespace MultiArrayTools
virtual bool link(IndefinitIndexBase* toLink) override; virtual bool link(IndefinitIndexBase* toLink) override;
virtual void linkTo(IndefinitIndexBase* target) override; virtual void linkTo(IndefinitIndexBase* target) override;
virtual void copyPos(const MultiIndex<Indices...>& in) override;
//virtual void assignRange(RangeBase<MultiIndex<Indices...> > const* range) override; //virtual void assignRange(RangeBase<MultiIndex<Indices...> > const* range) override;
}; };

View file

@ -6,28 +6,53 @@ namespace MultiArrayTools
{ {
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
Slice<T,Range,MARange,Index>:: Slice<T,Range,MARange>::
Slice(const Range& range) : Slice(const Range& range) :
MultiArrayBase<T,Range>(range), MultiArrayBase<T,Range>(range) {}
mMultiArrayRef(*this) {}
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
bool Slice<T,Range,MARange,Index>::isSlice() const bool Slice<T,Range,MARange>::isSlice() const
{ {
return true; return true;
} }
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
void Slice<T,Range,MARange,Index>::set(MultiArrayBase<T,MARange>& multiArrayRef, void Slice<T,Range,MARange>::set(MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm, // for correct linkage const Name& ownNm, // for correct linkage
IndefinitIndexBase* MAPtr, // for desired slice position IndefinitIndexBase* MAPtr, // for desired slice position
const Name& MANm) // for correct linkage) const Name& MANm) // for correct linkage)
{ {
mMultiArrayRef = multiArrayRef; mMultiArrayPtr = &multiArrayRef;
mMAPtr.reset(new typename MARange::IndexType(*MAPtr)); mMAPtr.reset(new typename MARange::IndexType( dynamic_cast<typename MARange::IndexType&>( *MAPtr ) ) );
mOwnPtr.reset(new typename Range::IndexType()); mOwnPtr.reset(new typename Range::IndexType());
mMAPtr->name(MANm); mMAPtr->name(MANm);
mOwnPtr->name(ownNm); mOwnPtr->name(ownNm);
mMAPtr->linkTo(mOwnPtr); mMAPtr->linkTo(mOwnPtr.get());
}
template <typename T, class Range, class MARange>
auto Slice<T,Range,MARange>::begin() -> decltype(Range().begin())
{
return MAB::mRange->begin();
}
template <typename T, class Range, class MARange>
auto Slice<T,Range,MARange>::end() -> decltype(Range().end())
{
return MAB::mRange->end();
}
template <typename T, class Range, class MARange>
T& Slice<T,Range,MARange>::operator[](const typename Range::IndexType& i)
{
dynamic_cast<typename Range::IndexType&>(*mOwnPtr).copyPos(i);
return (*mMultiArrayPtr)[ dynamic_cast<typename MARange::IndexType&>( *mMAPtr ) ];
}
template <typename T, class Range, class MARange>
const T& Slice<T,Range,MARange>::operator[](const typename Range::IndexType& i) const
{
dynamic_cast<typename Range::IndexType&>(*mOwnPtr).copyPos(i);
return (*mMultiArrayPtr)[ dynamic_cast<typename MARange::IndexType&>( *mMAPtr ) ];
} }
} }

View file

@ -23,14 +23,14 @@ namespace MultiArrayTools
Slice(const Range& range); Slice(const Range& range);
T& operator[](const typename Range::IndexType& i) override; virtual T& operator[](const typename Range::IndexType& i) override;
const T& operator[](const typename Range::IndexType& i) const override; virtual const T& operator[](const typename Range::IndexType& i) const override;
//Slice& setSlicePos(const Index& slicePos); //Slice& setSlicePos(const Index& slicePos);
// link given Index to mMAPtr which is index of total array // link given Index to mMAPtr which is index of total array
auto begin() override -> decltype(Range().begin()); virtual auto begin() -> decltype(Range().begin()) override;
auto end() override -> decltype(Range().end()); virtual auto end() -> decltype(Range().end()) override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
@ -41,7 +41,7 @@ namespace MultiArrayTools
private: private:
MultiArrayBase<T,MARange>& mMultiArrayRef; MultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr;
std::shared_ptr<IndefinitIndexBase> mMAPtr; // idx ptr for original MA Range std::shared_ptr<IndefinitIndexBase> mMAPtr; // idx ptr for original MA Range
std::shared_ptr<IndefinitIndexBase> mOwnPtr; // idx ptr for own Range std::shared_ptr<IndefinitIndexBase> mOwnPtr; // idx ptr for own Range
//Index mSlicePos; //Index mSlicePos;

View file

@ -109,6 +109,33 @@ namespace {
MultiArray3dAny ma3d; MultiArray3dAny ma3d;
}; };
class SliceTest : public ::testing::Test
{
protected:
typedef MAT::SingleRange<char,MAT::RangeType::ANY> Range1dAny;
typedef MAT::MultiRange<Range1dAny,Range1dAny> Range2dAny;
typedef MAT::MultiRange<Range1dAny,Range1dAny,Range1dAny> Range3dAny;
typedef MAT::MultiArray<int,Range3dAny> MultiArray3dAny;
typedef MAT::Slice<int,Range2dAny,Range3dAny> Slice2d3dAny;
SliceTest() : r1({'a','b','c'}), r2({'a','b','c','d'}), r3({'a','b'}),
ra(r1,r3),
rb(r1,r2),
r3d(r1,r2,r3),
ma(r3d, {-5,6,2,1,9,54,27,-7,-13,32,90,-67,
-10,16,-2,101,39,-64,81,-22,14,34,95,-62}) {}
Range1dAny r1;
Range1dAny r2;
Range1dAny r3;
Range2dAny ra;
Range2dAny rb;
Range3dAny r3d;
MultiArray3dAny ma;
//Slice2d3dAny sl;
};
TEST_F(OneDimTest, CorrectExtensions) TEST_F(OneDimTest, CorrectExtensions)
{ {
EXPECT_EQ(ma.size(), 5); EXPECT_EQ(ma.size(), 5);
@ -129,7 +156,6 @@ namespace {
EXPECT_EQ(ma.size(), 12); EXPECT_EQ(ma.size(), 12);
} }
TEST_F(TwoDimTest, CorrectAssigned) TEST_F(TwoDimTest, CorrectAssigned)
{ {
auto i = ma.begin(); auto i = ma.begin();
@ -324,7 +350,46 @@ namespace {
EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 2, i3 = 0)], 106); EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 2, i3 = 0)], 106);
EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 2, i3 = 1)], 126); EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 2, i3 = 1)], 126);
EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 3, i3 = 0)], 30); EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 3, i3 = 0)], 30);
EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 3, i3 = 1)], -127); } EXPECT_EQ(ma3d2[i(i1 = 2, i2 = 3, i3 = 1)], -127);
}
TEST_F(SliceTest, CorrectSize)
{
auto i = ma.begin();
auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>();
Slice2d3dAny sl(ra);
sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)];
EXPECT_EQ(sl.size(), 6);
Slice2d3dAny sl2(rb);
sl("alpha","beta") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 0, i3 = 1)];
EXPECT_EQ(sl.size(), 12);
}
TEST_F(SliceTest, CorrectContent)
{
auto i = ma.begin();
auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>();
Slice2d3dAny sl(ra);
sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)];
auto j = sl.begin();
auto j1 = j.template getIndex<0>();
auto j2 = j.template getIndex<1>();
EXPECT_EQ(sl[j(j1 = 0, j2 = 0)], 9);
EXPECT_EQ(sl[j(j1 = 0, j2 = 1)], 54);
EXPECT_EQ(sl[j(j1 = 1, j2 = 0)], -10);
EXPECT_EQ(sl[j(j1 = 1, j2 = 1)], 16);
EXPECT_EQ(sl[j(j1 = 2, j2 = 0)], 14);
EXPECT_EQ(sl[j(j1 = 2, j2 = 1)], 34);
}
} // end namespace } // end namespace