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__& 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
{
/***********************

View file

@ -81,14 +81,36 @@ namespace MultiArrayTools
int res = pos() - max();
return res > 0 ? static_cast<size_t>(res) : 0;
}
bool IndefinitIndexBase::toNull() const
{
return true;
}
/**************
* IndexBase *
**************/
template <class Index>
IndexBase<Index>::IndexBase(RangeBase<Index> const* range) : mRange(range) {}
template <class Index>
size_t IndexBase<Index>::max() const
{
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 size_t dim() const = 0;
virtual size_t pos() const; // = mPos; implement !!!
virtual size_t pos() const;
virtual const std::string& name() const;
virtual void name(const std::string& str);
@ -46,6 +46,8 @@ namespace MultiArrayTools
virtual size_t max() const;
virtual size_t outOfRange() const;
virtual bool toNull() const;
protected:
@ -54,7 +56,7 @@ namespace MultiArrayTools
std::string mName;
size_t mPos;
IndefinitIndexBase* mLinked;
IndefinitIndexBase* mLinked = nullptr;
};
template <class Index>
@ -64,14 +66,19 @@ namespace MultiArrayTools
//virtual size_t pos() const override; // = mPos; implement !!!
virtual size_t max() const override;
virtual bool toNull() const override;
virtual void assignRange(RangeBase<Index> const* range);
protected:
DEFAULT_MEMBERS(IndexBase);
IndexBase(RangeBase<Index> const* range);
// translate index into position
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>
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();
}
};
@ -68,7 +68,7 @@ namespace MultiArrayTools
template <class MultiIndex>
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();
}
};
@ -108,13 +108,17 @@ namespace MultiArrayTools
}
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);
}
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);
}
@ -193,6 +197,14 @@ namespace MultiArrayTools
IIB::mPos = evaluate(*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>
size_t MultiIndex<Indices...>::dim() const
@ -230,15 +242,14 @@ namespace MultiArrayTools
template <class... Indices>
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);
}
template <class... Indices>
template <size_t N>
auto MultiIndex<Indices...>::getIndex() const ->
const decltype(std::get<N>(mIPack))
typename std::tuple_element<N, std::tuple<Indices...> >::type const& MultiIndex<Indices...>::getIndex() const
{
return std::get<N>(mIPack);
}
@ -281,7 +292,41 @@ namespace MultiArrayTools
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 *
******************/
@ -396,7 +441,7 @@ namespace MultiArrayTools
{
std::tuple<typename Ranges::IndexType...> is;
IndexSetter<sizeof...(Ranges)-1>::setBegin(is,mSpace);
return MultiIndex<typename Ranges::IndexType...>(is);
return MultiIndex<typename Ranges::IndexType...>(this, is);
}
template <class... Ranges>
@ -404,6 +449,6 @@ namespace MultiArrayTools
{
std::tuple<typename Ranges::IndexType...> is;
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);
MultiIndex(Indices&&... inds);
MultiIndex(const IndexPack& ipack);
MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
Indices&&... inds);
MultiIndex(RangeBase<MultiIndex<Indices...> > const* range,
const IndexPack& ipack);
virtual MultiIndex& operator++() override;
virtual MultiIndex& operator--() override;
@ -44,15 +47,16 @@ namespace MultiArrayTools
virtual MultiRangeType rangeType() const override;
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>
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);
const IndefinitIndexBase& get(size_t n) const;
MultiIndex& operator()(Indices&&... inds);
MultiIndex& operator()(const Indices&... inds);
virtual void name(const Name& nm) override;
@ -61,8 +65,15 @@ namespace MultiArrayTools
virtual bool link(IndefinitIndexBase* toLink) override;
virtual void linkTo(IndefinitIndexBase* target) override;
//virtual void assignRange(RangeBase<MultiIndex<Indices...> > const* range) override;
};
/*****************************
* IndexGetter Functions *
****************************/
template <class... Ranges>
class MultiRange : public RangeBase<MultiIndex<typename Ranges::IndexType...> >
{
@ -81,7 +92,7 @@ namespace MultiArrayTools
template <size_t N>
auto getRange() const -> decltype( std::get<N>(SpaceType()) );
size_t size() const override;
virtual MultiRangeType type() const override;

View file

@ -45,13 +45,13 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE>
SingleIndex<U,TYPE> SingleRange<U,TYPE>::begin() const
{
return SingleIndex<U,TYPE>(0);
return SingleIndex<U,TYPE>(this, 0);
}
template <typename U, RangeType TYPE>
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>
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>
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 );
}
@ -126,7 +128,7 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE>
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>
@ -147,11 +149,12 @@ namespace MultiArrayTools
target->link(this);
}
/*
template <typename U, RangeType TYPE>
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;
}
}*/
}

View file

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

View file

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