Skip to content

Commit 848b2dd

Browse files
add compile_time_while as a different implementation of constexpr_while
1 parent 005a9e6 commit 848b2dd

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

compile_time_while.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#pragma once
2+
3+
4+
#include<tuple>
5+
#include<utility>
6+
#include<cstddef>
7+
#include<iostream>
8+
9+
namespace mlib
10+
{
11+
12+
template<auto start_value, auto condition_for_val, auto operation_on_val>
13+
struct pack
14+
{
15+
constexpr auto operator()()
16+
{
17+
if constexpr (!condition_for_val(start_value))
18+
{
19+
return 0;
20+
}
21+
else
22+
{
23+
return 1 + pack<operation_on_val(start_value), condition_for_val, operation_on_val>{}();
24+
}
25+
}
26+
};
27+
28+
template<auto Care, auto DontCare>
29+
struct any_first
30+
{
31+
constexpr auto operator()()
32+
{
33+
return Care;
34+
}
35+
};
36+
37+
template<auto iterations, auto lambda>
38+
constexpr auto make_tuple_sequence()
39+
{
40+
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
41+
{
42+
return std::tuple{ (any_first<lambda, indexes>{}())... };
43+
}(std::make_index_sequence<iterations>{});
44+
}
45+
46+
template<typename... Ts>
47+
constexpr auto for_each_lambda(std::tuple<Ts...> tup)
48+
{
49+
[&] <std::size_t... indexes>(std::make_index_sequence<indexes...>)
50+
{
51+
(std::get<indexes>(tup)(), ...);
52+
}(std::make_index_sequence<sizeof...(Ts)>{});
53+
}
54+
55+
template<auto start_value, auto condition_for_val, auto operation_on_val>
56+
struct compile_time_while
57+
{
58+
constexpr auto operator()(auto operation_to_do)
59+
{
60+
// first of all, make a value that is how many times the operations are true.
61+
constexpr auto amount_of_iterations = pack<start_value, condition_for_val, operation_on_val>{}();
62+
// so now that we have the number of iterations, we need to make a tuple of size: amount_of_iterations
63+
// and then we need to do a for_each on them! simple!
64+
constexpr auto tuple_sequence = make_tuple_sequence<amount_of_iterations, operation_to_do>();
65+
// now do for_each
66+
for_each_lambda(tuple_sequence);
67+
}
68+
};
69+
} // namespace mlib

mlib.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<ItemGroup>
134134
<ClInclude Include="all_of.hpp" />
135135
<ClInclude Include="amount.hpp" />
136+
<ClInclude Include="compile_time_while.hpp" />
136137
<ClInclude Include="constant_parameter.hpp" />
137138
<ClInclude Include="constexpr_for.hpp" />
138139
<ClInclude Include="constexpr_map.hpp" />

mlib.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
<ClInclude Include="parse_rules.hpp">
148148
<Filter>Header Files</Filter>
149149
</ClInclude>
150+
<ClInclude Include="compile_time_while.hpp">
151+
<Filter>Header Files</Filter>
152+
</ClInclude>
150153
</ItemGroup>
151154
<ItemGroup>
152155
<ClCompile Include="Main.cpp">

0 commit comments

Comments
 (0)