2017-07-27 14:48:41 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
2017-08-04 11:27:47 +02:00
|
|
|
#ifndef __pack_num_h__
|
|
|
|
#define __pack_num_h__
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
#include <cstdlib>
|
2017-12-18 11:19:04 +01:00
|
|
|
//#include <type_traits>
|
2017-07-27 14:48:41 +02:00
|
|
|
#include <tuple>
|
2017-12-18 11:19:04 +01:00
|
|
|
#include <ostream>
|
2017-07-27 14:48:41 +02:00
|
|
|
|
2017-08-04 11:27:47 +02:00
|
|
|
#include "base_def.h"
|
2018-02-12 18:26:56 +01:00
|
|
|
#include "xfor/exttype.h"
|
2017-08-04 11:27:47 +02:00
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
namespace MultiArrayHelper
|
|
|
|
{
|
2018-04-28 17:33:57 +02:00
|
|
|
template <bool ISSTATIC>
|
|
|
|
struct Application
|
|
|
|
{
|
2018-05-20 16:45:03 +02:00
|
|
|
template <class OpFunction, typename... Ts>
|
|
|
|
static inline auto apply(std::shared_ptr<OpFunction> f, Ts... as)
|
|
|
|
-> decltype((*f)(as...))
|
2018-04-28 17:33:57 +02:00
|
|
|
{
|
2018-05-20 16:45:03 +02:00
|
|
|
return (*f)(as...);
|
2018-04-28 17:33:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Application<true>
|
|
|
|
{
|
2018-05-20 16:45:03 +02:00
|
|
|
template <class OpFunction, typename... Ts>
|
|
|
|
static inline auto apply(std::shared_ptr<OpFunction> f, Ts... as)
|
|
|
|
-> decltype(OpFunction::apply(as...))
|
2018-04-28 17:33:57 +02:00
|
|
|
{
|
2018-05-20 16:45:03 +02:00
|
|
|
return OpFunction::apply(as...);
|
2018-04-28 17:33:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
template <size_t N>
|
|
|
|
struct PackNum
|
|
|
|
{
|
2018-05-20 15:15:05 +02:00
|
|
|
template <class MA, class ITuple, class... IPtrs>
|
|
|
|
static auto mkElemOperation(const MA& ma, const ITuple& ituple, IPtrs... iptrs)
|
|
|
|
-> decltype(PackNum<N-1>::mkElemOperation(ma, ituple, std::get<N>(ituple), iptrs...))
|
|
|
|
{
|
|
|
|
return PackNum<N-1>::mkElemOperation(ma, ituple, std::get<N>(ituple), iptrs...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-11 12:54:24 +02:00
|
|
|
template <typename... T>
|
2018-01-09 17:24:10 +01:00
|
|
|
static void printTuple(std::ostream& out, const std::tuple<T...>& tp)
|
|
|
|
{
|
2017-09-11 12:54:24 +02:00
|
|
|
out << std::get<sizeof...(T)-N-1>(tp) << ", ";
|
|
|
|
PackNum<N-1>::printTuple(out, tp);
|
|
|
|
}
|
2017-12-05 17:31:57 +01:00
|
|
|
|
2018-01-09 17:24:10 +01:00
|
|
|
template <class... Ops>
|
2018-02-13 21:36:41 +01:00
|
|
|
static auto mkSteps(std::intptr_t ii, const std::tuple<Ops...>& otp)
|
2018-02-14 00:38:44 +01:00
|
|
|
-> decltype(PackNum<N-1>::mkSteps(ii, otp).extend( std::get<N>(otp).rootSteps(ii)) )
|
2018-01-09 17:24:10 +01:00
|
|
|
{
|
2018-02-14 00:38:44 +01:00
|
|
|
return PackNum<N-1>::mkSteps(ii, otp).extend( std::get<N>(otp).rootSteps(ii));
|
2018-01-09 17:24:10 +01:00
|
|
|
}
|
2018-01-09 22:38:46 +01:00
|
|
|
|
|
|
|
template <class RootStepTuple, class IndexClass, class OpClass>
|
|
|
|
static void mkExt(std::array<RootStepTuple,IndexClass::totalDim()>& out,
|
|
|
|
const std::array<std::intptr_t,IndexClass::totalDim()>& siar,
|
|
|
|
const OpClass& second)
|
2018-01-09 17:24:10 +01:00
|
|
|
{
|
2018-01-09 22:38:46 +01:00
|
|
|
std::get<N>(out) = second.rootSteps( std::get<N>(siar) );
|
|
|
|
PackNum<N-1>::mkExt(out, siar, second);
|
|
|
|
}
|
2018-01-14 19:15:05 +01:00
|
|
|
|
2018-02-13 15:38:03 +01:00
|
|
|
template <size_t LAST, typename T, class ETuple, class OpTuple, class OpFunction, typename... Args>
|
2018-04-28 17:33:57 +02:00
|
|
|
static inline T mkOpExpr(std::shared_ptr<OpFunction> f, const ETuple& pos, const OpTuple& ops, const Args&... args)
|
2018-01-14 19:15:05 +01:00
|
|
|
{
|
2018-02-13 15:38:03 +01:00
|
|
|
typedef typename std::remove_reference<decltype(std::get<N>(ops))>::type NextOpType;
|
|
|
|
static_assert(LAST > NextOpType::SIZE, "inconsistent array positions");
|
|
|
|
static constexpr size_t NEXT = LAST - NextOpType::SIZE;
|
2018-05-20 16:45:03 +02:00
|
|
|
typedef decltype(std::get<N>(ops).get(Getter<NEXT>::template getX<ETuple>( pos ))) ArgT;
|
|
|
|
return PackNum<N-1>::template mkOpExpr<NEXT,T,ETuple,OpTuple,OpFunction,ArgT,Args...>
|
2018-04-28 17:33:57 +02:00
|
|
|
( f, pos, ops, std::get<N>(ops).get(Getter<NEXT>::template getX<ETuple>( pos )), args...);
|
2018-01-14 19:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class OpTuple, class Expr>
|
2018-03-05 21:52:12 +01:00
|
|
|
static auto mkLoop( const OpTuple& ot, Expr exp )
|
2018-02-14 00:38:44 +01:00
|
|
|
-> decltype(std::get<N>(ot).loop( PackNum<N-1>::mkLoop(ot,exp) ))
|
2018-01-14 19:15:05 +01:00
|
|
|
{
|
|
|
|
return std::get<N>(ot).loop( PackNum<N-1>::mkLoop(ot,exp) );
|
|
|
|
}
|
2018-03-06 13:58:17 +01:00
|
|
|
|
|
|
|
template <typename T, class Op, class... SRanges>
|
|
|
|
static void mkSliceBlocks(std::array<size_t, sizeof...(SRanges)+1>& blocks,
|
|
|
|
const ContainerIndex<T,typename SRanges::IndexType...>& index,
|
|
|
|
const Op& op, size_t total = 1)
|
|
|
|
{
|
|
|
|
const size_t tmp =
|
|
|
|
op.rootSteps(reinterpret_cast<std::intptr_t>
|
|
|
|
( index.template getPtr<N>().get() ) )
|
|
|
|
.val();
|
|
|
|
std::get<N+1>(blocks) = tmp;
|
2018-03-06 14:35:32 +01:00
|
|
|
PackNum<N-1>::template mkSliceBlocks<T,Op,SRanges...>(blocks, index, op, total * tmp);
|
2018-03-06 13:58:17 +01:00
|
|
|
}
|
2018-07-19 19:29:52 +02:00
|
|
|
|
|
|
|
template <class... SRanges>
|
|
|
|
static bool checkIfSameInstance(const std::tuple<std::shared_ptr<SRanges>...>& rtp1,
|
|
|
|
const std::tuple<std::shared_ptr<SRanges>...>& rtp2)
|
|
|
|
{
|
|
|
|
return std::get<N>(rtp1).get() == std::get<N>(rtp2).get() and PackNum<N-1>::checkIfSameInstance(rtp1,rtp2);
|
|
|
|
}
|
2018-08-08 18:20:08 +02:00
|
|
|
|
|
|
|
template <class MA, class ITuple, class... Indices>
|
|
|
|
static inline auto mkMapOp(const MA& ma, const ITuple& itp, const std::shared_ptr<Indices>&... inds)
|
|
|
|
-> decltype(PackNum<N-1>::mkMapOp(ma, itp, std::get<N>(itp), inds...))
|
|
|
|
{
|
|
|
|
return PackNum<N-1>::mkMapOp(ma, itp, std::get<N>(itp), inds...);
|
|
|
|
}
|
2018-09-15 01:58:17 +02:00
|
|
|
|
|
|
|
template <size_t LAST,class OpTuple, class ETuple>
|
|
|
|
static inline void setOpPos(const OpTuple& ot, const ETuple& et)
|
|
|
|
{
|
|
|
|
typedef typename std::remove_reference<decltype(std::get<N>(ot))>::type NextOpType;
|
|
|
|
static_assert(LAST > NextOpType::SIZE, "inconsistent array positions");
|
|
|
|
static constexpr size_t NEXT = LAST - NextOpType::SIZE;
|
|
|
|
std::get<N>( ot ).set( Getter<NEXT>::template getX<ETuple>( et ) );
|
|
|
|
PackNum<N-1>::template setOpPos<NEXT>(ot, et);
|
|
|
|
}
|
2017-07-27 14:48:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct PackNum<0>
|
|
|
|
{
|
2018-05-20 15:15:05 +02:00
|
|
|
template <class MA, class ITuple, class... IPtrs>
|
|
|
|
static auto mkElemOperation(const MA& ma, const ITuple& ituple, IPtrs... iptrs)
|
|
|
|
-> decltype(ma(iptrs...))
|
|
|
|
{
|
|
|
|
return ma(iptrs...);
|
|
|
|
}
|
|
|
|
|
2017-09-11 12:54:24 +02:00
|
|
|
template <typename... T>
|
2018-01-09 17:24:10 +01:00
|
|
|
static void printTuple(std::ostream& out, const std::tuple<T...>& tp)
|
|
|
|
{
|
2017-09-11 12:54:24 +02:00
|
|
|
out << std::get<sizeof...(T)-1>(tp);
|
|
|
|
}
|
2018-01-09 17:24:10 +01:00
|
|
|
|
|
|
|
template <class... Ops>
|
2018-02-13 21:36:41 +01:00
|
|
|
static auto mkSteps(std::intptr_t ii, const std::tuple<Ops...>& otp)
|
2018-01-09 17:24:10 +01:00
|
|
|
-> decltype(std::get<0>(otp).rootSteps(ii))
|
|
|
|
{
|
|
|
|
return std::get<0>(otp).rootSteps(ii);
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:38:46 +01:00
|
|
|
template <class RootStepTuple, class IndexClass, class OpClass>
|
|
|
|
static void mkExt(std::array<RootStepTuple,IndexClass::totalDim()>& out,
|
|
|
|
const std::array<std::intptr_t,IndexClass::totalDim()>& siar,
|
|
|
|
const OpClass& second)
|
|
|
|
{
|
|
|
|
std::get<0>(out) = second.rootSteps( std::get<0>(siar) );
|
|
|
|
}
|
2018-04-28 17:33:57 +02:00
|
|
|
|
2018-02-13 15:38:03 +01:00
|
|
|
template <size_t LAST, typename T, class ETuple, class OpTuple, class OpFunction, typename... Args>
|
2018-04-28 17:33:57 +02:00
|
|
|
static inline T mkOpExpr(std::shared_ptr<OpFunction> f, const ETuple& pos, const OpTuple& ops, const Args&... args)
|
2018-01-14 19:15:05 +01:00
|
|
|
{
|
2018-02-14 00:38:44 +01:00
|
|
|
typedef typename std::remove_reference<decltype(std::get<0>(ops))>::type NextOpType;
|
|
|
|
static constexpr size_t NEXT = LAST - NextOpType::SIZE;
|
|
|
|
static_assert(NEXT == 0, "inconsistent array positions");
|
2018-05-20 16:45:03 +02:00
|
|
|
typedef decltype(std::get<0>(ops).get(Getter<0>::template getX<ETuple>( pos ))) ArgT;
|
|
|
|
return Application<OpFunction::FISSTATIC>::template apply<OpFunction,ArgT,Args...>(f, std::get<0>(ops).get(Getter<0>::template getX<ETuple>( pos )), args...);
|
2018-04-28 17:33:57 +02:00
|
|
|
//return OpFunction::apply(std::get<0>(ops).get(Getter<0>::template getX<ETuple>( pos )), args...);
|
2018-01-14 19:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class OpTuple, class Expr>
|
2018-03-05 21:52:12 +01:00
|
|
|
static auto mkLoop( const OpTuple& ot, Expr exp )
|
2018-02-14 00:38:44 +01:00
|
|
|
-> decltype(std::get<0>(ot).loop( exp ))
|
2018-01-14 19:15:05 +01:00
|
|
|
{
|
|
|
|
return std::get<0>(ot).loop( exp );
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:58:17 +01:00
|
|
|
template <typename T, class Op, class... SRanges>
|
|
|
|
static void mkSliceBlocks(std::array<size_t, sizeof...(SRanges)+1>& blocks,
|
|
|
|
const ContainerIndex<T,typename SRanges::IndexType...>& index,
|
|
|
|
const Op& op, size_t total = 1)
|
|
|
|
{
|
|
|
|
const size_t tmp =
|
|
|
|
op.rootSteps(reinterpret_cast<std::intptr_t>
|
|
|
|
( index.template getPtr<0>().get() ) )
|
|
|
|
.val();
|
|
|
|
std::get<1>(blocks) = tmp;
|
|
|
|
std::get<0>(blocks) = total * tmp; // this is not correct, but not used so far ... !!!
|
|
|
|
}
|
|
|
|
|
2018-07-19 19:29:52 +02:00
|
|
|
template <class... SRanges>
|
|
|
|
static bool checkIfSameInstance(const std::tuple<std::shared_ptr<SRanges>...>& rtp1,
|
|
|
|
const std::tuple<std::shared_ptr<SRanges>...>& rtp2)
|
|
|
|
{
|
|
|
|
return std::get<0>(rtp1).get() == std::get<0>(rtp2).get();
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:20:08 +02:00
|
|
|
template <class MA, class ITuple, class... Indices>
|
|
|
|
static inline auto mkMapOp(const MA& ma, const ITuple& itp, const std::shared_ptr<Indices>&... inds)
|
|
|
|
-> decltype(ma.exec(std::get<0>(itp), inds...))
|
|
|
|
{
|
|
|
|
return ma.exec(std::get<0>(itp), inds...);
|
|
|
|
}
|
|
|
|
|
2018-09-15 01:58:17 +02:00
|
|
|
template <size_t LAST,class OpTuple, class ETuple>
|
|
|
|
static inline void setOpPos(const OpTuple& ot, const ETuple& et)
|
|
|
|
{
|
|
|
|
typedef typename std::remove_reference<decltype(std::get<0>(et))>::type NextOpType;
|
|
|
|
static_assert(LAST > NextOpType::SIZE, "inconsistent array positions");
|
|
|
|
static constexpr size_t NEXT = LAST - NextOpType::SIZE;
|
|
|
|
std::get<0>( ot ).set( Getter<NEXT>::template getX<ETuple>( et ) );
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
};
|
2017-09-12 18:36:05 +02:00
|
|
|
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
|
|
|
|
} // end namespace MultiArrayHelper
|
2017-08-04 11:27:47 +02:00
|
|
|
|
|
|
|
#endif
|