This commit is contained in:
Christian Zimmermann 2022-09-11 17:20:09 +02:00
parent bc4ebf317b
commit 72e898606f
3 changed files with 129 additions and 261 deletions

View file

@ -56,6 +56,60 @@ namespace CNORXZ
// definition: ranges/range_base.h // definition: ranges/range_base.h
class RangeBase; class RangeBase;
typedef Sptr<RangeBase> RangePtr;
// definition: ranges/index_base.h
template <class I, typename MetaType>
class IndexInterface;
template <class I, typename MetaType>
using IndexPtr = Sptr<IndexInterface<I,MetaType>>;
// definition: ranges/urange.h
template <typename Meta>
class URange; // generic simple range (uni-dimensional)
// definition: ranges/urange.h
template <typename Meta>
class UIndex;
// definition: ranges/crange.h
class CRange; // classic range, trivial meta data
// definition: ranges/crange.h
class CIndex;
// definition: ranges/srange.h
template <typename Meta, SizeT S>
class SRange; // generic static size range
// definition: ranges/srange.h
template <typename Meta, SizeT S>
class SIndex;
// definition: ranges/mrange.h
template <class... Ranges>
class MRange; // multi range
// definition: ranges/mrange.h
template <class... Indices>
class MIndex;
// definition: ranges/xindex.h
class XIndexBase; // dynamic index wrapper
// definition: ranges/yrange.h
class YRange; // dynamic multi range
// definition: ranges/yrange.h
class YIndex;
// definition: ranges/pindex.h
template <class Index>
class PIndex; // partial index (index over sub-ranges and permutations)
// there should be also a static analogue
/********************* /*********************
* derived types * * derived types *
*********************/ *********************/

View file

@ -22,6 +22,11 @@ namespace CNORXZ
I& operator=(SizeT pos) { return THIS() = pos; } I& operator=(SizeT pos) { return THIS() = pos; }
I& operator++() { return THIS()++; } I& operator++() { return THIS()++; }
I& operator--() { return THIS()--;} I& operator--() { return THIS()--;}
I operator+(Int n) const { return THIS() + n; }
I operator-(Int n) const { return THIS() - n; }
I& operator+=(Int n) { return THIS() += n; }
I& operator-=(Int n) { return THIS() -= n; }
Int operator-(const IndexInterface& i) const { return mPos - i.mPos; }
SizeT pos() const; SizeT pos() const;
SizeT max() const; SizeT max() const;
@ -40,7 +45,7 @@ namespace CNORXZ
Int mm(PtrId idxPtrNum) { return THIS().mm(idxPtrNum); } Int mm(PtrId idxPtrNum) { return THIS().mm(idxPtrNum); }
SizeT dim() const { return THIS().dim(); } SizeT dim() const { return THIS().dim(); }
RangePtr range() const { return mRangePtr; } auto range() const { return THIS().range(); }
SizeT getStepSize(SizeT n) const { return THIS().getStepSize(n); } SizeT getStepSize(SizeT n) const { return THIS().getStepSize(n); }
String stringMeta() const { return THIS().stringMeta(); } String stringMeta() const { return THIS().stringMeta(); }
@ -67,14 +72,18 @@ namespace CNORXZ
IndexInterface& operator=(IndexInterface&& in); IndexInterface& operator=(IndexInterface&& in);
IndexInterface(const RangePtr& range, SizeT pos); IndexInterface(const RangePtr& range, SizeT pos);
RangePtr mRangePtr = nullptr; IndexPtr mRel = nullptr;
SizeT mPos = 0; SizeT mPos = 0;
SizeT mMax = 0; SizeT mMax = 0;
PtrId mPtrId = 0; PtrId mPtrId = 0;
}; };
template <class Index, typename MetaType> // to define relative indices:
using IndexPtr = Sptr<IndexInterface<Index,MetaType>>; template <class I, typename MetaType>
IndexPtr<I,MetaType> operator+(const IndexPtr<I,MetaType>& i, Int n);
template <class I, typename MetaType>
IndexPtr<I,MetaType> operator-(const IndexPtr<I,MetaType>& i, Int n);
} }

View file

@ -1,270 +1,107 @@
// -*- C++ -*- // -*- C++ -*-
#ifndef __cxz_single_range_h__ #ifndef __cxz_urange_h__
#define __cxz_single_range_h__ #define __cxz_urange_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 "ranges/type_map.h"
#include "xfor/for_type.h" #include "xfor/for_type.h"
namespace CNORXZ namespace CNORXZ
{ {
namespace
{
using namespace CNORXZInternal;
}
template <typename MetaType>
template <typename U, SpaceType TYPE, size_t S> class UIndex : public IndexInterface<UIndex<MetaType>,MetaType>
class GenSingleIndex : public IndexInterface<GenSingleIndex<U,TYPE,S>,U>
{ {
public: public:
typedef IndexInterface<GenSingleIndex<U,TYPE,S>,U> IB; typedef IndexInterface<UIndex<MetaType>,MetaType> IB;
typedef U MetaType; typedef URange<MetaType> RangeType;
typedef GenSingleRange<U,TYPE,S> RangeType;
typedef GenSingleIndex IType;
GenSingleIndex(const std::shared_ptr<GenSingleRange<U,TYPE,S> >& range); UIndex(const RangePtr& range);
static constexpr IndexType sType() { return IndexType::SINGLE; } UIndex& operator=(SizeT pos);
static constexpr size_t totalDim() { return 1; } UIndex& operator++();
static constexpr size_t sDim() { return 1; } UIndex& operator--();
UIndex operator+(Int n) const;
UIndex operator-(Int n) const;
UIndex& operator+=(Int n);
UIndex& operator-=(Int n);
static constexpr SpaceType STYPE = TYPE; const MetaType& operator*() const;
static constexpr bool PARALLEL = true; const MetaType* operator->() const;
// ==== >>>>> STATIC POLYMORPHISM <<<<< ==== Int pp(PtrId idxPtrNum);
Int mm(PtrId idxPtrNum);
IndexType type() const; SizeT dim() const; // = 1
Sptr<RangeType> range() const;
SizeT getStepSize(SizeT n) const;
GenSingleIndex& operator=(size_t pos); String stringMeta() const;
GenSingleIndex& operator++(); MetaType meta() const;
GenSingleIndex& operator--(); UIndex& at(const MetaType& metaPos);
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
std::string stringMeta() const;
U meta() const;
const U* metaPtr() const;
GenSingleIndex& at(const U& metaPos);
size_t posAt(const U& metaPos) const;
bool isMeta(const U& metaPos) const;
size_t dim(); // = 1
bool last();
bool first();
std::shared_ptr<RangeType> range();
template <size_t N>
void getPtr();
size_t getStepSize(size_t n);
template <class Expr> template <class Expr>
auto ifor(size_t step, Expr ex) const auto ifor(SizeT step, Expr ex) const
-> For<GenSingleIndex<U,TYPE,S>,Expr>; -> For<UIndex<MetaType>,Expr>;
template <class Expr> template <class Expr>
auto iforh(size_t step, Expr ex) const auto iforh(SizeT step, Expr ex) const
-> For<GenSingleIndex<U,TYPE,S>,Expr,ForType::HIDDEN>; -> For<UIndex<MetaType>,Expr,ForType::HIDDEN>;
template <class Expr> template <class Expr>
auto pifor(size_t step, Expr ex) const auto pifor(SizeT step, Expr ex) const
-> PFor<GenSingleIndex<U,TYPE,S>,Expr>; -> PFor<UIndex<MetaType>,Expr>;
private: private:
std::shared_ptr<RangeType> mExplicitRangePtr; Sptr<RangeType> mRangePtr;
const U* mMetaPtr; const MetaType* mMetaPtr;
}; };
template <typename U, SpaceType TYPE, size_t S> template <typename MetaType>
class GenSingleRangeFactory : public RangeFactoryBase class URangeFactory : public RangeFactoryBase
{ {
public: public:
URangeFactory() = delete;
URangeFactory(const Vector<U>& space);
URangeFactory(Vector<U>&& space);
typedef GenSingleRange<U,TYPE,S> oType; protected:
void make();
GenSingleRangeFactory() = delete;
GenSingleRangeFactory(const vector<U>& space);
GenSingleRangeFactory(vector<U>&& space);
std::shared_ptr<RangeBase> create();
}; };
template <typename U> template <typename MetaType>
class MetaMap class URange : public RangeInterface<UIndex<MetaType>>
{
private:
std::map<U,size_t> mMap;
public:
MetaMap() = default;
MetaMap(const MetaMap& in) = default;
MetaMap(MetaMap&& in) = default;
MetaMap& operator=(const MetaMap& in) = default;
MetaMap& operator=(MetaMap&& in) = default;
MetaMap(const vector<U>& in)
{
for(size_t i = 0; i != in.size(); ++i){
mMap[in[i]] = i;
}
}
size_t at(const U& in) const
{
auto x = mMap.find(in);
if(x != mMap.end()){
return x->second;
}
else {
return mMap.size();
}
}
size_t count(const U& in) const { return mMap.count(in); }
};
template <size_t S>
struct CheckStatic
{
static constexpr size_t ISSTATIC = true;
static constexpr size_t SIZE = S;
};
template <>
struct CheckStatic<MUI>
{
static constexpr size_t ISSTATIC = false;
static constexpr size_t SIZE = MUI;
};
template <SpaceType TYPE>
struct CheckDefault
{
static constexpr size_t ISDEFAULT = false;
static constexpr size_t HASMETACONT = true;
};
template <>
struct CheckDefault<SpaceType::NONE>
{
static constexpr size_t ISDEFAULT = true;
static constexpr size_t HASMETACONT = false;
};
template <typename U>
struct ToCMeta
{
static inline size_t apply(char* target, const U& elem)
{
*reinterpret_cast<U*>(target) = elem;
return sizeof(U);
}
static inline size_t size(const U& elem)
{
return sizeof(U);
}
};
template <typename V>
struct ToCMeta<vector<V>>
{
static inline size_t apply(char* target, const vector<V>& elem)
{
size_t o = 0;
for(auto& e: elem){
o += ToCMeta<V>::apply(target+o, e);
}
return o;
}
static inline size_t size(const vector<V>& elem)
{
size_t out = 0;
for(auto& x: elem){
out += ToCMeta<V>::size(x);
}
return out;
}
};
template <typename U, SpaceType TYPE, size_t S>
class GenSingleRange : public RangeInterface<GenSingleIndex<U,TYPE,S> >
{ {
public: public:
typedef RangeBase RB; typedef RangeBase RB;
typedef GenSingleIndex<U,TYPE,S> IndexType; typedef UIndex<MetaType> IndexType;
typedef GenSingleRange RangeType; typedef URangeFactory<MetaType> FType;
typedef U MetaType;
typedef GenSingleRangeFactory<U,TYPE,S> FType;
virtual size_t size() const final; virtual SizeT size() const final;
virtual size_t dim() const final; virtual SizeT dim() const final;
virtual SpaceType spaceType() const final; virtual std::string stringMeta(SizeT pos) const final;
virtual DataHeader dataHeader() const final; const MetaType& get(SizeT pos) const;
SizeT getMeta(const MetaType& metaPos) const;
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;
bool isMeta(const U& metaPos) const;
const U& get(size_t pos) const;
size_t getMeta(const U& metaPos) const;
virtual IndexType begin() const final; virtual IndexType begin() const final;
virtual IndexType end() const final; virtual IndexType end() const final;
friend GenSingleRangeFactory<U,TYPE,S>; friend URangeFactory<MetaType>;
static GenSingleRangeFactory<U,TYPE,S> factory()
{
static_assert( not CheckDefault<TYPE>::HASMETACONT,
"asked for default factory for meta data containing range" );
return GenSingleRangeFactory<U,TYPE,S>(vector<U>());
}
static constexpr bool defaultable = CheckDefault<TYPE>::ISDEFAULT;
static constexpr size_t ISSTATIC = CheckStatic<S>::ISSTATIC;
static constexpr size_t SIZE = CheckStatic<S>::SIZE;
static constexpr bool HASMETACONT = CheckDefault<TYPE>::HASMETACONT;
protected: protected:
GenSingleRange() = delete; URange() = delete;
GenSingleRange(const GenSingleRange& in) = delete; URange(const URange& in) = delete;
URange(const vector<MetaType>& space);
URange(vector<MetaType>&& space);
GenSingleRange(const vector<U>& space); Vector<MetaType> mSpace; // SORTED!!!
GenSingleRange(vector<U>&& space);
vector<U> mSpace;
MetaMap<U> mMSpace;
}; };
template <typename U, SpaceType TYPE>
using SingleRange = GenSingleRange<U,TYPE,MUI>;
template <typename U, SpaceType TYPE>
using SingleIndex = GenSingleIndex<U,TYPE,MUI>;
template <typename U, SpaceType TYPE>
using SingleRangeFactory = GenSingleRangeFactory<U,TYPE,MUI>;
} }
/* ========================= * /* ========================= *
@ -273,41 +110,9 @@ namespace CNORXZ
namespace CNORXZ namespace CNORXZ
{ {
/****************** /*****************
* GenSingleIndex * * UIndex *
******************/ *****************/
template <bool HASMETACONT>
struct MetaPtrHandle
{
template <class Range>
static const typename Range::MetaType* set(Range* r)
{
return &r->get(0);
}
template <typename U, class Range>
static inline U getMeta(U* metaPtr, size_t pos, std::shared_ptr<Range> r)
{
return metaPtr[pos];
}
};
template <>
struct MetaPtrHandle<false>
{
template <class Range>
static const typename Range::MetaType* set(Range* r)
{
return nullptr;
}
template <typename U, class Range>
static inline U getMeta(U* metaPtr, size_t pos, std::shared_ptr<Range> r)
{
return r->get(pos);
}
};
template <typename U, SpaceType TYPE, size_t S> template <typename U, SpaceType TYPE, size_t S>
GenSingleIndex<U,TYPE,S>::GenSingleIndex(const std::shared_ptr<GenSingleRange<U,TYPE,S> >& range) : GenSingleIndex<U,TYPE,S>::GenSingleIndex(const std::shared_ptr<GenSingleRange<U,TYPE,S> >& range) :