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

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

Paul's avatar
Paul committed
7
namespace migraphx {
Paul's avatar
Paul committed
8
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
9

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

Paul's avatar
Paul committed
18
template <class T>
Paul's avatar
Paul committed
19
20
21
22
23
auto tuple_size(const T&)
{
    return typename std::tuple_size<T>::type{};
}

Paul's avatar
Paul committed
24
25
namespace detail {

Paul's avatar
Paul committed
26
template <class R, class F>
Paul's avatar
Paul committed
27
28
29
30
struct fix_f
{
    F f;

Paul's avatar
Paul committed
31
    template <class... Ts>
Paul's avatar
Paul committed
32
33
34
35
36
37
    R operator()(Ts&&... xs) const
    {
        return f(*this, std::forward<Ts>(xs)...);
    }
};

Paul's avatar
Paul committed
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
65
66
67
68
69
70
71
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
72
73
74
75
76
77
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
78
79
} // namespace detail

Paul's avatar
Paul committed
80
template <std::size_t N, class F>
Paul's avatar
Paul committed
81
82
83
84
85
constexpr void repeat_c(F f)
{
    detail::repeat_c_impl(f, detail::gens<N>{});
}

Paul's avatar
Paul committed
86
87
88
89
90
91
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
92
93
94
95
96
97
template <class IntegerConstant, class F>
constexpr auto sequence(IntegerConstant ic, F&& f)
{
    return sequence_c<ic>(f);
}

Paul's avatar
Paul committed
98
99
100
101
102
103
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
104
105
106
107
108
template <class F>
constexpr void each_args(F)
{
}

Paul's avatar
Paul committed
109
template <class F, class T>
Paul's avatar
Paul committed
110
auto unpack(F f, T&& x)
Paul's avatar
Paul committed
111
{
Paul's avatar
Paul committed
112
    return sequence(tuple_size(x), [&](auto... is) { f(std::get<is>(static_cast<T&&>(x))...); });
Paul's avatar
Paul committed
113
114
}

Paul's avatar
Paul committed
115
/// Implements a fix-point combinator
Paul's avatar
Paul committed
116
template <class R, class F>
Paul's avatar
Paul committed
117
118
119
120
121
detail::fix_f<R, F> fix(F f)
{
    return {f};
}

Paul's avatar
Paul committed
122
template <class F>
Paul's avatar
Paul committed
123
124
125
126
127
auto fix(F f)
{
    return fix<void>(f);
}

Paul's avatar
Paul committed
128
template <class F, class T>
Paul's avatar
Paul committed
129
130
auto fold_impl(F&&, T&& x)
{
Paul Fultz II's avatar
Paul Fultz II committed
131
    return std::forward<T>(x);
Paul's avatar
Paul committed
132
133
}

Paul's avatar
Paul committed
134
template <class F, class T, class U, class... Ts>
Paul's avatar
Paul committed
135
136
137
138
139
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
140
template <class F>
Paul's avatar
Paul committed
141
142
auto fold(F f)
{
Paul's avatar
Paul committed
143
    return [=](auto&&... xs) { return fold_impl(f, std::forward<decltype(xs)>(xs)...); };
Paul's avatar
Paul committed
144
145
}

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
template <class... Ts>
auto pack(Ts... xs)
{
    return [=](auto f) { return f(xs...); };
}

inline auto pack_join() { return pack(); }

template <class... Ps>
auto pack_join(Ps... ps)
{
    return fold([](auto p1, auto p2) {
        return p1([=](auto... xs) { return p2([=](auto... ys) { return pack(xs..., ys...); }); });
    })(ps...);
}

Paul's avatar
Paul committed
162
163
164
165
166
167
168
169
170
171
172
173
template <class F, class Proj>
auto by(F f, Proj proj)
{
    return [=](auto&&... xs) { return f(proj(std::forward<decltype(xs)>(xs))...); };
}

template <class T>
auto index_of(T& x)
{
    return [&](auto&& y) { return x[y]; };
}

Paul's avatar
Paul committed
174
template <class T, class... Ts>
Paul's avatar
Paul committed
175
176
177
178
179
decltype(auto) front_args(T&& x, Ts&&...)
{
    return static_cast<T&&>(x);
}

Paul's avatar
Paul committed
180
template <class... Ts>
Paul's avatar
Paul committed
181
182
183
184
185
decltype(auto) back_args(Ts&&... xs)
{
    return std::get<sizeof...(Ts) - 1>(std::tuple<Ts&&...>(static_cast<Ts&&>(xs)...));
}

Paul's avatar
Paul committed
186
template <class T, class... Ts>
Paul's avatar
Paul committed
187
188
auto pop_front_args(T&&, Ts&&... xs)
{
Paul's avatar
Paul committed
189
    return [&](auto f) { f(static_cast<Ts&&>(xs)...); };
Paul's avatar
Paul committed
190
191
}

Paul's avatar
Paul committed
192
template <class... Ts>
Paul's avatar
Paul committed
193
194
195
196
auto pop_back_args(Ts&&... xs)
{
    return [&](auto f) {
        using tuple_type = std::tuple<Ts&&...>;
Paul's avatar
Paul committed
197
        auto t           = tuple_type(static_cast<Ts&&>(xs)...);
Paul's avatar
Paul committed
198
199
        return sequence_c<sizeof...(Ts) - 1>(
            [&](auto... is) { return f(std::get<is>(static_cast<tuple_type&&>(t))...); });
Paul's avatar
Paul committed
200
201
202
    };
}

Paul's avatar
Paul committed
203
template <class T>
Paul's avatar
Paul committed
204
205
206
struct always_f
{
    T x;
Paul's avatar
Paul committed
207
    template <class... Ts>
Paul's avatar
Paul committed
208
209
210
211
212
213
    constexpr T operator()(Ts&&...) const
    {
        return x;
    }
};

Paul's avatar
Paul committed
214
template <class T>
215
216
auto always(T x)
{
Paul's avatar
Paul committed
217
    return always_f<T>{x};
218
219
}

220
221
222
223
224
225
226
227
228
struct id
{
    template <class T>
    constexpr T operator()(T&& x) const
    {
        return static_cast<T&&>(x);
    }
};

229
230
231
232
233
template <class... Ts>
void nop(Ts&&...)
{
}

Paul's avatar
Paul committed
234
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
235
} // namespace migraphx
Paul's avatar
Paul committed
236
237

#endif