2018-01-15 18:31:47 +01:00
|
|
|
|
2021-07-28 20:59:31 +02:00
|
|
|
#ifndef __cxz_arith_h__
|
|
|
|
#define __cxz_arith_h__
|
2018-01-15 18:31:47 +01:00
|
|
|
|
2018-11-26 12:10:38 +01:00
|
|
|
#include <functional>
|
|
|
|
|
2021-07-28 20:29:56 +02:00
|
|
|
namespace CNORXZ
|
2018-01-15 18:31:47 +01:00
|
|
|
{
|
2018-07-17 20:01:25 +02:00
|
|
|
|
2021-06-09 17:25:02 +02:00
|
|
|
//template <typename T, class F, typename... As>
|
|
|
|
template <class F>
|
2018-07-17 20:01:25 +02:00
|
|
|
struct StaticFunctionBase
|
|
|
|
{
|
|
|
|
static constexpr bool FISSTATIC = true;
|
2021-01-18 19:00:12 +01:00
|
|
|
typedef F function;
|
2021-06-09 17:25:02 +02:00
|
|
|
//typedef typename F::value_type value_type;
|
2021-01-18 19:00:12 +01:00
|
|
|
|
2018-07-17 20:01:25 +02:00
|
|
|
template <class... Ops>
|
2021-06-09 17:25:02 +02:00
|
|
|
static auto mk(const Ops&... ops);
|
2018-07-17 20:01:25 +02:00
|
|
|
|
2021-06-09 17:25:02 +02:00
|
|
|
template <size_t N, class Tuple, typename... As>
|
|
|
|
static inline auto xapply(const Tuple& tp, As... as);
|
|
|
|
|
|
|
|
template <typename... As>
|
|
|
|
static inline auto apply(const std::tuple<As...>& arg);
|
2018-07-17 20:01:25 +02:00
|
|
|
};
|
2018-11-26 12:10:38 +01:00
|
|
|
|
|
|
|
|
2018-01-15 18:31:47 +01:00
|
|
|
// OPERATIONS (STATIC)
|
2018-07-17 20:01:25 +02:00
|
|
|
template <typename T>
|
2021-06-09 17:25:02 +02:00
|
|
|
struct identity : public StaticFunctionBase<identity<T>>
|
2018-07-17 20:01:25 +02:00
|
|
|
{
|
|
|
|
//static constexpr bool FISSTATIC = true;
|
2021-06-09 17:25:02 +02:00
|
|
|
using StaticFunctionBase<identity<T>>::apply;
|
|
|
|
typedef T value_type;
|
|
|
|
|
2018-07-17 20:01:25 +02:00
|
|
|
static inline T apply(T a)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
2021-01-18 19:00:12 +01:00
|
|
|
|
|
|
|
static inline T selfApply(T& a1, const T& a2)
|
|
|
|
{
|
|
|
|
return a1 = a2;
|
|
|
|
}
|
2018-07-17 20:01:25 +02:00
|
|
|
};
|
2019-07-24 18:49:53 +02:00
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
using plusv = decltype(std::declval<T>()+std::declval<U>());
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
using minusv = decltype(std::declval<T>()-std::declval<U>());
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
using multipliesv = decltype(std::declval<T>()*std::declval<U>());
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
using dividesv = decltype(std::declval<T>()/std::declval<U>());
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2021-06-09 17:25:02 +02:00
|
|
|
struct plusx : public StaticFunctionBase<plusx<T,U>>
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
static constexpr bool FISSTATIC = true;
|
2021-06-09 17:25:02 +02:00
|
|
|
using StaticFunctionBase<plusx<T,U>>::apply;
|
|
|
|
typedef plusv<T,U> value_type;
|
|
|
|
|
|
|
|
static inline value_type apply(T a1, U a2)
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
return a1 + a2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline T& selfApply(T& a1, const T& a2)
|
|
|
|
{
|
|
|
|
return a1 += a2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2021-06-09 17:25:02 +02:00
|
|
|
struct minusx : public StaticFunctionBase<minusx<T,U>>
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
static constexpr bool FISSTATIC = true;
|
2021-06-09 17:25:02 +02:00
|
|
|
using StaticFunctionBase<minusx<T,U>>::apply;
|
|
|
|
typedef minusv<T,U> value_type;
|
2019-07-24 18:49:53 +02:00
|
|
|
|
2021-06-09 17:25:02 +02:00
|
|
|
static inline value_type apply(T a1, U a2)
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
return a1 - a2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2021-06-09 17:25:02 +02:00
|
|
|
struct multipliesx : public StaticFunctionBase<multipliesx<T,U>>
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
static constexpr bool FISSTATIC = true;
|
2021-06-09 17:25:02 +02:00
|
|
|
using StaticFunctionBase<multipliesx<T,U>>::apply;
|
|
|
|
typedef multipliesv<T,U> value_type;
|
2019-07-24 18:49:53 +02:00
|
|
|
|
2021-06-09 17:25:02 +02:00
|
|
|
static inline value_type apply(T a1, U a2)
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
return a1 * a2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2021-06-09 17:25:02 +02:00
|
|
|
struct dividesx : public StaticFunctionBase<dividesx<T,U>>
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
static constexpr bool FISSTATIC = true;
|
2021-06-09 17:25:02 +02:00
|
|
|
using StaticFunctionBase<dividesx<T,U>>::apply;
|
|
|
|
typedef dividesv<T,U> value_type;
|
2019-07-24 18:49:53 +02:00
|
|
|
|
2021-06-09 17:25:02 +02:00
|
|
|
static inline value_type apply(T a1, U a2)
|
2019-07-24 18:49:53 +02:00
|
|
|
{
|
|
|
|
return a1 / a2;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-12-07 13:46:01 +01:00
|
|
|
template <typename T>
|
2021-06-09 17:25:02 +02:00
|
|
|
struct negate : public StaticFunctionBase<negate<T>>
|
2020-12-07 13:46:01 +01:00
|
|
|
{
|
|
|
|
static constexpr bool FISSTATIC = true;
|
2021-06-09 17:25:02 +02:00
|
|
|
using StaticFunctionBase<negate<T>>::apply;
|
|
|
|
typedef T value_type;
|
|
|
|
|
2020-12-07 13:46:01 +01:00
|
|
|
static inline T apply(T a)
|
|
|
|
{
|
|
|
|
return -a;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-07-24 18:49:53 +02:00
|
|
|
template <typename T>
|
|
|
|
using plus = plusx<T,T>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using minus = minusx<T,T>;
|
2018-07-17 20:01:25 +02:00
|
|
|
|
2019-07-24 18:49:53 +02:00
|
|
|
template <typename T>
|
|
|
|
using multiplies = multipliesx<T,T>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using divides = dividesx<T,T>;
|
|
|
|
|
2018-11-26 12:10:38 +01:00
|
|
|
// 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...);
|
|
|
|
}
|
|
|
|
|
2021-06-09 17:25:02 +02:00
|
|
|
template <size_t N, class Tuple, typename... As>
|
|
|
|
static inline auto xapply(const std::function<R(Args...)>& ff, const Tuple& tp, As... as)
|
|
|
|
{
|
|
|
|
if constexpr(N > 0){
|
|
|
|
return xapply<N-1>(ff, tp, std::get<N>(tp), as...);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ff(std::get<0>(tp), as...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 12:10:38 +01:00
|
|
|
inline R operator()(const std::tuple<Args...>& args)
|
|
|
|
{
|
2021-06-09 17:25:02 +02:00
|
|
|
return xapply<sizeof...(Args)-1>(mF, args);
|
2018-11-26 12:10:38 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-19 19:29:52 +02:00
|
|
|
#include <cmath>
|
|
|
|
#define regFunc1(fff) template <typename T>\
|
2021-06-09 17:25:02 +02:00
|
|
|
struct x_##fff : public StaticFunctionBase<x_##fff<T>> {\
|
2018-07-19 19:29:52 +02:00
|
|
|
static constexpr bool FISSTATIC = true;\
|
2021-06-09 17:25:02 +02:00
|
|
|
typedef T value_type; \
|
2018-07-19 19:29:52 +02:00
|
|
|
static inline T apply(T a){\
|
|
|
|
return fff(a); } };
|
|
|
|
|
|
|
|
#include "extensions/math.h"
|
|
|
|
#undef regFunc1
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
struct x_ipow
|
|
|
|
{
|
|
|
|
static constexpr bool FISSTATIC = true;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T apply(T a)
|
|
|
|
{
|
2021-06-09 17:25:02 +02:00
|
|
|
if constexpr(N > 0){
|
|
|
|
return a * x_ipow<N-1>::apply(a);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return a;
|
|
|
|
}
|
2018-07-19 19:29:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-28 20:29:56 +02:00
|
|
|
} // end namespace CNORXZInternal
|
2021-06-09 17:25:02 +02:00
|
|
|
|
|
|
|
#include "arith.cc.h"
|
|
|
|
|
2018-01-15 18:31:47 +01:00
|
|
|
#endif
|