anonymous range: save cast test works

This commit is contained in:
Christian Zimmermann 2018-07-16 15:55:55 +02:00
parent fcb7ce575e
commit 41f2e60d6e
6 changed files with 62 additions and 38 deletions

View file

@ -12,11 +12,13 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
typedef SingleIndex<size_t,SpaceType::NONE> AnonymousIndex; typedef SingleIndex<size_t,SpaceType::ANON> AnonymousIndex;
template <class R> template <class R>
using SIZET = size_t; using SIZET = size_t;
typedef SingleRange<size_t,SpaceType::ANON> AnonymousRange;
// NOT THREAD SAVE!! // NOT THREAD SAVE!!
class AnonymousRangeFactory : public RangeFactoryBase class AnonymousRangeFactory : public RangeFactoryBase
{ {
@ -24,7 +26,7 @@ namespace MultiArrayTools
typedef AnonymousRange oType; typedef AnonymousRange oType;
AnonymousRangeFactory() = default; AnonymousRangeFactory();
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs); AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
@ -46,17 +48,20 @@ namespace MultiArrayTools
bool mProductCreated = false; bool mProductCreated = false;
}; };
class AnonymousRange : public RangeInterface<AnonymousIndex> template <>
class SingleRange<size_t,SpaceType::ANON> : public RangeInterface<AnonymousIndex>
{ {
public: public:
static constexpr bool defaultable = false; static constexpr bool defaultable = true;
static constexpr size_t ISSTATIC = 0; static constexpr size_t ISSTATIC = 0;
static constexpr size_t SIZE = -1; static constexpr size_t SIZE = -1;
static constexpr bool HASMETACONT = false; static constexpr bool HASMETACONT = false;
typedef RangeBase RB; typedef RangeBase RB;
typedef typename RangeInterface<AnonymousIndex>::IndexType IndexType; typedef typename RangeInterface<AnonymousIndex>::IndexType IndexType;
typedef SingleRange<size_t,SpaceType::ANON> RangeType;
typedef size_t MetaType;
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;
@ -73,22 +78,25 @@ namespace MultiArrayTools
std::shared_ptr<Range> fullsub(size_t num) const; std::shared_ptr<Range> fullsub(size_t num) const;
template <class... Ranges> template <class... Ranges>
MultiRange<Ranges...> scast(SIZET<Ranges>... sizes) const; // save cast std::shared_ptr<MultiRange<Ranges...> > scast(SIZET<Ranges>... sizes) const; // save cast
friend AnonymousRangeFactory; friend AnonymousRangeFactory;
static AnonymousRangeFactory factory()
{ return AnonymousRangeFactory(); }
protected: protected:
AnonymousRange() = delete; SingleRange() = default;
AnonymousRange(const AnonymousRange& in) = default; SingleRange(const AnonymousRange& in) = default;
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs); SingleRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRange(std::shared_ptr<RangeTypes>... origs); SingleRange(std::shared_ptr<RangeTypes>... origs);
size_t mSize = 0; size_t mSize = 1;
std::vector<std::shared_ptr<RangeBase> > mOrig; std::vector<std::shared_ptr<RangeBase> > mOrig;
}; };
@ -105,7 +113,7 @@ namespace MultiArrayTools
/*********************** /***********************
* AnonymousRange * * AnonymousRange *
***********************/ ***********************/
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs)
{ {
@ -141,12 +149,12 @@ namespace MultiArrayHelper
using namespace MultiArrayTools; using namespace MultiArrayTools;
template <> template <>
inline void resolveSetRange<AnonymousRange>(std::shared_ptr<AnonymousRange> rp, inline void resolveSetRange<AnonymousRange>(std::shared_ptr<AnonymousRange>& rp,
std::vector<std::shared_ptr<RangeBase> > orig, const std::vector<std::shared_ptr<RangeBase> >& orig,
size_t origpos, size_t size) size_t origpos, size_t size)
{ {
AnonymousRangeFactory arf; AnonymousRangeFactory arf;
for(size_t op = origpos - size + 1; op != origpos + 1; ++op){ for(size_t op = origpos; op != origpos + size; ++op){
arf.append(orig[op]); arf.append(orig[op]);
} }
rp = std::dynamic_pointer_cast<AnonymousRange>( arf.create() ); rp = std::dynamic_pointer_cast<AnonymousRange>( arf.create() );
@ -170,7 +178,7 @@ namespace MultiArrayTools
***********************/ ***********************/
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRange::AnonymousRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) : SingleRange<size_t,SpaceType::ANON>::SingleRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) :
RangeInterface<AnonymousIndex>() RangeInterface<AnonymousIndex>()
{ {
mOrig.resize(sizeof...(RangeTypes)); mOrig.resize(sizeof...(RangeTypes));
@ -179,7 +187,7 @@ namespace MultiArrayTools
} }
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRange::AnonymousRange(std::shared_ptr<RangeTypes>... origs) : SingleRange<size_t,SpaceType::ANON>::SingleRange(std::shared_ptr<RangeTypes>... origs) :
RangeInterface<AnonymousIndex>() RangeInterface<AnonymousIndex>()
{ {
auto rst = std::make_tuple(origs...); auto rst = std::make_tuple(origs...);
@ -189,18 +197,18 @@ namespace MultiArrayTools
} }
template <class Range> template <class Range>
std::shared_ptr<Range> AnonymousRange::fullsub(size_t num) const std::shared_ptr<Range> SingleRange<size_t,SpaceType::ANON>::fullsub(size_t num) const
{ {
return std::dynamic_pointer_cast<Range>( mOrig.at(num) ); return std::dynamic_pointer_cast<Range>( mOrig.at(num) );
} }
template <class... Ranges> template <class... Ranges>
MultiRange<Ranges...> AnonymousRange::scast(SIZET<Ranges>... sizes) const std::shared_ptr<MultiRange<Ranges...> > SingleRange<size_t,SpaceType::ANON>::scast(SIZET<Ranges>... sizes) const
{ {
std::tuple<Ranges...> rtp; std::tuple<std::shared_ptr<Ranges>...> rtp;
RPackNum<sizeof...(Ranges)-1>::resolveRangeType(mOrig, rtp, mOrig.size()-1, sizes...); RPackNum<sizeof...(Ranges)-1>::resolveRangeType(mOrig, rtp, 0, sizes...);
MultiRangeFactory<Ranges...> mrf(rtp); MultiRangeFactory<Ranges...> mrf(rtp);
return std::dynamic_pointer_cast<MultiRange<Ranges...> >( mrf->create() ); return std::dynamic_pointer_cast<MultiRange<Ranges...> >( mrf.create() );
} }

View file

@ -26,6 +26,7 @@ namespace MultiArrayTools
#define include_range_type(x,n) x = n, #define include_range_type(x,n) x = n,
#include "range_types/header.h" #include "range_types/header.h"
#undef include_range_type #undef include_range_type
ANON = -1
}; };
class RangeFactoryBase class RangeFactoryBase

View file

@ -70,7 +70,7 @@ namespace MultiArrayTools
class AnonymousRangeFactory; class AnonymousRangeFactory;
// anonymous_range.h // anonymous_range.h
class AnonymousRange; //class AnonymousRange;
} }

View file

@ -46,7 +46,7 @@ namespace MultiArrayHelper
template <class Range> template <class Range>
inline void resolveSetRange(std::shared_ptr<Range> rp, std::vector<std::shared_ptr<RangeBase> > orig, inline void resolveSetRange(std::shared_ptr<Range>& rp, const std::vector<std::shared_ptr<RangeBase> >& orig,
size_t origpos, size_t size) size_t origpos, size_t size)
{ {
assert(size == 1); assert(size == 1);
@ -278,12 +278,12 @@ namespace MultiArrayHelper
} }
template <class RangeTuple, typename... SIZET> template <class RangeTuple, typename... SIZET>
static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> > orig, static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> >& orig,
const RangeTuple& rtp, size_t off, size_t size, SIZET... sizes) RangeTuple& rtp, size_t off, size_t size, SIZET... sizes)
{ {
constexpr size_t tps = std::tuple_size<RangeTuple>::value; constexpr size_t tps = std::tuple_size<RangeTuple>::value;
resolveSetRange(std::get<tps-N-1>(rtp), orig, off, size); resolveSetRange(std::get<tps-N-1>(rtp), orig, off, size);
RPackNum<N-1>::resolveRangeType(orig, rtp, off-size, sizes...); RPackNum<N-1>::resolveRangeType(orig, rtp, off+size, sizes...);
} }
}; };
@ -418,7 +418,7 @@ namespace MultiArrayHelper
template <class... Ranges> template <class... Ranges>
static inline void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst, static inline void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<std::shared_ptr<RangeBase> >& v) std::vector<std::shared_ptr<RangeBase> >& v)
{ {
setRangeToVec(v, std::get<0>(rst)); setRangeToVec(v, std::get<0>(rst));
} }
@ -465,8 +465,8 @@ namespace MultiArrayHelper
} }
template <class RangeTuple, typename... SIZET> template <class RangeTuple, typename... SIZET>
static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> > orig, static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> >& orig,
const RangeTuple& rtp, size_t off, size_t size) RangeTuple& rtp, size_t off, size_t size)
{ {
constexpr size_t tps = std::tuple_size<RangeTuple>::value; constexpr size_t tps = std::tuple_size<RangeTuple>::value;
resolveSetRange(std::get<tps-1>(rtp), orig, off, size); resolveSetRange(std::get<tps-1>(rtp), orig, off, size);

View file

@ -7,6 +7,11 @@ namespace MultiArrayTools
* AnonymousRangeFactory * * AnonymousRangeFactory *
******************************/ ******************************/
AnonymousRangeFactory::AnonymousRangeFactory()
{
mProd = std::shared_ptr<oType>( new AnonymousRange() );
}
std::map<std::shared_ptr<RangeBase>,std::vector<std::intptr_t> > AnonymousRangeFactory::mAleadyCreated; std::map<std::shared_ptr<RangeBase>,std::vector<std::intptr_t> > AnonymousRangeFactory::mAleadyCreated;
std::shared_ptr<RangeBase> AnonymousRangeFactory::checkIfCreated(const std::vector<std::shared_ptr<RangeBase> >& pvec) std::shared_ptr<RangeBase> AnonymousRangeFactory::checkIfCreated(const std::vector<std::shared_ptr<RangeBase> >& pvec)
@ -75,8 +80,8 @@ namespace MultiArrayTools
typename AnonymousRange::IndexType AnonymousRange::begin() const typename AnonymousRange::IndexType AnonymousRange::begin() const
{ {
AnonymousIndex i AnonymousIndex i
(std::dynamic_pointer_cast<ClassicRange> (std::dynamic_pointer_cast<AnonymousRange>
( ClassicRange::factory(size()).create() ) ); // !!! ( std::shared_ptr<RangeBase>(RB::mThis) ) );
i = 0; i = 0;
return i; return i;
} }
@ -84,8 +89,8 @@ namespace MultiArrayTools
typename AnonymousRange::IndexType AnonymousRange::end() const typename AnonymousRange::IndexType AnonymousRange::end() const
{ {
AnonymousIndex i AnonymousIndex i
( std::dynamic_pointer_cast<ClassicRange> (std::dynamic_pointer_cast<AnonymousRange>
( ClassicRange::factory(size()).create() ));// !!! ( std::shared_ptr<RangeBase>(RB::mThis) ) );
i = size(); i = size();
return i; return i;
} }
@ -103,8 +108,8 @@ namespace MultiArrayTools
std::shared_ptr<AnonymousRange> defaultRange(size_t size ) std::shared_ptr<AnonymousRange> defaultRange(size_t size )
{ {
AnonymousRangeFactory arf AnonymousRangeFactory arf
( std::dynamic_pointer_cast<ClassicRange> ( std::dynamic_pointer_cast<AnonymousRange>
(ClassicRange::factory(size).create() ) ); (AnonymousRange::factory().create() ) );
return std::dynamic_pointer_cast<AnonymousRange>( arf.create() ); return std::dynamic_pointer_cast<AnonymousRange>( arf.create() );
} }

View file

@ -112,6 +112,16 @@ namespace {
EXPECT_EQ(ar2->sub(1)->size(), m3rptr->size()); EXPECT_EQ(ar2->sub(1)->size(), m3rptr->size());
EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size()); EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size());
} }
TEST_F(AnonymousIndexTest, Cast2)
{
AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr);
auto ar = std::dynamic_pointer_cast<AnonymousRange>( arf2.create() );
auto mr = ar->template scast<SRange,AnonymousRange>(1,2);
EXPECT_EQ(mr->template getPtr<0>()->size(), sr1ptr->size());
EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size());
}
} // end namespace } // end namespace