Skip to content

Commit c9d0abc

Browse files
add compile_time_while example.
1 parent 182764c commit c9d0abc

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

Main.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
#include<iostream> // std::cout
33
#include<utility> // std::make_index_sequence
44

5-
#include "for_each.hpp" // mlib::for_each
6-
#include "tuple.hpp" // mlib::tuple
7-
#include "transform.hpp" // mlib::transform
8-
#include "select.hpp" // mlib::select
9-
#include "find.hpp" // mlib::find
10-
#include "tuple_reverse.hpp" // mlib::tuple_reverse
11-
#include "constexpr_for.hpp" // mlib::constexpr_for
12-
#include "constexpr_while.hpp" // mlib::constexpr_while
13-
#include "refl_get.hpp" // mlib::refl_get
14-
#include "get_nth_element.hpp" // mlib::get_nth_element
15-
#include "pack.hpp" // mlib::value_pack
16-
#include "constexpr_map.hpp" // mlib::constexpr_map
17-
#include "count_if.hpp" // mlib::count_if
18-
#include "pack_find.hpp" // mlib::pack_find
19-
#include "parse_rules.hpp" // mlib::parse_rules
20-
#include "constexpr_switch.hpp"// mlib::constexpr_switch
21-
#include "constexpr_range.hpp" // mlib::constexpr_range
22-
#include "string_parse.hpp" // mlib::string_parse
5+
#include "for_each.hpp" // mlib::for_each
6+
#include "tuple.hpp" // mlib::tuple
7+
#include "transform.hpp" // mlib::transform
8+
#include "select.hpp" // mlib::select
9+
#include "find.hpp" // mlib::find
10+
#include "tuple_reverse.hpp" // mlib::tuple_reverse
11+
#include "constexpr_for.hpp" // mlib::constexpr_for
12+
#include "constexpr_while.hpp" // mlib::constexpr_while
13+
#include "refl_get.hpp" // mlib::refl_get
14+
#include "get_nth_element.hpp" // mlib::get_nth_element
15+
#include "pack.hpp" // mlib::value_pack
16+
#include "constexpr_map.hpp" // mlib::constexpr_map
17+
#include "count_if.hpp" // mlib::count_if
18+
#include "pack_find.hpp" // mlib::pack_find
19+
#include "parse_rules.hpp" // mlib::parse_rules
20+
#include "constexpr_switch.hpp" // mlib::constexpr_switch
21+
#include "constexpr_range.hpp" // mlib::constexpr_range
22+
#include "string_parse.hpp" // mlib::string_parse
23+
#include "compile_time_while.hpp" // mlib::compile_time_while
2324

2425
int main()
2526
{
@@ -40,7 +41,11 @@ int main()
4041
mlib::constexpr_while < 0, [](int t) {t++; return t < 3; }, [&]() {std::cout << "Hello\n"; }, [](int t) {return t + 1; } > ();
4142

4243
// highest number of recursion.
43-
mlib::constexpr_while < 0, [](int t) {t++; return t < 501; }, [&]() {std::cout << "."; }, [](int t) {return t + 1; } > ();
44+
mlib::constexpr_while < 0, [](int t) {t++; return t < 501; }, []() {std::cout << "."; }, [](int t) {return t + 1; } > ();
45+
46+
std::cout << "\n\n\n";
47+
48+
mlib::compile_time_while < 0, [](int t) {return t < 5; }, [](int t) {return t + 1; }, []() {std::cout << "."; } > ();
4449

4550
struct foo { int a; double b; char c; };
4651
foo f{42, 3.14, 'c'};

compile_time_while.hpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,10 @@ namespace mlib
3333
}
3434
};
3535

36-
template<auto iterations, auto lambda>
37-
constexpr auto make_tuple_sequence()
38-
{
39-
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
40-
{
41-
return std::tuple{ (any_first<lambda, indexes>{}())... };
42-
}(std::make_index_sequence<iterations>{});
43-
}
44-
45-
template<typename... Ts>
46-
constexpr auto for_each_lambda(std::tuple<Ts...> tup)
47-
{
48-
[&] <std::size_t... indexes>(std::index_sequence<indexes...>)
49-
{
50-
(std::get<indexes>(tup)(), ...);
51-
}(std::make_index_sequence<sizeof...(Ts)>{});
52-
}
53-
54-
template<auto start_value, auto condition_for_val, auto operation_on_val>
36+
template<auto start_value, auto condition_for_val, auto operation_on_val, auto operation_to_do>
5537
struct compile_time_while
5638
{
57-
constexpr auto operator()(auto operation_to_do)
39+
constexpr auto operator()()
5840
{
5941
// first of all, make a value that is how many times the operations are true.
6042
constexpr auto amount_of_iterations = pack<start_value, condition_for_val, operation_on_val>{}();
@@ -64,5 +46,23 @@ namespace mlib
6446
// now do for_each
6547
for_each_lambda(tuple_sequence);
6648
}
49+
50+
template<auto iterations, auto lambda>
51+
constexpr auto make_tuple_sequence()
52+
{
53+
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
54+
{
55+
return std::tuple{ (any_first<lambda, indexes>{}())... };
56+
}(std::make_index_sequence<iterations>{});
57+
}
58+
59+
template<typename... Ts>
60+
constexpr auto for_each_lambda(std::tuple<Ts...> tup)
61+
{
62+
[&] <std::size_t... indexes>(std::index_sequence<indexes...>)
63+
{
64+
(std::get<indexes>(tup)(), ...);
65+
}(std::make_index_sequence<sizeof...(Ts)>{});
66+
}
6767
};
6868
} // namespace mlib

0 commit comments

Comments
 (0)