cnorxz/orig/include/ranges/anonymous_range.h

262 lines
7.3 KiB
C
Raw Normal View History

2017-05-31 16:44:28 +02:00
// -*- C++ -*-
#ifndef __cxz_anonymous_range_h__
#define __cxz_anonymous_range_h__
2017-05-31 16:44:28 +02:00
#include <cstdlib>
#include <map>
2018-07-16 00:12:05 +02:00
#include "rbase_def.h"
2017-12-17 17:40:55 +01:00
#include "ranges/range_base.h"
#include "ranges/rpheader.h"
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
2017-05-31 16:44:28 +02:00
2021-07-28 20:29:56 +02:00
namespace CNORXZ
2017-05-31 16:44:28 +02:00
{
typedef GenSingleIndex<size_t,SpaceType::ANON,MUI> AnonymousIndex;
2018-10-29 14:19:42 +01:00
//template <class R>
//using SIZET = size_t;
typedef GenSingleRange<size_t,SpaceType::ANON,MUI> AnonymousRange;
2018-07-16 15:55:55 +02:00
// NOT THREAD SAVE!!
class AnonymousRangeFactory : public RangeFactoryBase
{
public:
2017-12-05 17:31:57 +01:00
typedef AnonymousRange oType;
2018-07-16 15:55:55 +02:00
AnonymousRangeFactory();
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
AnonymousRangeFactory(std::shared_ptr<RangeTypes>... origs);
2019-02-13 21:59:13 +01:00
AnonymousRangeFactory(const vector<std::shared_ptr<RangeBase>>& origs);
template <class Range>
void append(std::shared_ptr<Range> r);
std::shared_ptr<RangeBase> create();
private:
2018-07-16 14:44:55 +02:00
2019-02-13 21:59:13 +01:00
std::shared_ptr<RangeBase> checkIfCreated(const vector<std::shared_ptr<RangeBase> >& pvec);
2019-02-13 21:59:13 +01:00
static std::map<std::shared_ptr<RangeBase>,vector<std::intptr_t> > mAleadyCreated;
2018-07-16 14:44:55 +02:00
bool mProductCreated = false;
};
2018-07-16 14:44:55 +02:00
2018-07-16 15:55:55 +02:00
template <>
class GenSingleRange<size_t,SpaceType::ANON,MUI> : public RangeInterface<AnonymousIndex>
2017-05-31 16:44:28 +02:00
{
public:
2018-07-16 15:55:55 +02:00
static constexpr bool defaultable = true;
static constexpr size_t ISSTATIC = 0;
static constexpr size_t SIZE = MUI;
2018-07-16 15:55:55 +02:00
static constexpr bool HASMETACONT = false;
typedef RangeBase RB;
typedef typename RangeInterface<AnonymousIndex>::IndexType IndexType;
typedef GenSingleRange<size_t,SpaceType::ANON,MUI> RangeType;
2018-07-16 15:55:55 +02:00
typedef size_t MetaType;
typedef AnonymousRangeFactory FType;
virtual size_t size() const final;
virtual size_t dim() const final;
size_t anonymousDim() const;
2017-12-05 17:31:57 +01:00
size_t get(size_t pos) const;
size_t getMeta(size_t metaPos) const;
virtual IndexType begin() const final;
virtual IndexType end() const final;
2018-07-28 15:27:11 +02:00
virtual SpaceType spaceType() const final;
2018-11-07 10:58:07 +01:00
virtual DataHeader dataHeader() const final;
virtual vector<size_t> typeNum() const final;
2019-03-14 14:20:38 +01:00
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;
2019-02-13 21:59:13 +01:00
virtual 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>
2018-07-16 15:55:55 +02:00
std::shared_ptr<MultiRange<Ranges...> > scast(SIZET<Ranges>... sizes) const; // save cast
2018-08-06 19:20:57 +02:00
2019-02-13 21:59:13 +01:00
const vector<std::shared_ptr<RangeBase> >& orig() const;
std::shared_ptr<AnonymousRange> sreplace(const std::shared_ptr<RangeBase> in, size_t num) const;
2019-02-13 21:59:13 +01:00
std::shared_ptr<AnonymousRange> sreplace(const vector<std::shared_ptr<RangeBase>>& in, size_t num) const;
std::shared_ptr<AnonymousRange> sreplace(const std::shared_ptr<RangeBase>& in,
const vector<size_t>& num) const;
2018-09-14 17:50:19 +02:00
2018-08-06 19:20:57 +02:00
bool isEmpty() const;
friend AnonymousRangeFactory;
2018-07-16 15:55:55 +02:00
static AnonymousRangeFactory factory()
{ return AnonymousRangeFactory(); }
protected:
GenSingleRange() = default;
GenSingleRange(const AnonymousRange& in) = default;
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
GenSingleRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
GenSingleRange(std::shared_ptr<RangeTypes>... origs);
2017-05-31 16:44:28 +02:00
2019-02-13 21:59:13 +01:00
GenSingleRange(const vector<std::shared_ptr<RangeBase>>& origs);
2018-07-16 15:55:55 +02:00
size_t mSize = 1;
2018-08-06 19:20:57 +02:00
bool mEmpty = true;
2019-02-13 21:59:13 +01:00
vector<std::shared_ptr<RangeBase> > mOrig;
2017-05-31 16:44:28 +02:00
};
}
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
2021-07-28 20:29:56 +02:00
namespace CNORXZ
{
2017-12-05 17:31:57 +01:00
/***********************
* AnonymousRange *
2017-12-05 17:31:57 +01:00
***********************/
2018-07-16 15:55:55 +02:00
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs)
{
mProd = std::shared_ptr<oType>( new AnonymousRange( origs ) );
}
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
AnonymousRangeFactory::AnonymousRangeFactory(std::shared_ptr<RangeTypes>... origs)
{
mProd = std::shared_ptr<oType>( new AnonymousRange( origs... ) );
}
template <class Range>
void AnonymousRangeFactory::append(std::shared_ptr<Range> r)
{
2018-07-16 14:44:55 +02:00
if(mProductCreated){
2018-07-16 14:44:55 +02:00
mProd = std::shared_ptr<oType>( new AnonymousRange( *std::dynamic_pointer_cast<oType>(mProd) ) );
mProductCreated = false;
}
std::dynamic_pointer_cast<oType>(mProd)->mOrig.push_back(r);
2018-07-16 14:44:55 +02:00
std::dynamic_pointer_cast<oType>(mProd)->mSize *= r->size();
2018-08-06 19:20:57 +02:00
std::dynamic_pointer_cast<oType>(mProd)->mEmpty = false;
}
2018-07-16 00:12:05 +02:00
/*****************
* Functions *
*****************/
std::shared_ptr<AnonymousRange> defaultRange(size_t size = 0);
}
2021-07-28 20:29:56 +02:00
namespace CNORXZ
2018-07-16 00:12:05 +02:00
{
2021-05-27 18:37:15 +02:00
namespace RangeHelper
2018-07-16 00:12:05 +02:00
{
2021-05-27 18:37:15 +02:00
template <>
inline void resolveSetRange<AnonymousRange>(std::shared_ptr<AnonymousRange>& rp,
const vector<std::shared_ptr<RangeBase> >& orig,
size_t origpos, size_t size)
{
AnonymousRangeFactory arf;
for(size_t op = origpos; op != origpos + size; ++op){
//VCHECK(op);
arf.append(orig[op]);
}
rp = std::dynamic_pointer_cast<AnonymousRange>( arf.create() );
2018-07-16 00:12:05 +02:00
}
2021-05-27 18:37:15 +02:00
2021-05-27 23:29:04 +02:00
template <>
inline void setRangeToVec<AnonymousRange>(vector<std::shared_ptr<RangeBase> >& v,
std::shared_ptr<AnonymousRange> r)
{
if(not r->isEmpty()){
for(size_t i = r->anonymousDim(); i != 0; --i){
v.insert(v.begin(), r->sub(i-1));
}
2018-08-06 19:20:57 +02:00
}
}
}
2018-07-16 00:12:05 +02:00
}
2021-07-28 20:29:56 +02:00
namespace CNORXZ
2018-07-16 00:12:05 +02:00
{
2017-12-05 17:31:57 +01:00
/***********************
* AnonymousRange *
2017-12-05 17:31:57 +01:00
***********************/
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
GenSingleRange<size_t,SpaceType::ANON,MUI>::GenSingleRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) :
RangeInterface<AnonymousIndex>()
{
2021-05-27 18:37:15 +02:00
mOrig.resize(sizeof...(RangeTypes));
sfor_pn<0,sizeof...(RangeTypes)>
( [&](auto i) { mOrig[i] = std::get<i>(origs); return 0; } );
mSize = sfor_p<0,sizeof...(RangeTypes)>
( [&](auto i) { return std::get<i>(origs)->size(); },
[&](auto a, auto b) { return a * b; } );
2018-08-06 19:20:57 +02:00
if(sizeof...(RangeTypes)){
mEmpty = false;
}
}
2017-12-17 17:40:55 +01:00
template <class... RangeTypes>
GenSingleRange<size_t,SpaceType::ANON,MUI>::GenSingleRange(std::shared_ptr<RangeTypes>... origs) :
RangeInterface<AnonymousIndex>()
{
auto rst = std::make_tuple(origs...);
2021-05-27 18:37:15 +02:00
mOrig.resize(sizeof...(RangeTypes));
sfor_pn<0,sizeof...(RangeTypes)>
( [&](auto i) { mOrig[i] = std::get<i>(rst); return 0; } );
mSize = sfor_p<0,sizeof...(RangeTypes)>
( [&](auto i) { return std::get<i>(rst)->size(); },
[&](auto a, auto b) { return a * b; } );
2018-08-06 19:20:57 +02:00
if(sizeof...(RangeTypes)){
mEmpty = false;
}
}
template <class Range>
std::shared_ptr<Range> GenSingleRange<size_t,SpaceType::ANON,MUI>::fullsub(size_t num) const
{
return std::dynamic_pointer_cast<Range>( mOrig.at(num) );
}
template <class... Ranges>
std::shared_ptr<MultiRange<Ranges...> > GenSingleRange<size_t,SpaceType::ANON,MUI>::scast(SIZET<Ranges>... sizes) const
{
2018-07-16 15:55:55 +02:00
std::tuple<std::shared_ptr<Ranges>...> rtp;
2021-05-27 18:37:15 +02:00
RangeHelper::resolveRangeType<0>(mOrig, rtp, 0, sizes...);
2018-07-16 00:12:05 +02:00
MultiRangeFactory<Ranges...> mrf(rtp);
2018-07-16 15:55:55 +02:00
return std::dynamic_pointer_cast<MultiRange<Ranges...> >( mrf.create() );
}
2018-07-16 00:12:05 +02:00
}
2017-05-31 16:44:28 +02:00
#endif