im com (dynamic range)

This commit is contained in:
Christian Zimmermann 2018-10-22 14:27:55 +02:00
parent 7dc015a97a
commit 1fa60ea9c8
6 changed files with 754 additions and 7 deletions

View file

@ -0,0 +1,422 @@
#ifndef __dynamic_range_h__
#define __dynamic_range_h__
#include "ranges/rbase_def.h"
#include "ranges/range_base.h"
#include "ranges/index_base.h"
namespace MultiArrayTools
{
typedef std::pair<const char*,size_t> DynamicMetaElem;
class DynamicMetaT
{
private:
std::vector<DynamicMetaElem> mMeta;
public:
DynamicMetaT() = default;
DynamicMetaT(const DynamicMetaT& in) = default;
DynamicMetaT(DynamicMetaT&& in) = default;
DynamicMetaT& operator=(const DynamicMetaT& in) = default;
DynamicMetaT& operator=(DynamicMetaT&& in) = default;
template <typename... Us>
DynamicMetaT(const std::tuple<Us...>& meta);
bool operator==(const DynamicMetaT& in) const;
bool operator!=(const DynamicMetaT& in) const;
DynamicMetaElem& operator[](size_t pos);
const DynamicMetaElem& operator[](size_t pos) const;
};
class IndexWrapperBase
{
public:
IndexWrapperBase() = default;
IndexWrapperBase(const IndexWrapperBase& in) = default;
IndexWrapperBase(IndexWrapperBase&& in) = default;
IndexWrapperBase& operator=(const IndexWrapperBase& in) = default;
IndexWrapperBase& operator=(IndexWrapperBase&& in) = default;
virtual IndexType type() const = 0;
virtual IndexWrapperBase& operator=(size_t pos) = 0;
virtual IndexWrapperBase& operator++() = 0;
virtual IndexWrapperBase& operator--() = 0;
virtual int pp(std::intptr_t idxPtrNum) = 0;
virtual int mm(std::intptr_t idxPtrNum) = 0;
virtual std::string stringMeta() const = 0;
virtual DynamicMetaT meta() const = 0;
virtual const DynamicMetaT* metaPtr() const = 0;
virtual IndexWrapperBase& at(const U& metaPos) = 0;
virtual size_t posAt(const U& metaPos) const = 0;
//virtual bool isMeta(const U& metaPos) const = 0;
virtual size_t dim() = 0;
virtual bool last() = 0;
virtual bool first() = 0;
virtual std::shared_ptr<RangeBase> range() = 0;
virtual size_t getStepSize(size_t n) = 0;
virtual std::intptr_t get() const = 0;
template <class Expr>
auto ifor(size_t step, Expr ex)
-> DynamicalExpression<Expr>;
template <class Expr>
auto iforh(size_t step, Expr ex)
-> DynamicalExpression<Expr>;
};
typedef IndexWrapperBase IndexW;
template <class Expr>
class MakeForBase
{
public:
virtual DynamicalExpression<Expr> ifor(size_t step, Expr ex) const = 0;
virtual DynamicalExpression<Expr> iforh(size_t step, Expr ex) const = 0;
};
template <class Expr, class Index>
class MakeFor : public MakeForBase<Expr>
{
private:
const Index& mI;
public:
MakeFor(const Index& i);
virtual DynamicalExpression<Expr> ifor(size_t step, Expr ex) const override;
virtual DynamicalExpression<Expr> iforh(size_t step, Expr ex) const override;
};
template <class Index>
class IndexWrapper : public IndexWrapperBase
{
private:
IndexWrapper() = default;
std::shared_ptr<Index> mI;
public:
IndexWrapper(const IndexWrapper& in) = default;
IndexWrapper(IndexWrapper&& in) = default;
IndexWrapper& operator=(const IndexWrapper& in) = default;
IndexWrapper& operator=(IndexWrapper&& in) = default;
IndexWrapper(const std::shared_ptr<Index>& i) : mI(i) {}
virtual IndexType type() const final { return mI->type(); }
virtual IndexWrapperBase& operator=(size_t pos) final { (*mI) = pos; return *this; }
virtual IndexWrapperBase& operator++() final { ++(*mI); return *this; }
virtual IndexWrapperBase& operator--() final { --(*mi); return *this; }
virtual int pp(std::intptr_t idxPtrNum) final { return mI->pp(idxPtrNum); }
virtual int mm(std::intptr_t idxPtrNum) final { return mI->mm(idxPtrNum); }
virtual std::string stringMeta() const final { return mI->stringMeta(); }
virtual DynamicMetaT meta() const final { return DynamicMetaT(mI->meta()); }
virtual const DynamicMetaT* metaPtr() const final { return nullptr; }
virtual IndexWrapperBase& at(const U& metaPos) final { mI->at(metaPos); return *this; }
virtual size_t posAt(const U& metaPos) const final { return mI->posAt(metaPos); }
//virtual bool isMeta(const U& metaPos) const final { return mI->isMeta(); }
virtual size_t dim() final { return mI->dim(); }
virtual bool last() final { return mI->last(); }
virtual bool first() final { return mI->first(); }
virtual std::shared_ptr<RangeBase> range() final { return mI->range(); }
virtual size_t getStepSize(size_t n) final { return mI->getStepSize(n); }
virtual std::intptr_t get() const final { return reinterpret_cast<std::intptr_t>(mI.get()); }
};
class DynamicIndex : public IndexInterface<DynamicIndex,DynamicMetaT>
{
private:
std::vector<std::shared_ptr<IndexWrapperBase>> mIVec;
public:
typedef IndexInterface<DynamicIndex,DynamicMetaT> IB;
typedef DynamicMetaT MetaType;
typedef DynamicRange RangeType;
typedef DynamicIndex IType;
DynamicIndex(const std::shared_ptr<DynamicRange>& range);
static constexpr IndexType sType() { return IndexType::SINGLE; }
static constexpr size_t totalDim() { return 1; }
static constexpr size_t sDim() { return 1; }
static constexpr SpaceType STYPE = SpaceType::DYN;
IndexType type() const;
DynamicIndex& operator=(size_t pos);
DynamicIndex& operator++();
DynamicIndex& operator--();
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
std::string stringMeta() const;
MetaType meta() const;
const MetaType* metaPtr() const;
DynamicIndex& at(const MetaType& metaPos);
size_t posAt(const MetaType& metaPos) const;
//bool isMeta(const MetaType& metaPos) const;
size_t dim();
bool last();
bool first();
std::shared_ptr<RangeType> range();
template <size_t N>
void getPtr();
size_t getStepSize(size_t n);
std::string id() const;
void print(size_t offset);
template <class Expr>
auto ifor(size_t step, Expr ex) const
-> DynamicalExpression<Expr>;
template <class Expr>
auto iforh(size_t step, Expr ex) const
-> DynamicalExpression<Expr>;
};
// NOT THREAD SAVE!!
class DynamicRangeFactory : public RangeFactoryBase
{
public:
typedef DynamicRange oType;
DynamicRangeFactory();
template <class... RangeTypes>
DynamicRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
template <class... RangeTypes>
DynamicRangeFactory(std::shared_ptr<RangeTypes>... origs);
template <class Range>
void append(std::shared_ptr<Range> r);
std::shared_ptr<RangeBase> create();
private:
std::shared_ptr<RangeBase> checkIfCreated(const std::vector<std::shared_ptr<RangeBase> >& pvec);
static std::map<std::shared_ptr<RangeBase>,std::vector<std::intptr_t> > mAleadyCreated;
bool mProductCreated = false;
};
class DynamicRange : public RangeInterface<DynamicIndex>
{
public:
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<DynamicIndex>::IndexType IndexType;
typedef DynamicRange RangeType;
typedef DynamicMetaT MetaType;
private:
DynamicRange() = default;
DynamicRange(const DynamicRange& in) = default;
template <class... RangeTypes>
DynamicRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
template <class... RangeTypes>
DynamicRange(std::shared_ptr<RangeTypes>... origs);
size_t mSize = 1;
bool mEmpty = true;
std::vector<std::shared_ptr<RangeBase> > mOrig;
public:
virtual size_t size() const final;
virtual size_t dim() const final;
MetaType get(size_t pos) const;
size_t getMeta(const MetaType& metaPos) const;
virtual IndexType begin() const final;
virtual IndexType end() const final;
virtual SpaceType spaceType() const final;
virtual std::string stringMeta(size_t pos) const final;
virtual std::vector<char> data() const final;
std::shared_ptr<RangeBase> sub(size_t num) const;
template <class Range>
std::shared_ptr<Range> fullsub(size_t num) const;
template <class... Ranges>
std::shared_ptr<MultiRange<Ranges...> > scast(SIZET<Ranges>... sizes) const; // save cast
void sreplace(const std::shared_ptr<RangeBase> in, size_t num);
bool isEmpty() const;
friend DynamicRangeFactory;
static DynamicRangeFactory factory()
{ return DynamicRangeFactory(); }
};
} // namespace MultiArrayTools
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayTools
{
/***********************
* DynamicRange *
***********************/
template <class... RangeTypes>
DynamicRangeFactory::DynamicRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs)
{
mProd = std::shared_ptr<oType>( new DynamicRange( origs ) );
}
template <class... RangeTypes>
DynamicRangeFactory::DynamicRangeFactory(std::shared_ptr<RangeTypes>... origs)
{
mProd = std::shared_ptr<oType>( new DynamicRange( origs... ) );
}
template <class Range>
void DynamicRangeFactory::append(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;
}
/*****************
* Functions *
*****************/
std::shared_ptr<DynamicRange> defaultRange(size_t size = 0);
}
namespace MultiArrayHelper
{
using namespace MultiArrayTools;
template <>
inline void resolveSetRange<DynamicRange>(std::shared_ptr<DynamicRange>& rp,
const std::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>(std::vector<std::shared_ptr<RangeBase> >& v,
std::shared_ptr<DynamicRange> r)
{
if(not r->isEmpty()){
for(size_t i = r->anonymousDim(); i != 0; --i){
v.insert(v.begin(), r->sub(i-1));
}
}
}
}
namespace MultiArrayTools
{
/***********************
* DynamicRange *
***********************/
template <class... RangeTypes>
SingleRange<size_t,SpaceType::DYN>::SingleRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) :
RangeInterface<DynamicIndex>()
{
RPackNum<sizeof...(RangeTypes)-1>::RangesToVec( origs, mOrig );
mSize = RPackNum<sizeof...(RangeTypes)-1>::getSize( origs );
if(sizeof...(RangeTypes)){
mEmpty = false;
}
}
template <class... RangeTypes>
SingleRange<size_t,SpaceType::DYN>::SingleRange(std::shared_ptr<RangeTypes>... origs) :
RangeInterface<DynamicIndex>()
{
auto rst = std::make_tuple(origs...);
RPackNum<sizeof...(RangeTypes)-1>::RangesToVec( rst, mOrig );
mSize = RPackNum<sizeof...(RangeTypes)-1>::getSize( rst );
if(sizeof...(RangeTypes)){
mEmpty = false;
}
}
template <class Range>
std::shared_ptr<Range> SingleRange<size_t,SpaceType::DYN>::fullsub(size_t num) const
{
return std::dynamic_pointer_cast<Range>( mOrig.at(num) );
}
template <class... Ranges>
std::shared_ptr<MultiRange<Ranges...> > SingleRange<size_t,SpaceType::DYN>::scast(SIZET<Ranges>... sizes) const
{
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() );
}
}
#endif

View file

@ -26,7 +26,8 @@ namespace MultiArrayTools
#define include_range_type(x,n) x = n,
#include "range_types/header.h"
#undef include_range_type
ANON = -1 // anonymous content
ANON = -1, // anonymous content
DYN = -2 // dynamic content
};
struct DataHeader

View file

@ -72,6 +72,15 @@ namespace MultiArrayTools
// anonymous_range.h
//class AnonymousRange;
// dynamic_range.h
class DynamicIndex;
// dynamic_range.h
class DynamicRangeFactory;
// dynamic_range.h
class DynamicRange;
// value_range.h
template <typename U>
class ValueRange;

View file

@ -4,6 +4,7 @@
#include "rpheader.h"
#include "anonymous_range.h"
#include "dynamic_range.h"
#include "value_range.h"
//#endif

View file

@ -40,6 +40,7 @@ namespace MultiArrayHelper
ExpressionBase& operator=(ExpressionBase&& in) = default;
ExpressionBase(ExtType ext, Expr&& expr);
ExpressionBase(ExtType ext);
virtual void operator()(size_t mlast, ExtType last) const = 0;
virtual void operator()(size_t mlast = 0) const = 0;
@ -194,8 +195,7 @@ namespace MultiArrayHelper
DynamicalExpression& operator=(const DynamicalExpression& in) = default;
DynamicalExpression& operator=(DynamicalExpression&& in) = default;
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next,
Expr expr);
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next);
inline void operator()(size_t mlast, ExtType last) const override final;
inline void operator()(size_t mlast = 0) const override final;
@ -222,7 +222,12 @@ namespace MultiArrayHelper
mExpr(std::forward<Expr>(expr)),
mExt(ext)
{}
template <class Expr>
ExpressionBase<Expr>::ExpressionBase(ExtType ext) :
mExt(ext)
{}
template <class Expr>
auto ExpressionBase<Expr>::rootSteps(std::intptr_t iPtrNum) const
-> ExtType
@ -352,9 +357,8 @@ namespace MultiArrayHelper
template <class Expr>
DynamicalExpression<Expr>::
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next,
Expr expr) :
ExpressionBase<Expr>(next->extension(), std::forward<Expr>(expr)),
DynamicalExpression(const std::shared_ptr<ExpressionBase<Expr>>& next) :
ExpressionBase<Expr>(next->extension()),
mNext(next)
{}

View file

@ -0,0 +1,310 @@
#include "ranges/dynamic_range.h"
namespace MultiArrayTools
{
/**********************
* DynamicIndex *
**********************/
DynamicIndex::DynamicIndex(const std::shared_ptr<DynamicRange >& range) :
IndexInterface<DynamicIndex,MetaType>(range, 0),
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr)),
{}
IndexType DynamicIndex::type() const
{
return IndexType::SINGLE;
}
DynamicIndex& DynamicIndex::operator=(size_t pos)
{
IB::mPos = pos;
return *this;
}
DynamicIndex& DynamicIndex::operator++()
{
// !!!
return *this;
}
DynamicIndex& DynamicIndex::operator--()
{
// !!!
return *this;
}
int DynamicIndex::pp(std::intptr_t idxPtrNum)
{
++(*this);
return 1;
}
int DynamicIndex::mm(std::intptr_t idxPtrNum)
{
--(*this);
return 1;
}
std::string DynamicIndex::stringMeta() const
{
return std::dynamic_pointer_cast<DynamicRange const>( IB::mRangePtr )->stringMeta(IB::mPos);
}
MetaType DynamicIndex::meta() const
{
return mExplicitRangePtr->meta(IB::mPos);
}
const MetaType* DynamicIndex::metaPtr() const
{
return nullptr;
}
/*
bool DynamicIndex::isMeta(const MetaType& metaPos) const
{
return mExplicitRangePtr->isMeta(metaPos);
}*/
DynamicIndex& DynamicIndex::at(const MetaType& metaPos)
{
(*this) = std::dynamic_pointer_cast<DynamicRange const>( IB::mRangePtr )->getMeta( metaPos );
return *this;
}
size_t DynamicIndex::posAt(const U& metaPos) const
{
return std::dynamic_pointer_cast<DynamicRange const>( IB::mRangePtr )->getMeta( metaPos );
}
size_t DynamicIndex::dim() // = 1
{
return 1;
}
bool DynamicIndex::last()
{
return IB::mPos == IB::mMax - 1;
}
bool DynamicIndex::first()
{
return IB::mPos == 0;
}
std::shared_ptr<typename DynamicIndex::RangeType> DynamicIndex::range()
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr );
}
template <size_t N>
void DynamicIndex::getPtr() {}
size_t DynamicIndex::getStepSize(size_t n)
{
return 1;
}
std::string DynamicIndex::id() const
{
return std::string("dyn") + std::to_string(IB::mId);
}
void DynamicIndex::print(size_t offset)
{
if(offset == 0){
std::cout << " === " << std::endl;
}
for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; }
std::cout << id() << "[" << reinterpret_cast<std::intptr_t>(this)
<< "](" << IB::mRangePtr << "): " << meta() << std::endl;
}
/******************************
* DynamicRangeFactory *
******************************/
DynamicRangeFactory::DynamicRangeFactory()
{
mProd = std::shared_ptr<oType>( new DynamicRange() );
}
std::map<std::shared_ptr<RangeBase>,std::vector<std::intptr_t> > DynamicRangeFactory::mAleadyCreated;
std::shared_ptr<RangeBase> DynamicRangeFactory::checkIfCreated(const std::vector<std::shared_ptr<RangeBase> >& pvec)
{
std::shared_ptr<RangeBase> out;
bool check = false;
for(auto& x: mAleadyCreated){
if(x.second.size() == pvec.size()){
check = true;
for(size_t i = 0; i != x.second.size(); ++i){
if(x.second[i] != reinterpret_cast<std::intptr_t>( pvec[i].get() ) ){
check = false;
break;
}
}
if(check == true){
out = x.first;
break;
}
}
}
if(not check){
std::vector<std::intptr_t> app(pvec.size());
for(size_t i = 0; i != app.size(); ++i){
app[i] = reinterpret_cast<std::intptr_t>( pvec[i].get() );
}
mAleadyCreated[mProd] = app;
out = mProd;
}
return out;
}
std::shared_ptr<RangeBase> DynamicRangeFactory::create()
{
mProd = checkIfCreated(std::dynamic_pointer_cast<DynamicRange>(mProd)->mOrig);
setSelf();
mProductCreated = true;
return mProd;
}
/***********************
* DynamicRange *
***********************/
size_t DynamicRange::get(size_t pos) const
{
return pos;
}
size_t DynamicRange::getMeta(size_t metaPos) const
{
return metaPos;
}
size_t DynamicRange::size() const
{
return mSize;
}
size_t DynamicRange::dim() const
{
return 1;
}
SpaceType DynamicRange::spaceType() const
{
return SpaceType::DYN;
}
bool DynamicRange::isEmpty() const
{
return mEmpty;
}
std::string DynamicRange::stringMeta(size_t pos) const
{
std::string out = "[ ";
//size_t xpos = pos;
for(size_t i = mOrig.size(); i != 0; --i) {
auto& x = mOrig[i-1];
const size_t redpos = pos % x->size();
out = ( (i == mOrig.size()) ? out : out + " , " ) + x->stringMeta(redpos);
pos -= redpos;
pos /= x->size();
}
out += " ]";
return out;
}
std::vector<char> DynamicRange::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::DYN );
h.metaSize = mOrig.size();
h.multiple = 1;
std::vector<char> out;
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
for(auto& x: mOrig){
auto part = x->data();
out.insert(out.end(), part.begin(), part.end());
}
return out;
}
size_t DynamicRange::anonymousDim() const
{
return mOrig.size();
}
typename DynamicRange::IndexType DynamicRange::begin() const
{
DynamicIndex i
(std::dynamic_pointer_cast<DynamicRange>
( std::shared_ptr<RangeBase>(RB::mThis) ) );
i = 0;
return i;
}
typename DynamicRange::IndexType DynamicRange::end() const
{
DynamicIndex i
(std::dynamic_pointer_cast<DynamicRange>
( std::shared_ptr<RangeBase>(RB::mThis) ) );
i = size();
return i;
}
std::shared_ptr<RangeBase> DynamicRange::sub(size_t num) const
{
return mOrig.at(num);
}
void DynamicRange::sreplace(const std::shared_ptr<RangeBase> in, size_t num)
{
assert(mOrig[num]->size() == in->size());
mOrig[num] = in;
}
/*****************
* Functions *
*****************/
std::shared_ptr<DynamicRange> defaultRange(size_t size )
{
DynamicRangeFactory arf
( std::dynamic_pointer_cast<DynamicRange>
(DynamicRange::factory().create() ) );
return std::dynamic_pointer_cast<DynamicRange>( arf.create() );
}
} // end namespace MultiArrayTools