... compiles now

This commit is contained in:
Christian Zimmermann 2017-08-04 14:57:19 +02:00
parent 225ecc0abc
commit d2a1286d17
9 changed files with 111 additions and 63 deletions

View file

@ -41,7 +41,7 @@ namespace MultiArrayTools
**********************/ **********************/
template <typename MetaType> template <typename MetaType>
IndexInterface<MetaType>::IndexInterface(const RangeBase& range, size_t pos) : IndexInterface<MetaType>::IndexInterface(const std::shared_ptr<RangeBase>& rangePtr, size_t pos) :
IndexBase(range, pos) {} IndexBase(rangePtr, pos) {}
} }

View file

@ -48,10 +48,11 @@ namespace MultiArrayTools
{ {
public: public:
IndexInterface(const RangeBase& range, size_t pos); DEFAULT_MEMBERS(IndexInterface);
MetaType& meta() = 0; IndexInterface(const std::shared_ptr<RangeBase>& rangePtr, size_t pos);
const MetaType& meta() const = 0; virtual MetaType meta() const = 0;
virtual IndexInterface& at(const MetaType& meta) = 0;
}; };
} }

View file

@ -11,6 +11,21 @@ namespace MAT = MultiArrayTools;
namespace { namespace {
using namespace MAT; using namespace MAT;
template <class Factory, typename T>
void swapFactory(std::shared_ptr<RangeFactoryBase>& fptr, std::initializer_list<T> ilist)
{
std::vector<T> tmp = ilist;
auto nptr = std::make_shared<Factory>( tmp );
fptr = nptr;
}
template <class Factory, class... Rs>
void swapMFactory(std::shared_ptr<RangeFactoryBase>& fptr, const Rs&... rs)
{
auto nptr = std::make_shared<Factory>( rs... );
fptr = nptr;
}
class IndexTest : public ::testing::Test class IndexTest : public ::testing::Test
{ {
@ -30,21 +45,21 @@ namespace {
IndexTest() IndexTest()
{ {
rfbptr.swap( std::make_shared<SRF>( { 'e', 'b', 'n' } ) ); swapFactory<SRF>(rfbptr, { 'e', 'b', 'n' } );
sr1ptr = std::dynamic_pointer_cast<SRange>( rfbptr.create() ); sr1ptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
rfbptr.swap( std::make_shared<SRF>( { 'x', 'y', 'l', 'f' } ) ); swapFactory<SRF>(rfbptr, { 'x', 'y', 'l', 'f' } );
sr2ptr = std::dynamic_pointer_cast<SRange>( rfbptr.create() ); sr2ptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
rfbptr.swap( std::make_shared<SRF>( { 'a', 'b' } ) ); swapFactory<SRF>(rfbptr, { 'a', 'b' } );
std::shared_ptr<SRange> temp1 = std::dynamic_pointer_cast<SRange>( rfbptr.create() ); std::shared_ptr<SRange> temp1 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
rfbptr.swap( std::make_shared<SRF>( { '1' } ) ); swapFactory<SRF>(rfbptr, { '1' } );
std::shared_ptr<SRange> temp2 = std::dynamic_pointer_cast<SRange>( rfbptr.create() ); std::shared_ptr<SRange> temp2 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
rfbptr.swap( std::make_shared<SRF>( { '0', '7' } ) ); swapFactory<SRF>(rfbptr, { '0', '7' } );
std::shared_ptr<SRange> temp3 = std::dynamic_pointer_cast<SRange>( rfbptr.create() ); std::shared_ptr<SRange> temp3 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
rfbptr.swap( std::make_shared<M3RF>( temp1, temp2, temp3 ) ); swapMFactory<M3RF>(rfbptr, temp1, temp2, temp3 );
m3rptr = std::dynamic_pointer_cast<M3Range>( rfbptr.create() ); m3rptr = std::dynamic_pointer_cast<M3Range>( rfbptr->create() );
} }
std::shared_ptr<RangeFactoryBase> rfbptr; std::shared_ptr<RangeFactoryBase> rfbptr;

View file

@ -10,9 +10,9 @@ namespace MultiArrayTools
using namespace MultiArrayHelper; using namespace MultiArrayHelper;
} }
/********************** /******************
* MultiIndexBase * * MultiIndex *
**********************/ ******************/
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) : MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) :
@ -123,6 +123,18 @@ namespace MultiArrayTools
return *this; return *this;
} }
template <class... Indices>
bool MultiIndex<Indices...>::first() const
{
return IB::mPos == 0;
}
template <class... Indices>
bool MultiIndex<Indices...>::last() const
{
return IB::mPos == IB::mRangePtr->size() - 1;
}
/************************* /*************************
* MultiRangeFactory * * MultiRangeFactory *
*************************/ *************************/
@ -130,13 +142,13 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const std::shared_ptr<Ranges>&... rs) MultiRangeFactory<Ranges...>::MultiRangeFactory(const std::shared_ptr<Ranges>&... rs)
{ {
mProd = std::make_shared< MultiRange<Ranges...> >( rs... ); mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( rs... ) );
} }
template <class... Ranges> template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const typename MultiRange<Ranges...>::SpaceType& st) MultiRangeFactory<Ranges...>::MultiRangeFactory(const typename MultiRange<Ranges...>::SpaceType& st)
{ {
mProd = std::make_shared< MultiRange<Ranges...> >( st ); mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( st ) );
} }
template <class... Ranges> template <class... Ranges>
@ -159,11 +171,23 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
template <size_t N> template <size_t N>
auto MultiRange<Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )& auto MultiRange<Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )&
//typename std::tuple_element<N, std::tuple<Ranges...> >::type const& MultiRange<Ranges...>::getRange() const
{ {
return *std::get<N>(mSpace); return *std::get<N>(mSpace);
} }
template <class... Ranges>
template <size_t N>
auto MultiRange<Ranges...>::getPtr() const -> decltype( std::get<N>( mSpace ) )&
{
return std::get<N>(mSpace);
}
template <class... Ranges>
size_t MultiRange<Ranges...>::dim() const
{
return sdim;
}
template <class... Ranges> template <class... Ranges>
size_t MultiRange<Ranges...>::size() const size_t MultiRange<Ranges...>::size() const
{ {

View file

@ -54,6 +54,9 @@ namespace MultiArrayTools
virtual MetaType meta() const override; virtual MetaType meta() const override;
virtual MultiIndex& at(const MetaType& metaPos) override; virtual MultiIndex& at(const MetaType& metaPos) override;
virtual bool first() const override;
virtual bool last() const override;
virtual size_t dim() const override; virtual size_t dim() const override;
}; };
@ -103,7 +106,9 @@ namespace MultiArrayTools
template <size_t N> template <size_t N>
auto get() const -> decltype( *std::get<N>( mSpace ) )&; auto get() const -> decltype( *std::get<N>( mSpace ) )&;
//typename std::tuple_element<N, std::tuple<std::shared_ptr<Ranges>...> >::type const& getRange() const;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
virtual size_t dim() const override; virtual size_t dim() const override;
virtual size_t size() const override; virtual size_t size() const override;

View file

@ -43,9 +43,9 @@ namespace MultiArrayHelper
static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip) static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip)
{ {
auto& si = *std::get<N>(ip); auto& si = *std::get<N>(ip);
if(si.pos() == si.last()){ if(si.last()){
si = 0; si = 0;
PackNum<N-1>::pp(index); PackNum<N-1>::pp(ip);
} }
else { else {
++si; ++si;
@ -56,9 +56,9 @@ namespace MultiArrayHelper
static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip) static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip)
{ {
auto& si = *std::get<N>(ip); auto& si = *std::get<N>(ip);
if(si.pos() == si.atEdge()){ if(si.first()){
si = si.max(); si = si.max();
PackNum<N-1>::mm(index); PackNum<N-1>::mm(ip);
} }
else { else {
--si; --si;
@ -68,12 +68,12 @@ namespace MultiArrayHelper
template <class RangeTuple> template <class RangeTuple>
static size_t getSize(const RangeTuple& rt) static size_t getSize(const RangeTuple& rt)
{ {
return std::get<N>(rt).size() * PackNum<N-1>::getSize(rt); return std::get<N>(rt)->size() * PackNum<N-1>::getSize(rt);
} }
template <template <class...> class IndexType, class... Indices> template <class IndexPack, class MetaType>
static void getMetaPos(std::tuple<decltype(Indices().meta())...>& target, static void getMetaPos(MetaType& target,
const typename IndexType<Indices...>::IndexPack& source) const IndexPack& source)
{ {
std::get<N>(target) = std::get<N>(source)->meta(); std::get<N>(target) = std::get<N>(source)->meta();
PackNum<N-1>::getMetaPos(target, source); PackNum<N-1>::getMetaPos(target, source);
@ -82,14 +82,14 @@ namespace MultiArrayHelper
template <class IndexPack, typename MetaType> template <class IndexPack, typename MetaType>
static void setMeta(IndexPack& target, const MetaType& source) static void setMeta(IndexPack& target, const MetaType& source)
{ {
std::get<N>(target).atMeta( std::get<N>(source) ); std::get<N>(target)->at( std::get<N>(source) );
PackNum<N-1>::setMeta(target, source); PackNum<N-1>::setMeta(target, source);
} }
template <class IndexPack> template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos) static void setIndexPack(IndexPack& iPack, size_t pos)
{ {
auto i = std::get<N>(iPack); auto i = *std::get<N>(iPack).get();
const size_t ownPos = pos % i.max(); const size_t ownPos = pos % i.max();
i = ownPos; i = ownPos;
if(ownPos == pos){ if(ownPos == pos){
@ -101,13 +101,14 @@ namespace MultiArrayHelper
static void construct(std::tuple<std::shared_ptr<Indices>...>& ip, static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range) const MRange& range)
{ {
typedef decltype(range.template get<N>()) SubIndexType; typedef typename std::remove_reference<decltype(range.template get<N>())>::type SubRangeType;
typedef decltype(std::get<N>(ip).get()) TypeFromIndexPack; typedef typename SubRangeType::IndexType SubIndexType;
typedef typename std::remove_reference<decltype(*std::get<N>(ip).get())>::type TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value, static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types"); "inconsiśtent types");
std::get<N>(ip).swap( std::make_shared<SubIndexType>( range.template get<N>() ) ); std::get<N>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( range.template getPtr<N>() ) );
PackNum<N-1>::construct(ip, range); PackNum<N-1>::construct(ip, range);
} }
@ -115,8 +116,8 @@ namespace MultiArrayHelper
static void copy(std::tuple<std::shared_ptr<Indices>...>& ip, static void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind) const IndexType<Indices...>& ind)
{ {
typedef decltype(ind.template get<N>()) SubIndexType; typedef typename std::remove_reference<decltype(ind.template get<N>())>::type SubIndexType;
std::get<N>(ip).swap( std::make_shared<SubIndexType>( ind.template get<N>() ) ); std::get<N>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( ind.template get<N>() ) );
PackNum<N-1>::copy(ip, ind); PackNum<N-1>::copy(ip, ind);
} }
@ -175,12 +176,12 @@ namespace MultiArrayHelper
template <class RangeTuple> template <class RangeTuple>
static size_t getSize(const RangeTuple& rt) static size_t getSize(const RangeTuple& rt)
{ {
return std::get<0>(rt).size(); return std::get<0>(rt)->size();
} }
template <template <class...> class IndexType, class... Indices> template <class IndexPack, class MetaType>
static void getMetaPos(std::tuple<decltype(Indices().meta())...>& target, static void getMetaPos(MetaType& target,
const typename IndexType<Indices...>::IndexPack& source) const IndexPack& source)
{ {
std::get<0>(target) = std::get<0>(source)->meta(); std::get<0>(target) = std::get<0>(source)->meta();
} }
@ -188,13 +189,13 @@ namespace MultiArrayHelper
template <class IndexPack, typename MetaType> template <class IndexPack, typename MetaType>
static void setMeta(IndexPack& target, const MetaType& source) static void setMeta(IndexPack& target, const MetaType& source)
{ {
std::get<0>(target).atMeta( std::get<0>( source ) ); std::get<0>(target)->at( std::get<0>( source ) );
} }
template <class IndexPack> template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos) static void setIndexPack(IndexPack& iPack, size_t pos)
{ {
auto i = std::get<0>(iPack); auto i = *std::get<0>(iPack);
const size_t ownPos = pos % i.max(); const size_t ownPos = pos % i.max();
i = ownPos; i = ownPos;
} }
@ -203,21 +204,22 @@ namespace MultiArrayHelper
static void construct(std::tuple<std::shared_ptr<Indices>...>& ip, static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range) const MRange& range)
{ {
typedef decltype(range.template get<0>()) SubIndexType; typedef typename std::remove_reference<decltype(range.template get<0>())>::type SubRangeType;
typedef decltype(std::get<0>(ip).get()) TypeFromIndexPack; typedef typename SubRangeType::IndexType SubIndexType;
typedef typename std::remove_reference<decltype(*std::get<0>(ip).get())>::type TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value, static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types"); "inconsiśtent types");
std::get<0>(ip).swap( std::make_shared<SubIndexType>( range.template get<0>() ) ); std::get<0>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( range.template getPtr<0>() ) );
} }
template <template<class...> class IndexType, class... Indices> template <template<class...> class IndexType, class... Indices>
static void copy(std::tuple<std::shared_ptr<Indices>...>& ip, static void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind) const IndexType<Indices...>& ind)
{ {
typedef decltype(ind.template get<0>()) SubIndexType; typedef typename std::remove_reference<decltype(ind.template get<0>())>::type SubIndexType;
std::get<0>(ip).swap( std::make_shared<SubIndexType>( ind.template get<0>() ) ); std::get<0>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( ind.template get<0>() ) );
} }
template <class... Indices> template <class... Indices>

View file

@ -72,8 +72,8 @@ namespace MultiArrayTools
typedef Index IndexType; typedef Index IndexType;
virtual Index begin() = 0; virtual Index begin() const = 0;
virtual Index end() = 0; virtual Index end() const = 0;
protected: protected:
RangeInterface() = default; RangeInterface() = default;

View file

@ -35,13 +35,13 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
U SingleIndex<U,TYPE>::meta() const U SingleIndex<U,TYPE>::meta() const
{ {
return dynamic_cast<SingleRange<U,TYPE> const*>( IB::mRangePtr )->get( IB::pos() ); return std::dynamic_pointer_cast<SingleRange<U,TYPE> const>( IB::mRangePtr )->get( IB::pos() );
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::at(const U& metaPos) SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::at(const U& metaPos)
{ {
operator=( dynamic_cast<SingleRange<U,TYPE> const*>( IB::mRangePtr )->getMeta( metaPos ) ); operator=( std::dynamic_pointer_cast<SingleRange<U,TYPE> const>( IB::mRangePtr )->getMeta( metaPos ) );
return *this; return *this;
} }
@ -70,7 +70,7 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleRangeFactory<U,TYPE>::SingleRangeFactory(const std::vector<U>& space) SingleRangeFactory<U,TYPE>::SingleRangeFactory(const std::vector<U>& space)
{ {
mProd = std::make_shared<oType>( space ); mProd = std::shared_ptr<oType>( new SingleRange<U,TYPE>( space ) );
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
@ -120,16 +120,16 @@ namespace MultiArrayTools
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE> SingleRange<U,TYPE>::begin() const typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::begin() const
{ {
SingleIndex<U,TYPE> i( RB::mThis ); SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >( RB::mThis ) );
return i = 0; return i = 0;
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
SingleIndex<U,TYPE> SingleRange<U,TYPE>::end() const typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::end() const
{ {
SingleIndex<U,TYPE> i( RB::mThis ); SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >( RB::mThis ) );
return i = size(); return i = size();
} }

View file

@ -20,6 +20,7 @@ namespace MultiArrayTools
public: public:
typedef IndexBase IB; typedef IndexBase IB;
typedef U MetaType;
DEFAULT_MEMBERS(SingleIndex); DEFAULT_MEMBERS(SingleIndex);
@ -29,8 +30,8 @@ namespace MultiArrayTools
virtual SingleIndex& operator++() override; virtual SingleIndex& operator++() override;
virtual SingleIndex& operator--() override; virtual SingleIndex& operator--() override;
virtual U meta() const; virtual U meta() const override;
virtual SingleIndex& at(const U& metaPos); virtual SingleIndex& at(const U& metaPos) override;
virtual size_t dim() const override; // = 1 virtual size_t dim() const override; // = 1
virtual bool last() const override; virtual bool last() const override;
@ -63,8 +64,8 @@ namespace MultiArrayTools
const U& get(size_t pos) const; const U& get(size_t pos) const;
size_t getMeta(const U& metaPos) const; size_t getMeta(const U& metaPos) const;
virtual SingleIndex<U,TYPE> begin() const override; virtual IndexType begin() const override;
virtual SingleIndex<U,TYPE> end() const override; virtual IndexType end() const override;
virtual std::shared_ptr<IndexBase> index() const override; virtual std::shared_ptr<IndexBase> index() const override;
friend SingleRangeFactory<U,TYPE>; friend SingleRangeFactory<U,TYPE>;