operation via lambda expression ( a(T,args...) function in OperationClass)

This commit is contained in:
Christian Zimmermann 2018-11-26 12:10:38 +01:00
parent 29c0f48f77
commit 1aa5ed10d9
3 changed files with 58 additions and 2 deletions

View file

@ -2,6 +2,8 @@
#ifndef __arith_h__ #ifndef __arith_h__
#define __arith_h__ #define __arith_h__
#include <functional>
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -14,6 +16,13 @@ namespace MultiArrayTools
{ {
return ArgPack<N-1>::template mk<F,Tuple,decltype(std::get<N>(tp)),As...>(tp, std::get<N>(tp), as...); return ArgPack<N-1>::template mk<F,Tuple,decltype(std::get<N>(tp)),As...>(tp, std::get<N>(tp), as...);
} }
template <class F, class Tuple, typename... As>
static inline auto mkd(const F& ff, const Tuple& tp, As... as)
-> decltype(ArgPack<N-1>::template mkd<F,Tuple,decltype(std::get<N>(tp)),As...>(ff, tp, std::get<N>(tp), as...))
{
return ArgPack<N-1>::template mkd<F,Tuple,decltype(std::get<N>(tp)),As...>(ff, tp, std::get<N>(tp), as...);
}
}; };
template <> template <>
@ -25,6 +34,13 @@ namespace MultiArrayTools
{ {
return F::apply(std::get<0>(tp), as...); return F::apply(std::get<0>(tp), as...);
} }
template <class F, class Tuple, typename... As>
static inline auto mkd(const F& ff, const Tuple& tp, As... as)
-> decltype(ff(std::get<0>(tp), as...))
{
return ff(std::get<0>(tp), as...);
}
}; };
template <typename T, class F, typename... As> template <typename T, class F, typename... As>
@ -45,7 +61,8 @@ namespace MultiArrayTools
return ArgPack<sizeof...(As)-1>::template mk<F,std::tuple<As...> >(arg); return ArgPack<sizeof...(As)-1>::template mk<F,std::tuple<As...> >(arg);
} }
}; };
// OPERATIONS (STATIC) // OPERATIONS (STATIC)
template <typename T> template <typename T>
struct identity : public StaticFunctionBase<T, identity<T>, T> struct identity : public StaticFunctionBase<T, identity<T>, T>
@ -107,6 +124,32 @@ namespace MultiArrayTools
} }
}; };
// OPERATIONS (STATIC)
template <typename R, typename... Args>
class function
{
public:
static constexpr bool FISSTATIC = false;
private:
std::function<R(Args...)> mF;
public:
function() = default;
function(const std::function<R(Args...)>& in) : mF(in) {}
inline R operator()(const Args&... args)
{
return mF(args...);
}
inline R operator()(const std::tuple<Args...>& args)
{
return ArgPack<sizeof...(Args)-1>::template mkd<std::function<R(Args...)>,std::tuple<Args...>>>(mF, args);
}
};
#include <cmath> #include <cmath>
#define regFunc1(fff) template <typename T>\ #define regFunc1(fff) template <typename T>\
struct x_##fff : public StaticFunctionBase<T, x_##fff<T>, T> {\ struct x_##fff : public StaticFunctionBase<T, x_##fff<T>, T> {\

View file

@ -76,6 +76,15 @@ namespace MultiArrayTools
(THIS(), inds...); (THIS(), inds...);
} }
template <typename T, class OperationClass>
template <typename R, class... Args>
auto OperationBase<T,OperationClass>::a(const function<R,T,typename Args::value_type...>& ll,
const Args&... args) const
-> Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>
{
return Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>(THIS(), args...);
}
/***************************************** /*****************************************
* OperationMaster::AssignmentExpr * * OperationMaster::AssignmentExpr *
*****************************************/ *****************************************/

View file

@ -60,7 +60,11 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
auto slc(const std::shared_ptr<Indices>&... inds) const auto slc(const std::shared_ptr<Indices>&... inds) const
-> SliceContraction<T,OperationClass,Indices...>; -> SliceContraction<T,OperationClass,Indices...>;
template <typename R, class... Args> // Args = Operation Classes
auto a(const function<R,T,typename Args::value_type...>& ll, const Args&... args) const
-> Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>;
private: private:
friend OperationClass; friend OperationClass;
friend OperationTemplate<T,OperationClass>; friend OperationTemplate<T,OperationClass>;