Merge branch 'dev' of git.f3l.de:chizeta/cnorxz into dev

This commit is contained in:
Christian Zimmermann 2023-02-21 16:52:21 +01:00
commit 9a7daabd24
13 changed files with 471 additions and 492 deletions

View file

@ -55,6 +55,12 @@ namespace CNORXZ
} );
}
template <typename T, typename S>
String ToString<std::pair<T,S>>::func(const std::pair<T,S>& p)
{
return String("(") + toString(p.first) + "," + toString(p.second) + ")";
}
template <typename T>
String toString(const T& a)
{

View file

@ -36,6 +36,12 @@ namespace CNORXZ
static String func(const Tuple<Ts...>& t);
};
template <typename T, typename S>
struct ToString<std::pair<T,S>>
{
static String func(const std::pair<T,S>& t);
};
template <>
struct ToString<DType>
{

View file

@ -0,0 +1,211 @@
#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
{
return ind;
}
template <class Index>
template <class I>
decltype(auto) PIndex<Index>::slice(const Sptr<I>& ind) const
{
if(ind != nullptr){
if(ind->dim() != 0){
return Sptr<PIndex<Index>>();
}
}
return std::make_shared<PIndex<Index>>(*this);
}
template <class Index>
template <class Xpr, class F>
decltype(auto) PIndex<Index>::ifor(const Xpr& xpr, F&& f) const
{
return PFor<0,0,Xpr,F>(this->lmax().val(), this->id(), mOrig->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__
#define __cxz_subrange_h__
#ifndef __cxz_prange_h__
#define __cxz_prange_h__
#include <cstdlib>
#include <vector>
#include <memory>
#include <map>
#include "base_def.h"
#include "base/base.h"
#include "ranges/index_base.h"
#include "ranges/range_base.h"
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
#include "xpr/for_type.h"
#include "xpr/xpr.h"
namespace CNORXZ
{
namespace
{
using namespace CNORXZInternal;
}
template <class Index>
class SubIndex : public IndexInterface<SubIndex<Index>,typename Index::MetaType>
class PIndex : public IndexInterface<Index,typename Index::MetaType>
{
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 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; }
static constexpr size_t totalDim() { return 1; }
static constexpr size_t sDim() { return 1; }
PIndex& operator=(SizeT lexpos);
PIndex& operator++();
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);
SubIndex& operator++();
SubIndex& operator--();
SizeT dim() const;
Sptr<RangeType> range() const;
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;
MetaType meta() const;
const MetaType* metaPtr() const;
SubIndex& at(const MetaType& metaPos);
size_t posAt(const MetaType& metaPos) const;
String stringMeta() const;
const MetaT& meta() const;
PIndex& at(const MetaT& metaPos);
decltype(auto) xpr(const Sptr<PIndex<Index>>& _this) const;
bool isMeta(const MetaType& metaPos) const;
template <class I>
decltype(auto) format(const Sptr<I>& ind) const;
size_t dim(); // = 1
bool last();
bool first();
template <class I>
decltype(auto) slice(const Sptr<I>& ind) const;
std::shared_ptr<RangeType> range();
template <class Xpr, class F>
decltype(auto) ifor(const Xpr& xpr, F&& f) const;
template <size_t N>
void getPtr();
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!!)
PIndex& operator()();
PIndex& operator()(const Sptr<Index>& i);
const Sptr<Index>& orig() const;
private:
std::shared_ptr<RangeType> mExplicitRangePtr;
//const U* mMetaPtr;
std::shared_ptr<Index> mFullIndex;
Sptr<RangeType> mRangePtr;
Sptr<Index> mOrig;
void mkPos();
};
template <class Range>
class SubRangeFactory : public RangeFactoryBase
class PRangeFactory : public RangeFactoryBase
{
public:
typedef SubRange<Range> oType;
PRangeFactory(const Sptr<Range>& range, const Vector<SizeT>& _parts);
SubRangeFactory() = delete;
SubRangeFactory(const std::shared_ptr<Range>& fullRange,
const vector<size_t>& subset);
std::shared_ptr<RangeBase> create();
private:
PRangeFactory() = default;
virtual void make() override final;
RangePtr mRef;
};
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:
typedef RangeBase RB;
typedef SubIndex<typename Range::IndexType> IndexType;
typedef SubRange RangeType;
typedef typename IndexType::MetaType MetaType;
typedef SubRangeFactory<Range> FType;
typedef PIndex<typename Range::IndexType> IndexType;
virtual size_t size() const final;
virtual size_t dim() const final;
friend PRangeFactory<Range>;
virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final;
virtual SizeT size() const override 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;
virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final;
RangePtr orig() const;
const Vector<SizeT>& parts() const;
RangePtr derive() const;
bool isMeta(const MetaType& metaPos) const;
private:
auto get(size_t pos) const
-> decltype(mFullRange->get(mSubSet[pos]));
size_t getMeta(const MetaType& metaPos) const;
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;
PRange() = delete;
PRange(const PRange& in) = delete;
PRange(const Sptr<Range>& range, const Vector<SizeT>& _parts);
Sptr<Range> mRange;
Vector<SizeT> mParts;
};
} // 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

View file

@ -158,10 +158,10 @@ namespace CNORXZ
{
if(ind != nullptr){
if(ind->dim() != 0) {
return Sptr<CIndex>();
return Sptr<UIndex<MetaType>>();
}
}
return std::make_shared<CIndex>(*this);
return std::make_shared<UIndex<MetaType>>(*this);
}
template <typename MetaType>
@ -233,7 +233,7 @@ namespace CNORXZ
{
std::sort(mSpace.begin(), mSpace.end(), std::less<MetaType>());
auto itdupl = std::adjacent_find(mSpace.begin(), mSpace.end());
CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << *itdupl);
CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << toString(*itdupl));
}

View file

@ -28,16 +28,16 @@ namespace CNORXZ
{
if constexpr(std::is_same<typename std::remove_reference<F>::type,NoF>::value){
for(SizeT i = 0; i != mSize; ++i){
const auto pos = last + mExt * UPos(i);
const auto pos = last + mExt( UPos(i) );
mXpr(pos);
}
}
else {
typedef typename
std::remove_reference<decltype(mXpr(last + mExt * UPos(0)))>::type OutT;
std::remove_reference<decltype(mXpr(last + mExt( UPos(0) )))>::type OutT;
auto o = OutT();
for(SizeT i = 0; i != mSize; ++i){
const auto pos = last + mExt * UPos(i);
const auto pos = last + mExt( UPos(i) );
mF(o, mXpr(pos));
}
return o;
@ -49,15 +49,15 @@ namespace CNORXZ
{
if constexpr(std::is_same<typename std::remove_reference<F>::type,NoF>::value){
for(SizeT i = 0; i != mSize; ++i){
const auto pos = mExt * UPos(i);
const auto pos = mExt( UPos(i) );
mXpr(pos);
}
}
else {
typedef typename std::remove_reference<decltype(mXpr(mExt * UPos(0)))>::type OutT;
typedef typename std::remove_reference<decltype(mXpr(mExt( UPos(0) )))>::type OutT;
auto o = OutT();
for(SizeT i = 0; i != mSize; ++i){
const auto pos = mExt * UPos(i);
const auto pos = mExt( UPos(i) );
mF(o, mXpr(pos));
}
return o;
@ -136,7 +136,7 @@ namespace CNORXZ
constexpr decltype(auto) SFor<N,L,Xpr,F>::exec(const PosT& last) const
{
constexpr SPos<I> i;
const auto pos = last + mExt * i;
const auto pos = last + mExt( i );
if constexpr(I < N-1){
return mF(mXpr(pos),exec<I+1>(last));
}
@ -150,7 +150,7 @@ namespace CNORXZ
constexpr decltype(auto) SFor<N,L,Xpr,F>::exec() const
{
constexpr SPos<I> i;
const auto pos = mExt * i;
const auto pos = mExt( i );
if constexpr(I < N-1){
return mF(mXpr(pos),exec<I+1>());
}
@ -164,7 +164,7 @@ namespace CNORXZ
inline void SFor<N,L,Xpr,F>::exec2(const PosT& last) const
{
constexpr SPos<I> i;
const auto pos = last + mExt * i;
const auto pos = last + mExt( i );
if constexpr(I < N-1){
mXpr(pos);
exec2<I+1>(last);
@ -180,7 +180,7 @@ namespace CNORXZ
inline void SFor<N,L,Xpr,F>::exec2() const
{
constexpr SPos<I> i;
const auto pos = mExt * i;
const auto pos = mExt( i );
if constexpr(I < N-1){
mXpr(pos);
exec2<I+1>();
@ -207,6 +207,94 @@ namespace CNORXZ
return SFor<N,L,Xpr>(id, xpr, NoF {});
}
/************
* PFor *
************/
template <SizeT L1, SizeT L2, class Xpr, class F>
constexpr PFor<L1,L2,Xpr,F>::PFor(SizeT size, const IndexId<L1>& id1, const IndexId<L2>& id2,
const SizeT* map, const Xpr& xpr, F&& f) :
mSize(size),
mId1(id1),
mId2(id2),
mXpr(xpr),
mExt1(mXpr.rootSteps(mId1)),
mExt2(mXpr.rootSteps(mId2)),
mPart(1, map),
mF(f)
{}
template <SizeT L1, SizeT L2, class Xpr, class F>
template <class PosT>
inline decltype(auto) PFor<L1,L2,Xpr,F>::operator()(const PosT& last) const
{
if constexpr(std::is_same<typename std::remove_reference<F>::type,NoF>::value){
for(SizeT i = 0; i != mSize; ++i){
const auto pos1 = last + mExt1( UPos(i) );
const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) );
mXpr(pos2);
}
}
else {
typedef typename
std::remove_reference<decltype(mXpr((last + mExt1( UPos(0) )) + mExt2( mPart( UPos(0) ) )))>::type OutT;
auto o = OutT();
for(SizeT i = 0; i != mSize; ++i){
const auto pos1 = last + mExt1( UPos(i) );
const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) );
mF(o, mXpr(pos2));
}
return o;
}
}
template <SizeT L1, SizeT L2, class Xpr, class F>
inline decltype(auto) PFor<L1,L2,Xpr,F>::operator()() const
{
if constexpr(std::is_same<typename std::remove_reference<F>::type,NoF>::value){
for(SizeT i = 0; i != mSize; ++i){
const auto pos1 = mExt1( UPos(i) );
const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) );
mXpr(pos2);
}
}
else {
typedef typename std::remove_reference<decltype(mExt1( UPos(0) ) + mExt2( mPart( UPos(0) ) ))>::type OutT;
auto o = OutT();
for(SizeT i = 0; i != mSize; ++i){
const auto pos1 = mExt1( UPos(i) );
const auto pos2 = pos1 + mExt2( mPart( UPos(i) ) );
mF(o, mXpr(pos2));
}
return o;
}
}
template <SizeT L1, SizeT L2, class Xpr, class F>
template <SizeT I>
inline decltype(auto) PFor<L1,L2,Xpr,F>::rootSteps(const IndexId<I>& id) const
{
return mXpr.rootSteps(id);
}
/*************************
* PFor (non-member) *
*************************/
template <SizeT L1, SizeT L2, class Xpr, class F>
constexpr decltype(auto) mkPFor(SizeT size, const IndexId<L1>& id1, const IndexId<L2>& id2,
const Xpr& xpr, F&& f)
{
return PFor<L1,L2,Xpr,F>(size, id1, id2, xpr, std::forward<F>(f));
}
template <SizeT L1, SizeT L2, class Xpr, class F>
constexpr decltype(auto) mkPFor(SizeT size, const IndexId<L1>& id1, const IndexId<L2>& id2,
const Xpr& xpr)
{
return PFor<L1,L2,Xpr>(size, id1, id2, xpr, NoF {});
}
/************
* TFor *
************/
@ -232,7 +320,7 @@ namespace CNORXZ
template <class PosT>
inline decltype(auto) TFor<L,Xpr,F>::operator()(const PosT& last) const
{
typedef typename std::remove_reference<decltype(mXpr(last + mExt * UPos(0)))>::type OutT;
typedef typename std::remove_reference<decltype(mXpr(last + mExt( UPos(0) )))>::type OutT;
int i = 0;
const int size = static_cast<int>(mSize);
Vector<OutT> ov;
@ -245,7 +333,7 @@ namespace CNORXZ
auto xpr = mXpr;
#pragma omp for
for(i = 0; i < size; i++){
const auto pos = last + mExt * UPos(i);
const auto pos = last + mExt( UPos(i) );
xpr(pos);
}
}
@ -262,7 +350,7 @@ namespace CNORXZ
template <SizeT L, class Xpr, class F>
inline decltype(auto) TFor<L,Xpr,F>::operator()() const
{
typedef typename std::remove_reference<decltype(mXpr(mExt * UPos(0)))>::type OutT;
typedef typename std::remove_reference<decltype(mXpr(mExt( UPos(0) )))>::type OutT;
int i = 0;
const int size = static_cast<int>(mSize);
Vector<OutT> ov;
@ -275,7 +363,7 @@ namespace CNORXZ
auto xpr = mXpr;
#pragma omp for
for(i = 0; i < size; i++){
const auto pos = mExt * UPos(i);
const auto pos = mExt( UPos(i) );
xpr(pos);
}
}

View file

@ -85,6 +85,43 @@ namespace CNORXZ
template <SizeT N, SizeT L, class Xpr>
constexpr decltype(auto) mkSFor(const IndexId<L>& id, const Xpr& xpr);
// partial for:
template <SizeT L1, SizeT L2, class Xpr, class F = NoF>
class PFor : public XprInterface<PFor<L1,L2,Xpr,F>>
{
public:
DEFAULT_MEMBERS(PFor);
constexpr PFor(SizeT size, const IndexId<L1>& id1, const IndexId<L2>& id2,
const SizeT* map, const Xpr& xpr, F&& f);
template <class PosT>
inline decltype(auto) operator()(const PosT& last) const;
inline decltype(auto) operator()() const;
template <SizeT I>
inline decltype(auto) rootSteps(const IndexId<I>& id) const;
private:
SizeT mSize = 0;
IndexId<L1> mId1;
IndexId<L2> mId2;
Xpr mXpr;
typedef decltype(mXpr.rootSteps(mId1)) XPosT1;
typedef decltype(mXpr.rootSteps(mId2)) XPosT2;
XPosT1 mExt1;
XPosT2 mExt2;
FPos mPart;
F mF;
};
template <SizeT L, class Xpr, class F>
constexpr decltype(auto) mkFor(SizeT size, const IndexId<L>& id, const Xpr& xpr, F&& f);
template <SizeT L, class Xpr>
constexpr decltype(auto) mkFor(SizeT size, const IndexId<L>& id, const Xpr& xpr);
// multi-threading
template <SizeT L, class Xpr, class F = NoF>
class TFor : public XprInterface<TFor<L,Xpr,F>>

View file

@ -37,7 +37,7 @@ namespace CNORXZ
template <typename... Ts>
Group& Group::addTable(const String& name, const ArrayBase<Tuple<Ts...>>& data,
const RangePtr& fields)
const Vector<String>& fnames)
{
CXZ_ASSERT(this->isOpen(), "tried to extend closed group");
Vector<String> nvec({name});
@ -46,7 +46,7 @@ namespace CNORXZ
mCont.extend(extr);
auto ii = mCont.begin();
ii.at(dvec); // 'at' returns YIndex&, so cannot use it inline...
auto tab = std::make_shared<STable<Ts...>>(name, this, fields);
auto tab = std::make_shared<STable<Ts...>>(name, this, fnames);
for(auto& d: data){
tab->appendRecord(d);
}

View file

@ -40,7 +40,7 @@ namespace CNORXZ
template <typename... Ts>
Group& addTable(const String& name, const ArrayBase<Tuple<Ts...>>& data,
const RangePtr& fields);
const Vector<String>& fnames);
protected:
MArray<ContentPtr> mCont;

View file

@ -18,13 +18,19 @@ namespace CNORXZ
}
template <typename... Ts>
STable<Ts...>::STable(const String& name, const ContentBase* _parent, const RangePtr& fields) :
STable<Ts...>::STable(const String& name, const ContentBase* _parent,
const Vector<String>& fnames) :
Table(name, _parent)
{
constexpr SizeT N = sizeof...(Ts);
if(mFields == nullptr){
CXZ_ASSERT(fields != nullptr, "field names have to be initialized");
mFields = fields;
CXZ_ASSERT(fnames.size() != 0, "field names have to be initialized");
Vector<FieldID> fields(fnames.size());
for(SizeT i = 0; i != fields.size(); ++i){
fields[i].first = i;
fields[i].second = fnames[i];
}
mFields = URangeFactory<FieldID>(fields).create();
}
CXZ_ASSERT(mFields->size() == sizeof...(Ts), "expected tuple of size = " << mFields->size()
<< ", got: " << sizeof...(Ts));

View file

@ -12,6 +12,8 @@ namespace CNORXZ
class Table : public ContentBase
{
public:
typedef std::pair<SizeT,String> FieldID;
DEFAULT_MEMBERS(Table);
Table(const String& name, const ContentBase* _parent);
~Table();
@ -44,7 +46,7 @@ namespace CNORXZ
{
public:
DEFAULT_MEMBERS(STable);
STable(const String& name, const ContentBase* _parent, const RangePtr& fields);
STable(const String& name, const ContentBase* _parent, const Vector<String>& fnames);
STable& appendRecord(const Tuple<Ts...>& t);
STable& appendRecord(const MArray<Tuple<Ts...>>& t);

View file

@ -20,11 +20,12 @@ namespace CNORXZ
SizeT typesize = 0;
H5TBget_field_info(mParent->id(), mName.c_str(), fieldsptr.data(), sizes.data(),
offsets.data(), &typesize);
Vector<String> fields(nfields);
Vector<FieldID> fields(nfields);
for(SizeT i = 0; i != nfields; ++i){
fields[i] = fieldsptr[i];
fields[i].first = i;
fields[i].second = fieldsptr[i];
}
mFields = URangeFactory<String>( std::move(fields) ).create();
mFields = URangeFactory<FieldID>( std::move(fields) ).create();
mSizes = MArray<SizeT>(mFields, std::move(sizes));
mOffsets = MArray<SizeT>(mFields, std::move(offsets));
this->open();
@ -87,7 +88,12 @@ namespace CNORXZ
Table& Table::initFieldNames(const Vector<String>& fnames)
{
CXZ_ASSERT(mFields == nullptr, "fields already initialized");
mFields = URangeFactory<String>(fnames).create();
Vector<FieldID> fields(fnames.size());
for(SizeT i = 0; i != fields.size(); ++i){
fields[i].first = i;
fields[i].second = fnames[i];
}
mFields = URangeFactory<FieldID>(fields).create();
return *this;
}
@ -96,9 +102,9 @@ namespace CNORXZ
const Int compress = 0;
mRecords = CRangeFactory(n).create();
Vector<const char*> fields(mFields->size());
auto fr = std::dynamic_pointer_cast<URange<String>>(mFields);
auto fr = std::dynamic_pointer_cast<URange<FieldID>>(mFields);
for(auto fi = fr->begin(); fi != fr->end(); ++fi){
fields[fi.lex()] = (*fi).c_str();
fields[fi.lex()] = (*fi).second.c_str();
}
const herr_t err = H5TBmake_table
(mName.c_str(), mParent->id(), mName.c_str(), mFields->size(), mRecords->size(), dsize,

View file

@ -40,7 +40,7 @@ namespace
{
mFileName = testh5file;
mGrps = { "gr1", "gr2" };
mFs = URangeFactory<String>(Vector<String>({"field1","second","real"})).create();
mFs = {"field1","second","real"};
Vector<Tuple<SizeT,Int,Double>> v
( { {0, -6, 3.141},
{3, -8, 0.789},
@ -55,7 +55,7 @@ namespace
String mFileName;
Vector<String> mGrps;
RangePtr mFs;
Vector<String> mFs;
MArray<Tuple<SizeT,Int,Double>> mTabA;
};