-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstexpr_for.hpp
77 lines (68 loc) · 1.9 KB
/
constexpr_for.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include<utility>
#include<cstddef>
namespace mlib
{
struct with_indexes {};
template<auto... Values>
struct sequence
{
constexpr auto operator()()
{
(Values(), ...);
}
// requires `indexes` to be the same length as `Values`
template<std::size_t... indexes>
constexpr auto operator()(std::index_sequence<indexes...>)
{
(Values(indexes), ...);
}
};
template<auto Val, auto condition, auto operation, auto to_do, auto... operations>
constexpr auto make_sequence_impl()
{
if constexpr (condition(Val))
{
return make_sequence_impl<operation(Val), condition, operation, to_do, operations..., to_do>();
}
else
{
return sequence<operations...>{};
}
}
template<auto Val, auto condition, auto operation, auto to_do>
constexpr auto make_sequence()
{
if constexpr (condition(Val))
{
return make_sequence_impl<condition(Val), condition, operation, to_do>();
}
else
{
return []() {};
}
}
template<auto Val, auto condition, auto operation>
struct constexpr_for
{
constexpr constexpr_for(auto loop_operation)
{
auto s = make_sequence<Val, condition, operation, loop_operation>();
auto x = []<auto... Is>(sequence<Is...> s_)
{
s_();
};
x(s);
}
constexpr constexpr_for(auto loop_operation, with_indexes)
{
auto s = make_sequence<Val, condition, operation, loop_operation>();
auto x = []<auto... Is>(sequence<Is...> s_)
{
constexpr auto x = std::make_index_sequence<sizeof...(Is)>{};
s_(x);
};
x(s);
}
};
} // namespace mlib