2018-10-30 15:06:29 +01:00
|
|
|
|
2020-08-27 12:00:47 +02:00
|
|
|
#include "ranges/dynamic_range.h"
|
2018-10-30 15:06:29 +01:00
|
|
|
|
2021-07-28 20:29:56 +02:00
|
|
|
namespace CNORXZ
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
2021-07-28 20:29:56 +02:00
|
|
|
using namespace CNORXZInternal;
|
2018-10-30 15:06:29 +01:00
|
|
|
}
|
2018-11-01 22:11:08 +01:00
|
|
|
|
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
/****************************
|
|
|
|
* DynamicRangeFactory *
|
|
|
|
****************************/
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class... RangeTypes>
|
2020-07-08 17:55:51 +02:00
|
|
|
DynamicRangeFactory::DynamicRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs)
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
2020-07-08 17:55:51 +02:00
|
|
|
mProd = std::shared_ptr<oType>( new DynamicRange( origs ) );
|
2018-10-30 15:06:29 +01:00
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class... RangeTypes>
|
2020-07-08 17:55:51 +02:00
|
|
|
DynamicRangeFactory::DynamicRangeFactory(std::shared_ptr<RangeTypes>... origs)
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
2020-07-08 17:55:51 +02:00
|
|
|
mProd = std::shared_ptr<oType>( new DynamicRange( origs... ) );
|
2018-10-30 15:06:29 +01:00
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class Range>
|
2020-07-08 17:55:51 +02:00
|
|
|
void DynamicRangeFactory::append(std::shared_ptr<Range> r)
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
|
|
|
if(mProductCreated){
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
mProd = std::shared_ptr<oType>( new DynamicRange( *std::dynamic_pointer_cast<oType>(mProd) ) );
|
2018-10-30 15:06:29 +01:00
|
|
|
mProductCreated = false;
|
|
|
|
}
|
|
|
|
std::dynamic_pointer_cast<oType>(mProd)->mOrig.push_back(r);
|
|
|
|
std::dynamic_pointer_cast<oType>(mProd)->mSize *= r->size();
|
|
|
|
std::dynamic_pointer_cast<oType>(mProd)->mEmpty = false;
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
/*********************
|
|
|
|
* DynamicIndex *
|
|
|
|
*********************/
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
|
|
|
|
2018-11-01 22:11:08 +01:00
|
|
|
template <class... Indices>
|
2020-07-08 17:55:51 +02:00
|
|
|
DynamicIndex& DynamicIndex::operator()(const std::shared_ptr<Indices>&... is)
|
2018-11-01 22:11:08 +01:00
|
|
|
{
|
2019-03-19 20:58:59 +01:00
|
|
|
mIvecInit = true;
|
2020-07-08 17:55:51 +02:00
|
|
|
vector<std::shared_ptr<IndexW>> tmp =
|
|
|
|
{ std::make_shared<IndexWrapper<Indices>>(is)... };
|
2018-11-01 22:11:08 +01:00
|
|
|
|
|
|
|
assert(mIVec.size() == tmp.size());
|
|
|
|
for(size_t i = 0; i != mIVec.size(); ++i){
|
|
|
|
mIVec[i].first = tmp[i];
|
|
|
|
}
|
2018-12-02 22:08:51 +01:00
|
|
|
sync();
|
2018-11-01 22:11:08 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2018-10-30 15:06:29 +01:00
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <size_t N>
|
2020-07-08 17:55:51 +02:00
|
|
|
void DynamicIndex::getPtr() {}
|
|
|
|
|
2018-11-01 22:11:08 +01:00
|
|
|
|
|
|
|
struct ForMaker
|
|
|
|
{
|
|
|
|
template <class IVecT>
|
2020-07-08 17:55:51 +02:00
|
|
|
static inline auto mk(size_t i, size_t step, DynamicExpression ex,
|
2018-11-01 22:11:08 +01:00
|
|
|
const IVecT& ivec, bool hidden = false)
|
2020-07-08 17:55:51 +02:00
|
|
|
-> DynamicExpression
|
2018-11-01 22:11:08 +01:00
|
|
|
{
|
2018-12-02 22:08:51 +01:00
|
|
|
if(i == 0) {
|
|
|
|
auto& ii = *ivec[0].first;
|
|
|
|
return hidden ? ii.iforh(step*ivec[i].second, ex) :
|
|
|
|
ii.ifor(step*ivec[i].second, ex);
|
|
|
|
}
|
|
|
|
else {
|
2018-11-01 22:11:08 +01:00
|
|
|
auto& ii = *ivec[i].first;
|
|
|
|
return mk(i-1, step,
|
|
|
|
(hidden ? ii.iforh(step*ivec[i].second, ex) :
|
|
|
|
ii.ifor(step*ivec[i].second, ex)),
|
|
|
|
ivec, hidden);
|
2018-12-02 22:08:51 +01:00
|
|
|
|
2018-11-01 22:11:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class Expr>
|
2020-07-09 17:37:28 +02:00
|
|
|
DynamicExpression DynamicIndex::ifor(size_t step, Expr ex) const
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
2020-09-16 00:09:55 +02:00
|
|
|
if(mIVec.size() == 0){
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
else if(mIVec.size() == 1){
|
2020-07-09 17:37:28 +02:00
|
|
|
return mIVec.back().first->ifor(step,ex);
|
2018-12-02 22:08:51 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-07-09 17:37:28 +02:00
|
|
|
return ForMaker::mk(mIVec.size()-2, step,
|
|
|
|
mIVec.back().first->ifor(step,ex),mIVec);
|
2018-12-02 22:08:51 +01:00
|
|
|
}
|
2018-10-30 15:06:29 +01:00
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class Expr>
|
2020-07-09 17:37:28 +02:00
|
|
|
DynamicExpression DynamicIndex::iforh(size_t step, Expr ex) const
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
2020-09-16 00:09:55 +02:00
|
|
|
if(mIVec.size() == 0){
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
else if(mIVec.size() == 1){
|
2020-07-09 17:37:28 +02:00
|
|
|
return mIVec.back().first->iforh(step,ex);
|
2018-12-02 22:08:51 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-07-09 17:37:28 +02:00
|
|
|
return ForMaker::mk(mIVec.size()-2, step,
|
|
|
|
mIVec.back().first->iforh(step,ex),
|
|
|
|
mIVec, true);
|
2018-12-02 22:08:51 +01:00
|
|
|
}
|
2018-10-30 15:06:29 +01:00
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2019-01-15 17:41:43 +01:00
|
|
|
template <class Expr>
|
2020-07-09 17:37:28 +02:00
|
|
|
DynamicExpression DynamicIndex::pifor(size_t step, Expr ex) const
|
2019-01-15 17:41:43 +01:00
|
|
|
{
|
|
|
|
return ifor(step, ex); // no multithreading here at the moment...
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
/***********************
|
|
|
|
* DynamicRange *
|
|
|
|
***********************/
|
|
|
|
|
|
|
|
template <class... RangeTypes>
|
2020-07-08 17:55:51 +02:00
|
|
|
DynamicRange::DynamicRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) :
|
|
|
|
RangeInterface<DynamicIndex>()
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
2021-05-27 18:37:15 +02:00
|
|
|
mOrig.resize(sizeof...(RangeTypes));
|
|
|
|
sfor_pn<0,sizeof...(RangeTypes)>
|
|
|
|
( [&](auto i) { mOrig[i] = std::get<i>(origs); return 0; } );
|
2021-05-27 11:02:36 +02:00
|
|
|
mSize = sfor_p<0,sizeof...(RangeTypes)>
|
|
|
|
( [&](auto i) { return std::get<i>(origs)->size(); },
|
|
|
|
[&](auto a, auto b) { return a * b; } );
|
2018-10-30 15:06:29 +01:00
|
|
|
if(sizeof...(RangeTypes)){
|
|
|
|
mEmpty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class... RangeTypes>
|
2020-07-08 17:55:51 +02:00
|
|
|
DynamicRange::DynamicRange(std::shared_ptr<RangeTypes>... origs) :
|
|
|
|
RangeInterface<DynamicIndex>()
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
|
|
|
auto rst = std::make_tuple(origs...);
|
2021-05-27 18:37:15 +02:00
|
|
|
mOrig.resize(sizeof...(RangeTypes));
|
|
|
|
sfor_pn<0,sizeof...(RangeTypes)>
|
|
|
|
( [&](auto i) { mOrig[i] = std::get<i>(rst); return 0; } );
|
2021-05-27 11:02:36 +02:00
|
|
|
mSize = sfor_p<0,sizeof...(RangeTypes)>
|
|
|
|
( [&](auto i) { return std::get<i>(rst)->size(); },
|
|
|
|
[&](auto a, auto b) { return a * b; } );
|
2018-10-30 15:06:29 +01:00
|
|
|
if(sizeof...(RangeTypes)){
|
|
|
|
mEmpty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class Range>
|
2020-07-08 17:55:51 +02:00
|
|
|
std::shared_ptr<Range> DynamicRange::fullsub(size_t num) const
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
|
|
|
return std::dynamic_pointer_cast<Range>( mOrig.at(num) );
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
template <class... Ranges>
|
2020-07-08 17:55:51 +02:00
|
|
|
std::shared_ptr<MultiRange<Ranges...> > DynamicRange::scast(SIZET<Ranges>... sizes) const
|
2018-10-30 15:06:29 +01:00
|
|
|
{
|
|
|
|
std::tuple<std::shared_ptr<Ranges>...> rtp;
|
2021-05-27 18:37:15 +02:00
|
|
|
RangeHelper::resolveRangeType<0>(mOrig, rtp, 0, sizes...);
|
2018-10-30 15:06:29 +01:00
|
|
|
MultiRangeFactory<Ranges...> mrf(rtp);
|
|
|
|
return std::dynamic_pointer_cast<MultiRange<Ranges...> >( mrf.create() );
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:55:51 +02:00
|
|
|
|
2021-07-28 20:29:56 +02:00
|
|
|
} // end namespace CNORXZ
|
2020-08-27 14:02:18 +02:00
|
|
|
|
2021-07-28 20:29:56 +02:00
|
|
|
namespace CNORXZ
|
2021-05-27 18:37:15 +02:00
|
|
|
{
|
|
|
|
namespace RangeHelper
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
inline void resolveSetRange<DynamicRange>(std::shared_ptr<DynamicRange>& rp,
|
|
|
|
const vector<std::shared_ptr<RangeBase> >& orig,
|
|
|
|
size_t origpos, size_t size)
|
|
|
|
{
|
|
|
|
DynamicRangeFactory arf;
|
|
|
|
for(size_t op = origpos; op != origpos + size; ++op){
|
|
|
|
//VCHECK(op);
|
|
|
|
arf.append(orig[op]);
|
|
|
|
}
|
|
|
|
rp = std::dynamic_pointer_cast<DynamicRange>( arf.create() );
|
|
|
|
}
|
|
|
|
|
2020-08-27 14:02:18 +02:00
|
|
|
|
2021-05-27 23:29:04 +02:00
|
|
|
template <>
|
|
|
|
inline void setRangeToVec<DynamicRange>(vector<std::shared_ptr<RangeBase> >& v,
|
|
|
|
std::shared_ptr<DynamicRange> r)
|
|
|
|
{
|
|
|
|
if(not r->isEmpty()){
|
|
|
|
for(size_t i = r->dim(); i != 0; --i){
|
|
|
|
v.insert(v.begin(), r->sub(i-1));
|
|
|
|
}
|
2020-08-27 14:02:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 23:29:04 +02:00
|
|
|
template <>
|
|
|
|
inline size_t getStepSize<DynamicIndex>(const DynamicIndex& ii, std::intptr_t j)
|
|
|
|
{
|
|
|
|
size_t ss = 0;
|
|
|
|
size_t sx = 1;
|
|
|
|
for(size_t k = ii.dim(); k != 0; --k){
|
|
|
|
const size_t i = k-1;
|
|
|
|
const auto& ni = ii.get(i);
|
|
|
|
const size_t max = ni.max();
|
|
|
|
const size_t tmp = ni.getStepSizeComp(j);
|
|
|
|
ss += tmp * ii.getStepSize(i);
|
|
|
|
sx *= max;
|
|
|
|
}
|
|
|
|
return ss;
|
|
|
|
|
|
|
|
}
|
2020-08-27 14:02:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|