WIP: prange

This commit is contained in:
Christian Zimmermann 2023-01-28 18:34:24 +01:00
parent f5f66461f7
commit 792c3bd84e
2 changed files with 283 additions and 459 deletions

View file

@ -0,0 +1,207 @@
#ifndef __cxz_prange_cc_h__
#define __cxz_prange_cc_h__
#include "prange.h"
namespace CNORXZ
{
/**************
* PIndex *
**************/
template <class Index>
PIndex<Index>::PIndex(const RangePtr& range, SizeT pos) :
IndexInterface<Index>(pos),
mRangePtr(rangeCast<RangeType>(range)),
mIndex(mRangePtr->orig(),mRangePtr->parts()[pos])
{}
template <class Index>
PIndex<Index>& PIndex<Index>::operator=(SizeT lexpos)
{
IB::mPos = lexpos;
*mOrig = mRangePtr->parts()[IB::mPos];
return *this;
}
template <class Index>
PIndex<Index>& PIndex<Index>::operator++()
{
++IB::mPos;
*mOrig = mRangePtr->parts()[IB::mPos];
return *this;
}
template <class Index>
PIndex<Index>& PIndex<Index>::operator--()
{
--IB::mPos;
*mOrig = mRangePtr->parts()[IB::mPos];
return *this;
}
template <class Index>
PIndex<Index> PIndex<Index>::operator+(Int n) const
{
return PIndex(mRangePtr, IB::mPos + n);
}
template <class Index>
PIndex<Index> PIndex<Index>::operator-(Int n) const
{
return PIndex(mRangePtr, IB::mPos - n);
}
template <class Index>
PIndex<Index>& PIndex<Index>::operator+=(Int n)
{
IB::mPos += n;
*mOrig = mRangePtr->parts()[IB::mPos];
return *this;
}
template <class Index>
PIndex<Index>& PIndex<Index>::operator-=(Int n)
{
IB::mPos -= n;
*mOrig = mRangePtr->parts()[IB::mPos];
return *this;
}
template <class Index>
SizeT PIndex<Index>::lex() const
{
return IB::mPos;
}
template <class Index>
UPos PIndex<Index>::pmax() const
{
return UPos(mRangePtr->size());
}
template <class Index>
UPos PIndex<Index>::lmax() const
{
return UPos(mRangePtr->size());
}
template <class Index>
IndexId<0> PIndex<Index>::id() const
{
return IndexId<0>(this->ptrId());
}
template <class Index>
const typename PIndex<Index>::MetaType& PIndex<Index>::operator*() const
{
return **mOrig;
}
template <class Index>
SizeT PIndex<Index>::dim() const
{
return 1;
}
template <class Index>
Sptr<RangeType> PIndex<Index>::range() const
{
return mRangePtr;
}
template <class Index>
template <SizeT I>
UPos PIndex<Index>::stepSize(const IndexId<I>& id) const
{
if(id == this->id()){
return UPos(1);
}
else {
return mOrig->stepSize(id);
}
}
template <class Index>
String PIndex<Index>::stringMeta() const
{
return mOrig->stringMeta();
}
template <class Index>
const MetaT& PIndex<Index>::meta() const
{
return mOrig->meta();
}
template <class Index>
PIndex& PIndex<Index>::at(const MetaT& metaPos)
{
mOrig->at(metaPos);
mkPos();
return *this;
}
template <class Index>
decltype(auto) PIndex<Index>::xpr(const Sptr<PIndex<Index>>& _this) const
{
return poperation( mOrig->xpr(mOrig), mRangePtr->parts(), _this );
}
template <class Index>
template <class I>
decltype(auto) PIndex<Index>::format(const Sptr<I>& ind) const
{
/*!!!*/
}
template <class Index>
template <class I>
decltype(auto) PIndex<Index>::slice(const Sptr<I>& ind) const
{
/*!!!*/
}
template <class Index>
template <class Xpr, class F>
decltype(auto) PIndex<Index>::ifor(const Xpr& xpr, F&& f) const
{
/*return For<0,Xpr,F>(this->pmax().val(), this->id(), xpr, std::forward<F>(f));*/
/*!!!!!*/
}
template <class Index>
PIndex<Index>& PIndex<Index>::operator()()
{
mkPos()
return *this;
}
template <class Index>
PIndex<Index>& PIndex<Index>::operator()(const Sptr<Index>& i)
{
mOrig = i;
mkPos();
return *this;
}
/************************
* PIndex (private) *
************************/
template <class Index>
void PIndex<Index>::mkPos()
{
const SizeT opos = mOrig->lex();
IB::mPos = 0;
for(const auto& x: mRangePtr->parts()){
if(x == opos){
return *this;
}
++IB::mPos;
}
CXZ_ERROR("meta position '" << metaPos << "' not part of range");
}
}

View file

@ -1,498 +1,115 @@
#ifndef __cxz_subrange_h__ #ifndef __cxz_prange_h__
#define __cxz_subrange_h__ #define __cxz_prange_h__
#include <cstdlib> #include "base/base.h"
#include <vector>
#include <memory>
#include <map>
#include "base_def.h"
#include "ranges/index_base.h" #include "ranges/index_base.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/x_to_string.h" #include "xpr/xpr.h"
#include "ranges/type_map.h"
#include "xpr/for_type.h"
namespace CNORXZ namespace CNORXZ
{ {
namespace
{
using namespace CNORXZInternal;
}
template <class Index> template <class Index>
class SubIndex : public IndexInterface<SubIndex<Index>,typename Index::MetaType> class PIndex : public IndexInterface<Index,typename Index::MetaType>
{ {
public: public:
typedef IndexInterface<SubIndex<Index>,typename Index::MetaType> IB; typedef public IndexInterface<Index,typename Index::MetaType> IB;
typedef PRange<typename Index::RangeType> RangeType;
typedef typename Index::MetaType MetaType; typedef typename Index::MetaType MetaType;
typedef SubRange<typename Index::RangeType> RangeType;
typedef SubIndex IType;
SubIndex(const std::shared_ptr<RangeType>& range); PIndex(const RangePtr& range, SizeT pos = 0);
static constexpr IndexType sType() { return IndexType::SINGLE; } PIndex& operator=(SizeT lexpos);
static constexpr size_t totalDim() { return 1; } PIndex& operator++();
static constexpr size_t sDim() { return 1; } PIndex& operator--();
PIndex operator+(Int n) const;
PIndex operator-(Int n) const;
PIndex& operator+=(Int n);
PIndex& operator-=(Int n);
static constexpr SpaceType STYPE = Index::STYPE; SizeT lex() const;
UPos pmax() const;
UPos lmax() const;
IndexId<0> id() const;
IndexType type() const; const MetaT& operator*() const;
SubIndex& operator=(size_t pos); SizeT dim() const;
SubIndex& operator++(); Sptr<RangeType> range() const;
SubIndex& operator--();
SubIndex& operator()(const std::shared_ptr<Index>& ind); // set full index template <SizeT I>
UPos stepSize(const IndexId<I>& id) const;
std::string stringMeta() const; String stringMeta() const;
MetaType meta() const; const MetaT& meta() const;
const MetaType* metaPtr() const; PIndex& at(const MetaT& metaPos);
SubIndex& at(const MetaType& metaPos); decltype(auto) xpr(const Sptr<PIndex<Index>>& _this) const;
size_t posAt(const MetaType& metaPos) const;
bool isMeta(const MetaType& metaPos) const; template <class I>
decltype(auto) format(const Sptr<I>& ind) const;
size_t dim(); // = 1 template <class I>
bool last(); decltype(auto) slice(const Sptr<I>& ind) const;
bool first();
std::shared_ptr<RangeType> range(); template <class Xpr, class F>
decltype(auto) ifor(const Xpr& xpr, F&& f) const;
template <size_t N> PIndex& operator()();
void getPtr(); PIndex& operator()(const Sptr<Index>& i);
const Sptr<Index>& orig() const;
size_t getStepSize(size_t n);
template <class Expr>
auto ifor(size_t step, Expr ex) const
-> For<SubIndex<Index>,SubExpr<Index,Expr>>;
template <class Expr>
auto iforh(size_t step, Expr ex) const
-> For<SubIndex<Index>,SubExpr<Index,Expr>,ForType::HIDDEN>;
template <class Expr>
auto pifor(size_t step, Expr ex) const
-> decltype(ifor(step, ex)); // no multithreading here (check!!)
private: private:
std::shared_ptr<RangeType> mExplicitRangePtr; Sptr<RangeType> mRangePtr;
//const U* mMetaPtr; Sptr<Index> mOrig;
std::shared_ptr<Index> mFullIndex;
void mkPos();
}; };
template <class Range> template <class Range>
class SubRangeFactory : public RangeFactoryBase class PRangeFactory : public RangeFactoryBase
{ {
public: public:
typedef SubRange<Range> oType; PRangeFactory(const Sptr<Range>& range, const Vector<SizeT>& _parts);
SubRangeFactory() = delete; private:
SubRangeFactory(const std::shared_ptr<Range>& fullRange, PRangeFactory() = default;
const vector<size_t>& subset); virtual void make() override final;
std::shared_ptr<RangeBase> create();
RangePtr mRef;
}; };
template <class Range> template <class Range>
class SubRange : public RangeInterface<SubIndex<typename Range::IndexType>> class PRange : public RangeInterface<PRange<Range>>
{ {
private:
SubRange() = delete;
SubRange(const SubRange& in) = delete;
SubRange(const std::shared_ptr<Range>& fullRange, const vector<size_t>& subset);
std::shared_ptr<Range> mFullRange;
vector<size_t> mSubSet;
public: public:
typedef RangeBase RB; typedef RangeBase RB;
typedef SubIndex<typename Range::IndexType> IndexType; typedef PIndex<typename Range::IndexType> IndexType;
typedef SubRange RangeType;
typedef typename IndexType::MetaType MetaType;
typedef SubRangeFactory<Range> FType;
virtual size_t size() const final; friend PRangeFactory<Range>;
virtual size_t dim() const final;
virtual SpaceType spaceType() const final; virtual SizeT size() const override final;
virtual DataHeader dataHeader() const final; virtual SizeT dim() const override final;
virtual String stringMeta(SizeT pos) const override final;
virtual const TypeInfo& type() const override final;
virtual const TypeInfo& metaType() const override final;
virtual RangePtr extend(const RangePtr& r) const override final;
virtual vector<size_t> typeNum() const final; RangePtr orig() const;
virtual size_t cmeta(char* target, size_t pos) const final; const Vector<SizeT>& parts() const;
virtual size_t cmetaSize() const final; RangePtr derive() const;
virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final;
bool isMeta(const MetaType& metaPos) const; private:
auto get(size_t pos) const PRange() = delete;
-> decltype(mFullRange->get(mSubSet[pos])); PRange(const PRange& in) = delete;
size_t getMeta(const MetaType& metaPos) const; PRange(const Sptr<Range>& range, const Vector<SizeT>& _parts);
virtual IndexType begin() const final;
virtual IndexType end() const final;
std::shared_ptr<Range> fullRange() const;
const vector<size_t>& subset() const;
std::shared_ptr<SingleRange<MetaType,SpaceType::ANY>> outRange() const;
friend SubRangeFactory<Range>;
static constexpr bool defaultable = false;
static constexpr size_t ISSTATIC = 0;
static constexpr size_t SIZE = -1;
static constexpr bool HASMETACONT = false;
Sptr<Range> mRange;
Vector<SizeT> mParts;
}; };
} // namespace CNORXZ } // namespace CNORXZ
namespace CNORXZ
{
/*****************
* SubIndex *
*****************/
template <class Index>
SubIndex<Index>::SubIndex(const std::shared_ptr<RangeType>& range) :
IndexInterface<SubIndex<Index>,typename Index::MetaType>(range, 0),
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr))
{
mFullIndex = std::make_shared<Index>(mExplicitRangePtr->fullRange());
}
template <class Index>
IndexType SubIndex<Index>::type() const
{
return IndexType::SINGLE;
}
template <class Index>
SubIndex<Index>& SubIndex<Index>::operator=(size_t pos)
{
IB::mPos = pos;
(*mFullIndex) = mExplicitRangePtr->subset()[IB::mPos];
return *this;
}
template <class Index>
SubIndex<Index>& SubIndex<Index>::operator++()
{
++IB::mPos;
(*mFullIndex) = mExplicitRangePtr->subset()[IB::mPos];
return *this;
}
template <class Index>
SubIndex<Index>& SubIndex<Index>::operator--()
{
--IB::mPos;
(*mFullIndex) = mExplicitRangePtr->subset()[IB::mPos];
return *this;
}
template <class Index>
SubIndex<Index>& SubIndex<Index>::operator()(const std::shared_ptr<Index>& ind)
{
assert(mFullIndex->range() == ind->range());
mFullIndex = ind;
return *this;
}
template <class Index>
int SubIndex<Index>::pp(std::intptr_t idxPtrNum)
{
++(*this);
return 1;
}
template <class Index>
int SubIndex<Index>::mm(std::intptr_t idxPtrNum)
{
--(*this);
return 1;
}
template <class Index>
std::string SubIndex<Index>::stringMeta() const
{
return std::dynamic_pointer_cast<SingleRange<MetaType,STYPE> const>( IB::mRangePtr )->stringMeta(IB::mPos);
}
template <class Index>
typename SubIndex<Index>::MetaType SubIndex<Index>::meta() const
{
MetaType* x = nullptr;
return MetaPtrHandle<SubIndex<Index>::RangeType::HASMETACONT>::getMeta
( x, IB::mPos, mExplicitRangePtr );
}
template <class Index>
const typename SubIndex<Index>::MetaType* SubIndex<Index>::metaPtr() const
{
assert(0); // not sure where it is used
return mFullIndex->metaPtr();
}
template <class Index>
SubIndex<Index>& SubIndex<Index>::at(const MetaType& metaPos)
{
(*this) = mExplicitRangePtr->getMeta( metaPos );
return *this;
}
template <class Index>
size_t SubIndex<Index>::posAt(const MetaType& metaPos) const
{
return mExplicitRangePtr->getMeta( metaPos );
}
template <class Index>
bool SubIndex<Index>::isMeta(const MetaType& metaPos) const
{
return mExplicitRangePtr->isMeta( metaPos );
}
template <class Index>
size_t SubIndex<Index>::dim()
{
return 1;
}
template <class Index>
bool SubIndex<Index>::last()
{
return IB::mPos == IB::mMax - 1;
}
template <class Index>
bool SubIndex<Index>::first()
{
return IB::mPos == 0;
}
template <class Index>
std::shared_ptr<typename SubIndex<Index>::RangeType> SubIndex<Index>::range()
{
return mExplicitRangePtr;
}
template <class Index>
template <size_t N>
void SubIndex<Index>::getPtr() {}
template <class Index>
size_t SubIndex<Index>::getStepSize(size_t n)
{
return 1;
}
template <class Index>
template <class Expr>
auto SubIndex<Index>::ifor(size_t step, Expr ex) const
-> For<SubIndex<Index>,SubExpr<Index,Expr>>
{
return For<SubIndex<Index>,SubExpr<Index,Expr>>
(this, step, SubExpr<Index,Expr>
( mFullIndex, reinterpret_cast<std::intptr_t>(this),
&mExplicitRangePtr->subset(), ex ) );
}
template <class Index>
template <class Expr>
auto SubIndex<Index>::iforh(size_t step, Expr ex) const
-> For<SubIndex<Index>,SubExpr<Index,Expr>,ForType::HIDDEN>
{
return For<SubIndex<Index>,SubExpr<Index,Expr>,ForType::HIDDEN>
(this, step, SubExpr<Index,Expr>
( mFullIndex, reinterpret_cast<std::intptr_t>(this),
&mExplicitRangePtr->subset(), ex ) );
}
template <class Index>
template <class Expr>
auto SubIndex<Index>::pifor(size_t step, Expr ex) const
-> decltype(ifor(step, ex))
{
return ifor(step, ex);
}
/************************
* SubRangeFactory *
************************/
template <class Range>
SubRangeFactory<Range>::SubRangeFactory(const std::shared_ptr<Range>& fullRange,
const vector<size_t>& subset)
{
mProd = std::shared_ptr<oType>( new SubRange<Range>( fullRange, subset ) );
}
template <class Range>
std::shared_ptr<RangeBase> SubRangeFactory<Range>::create()
{
setSelf();
return mProd;
}
/*****************
* SubRange *
*****************/
template <class Range>
SubRange<Range>::SubRange(const std::shared_ptr<Range>& fullRange,
const vector<size_t>& subset) :
RangeInterface<SubIndex<typename Range::IndexType>>(),
mFullRange(fullRange), mSubSet(subset) {}
template <class Range>
size_t SubRange<Range>::size() const
{
return mSubSet.size();
}
template <class Range>
size_t SubRange<Range>::dim() const
{
return 1;
}
template <class Range>
SpaceType SubRange<Range>::spaceType() const
{
return SpaceType::ANY;
}
template <class Range>
DataHeader SubRange<Range>::dataHeader() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::ANY );
h.metaSize = metaSize(mSubSet);
h.metaType = NumTypeMap<MetaType>::num();
h.multiple = 0;
return h;
}
template <class Range>
vector<size_t> SubRange<Range>::typeNum() const
{
return mFullRange->typeNum();
}
template <class Range>
size_t SubRange<Range>::cmeta(char* target, size_t pos) const
{
return mFullRange->cmeta(target, mSubSet[pos]);
}
template <class Range>
size_t SubRange<Range>::cmetaSize() const
{
return mFullRange->cmetaSize();
}
template <class Range>
std::string SubRange<Range>::stringMeta(size_t pos) const
{
return xToString(get(pos));
}
template <class Range>
vector<char> SubRange<Range>::data() const
{
DataHeader h = dataHeader();
vector<char> out;
out.reserve(h.metaSize + sizeof(DataHeader));
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
vector<MetaType> subvec(mSubSet.size());
size_t i = 0;
for(auto& x: mSubSet){
subvec[i++] = mFullRange->get(x);
}
stringCat(out, subvec);
return out;
}
template <class Range>
bool SubRange<Range>::isMeta(const MetaType& metaPos) const
{
for(size_t i = 0; i != size(); ++i){
if(get(i) == metaPos){
return true;
}
}
return false;
}
template <class Range>
auto SubRange<Range>::get(size_t pos) const
-> decltype(mFullRange->get(mSubSet[pos]))
{
return mFullRange->get( mSubSet[pos] );
}
template <class Range>
size_t SubRange<Range>::getMeta(const MetaType& metaPos) const
{
for(size_t i = 0; i != size(); ++i){
if(get(i) == metaPos){
return i;
}
}
return -1;
}
template <class Range>
typename SubRange<Range>::IndexType SubRange<Range>::begin() const
{
SubRange<Range>::IndexType i( std::dynamic_pointer_cast<SubRange<Range>>
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0;
return i;
}
template <class Range>
typename SubRange<Range>::IndexType SubRange<Range>::end() const
{
SubRange<Range>::IndexType i( std::dynamic_pointer_cast<SubRange<Range>>
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = size();
return i;
}
template <class Range>
std::shared_ptr<Range> SubRange<Range>::fullRange() const
{
return mFullRange;
}
template <class Range>
const vector<size_t>& SubRange<Range>::subset() const
{
return mSubSet;
}
template <class Range>
std::shared_ptr<SingleRange<typename SubRange<Range>::MetaType,SpaceType::ANY>> SubRange<Range>::outRange() const
{
vector<MetaType> ometa(mSubSet.size());
size_t i = 0;
for(auto& x: mSubSet){
ometa[i++] = mFullRange->get(x);
}
SingleRangeFactory<MetaType,SpaceType::ANY> srf(ometa);
return std::dynamic_pointer_cast<SingleRange<MetaType,SpaceType::ANY>>( srf.create() );
}
}
#endif #endif