functional.hpp 2.21 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
/// Implements a fix-point combinator
Paul's avatar
Paul committed
91
template <class R, class F>
Paul's avatar
Paul committed
92
93
94
95
96
detail::fix_f<R, F> fix(F f)
{
    return {f};
}

Paul's avatar
Paul committed
97
template <class F>
Paul's avatar
Paul committed
98
99
100
101
102
auto fix(F f)
{
    return fix<void>(f);
}

Paul's avatar
Paul committed
103
template <class... Ts>
Paul's avatar
Paul committed
104
auto pack(Ts... xs)
105
{
Paul's avatar
Paul committed
106
    return [=](auto f) { return f(xs...); };
107
108
}

Paul's avatar
Paul committed
109
template <class F, class T>
Paul's avatar
Paul committed
110
111
112
113
114
auto fold_impl(F&&, T&& x)
{
    return x;
}

Paul's avatar
Paul committed
115
template <class F, class T, class U, class... Ts>
Paul's avatar
Paul committed
116
117
118
119
120
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
121
template <class F>
Paul's avatar
Paul committed
122
123
auto fold(F f)
{
Paul's avatar
Paul committed
124
    return [=](auto&&... xs) { return fold_impl(f, std::forward<decltype(xs)>(xs)...); };
Paul's avatar
Paul committed
125
126
}

Paul's avatar
Paul committed
127
128
129
} // namespace migraph

#endif