refactor: first_100_primes refactored to use a varaible template for caching the result

This commit is contained in:
Mateusz Pusz
2024-11-16 22:00:21 +01:00
parent 658c0950f8
commit e1b0fb7e6e

View File

@@ -442,20 +442,12 @@ constexpr auto get_first_of(const Rng& rng, UnaryFunction f)
return get_first_of(begin(rng), end(rng), f); return get_first_of(begin(rng), end(rng), f);
} }
#if __cpp_constexpr < 202211L
template<std::size_t N> template<std::size_t N>
struct first_n_primes_impl { constexpr auto first_n_primes_result = first_n_primes<N>();
static constexpr auto value = first_n_primes<N>();
};
#endif
[[nodiscard]] consteval std::uintmax_t find_first_factor(std::uintmax_t n) [[nodiscard]] consteval std::uintmax_t find_first_factor(std::uintmax_t n)
{ {
#if __cpp_constexpr >= 202211L constexpr auto first_100_primes = first_n_primes_result<100>;
static constexpr auto first_100_primes = first_n_primes<100>();
#else
constexpr auto first_100_primes = first_n_primes_impl<100>::value;
#endif
for (const auto& p : first_100_primes) { for (const auto& p : first_100_primes) {
if (n % p == 0u) { if (n % p == 0u) {