first scast version (UNTESTED)

This commit is contained in:
Christian Zimmermann 2018-07-16 00:12:05 +02:00
parent 0d951f892a
commit b9e5d6a898
5 changed files with 50 additions and 32 deletions

View file

@ -5,7 +5,7 @@
#include <cstdlib> #include <cstdlib>
#include <map> #include <map>
//#include "base_def.h" #include "rbase_def.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/rpheader.h" #include "ranges/rpheader.h"
@ -24,7 +24,7 @@ namespace MultiArrayTools
typedef AnonymousRange oType; typedef AnonymousRange oType;
AnonymousRangeFactory() = delete; AnonymousRangeFactory() = default;
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs); AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
@ -121,6 +121,31 @@ namespace MultiArrayTools
std::dynamic_pointer_cast<oType>(mProd)->mOrig.push_back(r); std::dynamic_pointer_cast<oType>(mProd)->mOrig.push_back(r);
} }
/*****************
* Functions *
*****************/
std::shared_ptr<AnonymousRange> defaultRange(size_t size = 0);
}
namespace MultiArrayHelper
{
using namespace MultiArrayTools;
template <>
inline void resolveSetRange<AnonymousRange>(std::shared_ptr<AnonymousRange> rp,
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){
arf.append(orig[op]);
}
rp = std::dynamic_pointer_cast<AnonymousRange>( arf.create() );
}
}
namespace MultiArrayTools
{
/*********************** /***********************
* AnonymousRange * * AnonymousRange *
***********************/ ***********************/
@ -155,15 +180,12 @@ namespace MultiArrayTools
{ {
std::tuple<Ranges...> rtp; std::tuple<Ranges...> rtp;
RPackNum<sizeof...(Ranges)-1>::resolveRangeType(mOrig, rtp, mOrig.size()-1, sizes...); RPackNum<sizeof...(Ranges)-1>::resolveRangeType(mOrig, rtp, mOrig.size()-1, 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() );
} }
/*****************
* Functions *
*****************/
std::shared_ptr<AnonymousRange> defaultRange(size_t size = 0);
} }
#endif #endif

View file

@ -71,6 +71,7 @@ namespace MultiArrayTools
// anonymous_range.h // anonymous_range.h
class AnonymousRange; class AnonymousRange;
} }
#endif #endif

View file

@ -3,6 +3,11 @@
#define __rpack_num_h__ #define __rpack_num_h__
#include <memory> #include <memory>
#include <vector>
#include <cassert>
#include "ranges/rbase_def.h"
#include "ranges/index_base.h"
namespace MultiArrayHelper namespace MultiArrayHelper
{ {
@ -39,6 +44,7 @@ namespace MultiArrayHelper
template <class Index> template <class Index>
inline size_t getStepSize(const Index& ii, std::intptr_t j); inline size_t getStepSize(const Index& ii, std::intptr_t j);
template <class Range> template <class Range>
void resolveSetRange(std::shared_ptr<Range> rp, std::vector<std::shared_ptr<RangeBase> > orig, void resolveSetRange(std::shared_ptr<Range> rp, std::vector<std::shared_ptr<RangeBase> > orig,
size_t origpos, size_t size) size_t origpos, size_t size)
@ -47,18 +53,6 @@ namespace MultiArrayHelper
rp = std::dynamic_pointer_cast<Range>( orig[origpos] ); // catch bad cast here!! rp = std::dynamic_pointer_cast<Range>( orig[origpos] ); // catch bad cast here!!
} }
template <>
void resolveSetRange<AnonymousRange>(std::shared_ptr<AnonymousRange> rp,
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){
arf.append(orig[op]);
}
rp = std::dynamic_pointer_cast<AnonymousRange>( arf.create() );
}
template <size_t N> template <size_t N>
struct RPackNum struct RPackNum
{ {
@ -276,11 +270,10 @@ namespace MultiArrayHelper
} }
template <class RangeTuple, typename... SIZET> template <class RangeTuple, typename... SIZET>
static 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) const 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;
typedef decltype(std::get<tps-N-1>(rtp)) RangeType;
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...);
} }
@ -464,11 +457,10 @@ namespace MultiArrayHelper
} }
template <class RangeTuple, typename... SIZET> template <class RangeTuple, typename... SIZET>
static 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) const 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;
typedef decltype(std::get<tps-1>(rtp)) RangeType;
resolveSetRange(std::get<tps-1>(rtp), orig, off, size); resolveSetRange(std::get<tps-1>(rtp), orig, off, size);
} }

View file

@ -8,6 +8,7 @@
#include <memory> #include <memory>
//#include "base_def.h" //#include "base_def.h"
//#include "ranges/rpack_num.h"
#include "ranges/index_base.h" #include "ranges/index_base.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"

View file

@ -7,15 +7,15 @@ namespace MultiArrayTools
* AnonymousRangeFactory * * AnonymousRangeFactory *
******************************/ ******************************/
std::shared_ptr<RangeBase> checkIfCreated(const std::vector<std::shared_ptr<RangeBase> >& pvec) std::shared_ptr<RangeBase> AnonymousRangeFactory::checkIfCreated(const std::vector<std::shared_ptr<RangeBase> >& pvec)
{ {
std::shared_ptr<RangeBase> out; std::shared_ptr<RangeBase> out;
bool check = false; bool check = false;
for(auto& x: mAleadyCreated){ for(auto& x: mAleadyCreated){
if(x.second.size() == pvec.size()){ if(x.second.size() == pvec.size()){
check = true; check = true;
for(size_t i = 0; i != x.size(); ++i){ for(size_t i = 0; i != x.second.size(); ++i){
if(x.second[i] != static_cast<std::intptr_t>( pvec[i].get() ) ){ if(x.second[i] != reinterpret_cast<std::intptr_t>( pvec[i].get() ) ){
check = false; check = false;
break; break;
} }
@ -29,7 +29,7 @@ namespace MultiArrayTools
if(not check){ if(not check){
std::vector<std::intptr_t> app(pvec.size()); std::vector<std::intptr_t> app(pvec.size());
for(size_t i = 0; i != app.size(); ++i){ for(size_t i = 0; i != app.size(); ++i){
app[i] = static_cast<std::intptr_t>( pvec[i].get() ); app[i] = reinterpret_cast<std::intptr_t>( pvec[i].get() );
} }
mAleadyCreated[mProd] = app; mAleadyCreated[mProd] = app;
out = mProd; out = mProd;
@ -40,7 +40,7 @@ namespace MultiArrayTools
std::shared_ptr<RangeBase> AnonymousRangeFactory::create() std::shared_ptr<RangeBase> AnonymousRangeFactory::create()
{ {
mProd = checkIfCreated(); mProd = checkIfCreated(std::dynamic_pointer_cast<AnonymousRange>(mProd)->mOrig);
setSelf(); setSelf();
return mProd; return mProd;
} }
@ -105,4 +105,6 @@ namespace MultiArrayTools
return std::dynamic_pointer_cast<AnonymousRange>( arf.create() ); return std::dynamic_pointer_cast<AnonymousRange>( arf.create() );
} }
} // end namespace MultiArrayTools } // end namespace MultiArrayTools