cnorxz/src/include/map_range.h

340 lines
9.9 KiB
C
Raw Normal View History

2018-08-07 21:49:21 +02:00
// -*- C++ -*-
#ifndef __map_range_h__
#define __map_range_h__
#include <cstdlib>
#include <tuple>
#include <memory>
#include <map>
2018-08-18 16:24:13 +02:00
#include "mbase_def.h"
2018-08-07 21:49:21 +02:00
#include "ranges/range_base.h"
#include "ranges/index_base.h"
#include "ranges/rpack_num.h"
2018-08-18 16:24:13 +02:00
#include "map_range_factory_product_map.h"
2018-08-07 21:49:21 +02:00
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
2018-08-07 23:15:31 +02:00
#include "xfor/xfor.h"
2018-08-07 21:49:21 +02:00
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
2019-03-29 12:58:39 +01:00
template <class Func, class... Indices>
auto mkMapOp(const std::shared_ptr<Func>& func,
const std::shared_ptr<Indices>&... is)
-> decltype(std::make_tuple(FunctionalMultiArray<typename Func::value_type,Func,
typename Indices::RangeType...>().exec(is...),
FunctionalMultiArray<typename Func::value_type,Func,
typename Indices::RangeType...>()))
{
2019-03-29 12:58:39 +01:00
typedef FunctionalMultiArray<typename Func::value_type,Func,typename Indices::RangeType...> FMA;
if(Func::FISSTATIC){
FMA fma(is->range()...);
return std::make_tuple(fma.exec(is...),fma);
}
else {
FMA fma(is->range()...,func);
return std::make_tuple(fma.exec(is...),fma);
}
}
2019-03-29 12:58:39 +01:00
2019-04-03 16:26:53 +02:00
template <class Op, class Index, class Expr, SpaceType STYPE = SpaceType::ANY>
2019-03-29 12:58:39 +01:00
//template <class MapF, class IndexPack, class Expr, SpaceType STYPE = SpaceType::ANY>
class OpExpr
{
public:
2019-04-03 16:26:53 +02:00
typedef typename Index::OIType OIType;
//typedef SingleIndex<typename Op::value_type,STYPE> OIType;
static constexpr size_t LAYER = Expr::LAYER + 1;
static constexpr size_t SIZE = Expr::SIZE + Op::SIZE;
private:
OpExpr() = default;
2019-04-03 16:26:53 +02:00
const Index* mIndPtr;
//const OIType* mIndPtr;
size_t mSPos;
size_t mMax;
size_t mStep;
Expr mExpr;
Op mOp;
2018-08-18 16:24:13 +02:00
typedef decltype(mOp.rootSteps(std::declval<intptr_t>()).extend( mExpr.rootSteps(std::declval<intptr_t>()) )) ExtType;
ExtType mExt;
public:
OpExpr(const OpExpr& in) = default;
OpExpr(OpExpr&& in) = default;
OpExpr& operator=(const OpExpr& in) = default;
OpExpr& operator=(OpExpr&& in) = default;
2019-03-29 12:58:39 +01:00
2019-04-03 16:26:53 +02:00
OpExpr(const Op& mapf, const Index* ind, size_t step, Expr ex);
2019-02-26 18:56:57 +01:00
inline void operator()(size_t mlast, ExtType last);
inline void operator()(size_t mlast = 0);
auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType;
};
2018-08-07 21:49:21 +02:00
2019-03-29 12:58:39 +01:00
template <class Op, SpaceType XSTYPE, class... Indices>
class GenMapIndex : public IndexInterface<GenMapIndex<Op,XSTYPE,Indices...>,
typename Op::value_type>
//std::tuple<typename Indices::MetaType...> >
2018-08-07 21:49:21 +02:00
{
public:
2019-03-29 12:58:39 +01:00
typedef IndexInterface<GenMapIndex<Op,XSTYPE,Indices...>,
typename Op::value_type> IB;
//std::tuple<typename Indices::MetaType...> > IB;
2018-08-07 21:49:21 +02:00
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
//typedef std::tuple<typename Indices::MetaType...> MetaType;
typedef typename Op::value_type MetaType;
2019-03-29 12:58:39 +01:00
typedef GenMapRange<Op,XSTYPE,typename Indices::RangeType...> RangeType;
typedef GenMapIndex IType;
2019-03-29 12:58:39 +01:00
typedef SingleIndex<typename Op::value_type,XSTYPE> OIType;
2018-08-07 21:49:21 +02:00
2019-04-03 16:26:53 +02:00
static constexpr IndexType sType() { return IndexType::SINGLE; }
2018-08-07 21:49:21 +02:00
static constexpr size_t sDim() { return sizeof...(Indices); }
static constexpr size_t totalDim() { return mkTotalDim<Indices...>(); }
2018-08-07 21:49:21 +02:00
static constexpr SpaceType STYPE = XSTYPE;
2019-01-15 17:41:43 +01:00
static constexpr bool PARALLEL = false;
2018-08-07 21:49:21 +02:00
private:
IndexPack mIPack;
std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
std::shared_ptr<OIType> mOutIndex;
public:
const IndexPack& pack() const { return mIPack; }
GenMapIndex() = delete;
2018-08-07 21:49:21 +02:00
// NO DEFAULT HERE !!!
// ( have to assign sub-indices (ptr!) correctly )
//MapIndex(const MapIndex& in);
//MapIndex& operator=(const MapIndex& in);
template <class MRange>
GenMapIndex(const std::shared_ptr<MRange>& range);
2018-08-07 21:49:21 +02:00
template <size_t DIR>
GenMapIndex& up();
2018-08-07 21:49:21 +02:00
template <size_t DIR>
GenMapIndex& down();
2018-08-07 21:49:21 +02:00
template <size_t N>
auto get() const -> decltype( *std::get<N>( mIPack ) )&;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mIPack ) )&;
2018-09-17 11:06:24 +02:00
template <size_t N>
size_t getBlockSize() const { return std::get<N>(mBlockSizes); }
2018-08-07 21:49:21 +02:00
std::shared_ptr<OIType> outIndex() const;
// raplace instances (in contrast to its analogon in ContainerIndex
// MultiIndices CANNOT be influences be its subindices, so there is
// NO foreign/external controll)
// Do NOT share index instances between two or more MapIndex instances
2019-04-03 16:26:53 +02:00
GenMapIndex& operator()(const std::shared_ptr<Indices>&... indices);
2018-08-07 21:49:21 +02:00
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
IndexType type() const;
GenMapIndex& operator=(size_t pos);
2018-08-07 21:49:21 +02:00
GenMapIndex& operator++();
GenMapIndex& operator--();
2018-08-07 21:49:21 +02:00
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
std::string stringMeta() const;
MetaType meta() const;
GenMapIndex& at(const MetaType& metaPos);
2019-04-03 16:26:53 +02:00
size_t posAt(const MetaType& metaPos) const;
size_t dim() const;
bool first() const;
bool last() const;
std::shared_ptr<RangeType> range() const;
2018-08-07 21:49:21 +02:00
template <size_t N>
auto getPtr() -> decltype( std::get<N>( mIPack ) )&;
size_t getStepSize(size_t n) const;
2018-08-07 21:49:21 +02:00
std::string id() const;
void print(size_t offset) const;
2018-08-07 21:49:21 +02:00
template <class Exprs>
auto ifor(size_t step, Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh
2019-04-03 16:26:53 +02:00
(step, mIPack, mBlockSizes, OpExpr<Op,GenMapIndex,Exprs,XSTYPE>( range()->map(), this, step, exs ) ) );
// first step arg not used!
2019-01-15 17:41:43 +01:00
template <class Exprs>
auto pifor(size_t step, Exprs exs) const
-> decltype(ifor(step, exs)); // NO MULTITHREADING
2018-08-07 21:49:21 +02:00
template <class Exprs>
auto iforh(size_t step, Exprs exs) const
-> decltype(ifor(step, exs));
2018-08-07 21:49:21 +02:00
};
/*************************
* MapRangeFactory *
*************************/
// NOT THREAD SAVE
2019-03-29 12:58:39 +01:00
template <class Op, SpaceType XSTYPE, class... Ranges>
class GenMapRangeFactory : public RangeFactoryBase
2018-08-07 21:49:21 +02:00
{
public:
2019-03-29 12:58:39 +01:00
typedef GenMapRange<Op,XSTYPE,Ranges...> oType;
2018-08-07 21:49:21 +02:00
GenMapRangeFactory() = delete;
2019-03-29 12:58:39 +01:00
template <class MA>
GenMapRangeFactory(const std::tuple<Op,MA>& mapf, const std::shared_ptr<Ranges>&... rs);
template <class MA>
GenMapRangeFactory(const std::tuple<Op,MA>& mapf,
const typename GenMapRange<Op,XSTYPE,Ranges...>::Space& space);
2018-08-07 21:49:21 +02:00
virtual std::shared_ptr<RangeBase> create() override;
private:
std::shared_ptr<RangeBase> checkIfCreated(const std::tuple<std::shared_ptr<Ranges>...>& ptp);
};
/******************
* MapRange *
******************/
2019-03-29 12:58:39 +01:00
template <class Op, SpaceType XSTYPE, class... Ranges>
class GenMapRange : public RangeInterface<GenMapIndex<Op,XSTYPE,typename Ranges::IndexType...> >
2018-08-07 21:49:21 +02:00
{
public:
typedef RangeBase RB;
typedef std::tuple<std::shared_ptr<Ranges>...> Space;
2019-03-29 12:58:39 +01:00
typedef GenMapIndex<Op,XSTYPE,typename Ranges::IndexType...> IndexType;
typedef GenMapRange RangeType;
2019-03-29 12:58:39 +01:00
typedef SingleRange<typename Op::value_type,XSTYPE> ORType;
typedef SingleRangeFactory<typename Op::value_type,XSTYPE> ORFType;
typedef typename Op::value_type MetaType;
2018-08-07 21:49:21 +02:00
//typedef typename RangeInterface<MapIndex<typename Ranges::IndexType...> >::IndexType IndexType;
protected:
GenMapRange() = delete;
GenMapRange(const GenMapRange& in) = delete;
GenMapRange& operator=(const GenMapRange& in) = delete;
2019-03-29 12:58:39 +01:00
template <class MA>
GenMapRange(const std::tuple<Op,MA>& mapf, const Space& space);
template <class MA>
GenMapRange(const std::tuple<Op,MA>& mapf, const std::shared_ptr<Ranges>&... rs);
2018-08-07 21:49:21 +02:00
Space mSpace;
2019-03-29 12:58:39 +01:00
Op mMapf;
//Op mMapf;
2018-08-18 16:24:13 +02:00
std::shared_ptr<ORType> mOutRange;
MultiArray<size_t,ORType> mMapMult;
2019-03-29 16:36:15 +01:00
vector<size_t> mMapPos;
private:
2019-03-29 12:58:39 +01:00
template <class MA>
void mkOutRange(const MA& mapf);
2018-08-07 21:49:21 +02:00
public:
static constexpr size_t sdim = sizeof...(Ranges);
2018-08-07 21:49:21 +02:00
template <size_t N>
auto get() const -> decltype( *std::get<N>( mSpace ) )&;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
2019-04-03 16:26:53 +02:00
std::shared_ptr<ORType> outRange() const;
2019-03-29 12:58:39 +01:00
const Op& map() const;
2018-08-07 21:49:21 +02:00
virtual size_t dim() const final;
virtual size_t size() const final;
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;
2018-08-07 21:49:21 +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-08-07 21:49:21 +02:00
const Space& space() const;
virtual IndexType begin() const final;
virtual IndexType end() const final;
const MultiArray<size_t,ORType>& mapMultiplicity() const;
2019-04-03 16:26:53 +02:00
ConstSlice<size_t,GenMapRange> explMapMultiplicity() const;
2018-09-17 11:06:24 +02:00
2019-03-11 19:24:34 +01:00
vector<size_t> mapPos() const;
/*
2018-08-07 21:49:21 +02:00
template <class... ERanges>
auto cat(const std::shared_ptr<GenMapRange<ERanges...> >& erange)
-> std::shared_ptr<GenMapRange<Ranges...,ERanges...> >;
*/
2019-03-29 12:58:39 +01:00
friend GenMapRangeFactory<Op,XSTYPE,Ranges...>;
2018-08-07 21:49:21 +02:00
static constexpr bool HASMETACONT = false;
static constexpr bool defaultable = false;
2019-03-29 12:58:39 +01:00
static constexpr size_t ISSTATIC = SubProp<Op,Ranges...>::ISSTATIC;
static constexpr size_t SIZE = SubProp<Op,Ranges...>::SIZE;
2018-08-07 21:49:21 +02:00
};
// for legacy
2019-03-29 12:58:39 +01:00
template <class Op, class... Indices>
using MapIndex = GenMapIndex<Op,SpaceType::ANY,Indices...>;
2019-03-29 12:58:39 +01:00
template <class Op, class... Ranges>
using MapRangeFactory = GenMapRangeFactory<Op,SpaceType::ANY,Ranges...>;
2019-03-29 12:58:39 +01:00
template <class Op, class... Ranges>
using MapRange = GenMapRange<Op,SpaceType::ANY,Ranges...>;
2019-03-29 12:58:39 +01:00
template <class Op, class... Indices>
auto mapResult/*<MapIndex<Op,Indices...> >*/(const std::shared_ptr<MapIndex<Op,Indices...> >& ind)
-> decltype(ind->outIndex())
{
return ind->outIndex();
}
2018-08-07 21:49:21 +02:00
}
#endif