replace proto indices

This commit is contained in:
Christian Zimmermann 2020-08-27 14:02:18 +02:00
parent 991a9bc5b4
commit fca75bbf79
6 changed files with 74 additions and 128 deletions

View file

@ -2,7 +2,7 @@
#include "ranges/dynamic_range.h"
//#include "ranges/dynamic_meta.h"
#include "rpack_num.h"
//#include "rpack_num.h"
namespace MultiArrayTools
{
@ -32,20 +32,6 @@ namespace MultiArrayTools
template <class Range>
void DynamicRangeFactory::appendx(std::shared_ptr<Range> r)
{
if(mProductCreated){
mProd = std::shared_ptr<oType>( new DynamicRange( *std::dynamic_pointer_cast<oType>(mProd) ) );
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;
//std::dynamic_pointer_cast<oType>(mProd)->mProtoI.push_back(mkIndexWrapper(r->begin()));
}
template <class Range>
void DynamicRangeFactory::append(std::shared_ptr<Range> r)
{
@ -57,7 +43,6 @@ namespace MultiArrayTools
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;
std::dynamic_pointer_cast<oType>(mProd)->mProtoI.push_back(mkIndexWrapper(r->begin()));
}
@ -148,51 +133,11 @@ namespace MultiArrayTools
* DynamicRange *
***********************/
template <size_t N>
struct InitProto
{
template <class... RangeTypes>
static void mk(vector<std::shared_ptr<IndexW>>& proto,
const std::tuple<std::shared_ptr<RangeTypes>...>& orig)
{
proto[N] = mkIndexWrapper(std::get<N>(orig)->begin());
InitProto<N-1>::mk(proto, orig);
}
template <class RangeType, class... RangeTypes>
static void mk(vector<std::shared_ptr<IndexW>>& proto, size_t n,
std::shared_ptr<RangeType> o, std::shared_ptr<RangeTypes>... origs)
{
proto[n-1-N] = mkIndexWrapper(o->begin());
InitProto<N-1>::mk(proto, n, origs...);
}
};
template <>
struct InitProto<0>
{
template <class... RangeTypes>
static void mk(vector<std::shared_ptr<IndexW>>& proto,
const std::tuple<std::shared_ptr<RangeTypes>...>& orig)
{
proto[0] = mkIndexWrapper(std::get<0>(orig)->begin());
}
template <class RangeType, class... RangeTypes>
static void mk(vector<std::shared_ptr<IndexW>>& proto, size_t n,
std::shared_ptr<RangeType> o, std::shared_ptr<RangeTypes>... origs)
{
proto[n-1] = mkIndexWrapper(o->begin());
}
};
template <class... RangeTypes>
DynamicRange::DynamicRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) :
RangeInterface<DynamicIndex>()
{
RPackNum<sizeof...(RangeTypes)-1>::RangesToVec( origs, mOrig );
mProtoI.resize(sizeof...(RangeTypes));
InitProto<sizeof...(RangeTypes)-1>::mk(mProtoI, origs);
mSize = RPackNum<sizeof...(RangeTypes)-1>::getSize( origs );
if(sizeof...(RangeTypes)){
mEmpty = false;
@ -206,8 +151,6 @@ namespace MultiArrayTools
{
auto rst = std::make_tuple(origs...);
RPackNum<sizeof...(RangeTypes)-1>::RangesToVec( rst, mOrig );
mProtoI.resize(sizeof...(RangeTypes));
InitProto<sizeof...(RangeTypes)-1>::mk(mProtoI, sizeof...(RangeTypes), origs...);
mSize = RPackNum<sizeof...(RangeTypes)-1>::getSize( rst );
if(sizeof...(RangeTypes)){
mEmpty = false;
@ -233,3 +176,53 @@ namespace MultiArrayTools
} // end namespace MultiArrayTools
namespace MultiArrayHelper
{
namespace
{
using namespace MultiArrayTools;
}
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() );
}
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));
}
}
}
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;
}
}

View file

@ -17,6 +17,7 @@
#include "ranges/dynamic_meta.h"
#include "index_wrapper.h"
#include "rpack_num.h"
namespace MultiArrayTools
{
@ -24,23 +25,6 @@ namespace MultiArrayTools
{
using namespace MultiArrayHelper;
}
//using MultiArrayHelper::DynamicExpression;
//using MultiArrayHelper::ExpressionHolder;
/*
class AbstractIW
{
public:
AbstractIW() = default;
AbstractIW(const AbstractIW& in) = default;
AbstractIW(AbstractIW&& in) = default;
AbstractIW& operator=(const AbstractIW& in) = default;
AbstractIW& operator=(AbstractIW&& in) = default;
};
*/
//typedef SingleRange<size_t,SpaceType::DYN> DynamicRange;
class DynamicIndex : public IndexInterface<DynamicIndex,vector<char>>
{
@ -142,9 +126,6 @@ namespace MultiArrayTools
template <class Range>
void append(std::shared_ptr<Range> r);
template <class Range>
void appendx(std::shared_ptr<Range> r);
std::shared_ptr<RangeBase> create();
private:
@ -164,7 +145,6 @@ namespace MultiArrayTools
static constexpr size_t SIZE = -1;
static constexpr bool HASMETACONT = false;
typedef vector<std::shared_ptr<IndexW>> IVecT;
typedef RangeBase RB;
typedef DynamicIndex IndexType;
typedef DynamicRange RangeType;
@ -187,7 +167,6 @@ namespace MultiArrayTools
bool mEmpty = true;
vector<std::shared_ptr<RangeBase> > mOrig;
IVecT mProtoI;
public:
virtual size_t size() const final;
@ -209,7 +188,6 @@ namespace MultiArrayTools
virtual vector<char> data() const final;
std::shared_ptr<RangeBase> sub(size_t num) const;
std::shared_ptr<IndexW> subI(size_t num) const;
template <class Range>
std::shared_ptr<Range> fullsub(size_t num) const;
@ -235,44 +213,22 @@ namespace MultiArrayTools
namespace MultiArrayHelper
{
namespace
{
using namespace MultiArrayTools;
}
inline void resolveSetRange(std::shared_ptr<DynamicRange>& rp,
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.appendx(orig[op]);
}
rp = std::dynamic_pointer_cast<DynamicRange>( arf.create() );
}
size_t origpos, size_t size);
inline void setRangeToVec(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));
}
}
}
template <>
inline void setRangeToVec<DynamicRange>(vector<std::shared_ptr<RangeBase> >& v,
std::shared_ptr<DynamicRange> r);
inline size_t getStepSize(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;
}
template <>
inline size_t getStepSize<DynamicIndex>(const DynamicIndex& ii, std::intptr_t j);
}

View file

@ -145,7 +145,7 @@ namespace MultiArrayTools
}
template <class Index>
std::shared_ptr<IndexWrapperBase> mkIndexWrapper(const Index& i)
inline std::shared_ptr<IndexWrapperBase> mkIndexWrapper(const Index& i)
{
return std::make_shared<IndexWrapper<Index>>(std::make_shared<Index>(i));
}

View file

@ -136,10 +136,10 @@ namespace MultiArrayTools
virtual std::shared_ptr<IndexWrapperBase> duplicate() const override final;
};
/*
template <class Index>
std::shared_ptr<IndexWrapperBase> mkIndexWrapper(const Index& i);
*/
}
#endif

View file

@ -101,6 +101,9 @@ namespace MultiArrayTools
std::weak_ptr<RangeBase> mThis;
};
template <class Index>
inline std::shared_ptr<IndexWrapperBase> mkIndexWrapper(const Index& i);
template <class Index>
class RangeInterface : public RangeBase
{
@ -114,8 +117,9 @@ namespace MultiArrayTools
virtual Index begin() const = 0;
virtual Index end() const = 0;
virtual std::shared_ptr<IndexWrapperBase> aindex() const override final
{ return mkIndexWrapper(this->begin()); }
//{ auto i = std::make_shared<Index>(this->begin()); return std::make_shared<IndexWrapper<Index>>(i); } //!!!
{ auto i = std::make_shared<Index>(this->begin()); return nullptr; } //!!!
//{ auto i = std::make_shared<Index>(this->begin()); return nullptr; } //!!!
protected:
RangeInterface() = default;

View file

@ -179,7 +179,7 @@ namespace MultiArrayTools
assert(this->range()->sub(i) == sIMap.at(iname)->range());
}
else {
sIMap[iname] = this->range()->subI(i);
sIMap[iname] = this->range()->sub(i)->aindex();
}
mIVec[i].first = sIMap.at(iname);
}
@ -445,13 +445,6 @@ namespace MultiArrayTools
return mOrig.at(num);
}
std::shared_ptr<IndexW> DynamicRange::subI(size_t num) const
{
assert(mProtoI.at(num) != nullptr);
return mProtoI.at(num)->duplicate();
}
void DynamicRange::sreplace(const std::shared_ptr<RangeBase> in, size_t num)
{
assert(mOrig[num]->size() == in->size());