OneDim and TwoDim - Tests work

This commit is contained in:
Christian Zimmermann 2017-02-21 17:41:48 +01:00
parent 68612646fb
commit 876dc58918
8 changed files with 158 additions and 48 deletions

View file

@ -9,6 +9,21 @@
__class_name__(__class_name__&& in) = default; \ __class_name__(__class_name__&& in) = default; \
__class_name__& operator=(__class_name__&& in) = default __class_name__& operator=(__class_name__&& in) = default
//#define DEBUG_MODE_X
#ifdef DEBUG_MODE_X
#include <iostream>
#define CHECK std::cout << __FILE__ << ": @" << __LINE__ << " in " << __func__ << std::endl;
#define VCHECK(a) std::cout << __FILE__ << ": @" << __LINE__ \
<< " in " << __func__ << ": " << #a << " = " << a << std::endl;
#else
#define CHECK
#define VCHECK(a)
#endif
namespace MultiArrayTools namespace MultiArrayTools
{ {
/*********************** /***********************

View file

@ -82,13 +82,35 @@ namespace MultiArrayTools
return res > 0 ? static_cast<size_t>(res) : 0; return res > 0 ? static_cast<size_t>(res) : 0;
} }
bool IndefinitIndexBase::toNull() const
{
return true;
}
/************** /**************
* IndexBase * * IndexBase *
**************/ **************/
template <class Index>
IndexBase<Index>::IndexBase(RangeBase<Index> const* range) : mRange(range) {}
template <class Index> template <class Index>
size_t IndexBase<Index>::max() const size_t IndexBase<Index>::max() const
{ {
return mRange->size(); return mRange->size();
} }
template <class Index>
bool IndexBase<Index>::toNull() const
{
return mRange == nullptr;
}
template <class Index>
void IndexBase<Index>::assignRange(RangeBase<Index> const* range)
{
if(toNull()){
mRange = range;
}
}
} }

View file

@ -29,7 +29,7 @@ namespace MultiArrayTools
//virtual bool operator!=(const IndefinitIndexBase& i) = 0; //virtual bool operator!=(const IndefinitIndexBase& i) = 0;
virtual size_t dim() const = 0; virtual size_t dim() const = 0;
virtual size_t pos() const; // = mPos; implement !!! virtual size_t pos() const;
virtual const std::string& name() const; virtual const std::string& name() const;
virtual void name(const std::string& str); virtual void name(const std::string& str);
@ -47,6 +47,8 @@ namespace MultiArrayTools
virtual size_t max() const; virtual size_t max() const;
virtual size_t outOfRange() const; virtual size_t outOfRange() const;
virtual bool toNull() const;
protected: protected:
DEFAULT_MEMBERS(IndefinitIndexBase); DEFAULT_MEMBERS(IndefinitIndexBase);
@ -54,7 +56,7 @@ namespace MultiArrayTools
std::string mName; std::string mName;
size_t mPos; size_t mPos;
IndefinitIndexBase* mLinked; IndefinitIndexBase* mLinked = nullptr;
}; };
template <class Index> template <class Index>
@ -64,14 +66,19 @@ namespace MultiArrayTools
//virtual size_t pos() const override; // = mPos; implement !!! //virtual size_t pos() const override; // = mPos; implement !!!
virtual size_t max() const override; virtual size_t max() const override;
virtual bool toNull() const override;
virtual void assignRange(RangeBase<Index> const* range);
protected: protected:
DEFAULT_MEMBERS(IndexBase); DEFAULT_MEMBERS(IndexBase);
IndexBase(RangeBase<Index> const* range);
// translate index into position // translate index into position
virtual size_t evaluate(const Index& in) const = 0; virtual size_t evaluate(const Index& in) const = 0;
RangeBase<Index>* mRange; RangeBase<Index> const* mRange;
}; };
} }

View file

@ -57,7 +57,7 @@ namespace MultiArrayTools
template <class MultiIndex> template <class MultiIndex>
static size_t evaluate(const MultiIndex& index) static size_t evaluate(const MultiIndex& index)
{ {
const IndefinitIndexBase& subIndex = IndexGetter<N>::getIndex(index, N); const auto& subIndex = index.template getIndex<N>();
return Evaluation<N-1>::evaluate(index) * subIndex.max() + subIndex.pos(); return Evaluation<N-1>::evaluate(index) * subIndex.max() + subIndex.pos();
} }
}; };
@ -68,7 +68,7 @@ namespace MultiArrayTools
template <class MultiIndex> template <class MultiIndex>
static size_t evaluate(const MultiIndex& index) static size_t evaluate(const MultiIndex& index)
{ {
const IndefinitIndexBase& subIndex = IndexGetter<0>::getIndex(index, 0); const auto& subIndex = index.template getIndex<0>();
return subIndex.pos(); return subIndex.pos();
} }
}; };
@ -108,13 +108,17 @@ namespace MultiArrayTools
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>::MultiIndex(Indices&&... inds) : mIPack(std::make_tuple(inds...)) MultiIndex<Indices...>::MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
Indices&&... inds) : IndexBase<MultiIndex<Indices...> >(range),
mIPack(std::make_tuple(inds...))
{ {
IIB::mPos = evaluate(*this); IIB::mPos = evaluate(*this);
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>::MultiIndex(const IndexPack& ipack) : mIPack(ipack) MultiIndex<Indices...>::MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
const IndexPack& ipack) : IndexBase<MultiIndex<Indices...> >(range),
mIPack(ipack)
{ {
IIB::mPos = evaluate(*this); IIB::mPos = evaluate(*this);
} }
@ -194,6 +198,14 @@ namespace MultiArrayTools
return *this; return *this;
} }
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator()(const Indices&... inds)
{
mIPack = std::make_tuple(Indices(inds)...);
IIB::mPos = evaluate(*this);
return *this;
}
template <class... Indices> template <class... Indices>
size_t MultiIndex<Indices...>::dim() const size_t MultiIndex<Indices...>::dim() const
{ {
@ -230,15 +242,14 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
template <size_t N> template <size_t N>
auto MultiIndex<Indices...>::getIndex() -> decltype(std::get<N>(mIPack)) typename std::tuple_element<N, std::tuple<Indices...> >::type& MultiIndex<Indices...>::getIndex()
{ {
return std::get<N>(mIPack); return std::get<N>(mIPack);
} }
template <class... Indices> template <class... Indices>
template <size_t N> template <size_t N>
auto MultiIndex<Indices...>::getIndex() const -> typename std::tuple_element<N, std::tuple<Indices...> >::type const& MultiIndex<Indices...>::getIndex() const
const decltype(std::get<N>(mIPack))
{ {
return std::get<N>(mIPack); return std::get<N>(mIPack);
} }
@ -281,6 +292,40 @@ namespace MultiArrayTools
get(i).linkTo(target); get(i).linkTo(target);
} }
} }
/*
template <size_t N>
struct RangeAssigner
{
template <class... Indices>
static void assignRange(const RangeBase<MultiIndex<Indices...> >& multiRange,
MultiIndex<Indices...>& multiIndex)
{
multiIndex.template getIndex<N>().assignRange(nullptr &multiRange.template getRange<N>());
RangeAssigner<N-1>::assignRange(multiRange, multiIndex);
}
};
template <>
struct RangeAssigner<0>
{
template <class... Indices>
static void assignRange(const RangeBase<MultiIndex<Indices...> >& multiRange,
MultiIndex<Indices...>& multiIndex)
{
multiIndex.template getIndex<0>().assignRange(nullptr &multiRange.template getRange<0>());
}
};
template <class... Indices>
void MultiIndex<Indices...>::assignRange(RangeBase<MultiIndex<Indices...> > const* range)
{
if(IB::toNull()){
IB::mRange = range;
}
MultiIndex<Indices...>& thisRef = *this;
RangeAssigner<sizeof...(Indices)-1>::assignRange(*range, thisRef);
}*/
/****************** /******************
* MultiRange * * MultiRange *
@ -396,7 +441,7 @@ namespace MultiArrayTools
{ {
std::tuple<typename Ranges::IndexType...> is; std::tuple<typename Ranges::IndexType...> is;
IndexSetter<sizeof...(Ranges)-1>::setBegin(is,mSpace); IndexSetter<sizeof...(Ranges)-1>::setBegin(is,mSpace);
return MultiIndex<typename Ranges::IndexType...>(is); return MultiIndex<typename Ranges::IndexType...>(this, is);
} }
template <class... Ranges> template <class... Ranges>
@ -404,6 +449,6 @@ namespace MultiArrayTools
{ {
std::tuple<typename Ranges::IndexType...> is; std::tuple<typename Ranges::IndexType...> is;
IndexSetter<sizeof...(Ranges)-1>::setEnd(is,mSpace); IndexSetter<sizeof...(Ranges)-1>::setEnd(is,mSpace);
return MultiIndex<typename Ranges::IndexType...>(is); return MultiIndex<typename Ranges::IndexType...>(this, is);
} }
} }

View file

@ -32,8 +32,11 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(MultiIndex); DEFAULT_MEMBERS(MultiIndex);
MultiIndex(Indices&&... inds); MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
MultiIndex(const IndexPack& ipack); Indices&&... inds);
MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
const IndexPack& ipack);
virtual MultiIndex& operator++() override; virtual MultiIndex& operator++() override;
virtual MultiIndex& operator--() override; virtual MultiIndex& operator--() override;
@ -44,15 +47,16 @@ namespace MultiArrayTools
virtual MultiRangeType rangeType() const override; virtual MultiRangeType rangeType() const override;
template <size_t N> template <size_t N>
auto getIndex() -> decltype(std::get<N>(mIPack)); typename std::tuple_element<N, std::tuple<Indices...> >::type& getIndex();
template <size_t N> template <size_t N>
auto getIndex() const -> const decltype(std::get<N>(mIPack)); typename std::tuple_element<N, std::tuple<Indices...> >::type const& getIndex() const;
IndefinitIndexBase& get(size_t n); IndefinitIndexBase& get(size_t n);
const IndefinitIndexBase& get(size_t n) const; const IndefinitIndexBase& get(size_t n) const;
MultiIndex& operator()(Indices&&... inds); MultiIndex& operator()(Indices&&... inds);
MultiIndex& operator()(const Indices&... inds);
virtual void name(const Name& nm) override; virtual void name(const Name& nm) override;
@ -61,8 +65,15 @@ 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 assignRange(RangeBase<MultiIndex<Indices...> > const* range) override;
}; };
/*****************************
* IndexGetter Functions *
****************************/
template <class... Ranges> template <class... Ranges>
class MultiRange : public RangeBase<MultiIndex<typename Ranges::IndexType...> > class MultiRange : public RangeBase<MultiIndex<typename Ranges::IndexType...> >
{ {

View file

@ -45,13 +45,13 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE> SingleRange<U,TYPE>::begin() const SingleIndex<U,TYPE> SingleRange<U,TYPE>::begin() const
{ {
return SingleIndex<U,TYPE>(0); return SingleIndex<U,TYPE>(this, 0);
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE> SingleRange<U,TYPE>::end() const SingleIndex<U,TYPE> SingleRange<U,TYPE>::end() const
{ {
return SingleIndex<U,TYPE>(mSpace.size()); return SingleIndex<U,TYPE>(this, mSpace.size());
} }
/****************** /******************
@ -59,13 +59,15 @@ namespace MultiArrayTools
******************/ ******************/
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>::SingleIndex(const U& upos, size_t disambig) : IndexBase<SingleIndex<U,TYPE> >() SingleIndex<U,TYPE>::SingleIndex(RangeBase<SingleIndex<U,TYPE> > const* range,
const U& upos, size_t disambig) : IndexBase<SingleIndex<U,TYPE> >(range)
{ {
IIB::setPos( dynamic_cast<SingleRange<U,TYPE>*>( IB::mRange )->get(upos) ); IIB::setPos( dynamic_cast<SingleRange<U,TYPE> const*>( IB::mRange )->get(upos) );
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>::SingleIndex(size_t pos) : IndexBase<SingleIndex<U,TYPE> >() SingleIndex<U,TYPE>::SingleIndex(RangeBase<SingleIndex<U,TYPE> > const* range,
size_t pos) : IndexBase<SingleIndex<U,TYPE> >(range)
{ {
IIB::setPos( pos ); IIB::setPos( pos );
} }
@ -126,7 +128,7 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
const U& SingleIndex<U,TYPE>::getMetaPos() const const U& SingleIndex<U,TYPE>::getMetaPos() const
{ {
return dynamic_cast<SingleRange<U,TYPE>*>( IB::mRange )->get(IIB::pos()); return dynamic_cast<SingleRange<U,TYPE> const*>( IB::mRange )->get(IIB::pos());
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
@ -147,11 +149,12 @@ namespace MultiArrayTools
target->link(this); target->link(this);
} }
/*
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator=(const U& upos) SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator=(const U& upos)
{ {
IIB::setPos( dynamic_cast<SingleRange<U,TYPE>*>( IB::mRange )->get(upos) ); IIB::setPos( dynamic_cast<SingleRange<U,TYPE> const*>( IB::mRange )->get(upos) );
return *this; return *this;
} }*/
} }

View file

@ -24,10 +24,13 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(SingleIndex); DEFAULT_MEMBERS(SingleIndex);
// find better solution !!! // find better solution !!!
SingleIndex(const U& upos, size_t disambig); SingleIndex(RangeBase<SingleIndex<U,TYPE> > const* range,
SingleIndex(size_t pos); const U& upos, size_t disambig);
virtual SingleIndex& operator=(const U& upos); SingleIndex(RangeBase<SingleIndex<U,TYPE> > const* range,
size_t pos);
//virtual SingleIndex& operator=(const U& upos);
virtual SingleIndex& operator=(size_t pos) override; virtual SingleIndex& operator=(size_t pos) override;
virtual SingleIndex& operator++() override; virtual SingleIndex& operator++() override;

View file

@ -55,11 +55,12 @@ namespace {
TEST_F(OneDimTest, CorrectAssigned) TEST_F(OneDimTest, CorrectAssigned)
{ {
EXPECT_EQ(ma[0], -5); auto i = ma.begin();
EXPECT_EQ(ma[1], 6); EXPECT_EQ(ma[i = 0], -5);
EXPECT_EQ(ma[2], 2); EXPECT_EQ(ma[i = 1], 6);
EXPECT_EQ(ma[3], 1); EXPECT_EQ(ma[i = 2], 2);
EXPECT_EQ(ma[4], 9); EXPECT_EQ(ma[i = 3], 1);
EXPECT_EQ(ma[i = 4], 9);
} }
TEST_F(TwoDimTest, CorrectExtensions) TEST_F(TwoDimTest, CorrectExtensions)
@ -67,23 +68,26 @@ 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();
EXPECT_EQ(ma[i(0,0)], -5); auto i1 = i.template getIndex<0>();
EXPECT_EQ(ma[i(0,1)], 6); auto i2 = i.template getIndex<1>();
EXPECT_EQ(ma[i(0,2)], 2);
EXPECT_EQ(ma[i(0,3)], 1);
EXPECT_EQ(ma[i(1,0)], 9);
EXPECT_EQ(ma[i(1,1)], 54);
EXPECT_EQ(ma[i(1,2)], 27);
EXPECT_EQ(ma[i(1,3)], -7);
EXPECT_EQ(ma[i(2,0)], -13);
EXPECT_EQ(ma[i(2,1)], 32);
EXPECT_EQ(ma[i(2,2)], 90);
EXPECT_EQ(ma[i(2,3)], -67);
}
EXPECT_EQ(ma[i(i1 = 0, i2 = 0)], -5);
EXPECT_EQ(ma[i(i1 = 0, i2 = 1)], 6);
EXPECT_EQ(ma[i(i1 = 0, i2 = 2)], 2);
EXPECT_EQ(ma[i(i1 = 0, i2 = 3)], 1);
EXPECT_EQ(ma[i(i1 = 1, i2 = 0)], 9);
EXPECT_EQ(ma[i(i1 = 1, i2 = 1)], 54);
EXPECT_EQ(ma[i(i1 = 1, i2 = 2)], 27);
EXPECT_EQ(ma[i(i1 = 1, i2 = 3)], -7);
EXPECT_EQ(ma[i(i1 = 2, i2 = 0)], -13);
EXPECT_EQ(ma[i(i1 = 2, i2 = 1)], 32);
EXPECT_EQ(ma[i(i1 = 2, i2 = 2)], 90);
EXPECT_EQ(ma[i(i1 = 2, i2 = 3)], -67);
}
} // end namespace } // end namespace