functional.hpp 2.37 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
#ifndef MIGRAPH_GUARD_RTGLIB_FUNCTIONAL_HPP
#define MIGRAPH_GUARD_RTGLIB_FUNCTIONAL_HPP

#include <utility>
5
#include <migraph/config.hpp>
Paul's avatar
Paul committed
6

7
namespace migraph { inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
8

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

Paul's avatar
Paul committed
17
18
namespace detail {

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

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

Paul's avatar
Paul committed
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
64
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
65
66
67
68
69
70
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
71
72
} // namespace detail

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

Paul's avatar
Paul committed
79
80
81
82
83
84
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
85
86
87
88
89
90
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
91
92
93
94
95
template <class F>
constexpr void each_args(F)
{
}

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

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

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

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

Paul's avatar
Paul committed
121
template <class F, class T, class U, class... Ts>
Paul's avatar
Paul committed
122
123
124
125
126
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
127
template <class F>
Paul's avatar
Paul committed
128
129
auto fold(F f)
{
Paul's avatar
Paul committed
130
    return [=](auto&&... xs) { return fold_impl(f, std::forward<decltype(xs)>(xs)...); };
Paul's avatar
Paul committed
131
132
}

133
} // inline namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
134
135
136
} // namespace migraph

#endif