2017-11-20 20:53:48 +01:00
|
|
|
|
2017-11-20 21:00:52 +01:00
|
|
|
#ifndef __helper_tools_h__
|
|
|
|
#define __helper_tools_h__
|
|
|
|
|
2017-11-20 20:53:48 +01:00
|
|
|
#include "base_def.h"
|
2018-03-05 00:04:50 +01:00
|
|
|
#include "slice.h"
|
2018-07-17 13:56:59 +02:00
|
|
|
#include <ostream>
|
|
|
|
#include "pack_num.h"
|
2019-02-06 12:21:42 +01:00
|
|
|
#include "map_range.h"
|
2018-09-11 18:38:30 +02:00
|
|
|
#include <functional>
|
2017-11-20 20:53:48 +01:00
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
2018-07-17 13:56:59 +02:00
|
|
|
|
|
|
|
template <typename... T>
|
|
|
|
std::ostream& operator<<(std::ostream& out, const std::tuple<T...>& tp);
|
2017-11-20 20:53:48 +01:00
|
|
|
|
|
|
|
template <class RangeType>
|
|
|
|
auto getIndex(std::shared_ptr<RangeType> range)
|
|
|
|
-> std::shared_ptr<typename RangeType::IndexType>;
|
2017-12-15 14:47:02 +01:00
|
|
|
|
|
|
|
// only if 'RangeType' is defaultable and unique (Singleton)
|
|
|
|
template <class RangeType>
|
|
|
|
auto getIndex() -> std::shared_ptr<typename RangeType::IndexType>;
|
|
|
|
|
|
|
|
template <class... RangeTypes>
|
|
|
|
auto mkMulti(std::shared_ptr<RangeTypes>... ranges)
|
|
|
|
-> std::shared_ptr<MultiRange<RangeTypes...> >;
|
|
|
|
|
2019-02-06 13:01:14 +01:00
|
|
|
template <SpaceType STYPE, class Func, class... RangeTypes>
|
2019-02-06 14:11:17 +01:00
|
|
|
auto mkGenMapR(const std::shared_ptr<Func>& f, std::shared_ptr<RangeTypes>... ranges)
|
2019-02-06 13:01:14 +01:00
|
|
|
-> std::shared_ptr<GenMapRange<FunctionalMultiArray<typename Func::value_type,Func,RangeTypes...>,
|
|
|
|
STYPE,RangeTypes...> >;
|
|
|
|
|
|
|
|
template <SpaceType STYPE, class Func, class... IndexTypes>
|
2019-02-06 14:11:17 +01:00
|
|
|
auto mkGenMapI(const std::shared_ptr<Func>& f, std::shared_ptr<IndexTypes>... indices)
|
2019-02-06 13:01:14 +01:00
|
|
|
-> decltype( getIndex( mkGenMapR<STYPE>( f, indices->range()... ) ) );
|
|
|
|
|
2018-09-16 16:35:46 +02:00
|
|
|
template <class Func, class... RangeTypes>
|
2019-02-06 14:11:17 +01:00
|
|
|
auto mkMapR(const std::shared_ptr<Func>& f, std::shared_ptr<RangeTypes>... ranges)
|
2019-02-06 13:01:14 +01:00
|
|
|
-> decltype( mkGenMapR<SpaceType::ANY>(f, ranges... ) );
|
2018-09-16 16:35:46 +02:00
|
|
|
|
|
|
|
template <class Func, class... IndexTypes>
|
2019-02-06 14:11:17 +01:00
|
|
|
auto mkMapI(const std::shared_ptr<Func>& f, std::shared_ptr<IndexTypes>... indices)
|
2019-02-06 13:01:14 +01:00
|
|
|
-> decltype( mkGenMapI<SpaceType::ANY>(f, indices... ) );
|
2018-09-16 16:35:46 +02:00
|
|
|
|
2017-12-15 14:47:02 +01:00
|
|
|
template <class... IndexTypes>
|
|
|
|
auto mkMIndex(std::shared_ptr<IndexTypes>... indices)
|
|
|
|
-> decltype( getIndex( mkMulti( indices.range()... ) ) );
|
|
|
|
|
2018-03-05 00:04:50 +01:00
|
|
|
template <class... RangeTypes>
|
|
|
|
auto mkMulti(std::tuple<std::shared_ptr<RangeTypes>...> rangesTuple)
|
|
|
|
-> MultiRange<RangeTypes...>;
|
2018-07-17 13:56:59 +02:00
|
|
|
|
|
|
|
template <class RangeFactory>
|
|
|
|
auto createExplicit(RangeFactory& rf)
|
|
|
|
-> std::shared_ptr<typename RangeFactory::oType>;
|
|
|
|
|
|
|
|
template <class RangeFactory>
|
|
|
|
auto createExplicit(std::shared_ptr<RangeFactory> rfp)
|
|
|
|
-> std::shared_ptr<typename RangeFactory::oType>;
|
|
|
|
|
2018-07-28 20:05:58 +02:00
|
|
|
template <class Range>
|
2019-02-13 21:59:13 +01:00
|
|
|
auto createRange(const vector<char>& cvec)
|
2018-07-28 20:05:58 +02:00
|
|
|
-> std::shared_ptr<Range>;
|
2018-08-05 18:45:20 +02:00
|
|
|
|
2019-03-13 19:17:22 +01:00
|
|
|
template <class Range, typename... Args>
|
|
|
|
auto createRangeE(const Args&... args)
|
|
|
|
-> std::shared_ptr<Range>;
|
|
|
|
|
2018-08-05 18:45:20 +02:00
|
|
|
template <size_t N, class MArray>
|
2018-11-03 22:03:35 +01:00
|
|
|
auto rptr(const MArray& ma)
|
2018-08-05 18:45:20 +02:00
|
|
|
-> decltype(ma.template getRangePtr<N>());
|
2018-09-11 18:38:30 +02:00
|
|
|
|
2019-03-01 18:13:51 +01:00
|
|
|
template <size_t I, class MIndex>
|
|
|
|
auto get(const std::shared_ptr<MIndex>& i)
|
|
|
|
-> decltype(i->template getPtr<I>())
|
|
|
|
{
|
|
|
|
return i->template getPtr<I>();
|
|
|
|
}
|
|
|
|
|
2018-11-01 22:11:08 +01:00
|
|
|
template <class EC, class MArray>
|
|
|
|
auto dynamic(const MArray& ma, bool slice = false)
|
|
|
|
-> std::shared_ptr<MultiArrayBase<typename MArray::value_type,DynamicRange<EC>>>;
|
2018-11-03 22:03:35 +01:00
|
|
|
|
|
|
|
template <typename T, class EC, class Range1, class... RangeTypes>
|
|
|
|
auto anonToDynView(const MultiArray<T,Range1,RangeTypes...,AnonymousRange>& ma)
|
|
|
|
-> ConstSlice<T,Range1,RangeTypes...,DynamicRange<EC>>;
|
|
|
|
|
|
|
|
template <typename T, class EC, class Range1, class... RangeTypes>
|
|
|
|
auto dynToAnonMove(MultiArray<T,Range1,RangeTypes...,DynamicRange<EC>>&& ma)
|
|
|
|
-> MultiArray<T,Range1,RangeTypes...,AnonymousRange>;
|
|
|
|
|
|
|
|
template <typename T, class EC>
|
|
|
|
auto anonToDynView(const MultiArray<T,AnonymousRange>& ma)
|
|
|
|
-> ConstSlice<T,DynamicRange<EC>>;
|
|
|
|
|
|
|
|
template <typename T, class EC>
|
|
|
|
auto dynToAnonMove(MultiArray<T,DynamicRange<EC>>&& ma)
|
|
|
|
-> MultiArray<T,AnonymousRange>;
|
|
|
|
|
2019-03-08 13:09:10 +01:00
|
|
|
template <class Range>
|
|
|
|
auto metaSlice(const std::shared_ptr<Range>& r)
|
2019-03-11 19:24:34 +01:00
|
|
|
-> ConstSlice<typename Range::MetaType,ClassicRange>;
|
2019-03-08 13:09:10 +01:00
|
|
|
|
|
|
|
template <class Range, class ORange>
|
|
|
|
auto metaSlice(const std::shared_ptr<Range>& r, const std::shared_ptr<ORange>& ro)
|
2019-03-11 19:24:34 +01:00
|
|
|
-> ConstSlice<typename Range::MetaType,ORange>;
|
2019-03-08 13:09:10 +01:00
|
|
|
|
2019-03-13 19:17:22 +01:00
|
|
|
template <typename T, class... Ranges>
|
|
|
|
auto mkArray(const std::shared_ptr<Ranges>&... rs)
|
|
|
|
-> MultiArray<T,Ranges...>;
|
|
|
|
|
|
|
|
template <typename T, class... Ranges>
|
|
|
|
auto mkArray(const std::shared_ptr<Ranges>&... rs, const T& val)
|
|
|
|
-> MultiArray<T,Ranges...>;
|
|
|
|
|
2018-09-11 18:38:30 +02:00
|
|
|
template <class IndexType>
|
|
|
|
inline void For(const std::shared_ptr<IndexType>& ind, const std::function<void(void)>& ll)
|
|
|
|
{
|
|
|
|
for((*ind) = 0; ind->pos() != ind->max(); ++(*ind)){
|
|
|
|
ll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 18:13:51 +01:00
|
|
|
// parallel:
|
|
|
|
template <class IndexType>
|
|
|
|
inline void PFor(const std::shared_ptr<IndexType>& ind,
|
|
|
|
const std::function<void(const std::shared_ptr<IndexType>&)>& ll)
|
|
|
|
{
|
|
|
|
const int max = static_cast<int>(ind->max());
|
|
|
|
int i = 0;
|
|
|
|
#pragma omp parallel shared(ind,ll) private(i)
|
|
|
|
{
|
|
|
|
#pragma omp for nowait
|
|
|
|
for(i = 0; i < max; i++) {
|
|
|
|
auto ii = getIndex( ind->range() );
|
|
|
|
((*ii) = i)();
|
|
|
|
ll(ii);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
|
2018-09-13 17:42:16 +02:00
|
|
|
template <class Index>
|
|
|
|
inline auto mkOp(const std::shared_ptr<Index>& i)
|
|
|
|
-> decltype(std::declval<FunctionalMultiArray<typename Index::MetaType,
|
|
|
|
identity<typename Index::MetaType>,typename Index::RangeType> >
|
|
|
|
().exec(i))
|
|
|
|
{
|
|
|
|
FunctionalMultiArray<typename Index::MetaType,
|
|
|
|
identity<typename Index::MetaType>,
|
|
|
|
typename Index::RangeType> fma(i->range());
|
|
|
|
return fma.exec(i);
|
|
|
|
}
|
2018-11-29 19:37:32 +01:00
|
|
|
|
|
|
|
template <typename R, typename... Ts>
|
|
|
|
struct Func
|
|
|
|
{
|
|
|
|
static inline std::shared_ptr<function<R,Ts...>> mk(const std::function<R(Ts...)>& ll)
|
|
|
|
{
|
|
|
|
return std::make_shared<function<R,Ts...>>(ll);
|
|
|
|
}
|
|
|
|
};
|
2017-11-20 20:53:48 +01:00
|
|
|
}
|
2017-11-20 21:00:52 +01:00
|
|
|
|
2018-10-30 15:06:29 +01:00
|
|
|
|
2017-11-20 21:00:52 +01:00
|
|
|
#endif
|