cnorxz/src/include/ranges/dynamic_range.h

398 lines
11 KiB
C
Raw Normal View History

2018-10-22 14:27:55 +02:00
#ifndef __dynamic_range_h__
#define __dynamic_range_h__
#include <cassert>
2018-10-22 14:27:55 +02:00
#include "ranges/rbase_def.h"
#include "ranges/range_base.h"
#include "ranges/index_base.h"
2018-10-29 14:19:42 +01:00
#include "xfor/xfor.h"
2018-10-22 14:27:55 +02:00
2018-10-29 14:19:42 +01:00
#include <map>
//#include "ranges/rpheader.h"
2018-10-29 14:19:42 +01:00
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
2018-10-22 14:27:55 +02:00
2018-10-29 14:19:42 +01:00
#include "ranges/dynamic_meta.h"
2018-10-22 14:27:55 +02:00
2018-10-29 14:19:42 +01:00
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
//using MultiArrayHelper::DynamicExpression;
2018-10-23 20:02:01 +02:00
//using MultiArrayHelper::ExpressionHolder;
2018-11-06 16:35:37 +01:00
class AbstractIW
2018-10-22 14:27:55 +02:00
{
public:
2018-11-06 16:35:37 +01:00
AbstractIW() = default;
AbstractIW(const AbstractIW& in) = default;
AbstractIW(AbstractIW&& in) = default;
AbstractIW& operator=(const AbstractIW& in) = default;
AbstractIW& operator=(AbstractIW&& in) = default;
2018-10-22 14:27:55 +02:00
virtual IndexType type() const = 0;
2018-11-06 16:35:37 +01:00
virtual AbstractIW& operator=(size_t pos) = 0;
virtual AbstractIW& operator++() = 0;
virtual AbstractIW& operator--() = 0;
2018-10-22 14:27:55 +02:00
virtual int pp(std::intptr_t idxPtrNum) = 0;
virtual int mm(std::intptr_t idxPtrNum) = 0;
virtual std::string stringMeta() const = 0;
2018-10-29 14:19:42 +01:00
virtual size_t pos() const = 0;
virtual size_t max() const = 0;
2018-10-27 14:58:34 +02:00
virtual size_t dim() const = 0;
virtual bool last() const = 0;
virtual bool first() const = 0;
2018-10-22 14:27:55 +02:00
2018-10-27 14:58:34 +02:00
virtual std::shared_ptr<RangeBase> range() const = 0;
2018-10-22 14:27:55 +02:00
2018-10-27 14:58:34 +02:00
virtual size_t getStepSize(size_t n) const = 0;
2018-10-29 14:19:42 +01:00
virtual size_t getStepSizeComp(std::intptr_t j) const = 0;
2018-10-22 14:27:55 +02:00
virtual std::intptr_t get() const = 0;
virtual std::intptr_t ptrNum() const = 0;
2018-11-06 16:35:37 +01:00
//virtual DynamicMetaT meta() const = 0;
//virtual const DynamicMetaT* metaPtr() const = 0;
//virtual AbstractIW& at(const U& metaPos) = 0;
//virtual size_t posAt(const U& metaPos) const = 0;
//virtual bool isMeta(const U& metaPos) const = 0;
inline AbstractIW& at(const std::string smeta)
{
for((*this) = 0; this->pos() != this->max(); ++(*this)){
if(this->stringMeta() == smeta){
break;
}
}
return *this;
}
2018-11-06 16:35:37 +01:00
};
template <class ExpressionCollection>
class IndexWrapperBase : public AbstractIW
{
protected:
std::shared_ptr<ExpressionCollection> mEc;
public:
IndexWrapperBase() = default;
IndexWrapperBase(const IndexWrapperBase& in) = default;
IndexWrapperBase(IndexWrapperBase&& in) = default;
IndexWrapperBase& operator=(const IndexWrapperBase& in) = default;
IndexWrapperBase& operator=(IndexWrapperBase&& in) = default;
template <class Expr>
ExpressionHolder<Expr> ifor(size_t step, ExpressionHolder<Expr> ex) const;
2018-10-22 14:27:55 +02:00
template <class Expr>
ExpressionHolder<Expr> iforh(size_t step, ExpressionHolder<Expr> ex) const;
template <class Expr>
ExpressionHolder<Expr> ifori(size_t step, Expr ex) const;
template <class Expr>
ExpressionHolder<Expr> iforhi(size_t step, Expr ex) const;
2018-10-22 14:27:55 +02:00
};
template <class EC>
using IndexW = IndexWrapperBase<EC>;
2018-10-22 14:27:55 +02:00
template <class Index, class ExpressionCollection>
class IndexWrapper : public IndexWrapperBase<ExpressionCollection>
2018-10-22 14:27:55 +02:00
{
public:
typedef IndexWrapperBase<ExpressionCollection> IWB;
typedef typename Index::MetaType MetaType;
static constexpr IndexType sType() { return IndexType::SINGLE; }
2018-10-23 20:02:01 +02:00
protected:
2018-10-22 14:27:55 +02:00
IndexWrapper() = default;
2018-10-23 20:02:01 +02:00
private:
2018-10-22 14:27:55 +02:00
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)
{ IWB::mEc = ExpressionCollection::make(mI); }
2018-10-22 14:27:55 +02:00
virtual IndexType type() const final { return mI->type(); }
virtual IndexWrapper& operator=(size_t pos) final { (*mI) = pos; return *this; }
virtual IndexWrapper& operator++() final { ++(*mI); return *this; }
virtual IndexWrapper& operator--() final { --(*mI); return *this; }
2018-10-22 14:27:55 +02:00
2018-10-29 14:19:42 +01:00
virtual size_t pos() const final { return mI->pos(); }
virtual size_t max() const final { return mI->max(); }
2018-10-22 14:27:55 +02:00
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; }
IndexWrapper& at(const typename Index::MetaType& metaPos) { mI->at(metaPos); return *this; }
2018-10-27 14:58:34 +02:00
size_t posAt(const typename Index::MetaType& metaPos) const { return mI->posAt(metaPos); }
2018-10-22 14:27:55 +02:00
//virtual bool isMeta(const U& metaPos) const final { return mI->isMeta(); }
2018-10-27 14:58:34 +02:00
virtual size_t dim() const final { return mI->dim(); }
virtual bool last() const final { return mI->last(); }
virtual bool first() const final { return mI->first(); }
2018-10-22 14:27:55 +02:00
2018-10-27 14:58:34 +02:00
virtual std::shared_ptr<RangeBase> range() const final { return mI->range(); }
2018-10-22 14:27:55 +02:00
2018-10-27 14:58:34 +02:00
virtual size_t getStepSize(size_t n) const final { return mI->getStepSize(n); }
virtual size_t getStepSizeComp(std::intptr_t j) const final;
2018-10-29 14:19:42 +01:00
2018-10-23 20:02:01 +02:00
virtual std::intptr_t get() const final { return reinterpret_cast<std::intptr_t>(mI.get()); }
virtual std::intptr_t ptrNum() const final { return mI->ptrNum(); }
2018-10-27 14:58:34 +02:00
2018-10-23 20:02:01 +02:00
};
//typedef SingleRange<size_t,SpaceType::DYN> DynamicRange;
template <class EC>
class DynamicIndex : public IndexInterface<DynamicIndex<EC>,DynamicMetaT>
2018-10-22 14:27:55 +02:00
{
private:
2019-02-13 21:59:13 +01:00
typedef vector<std::pair<std::shared_ptr<IndexW<EC>>,size_t>> IVecT;
2018-10-29 14:19:42 +01:00
IVecT mIVec;
2018-10-27 14:58:34 +02:00
2018-10-22 14:27:55 +02:00
public:
typedef IndexInterface<DynamicIndex<EC>,DynamicMetaT> IB;
2019-03-13 21:51:48 +01:00
typedef std::vector<char> MetaType;
typedef DynamicRange<EC> RangeType;
2018-10-22 14:27:55 +02:00
typedef DynamicIndex IType;
DynamicIndex(const std::shared_ptr<RangeType>& range);
2018-10-22 14:27:55 +02:00
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--();
2018-10-29 14:19:42 +01:00
DynamicIndex& operator()(const IVecT& ivec);
2019-02-13 21:59:13 +01:00
DynamicIndex& operator()(const vector<std::shared_ptr<IndexW<EC>>>& ivec);
template <class... Indices>
DynamicIndex& operator()(const std::shared_ptr<Indices>&... is);
DynamicIndex<EC>& sync();
2018-10-22 14:27:55 +02:00
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;
2018-10-29 14:19:42 +01:00
size_t dim() const;
bool last() const;
bool first() const;
const IndexW<EC>& get(size_t n) const;
2018-10-22 14:27:55 +02:00
std::shared_ptr<RangeType> range();
template <size_t N>
void getPtr();
2018-10-29 14:19:42 +01:00
size_t getStepSize(size_t n) const;
2018-10-22 14:27:55 +02:00
std::string id() const;
void print(size_t offset);
template <class Expr>
ExpressionHolder<Expr> ifor(size_t step, Expr ex) const;
2018-10-22 14:27:55 +02:00
template <class Expr>
ExpressionHolder<Expr> iforh(size_t step, Expr ex) const;
2018-10-22 14:27:55 +02:00
2019-01-15 17:41:43 +01:00
template <class Expr>
ExpressionHolder<Expr> pifor(size_t step, Expr ex) const;
2018-10-22 14:27:55 +02:00
};
2018-10-27 14:58:34 +02:00
2018-10-22 14:27:55 +02:00
// NOT THREAD SAVE!!
template <class EC>
2018-10-22 14:27:55 +02:00
class DynamicRangeFactory : public RangeFactoryBase
{
public:
typedef DynamicRange<EC> oType;
2018-10-22 14:27:55 +02:00
DynamicRangeFactory();
template <class... RangeTypes>
DynamicRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
template <class... RangeTypes>
DynamicRangeFactory(std::shared_ptr<RangeTypes>... origs);
2019-02-13 21:59:13 +01:00
DynamicRangeFactory(const vector<std::shared_ptr<RangeBase>>& origs);
2018-10-22 14:27:55 +02:00
template <class Range>
void append(std::shared_ptr<Range> r);
std::shared_ptr<RangeBase> create();
private:
2019-02-13 21:59:13 +01:00
std::shared_ptr<RangeBase> checkIfCreated(const vector<std::shared_ptr<RangeBase> >& pvec);
2018-10-22 14:27:55 +02:00
2019-02-13 21:59:13 +01:00
static std::map<std::shared_ptr<RangeBase>,vector<std::intptr_t> > mAleadyCreated;
2018-10-22 14:27:55 +02:00
bool mProductCreated = false;
};
2018-10-27 14:58:34 +02:00
template <class EC>
class DynamicRange : public RangeInterface<DynamicIndex<EC>>
2018-10-22 14:27:55 +02:00
{
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 DynamicIndex<EC> IndexType;
2018-10-22 14:27:55 +02:00
typedef DynamicRange RangeType;
2019-03-13 21:51:48 +01:00
typedef std::vector<char> MetaType;
2018-10-22 14:27:55 +02:00
private:
DynamicRange() = default;
DynamicRange(const DynamicRange& in) = default;
2018-10-22 14:27:55 +02:00
template <class... RangeTypes>
DynamicRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
2018-10-22 14:27:55 +02:00
template <class... RangeTypes>
DynamicRange(std::shared_ptr<RangeTypes>... origs);
2018-10-22 14:27:55 +02:00
2019-02-13 21:59:13 +01:00
DynamicRange(const vector<std::shared_ptr<RangeBase>>& origs);
2018-10-22 14:27:55 +02:00
size_t mSize = 1;
bool mEmpty = true;
2019-02-13 21:59:13 +01:00
vector<std::shared_ptr<RangeBase> > mOrig;
2018-10-22 14:27:55 +02:00
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;
2018-11-07 10:58:07 +01:00
virtual DataHeader dataHeader() const final;
2018-10-22 14:27:55 +02:00
virtual std::string stringMeta(size_t pos) const final;
2019-02-13 21:59:13 +01:00
virtual vector<char> data() const final;
2018-10-22 14:27:55 +02:00
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
2019-02-13 21:59:13 +01:00
const vector<std::shared_ptr<RangeBase> >& orig() const;
2018-10-22 14:27:55 +02:00
void sreplace(const std::shared_ptr<RangeBase> in, size_t num);
bool isEmpty() const;
friend DynamicRangeFactory<EC>;
2018-10-22 14:27:55 +02:00
static DynamicRangeFactory<EC> factory()
{ return DynamicRangeFactory<EC>(); }
2018-10-22 14:27:55 +02:00
};
} // namespace MultiArrayTools
namespace MultiArrayHelper
{
using namespace MultiArrayTools;
template <class EC>
inline void resolveSetRange(std::shared_ptr<DynamicRange<EC>>& rp,
2019-02-13 21:59:13 +01:00
const vector<std::shared_ptr<RangeBase> >& orig,
size_t origpos, size_t size)
2018-10-22 14:27:55 +02:00
{
DynamicRangeFactory<EC> arf;
2018-10-22 14:27:55 +02:00
for(size_t op = origpos; op != origpos + size; ++op){
//VCHECK(op);
arf.append(orig[op]);
}
rp = std::dynamic_pointer_cast<DynamicRange<EC>>( arf.create() );
2018-10-22 14:27:55 +02:00
}
template <class EC>
2019-02-13 21:59:13 +01:00
inline void setRangeToVec(vector<std::shared_ptr<RangeBase> >& v,
std::shared_ptr<DynamicRange<EC>> r)
2018-10-22 14:27:55 +02:00
{
if(not r->isEmpty()){
2018-10-27 14:58:34 +02:00
for(size_t i = r->dim(); i != 0; --i){
2018-10-22 14:27:55 +02:00
v.insert(v.begin(), r->sub(i-1));
}
}
}
template <class EC>
inline size_t getStepSize(const DynamicIndex<EC>& ii, std::intptr_t j)
2018-10-29 14:19:42 +01:00
{
size_t ss = 0;
size_t sx = 1;
for(size_t k = ii.dim(); k != 0; --k){
const size_t i = k-1;
2018-10-29 14:19:42 +01:00
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;
}
2018-10-22 14:27:55 +02:00
}
//#include "dynamic_range.cc.h"
2018-10-22 14:27:55 +02:00
#endif