some fixes + container range test -> working

This commit is contained in:
Christian Zimmermann 2017-08-07 17:11:03 +02:00
parent 40864ace00
commit 16e8e588cb
6 changed files with 159 additions and 26 deletions

View file

@ -34,7 +34,8 @@ namespace MultiArrayTools
template <class... Indices>
template <class MRange>
ContainerIndex<Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range)
ContainerIndex<Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<std::tuple<decltype(Indices().meta())...> >(range, 0)
{
PackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
@ -93,24 +94,31 @@ namespace MultiArrayTools
}
template <class... Indices>
size_t ContainerIndex<Indices...>::pos() const
ContainerIndex<Indices...>& ContainerIndex<Indices...>::sync()
{
if(mExternControl){
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
}
return IB::mPos;
return *this;
}
template <class... Indices>
template <size_t N>
auto ContainerIndex<Indices...>::get() const -> decltype( *std::get<N>( mIPack ) )&
{
return *std::get<N>( mIPack );
}
template <class... Indices>
bool ContainerIndex<Indices...>::first() const
{
return pos() == 0;
return IB::pos() == 0;
}
template <class... Indices>
bool ContainerIndex<Indices...>::last() const
{
return pos() == IB::mRangePtr->size() - 1;
return IB::pos() == IB::mRangePtr->size() - 1;
}
@ -119,8 +127,14 @@ namespace MultiArrayTools
{
PackNum<sizeof...(Indices)-1>::swapIndices(mIPack, inds...);
mExternControl = true;
return *this;
return sync();
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()()
{
return sync();
}
/*****************************
* ContainerRangeFactory *
@ -169,6 +183,20 @@ namespace MultiArrayTools
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace);
}
template <class... Ranges>
template <size_t N>
auto ContainerRange<Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )&
{
return *std::get<N>( mSpace );
}
template <class... Ranges>
template <size_t N>
auto ContainerRange<Ranges...>::getPtr() const -> decltype( std::get<N>( mSpace ) )&
{
return std::get<N>( mSpace );
}
template <class... Ranges>
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::begin() const
{
@ -188,7 +216,7 @@ namespace MultiArrayTools
i = size();
return i;
}
template <class... Ranges>
std::shared_ptr<IndexBase> ContainerRange<Ranges...>::index() const
{

View file

@ -48,10 +48,16 @@ namespace MultiArrayTools
virtual bool last() const override;
virtual size_t dim() const override;
virtual size_t pos() const override; // recalculate when externalControl == true
ContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true
template <size_t N>
auto get() const -> decltype( *std::get<N>( mIPack ) )&;
ContainerIndex& operator()(const std::shared_ptr<Indices>&... inds); // control via external indices
ContainerIndex& operator()(); // -> sync; just to shorten the code
};
@ -97,9 +103,15 @@ namespace MultiArrayTools
virtual size_t dim() const override;
virtual size_t size() const override;
template <size_t N>
auto get() const -> decltype( *std::get<N>( mSpace ) )&;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
virtual IndexType begin() const override;
virtual IndexType end() const override;
virtual std::shared_ptr<IndexBase> index() const override;
friend ContainerRangeFactory<Ranges...>;

View file

@ -26,6 +26,12 @@ namespace {
auto nptr = std::make_shared<Factory>( rs... );
fptr = nptr;
}
template <typename... Ts>
auto mkt(Ts&&... ts) -> decltype(std::make_tuple(ts...))
{
return std::make_tuple(ts...);
}
class IndexTest : public ::testing::Test
{
@ -60,12 +66,24 @@ namespace {
swapMFactory<M3RF>(rfbptr, temp1, temp2, temp3 );
m3rptr = std::dynamic_pointer_cast<M3Range>( rfbptr->create() );
swapMFactory<MasterRF>(rfbptr, sr1ptr, m3rptr, sr2ptr);
mstrptr = std::dynamic_pointer_cast<MasterRange>( rfbptr->create() );
swapMFactory<CRF>(rfbptr, m3rptr, sr2ptr);
cr1ptr = std::dynamic_pointer_cast<CRange>( rfbptr->create() );
swapMFactory<CRF>(rfbptr, m3rptr, sr1ptr);
cr2ptr = std::dynamic_pointer_cast<CRange>( rfbptr->create() );
}
std::shared_ptr<RangeFactoryBase> rfbptr;
std::shared_ptr<SRange> sr1ptr;
std::shared_ptr<SRange> sr2ptr;
std::shared_ptr<M3Range> m3rptr;
std::shared_ptr<MasterRange> mstrptr;
std::shared_ptr<CRange> cr1ptr;
std::shared_ptr<CRange> cr2ptr;
};
TEST_F(IndexTest, SingleIndex_SimpleCall)
@ -101,15 +119,15 @@ namespace {
EXPECT_EQ(mi.pos(), 0);
EXPECT_EQ(mi.first(), true);
EXPECT_EQ(mi.last(), false);
EXPECT_EQ(mi.meta() == std::make_tuple('a','1','0'), true);
mi.at(std::make_tuple('b','1','7'));
EXPECT_EQ(mi.meta() == mkt('a','1','0'), true);
mi.at( mkt('b','1','7') );
EXPECT_EQ(mi.pos(), mi.max()-1);
EXPECT_EQ(mi.first(), false);
EXPECT_EQ(mi.last(), true);
mi = 1;
EXPECT_EQ(mi.meta() == std::make_tuple('a','1','7'), true);
EXPECT_EQ(mi.meta() == mkt('a','1','7'), true);
++mi;
EXPECT_EQ(mi.meta() == std::make_tuple('b','1','0'), true);
EXPECT_EQ(mi.meta() == mkt('b','1','0'), true);
++mi;
auto mi2 = m3rptr->end();
--mi2;
@ -118,8 +136,75 @@ namespace {
--mi2;
EXPECT_EQ(mi == mi2, false);
EXPECT_EQ(mi != mi2, true);
auto si = sr1ptr->begin();
EXPECT_EQ(si == mi, false);
EXPECT_EQ(si != mi, true);
}
TEST_F(IndexTest, MasterRange_Check)
{
EXPECT_EQ(mstrptr->size(), 48);
EXPECT_EQ(mstrptr->template get<0>().size(), 3);
EXPECT_EQ(mstrptr->template get<1>().size(), 4);
EXPECT_EQ(mstrptr->template get<2>().size(), 4);
EXPECT_EQ(mstrptr->dim(), 3);
EXPECT_EQ(mstrptr->template get<0>().dim(), 1);
EXPECT_EQ(mstrptr->template get<1>().dim(), 3);
auto mi = mstrptr->begin();
EXPECT_EQ(mi.meta() == mkt( 'e' , mkt('a', '1', '0') , 'x' ), true);
mi = mi.max()-1;
EXPECT_EQ(mi.meta() == mkt( 'n' , mkt('b', '1', '7') , 'f' ), true);
mi.template down<1>();
EXPECT_EQ(mi.meta() == mkt( 'n' , mkt('b', '1', '0') , 'f' ), true);
mi.template down<0>();
EXPECT_EQ(mi.meta() == mkt( 'b' , mkt('b', '1', '0') , 'f' ), true);
mi.template down<2>();
EXPECT_EQ(mi.meta() == mkt( 'b' , mkt('b', '1', '0') , 'l' ), true);
mi.template up<1>();
EXPECT_EQ(mi.meta() == mkt( 'b' , mkt('b', '1', '7') , 'l' ), true);
auto& subI = mi.template get<0>();
EXPECT_EQ(subI.meta(), 'b');
mi.template up<0>();
EXPECT_EQ(subI.meta(), 'n');
auto& subI2 = mi.template get<1>();
EXPECT_EQ(subI2.meta() == mkt('b', '1', '7'), true);
mi.template down<1>();
EXPECT_EQ(subI2.meta() == mkt('b', '1', '0'), true);
}
TEST_F(IndexTest, ContainerRange_Check)
{
EXPECT_EQ(cr1ptr->size(), 16);
EXPECT_EQ(cr2ptr->size(), 12);
auto mi = mstrptr->begin();
auto ci1 = cr1ptr->begin();
auto ci2 = cr2ptr->begin();
EXPECT_EQ(ci1.max(), 16);
EXPECT_EQ(ci2.max(), 12);
ci1(mi.template getPtr<1>(), mi.template getPtr<2>());
ci2(mi.template getPtr<1>(), mi.template getPtr<0>());
EXPECT_EQ(ci1.pos(), 0);
EXPECT_EQ(ci2.pos(), 0);
++mi;
EXPECT_EQ(ci1().pos(), 1);
EXPECT_EQ(ci2().pos(), 0);
mi.template up<1>();
EXPECT_EQ(ci1().pos(), 5);
EXPECT_EQ(ci2().pos(), 3);
mi.template up<0>();
EXPECT_EQ(ci1().pos(), 5);
EXPECT_EQ(ci2().pos(), 4);
mi = mi.max()-1;
EXPECT_EQ(ci1().pos(), ci1.max()-1);
EXPECT_EQ(ci2().pos(), ci2.max()-1);
}
} // end namespace
int main(int argc, char** argv)

View file

@ -96,6 +96,13 @@ namespace MultiArrayTools
{
return *std::get<N>(mIPack);
}
template <class... Indices>
template <size_t N>
auto MultiIndex<Indices...>::getPtr() const -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>(mIPack);
}
template <class... Indices>
const IndexBase& MultiIndex<Indices...>::get(size_t n) const

View file

@ -49,7 +49,10 @@ namespace MultiArrayTools
template <size_t N>
auto get() const -> decltype( *std::get<N>( mIPack ) )&;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mIPack ) )&;
const IndexBase& get(size_t n) const;
virtual MetaType meta() const override;

View file

@ -92,9 +92,7 @@ namespace MultiArrayHelper
auto& i = *std::get<N>(iPack).get();
const size_t ownPos = pos % i.max();
i = ownPos;
if(ownPos == pos){
PackNum<N-1>::setIndexPack(iPack, (pos - ownPos) / i.max() );
}
PackNum<N-1>::setIndexPack(iPack, (pos - ownPos) / i.max() );
}
template <class MRange, class... Indices>
@ -125,21 +123,21 @@ namespace MultiArrayHelper
static size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
return std::get<N>(iPtrTup)->pos() +
PackNum<N-1>::makePos(iPtrTup) * std::get<N-1>(iPtrTup)->max();
PackNum<N-1>::makePos(iPtrTup) * std::get<N>(iPtrTup)->max();
}
template <class Pack, class IndexType, class... Indices>
static void swapIndices(Pack& ipack, const std::shared_ptr<Indices>&... ninds,
const std::shared_ptr<IndexType>& nind)
static void swapIndices(Pack& ipack, const std::shared_ptr<IndexType>& nind,
const std::shared_ptr<Indices>&... ninds)
{
std::get<N>(ipack).swap( nind );
std::get<std::tuple_size<Pack>::value-N-1>(ipack) = nind;
PackNum<N-1>::swapIndices(ipack, ninds...);
}
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-N-1>(pack)->size() * PackNum<N-1>::blockSize(pack);
return std::get<sizeof...(Indices)-N-1>(pack)->max() * PackNum<N-1>::blockSize(pack);
}
};
@ -231,13 +229,13 @@ namespace MultiArrayHelper
template <class Pack, class IndexType>
static void swapIndices(Pack& ipack, const std::shared_ptr<IndexType>& nind)
{
std::get<0>(ipack).swap( nind );
std::get<std::tuple_size<Pack>::value-1>(ipack) = nind;
}
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-1>(pack)->size();
return std::get<sizeof...(Indices)-1>(pack)->max();
}
};