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
{
typedef SingleIndex<size_t,SpaceType::NONE> AnonymousIndex;
typedef SingleIndex<size_t,SpaceType::ANON> AnonymousIndex;
template <class R>
using SIZET = size_t;
typedef SingleRange<size_t,SpaceType::ANON> AnonymousRange;
// NOT THREAD SAVE!!
class AnonymousRangeFactory : public RangeFactoryBase
{
@ -24,7 +26,7 @@ namespace MultiArrayTools
typedef AnonymousRange oType;
AnonymousRangeFactory() = default;
AnonymousRangeFactory();
template <class... RangeTypes>
AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
@ -46,17 +48,20 @@ namespace MultiArrayTools
bool mProductCreated = false;
};
class AnonymousRange : public RangeInterface<AnonymousIndex>
template <>
class SingleRange<size_t,SpaceType::ANON> : public RangeInterface<AnonymousIndex>
{
public:
static constexpr bool defaultable = false;
static constexpr bool defaultable = true;
static constexpr size_t ISSTATIC = 0;
static constexpr size_t SIZE = -1;
static constexpr bool HASMETACONT = false;
typedef RangeBase RB;
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 dim() const override;
@ -73,22 +78,25 @@ namespace MultiArrayTools
std::shared_ptr<Range> fullsub(size_t num) const;
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;
static AnonymousRangeFactory factory()
{ return AnonymousRangeFactory(); }
protected:
AnonymousRange() = delete;
AnonymousRange(const AnonymousRange& in) = default;
SingleRange() = default;
SingleRange(const AnonymousRange& in) = default;
template <class... RangeTypes>
AnonymousRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
SingleRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
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;
};
@ -141,12 +149,12 @@ namespace MultiArrayHelper
using namespace MultiArrayTools;
template <>
inline void resolveSetRange<AnonymousRange>(std::shared_ptr<AnonymousRange> rp,
std::vector<std::shared_ptr<RangeBase> > orig,
inline void resolveSetRange<AnonymousRange>(std::shared_ptr<AnonymousRange>& rp,
const std::vector<std::shared_ptr<RangeBase> >& orig,
size_t origpos, size_t size)
{
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]);
}
rp = std::dynamic_pointer_cast<AnonymousRange>( arf.create() );
@ -170,7 +178,7 @@ namespace MultiArrayTools
***********************/
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>()
{
mOrig.resize(sizeof...(RangeTypes));
@ -179,7 +187,7 @@ namespace MultiArrayTools
}
template <class... RangeTypes>
AnonymousRange::AnonymousRange(std::shared_ptr<RangeTypes>... origs) :
SingleRange<size_t,SpaceType::ANON>::SingleRange(std::shared_ptr<RangeTypes>... origs) :
RangeInterface<AnonymousIndex>()
{
auto rst = std::make_tuple(origs...);
@ -189,18 +197,18 @@ namespace MultiArrayTools
}
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) );
}
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;
RPackNum<sizeof...(Ranges)-1>::resolveRangeType(mOrig, rtp, mOrig.size()-1, sizes...);
std::tuple<std::shared_ptr<Ranges>...> rtp;
RPackNum<sizeof...(Ranges)-1>::resolveRangeType(mOrig, rtp, 0, sizes...);
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,
#include "range_types/header.h"
#undef include_range_type
ANON = -1
};
class RangeFactoryBase

View file

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

View file

@ -46,7 +46,7 @@ namespace MultiArrayHelper
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)
{
assert(size == 1);
@ -278,12 +278,12 @@ namespace MultiArrayHelper
}
template <class RangeTuple, typename... SIZET>
static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> > orig,
const RangeTuple& rtp, size_t off, size_t size, SIZET... sizes)
static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> >& orig,
RangeTuple& rtp, size_t off, size_t size, SIZET... sizes)
{
constexpr size_t tps = std::tuple_size<RangeTuple>::value;
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...);
}
};
@ -465,8 +465,8 @@ namespace MultiArrayHelper
}
template <class RangeTuple, typename... SIZET>
static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> > orig,
const RangeTuple& rtp, size_t off, size_t size)
static inline void resolveRangeType(const std::vector<std::shared_ptr<RangeBase> >& orig,
RangeTuple& rtp, size_t off, size_t size)
{
constexpr size_t tps = std::tuple_size<RangeTuple>::value;
resolveSetRange(std::get<tps-1>(rtp), orig, off, size);

View file

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

View file

@ -113,6 +113,16 @@ namespace {
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
int main(int argc, char** argv)