|
| 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 |
0 commit comments