functional.hpp 2.26 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
#ifndef MIGRAPH_GUARD_RTGLIB_FUNCTIONAL_HPP
#define MIGRAPH_GUARD_RTGLIB_FUNCTIONAL_HPP

#include <utility>

namespace migraph {

Paul's avatar
Paul committed
8
9
10
11
12
13
14
15
struct swallow
{
    template <class... Ts>
    constexpr swallow(Ts&&...)
    {
    }
};

Paul's avatar
Paul committed
16
17
namespace detail {

Paul's avatar
Paul committed
18
template <class R, class F>
Paul's avatar
Paul committed
19
20
21
22
struct fix_f
{
    F f;

Paul's avatar
Paul committed
23
    template <class... Ts>
Paul's avatar
Paul committed
24
25
26
27
28
29
    R operator()(Ts&&... xs) const
    {
        return f(*this, std::forward<Ts>(xs)...);
    }
};

Paul's avatar
Paul committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
template <std::size_t...>
struct seq
{
    using type = seq;
};

template <class, class>
struct merge_seq;

template <std::size_t... Xs, std::size_t... Ys>
struct merge_seq<seq<Xs...>, seq<Ys...>> : seq<Xs..., (sizeof...(Xs) + Ys)...>
{
};

template <std::size_t N>
struct gens : merge_seq<typename gens<N / 2>::type, typename gens<N - N / 2>::type>
{
};

template <>
struct gens<0> : seq<>
{
};
template <>
struct gens<1> : seq<0>
{
};

template <class F, std::size_t... Ns>
constexpr void repeat_c_impl(F f, seq<Ns...>)
{
    swallow{(f(std::integral_constant<std::size_t, Ns>{}), 0)...};
}

Paul's avatar
Paul committed
64
65
66
67
68
69
template <class F, std::size_t... Ns>
constexpr auto sequence_c_impl(F&& f, seq<Ns...>)
{
    return f(std::integral_constant<std::size_t, Ns>{}...);
}

Paul's avatar
Paul committed
70
71
} // namespace detail

Paul's avatar
Paul committed
72
template <std::size_t N, class F>
Paul's avatar
Paul committed
73
74
75
76
77
constexpr void repeat_c(F f)
{
    detail::repeat_c_impl(f, detail::gens<N>{});
}

Paul's avatar
Paul committed
78
79
80
81
82
83
template <std::size_t N, class F>
constexpr auto sequence_c(F&& f)
{
    return detail::sequence_c_impl(f, detail::gens<N>{});
}

Paul's avatar
Paul committed
84
85
86
87
88
89
template <class F, class... Ts>
constexpr void each_args(F f, Ts&&... xs)
{
    swallow{(f(std::forward<Ts>(xs)), 0)...};
}

Paul's avatar
Paul committed
90
91
92
93
94
template <class F>
constexpr void each_args(F)
{
}

Paul's avatar
Paul committed
95
/// Implements a fix-point combinator
Paul's avatar
Paul committed
96
template <class R, class F>
Paul's avatar
Paul committed
97
98
99
100
101
detail::fix_f<R, F> fix(F f)
{
    return {f};
}

Paul's avatar
Paul committed
102
template <class F>
Paul's avatar
Paul committed
103
104
105
106
107
auto fix(F f)
{
    return fix<void>(f);
}

Paul's avatar
Paul committed
108
template <class... Ts>
Paul's avatar
Paul committed
109
auto pack(Ts... xs)
110
{
Paul's avatar
Paul committed
111
    return [=](auto f) { return f(xs...); };
112
113
}

Paul's avatar
Paul committed
114
template <class F, class T>
Paul's avatar
Paul committed
115
116
117
118
119
auto fold_impl(F&&, T&& x)
{
    return x;
}

Paul's avatar
Paul committed
120
template <class F, class T, class U, class... Ts>
Paul's avatar
Paul committed
121
122
123
124
125
auto fold_impl(F&& f, T&& x, U&& y, Ts&&... xs)
{
    return fold_impl(f, f(std::forward<T>(x), std::forward<U>(y)), std::forward<Ts>(xs)...);
}

Paul's avatar
Paul committed
126
template <class F>
Paul's avatar
Paul committed
127
128
auto fold(F f)
{
Paul's avatar
Paul committed
129
    return [=](auto&&... xs) { return fold_impl(f, std::forward<decltype(xs)>(xs)...); };
Paul's avatar
Paul committed
130
131
}

Paul's avatar
Paul committed
132
133
134
} // namespace migraph

#endif