Run clang-format-15 on changed files
This commit is contained in:
1 parent
1e8460d401
commit
28c4fe3c08
4 files changed
+238
-215
No files matched your search
@@ -29,12 +29,14 @@
|
||||
#include <optional>
|
||||
#include <tuple>
|
||||
|
||||
namespace units::detail
|
||||
{
|
||||
namespace units::detail {
|
||||
|
||||
constexpr bool is_prime_by_trial_division(std::size_t n) {
|
||||
constexpr bool is_prime_by_trial_division(std::size_t n)
|
||||
{
|
||||
for (std::size_t f = 2; f * f <= n; f += 1 + (f % 2)) {
|
||||
if (n % f == 0) { return false; }
|
||||
if (n % f == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -42,25 +44,34 @@ constexpr bool is_prime_by_trial_division(std::size_t n) {
|
||||
// Return the first factor of n, as long as it is either k or n.
|
||||
//
|
||||
// Precondition: no integer smaller than k evenly divides n.
|
||||
constexpr std::optional<std::size_t> first_factor_maybe(std::size_t n, std::size_t k) {
|
||||
if (n % k == 0) { return k; }
|
||||
if (k * k > n) { return n; }
|
||||
constexpr std::optional<std::size_t> first_factor_maybe(std::size_t n, std::size_t k)
|
||||
{
|
||||
if (n % k == 0) {
|
||||
return k;
|
||||
}
|
||||
if (k * k > n) {
|
||||
return n;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
constexpr std::array<std::size_t, N> first_n_primes() {
|
||||
constexpr std::array<std::size_t, N> first_n_primes()
|
||||
{
|
||||
std::array<std::size_t, N> primes;
|
||||
primes[0] = 2;
|
||||
for (std::size_t i = 1; i < N; ++i) {
|
||||
primes[i] = primes[i - 1] + 1;
|
||||
while (!is_prime_by_trial_division(primes[i])) { ++primes[i]; }
|
||||
while (!is_prime_by_trial_division(primes[i])) {
|
||||
++primes[i];
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
template<std::size_t N, typename Callable>
|
||||
constexpr void call_for_coprimes_up_to(std::size_t n, const std::array<std::size_t, N> &basis, Callable &&call) {
|
||||
constexpr void call_for_coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis, Callable&& call)
|
||||
{
|
||||
for (std::size_t i = 0u; i < n; ++i) {
|
||||
if (std::apply([&i](auto... primes) { return ((i % primes != 0) && ...); }, basis)) {
|
||||
call(i);
|
||||
@@ -69,14 +80,16 @@ constexpr void call_for_coprimes_up_to(std::size_t n, const std::array<std::size
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
constexpr std::size_t num_coprimes_up_to(std::size_t n, const std::array<std::size_t, N> &basis) {
|
||||
constexpr std::size_t num_coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis)
|
||||
{
|
||||
std::size_t count = 0u;
|
||||
call_for_coprimes_up_to(n, basis, [&count](auto) { ++count; });
|
||||
return count;
|
||||
}
|
||||
|
||||
template<std::size_t ResultSize, std::size_t N>
|
||||
constexpr auto coprimes_up_to(std::size_t n, const std::array<std::size_t, N> &basis) {
|
||||
constexpr auto coprimes_up_to(std::size_t n, const std::array<std::size_t, N>& basis)
|
||||
{
|
||||
std::array<std::size_t, ResultSize> coprimes;
|
||||
std::size_t i = 0u;
|
||||
|
||||
@@ -86,7 +99,8 @@ constexpr auto coprimes_up_to(std::size_t n, const std::array<std::size_t, N> &b
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
constexpr std::size_t product(const std::array<std::size_t, N> &values) {
|
||||
constexpr std::size_t product(const std::array<std::size_t, N>& values)
|
||||
{
|
||||
return std::accumulate(std::begin(values), std::end(values), std::size_t{1u}, std::multiplies{});
|
||||
}
|
||||
|
||||
@@ -116,29 +130,36 @@ struct WheelFactorizer {
|
||||
static constexpr auto coprimes_in_first_wheel =
|
||||
coprimes_up_to<num_coprimes_up_to(wheel_size, basis)>(wheel_size, basis);
|
||||
|
||||
static constexpr std::size_t find_first_factor(std::size_t n) {
|
||||
static constexpr std::size_t find_first_factor(std::size_t n)
|
||||
{
|
||||
for (const auto& p : basis) {
|
||||
if (const auto k = first_factor_maybe(n, p)) { return *k; }
|
||||
if (const auto k = first_factor_maybe(n, p)) {
|
||||
return *k;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto it = std::next(std::begin(coprimes_in_first_wheel)); it != std::end(coprimes_in_first_wheel); ++it) {
|
||||
if (const auto k = first_factor_maybe(n, *it)) { return *k; }
|
||||
if (const auto k = first_factor_maybe(n, *it)) {
|
||||
return *k;
|
||||
}
|
||||
}
|
||||
|
||||
for (std::size_t wheel = wheel_size; wheel < n; wheel += wheel_size) {
|
||||
if (const auto k = first_factor_maybe(n, wheel + 1)) { return *k; }
|
||||
if (const auto k = first_factor_maybe(n, wheel + 1)) {
|
||||
return *k;
|
||||
}
|
||||
|
||||
for (const auto &p : coprimes_in_first_wheel) {
|
||||
if (const auto k = first_factor_maybe(n, wheel + p)) { return *k; }
|
||||
for (const auto& p : coprimes_in_first_wheel) {
|
||||
if (const auto k = first_factor_maybe(n, wheel + p)) {
|
||||
return *k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
static constexpr bool is_prime(std::size_t n) {
|
||||
return (n > 1) && find_first_factor(n) == n;
|
||||
}
|
||||
static constexpr bool is_prime(std::size_t n) { return (n > 1) && find_first_factor(n) == n; }
|
||||
};
|
||||
|
||||
} // namespace units::detail
|
||||
} // namespace units::detail
|
||||
Reference in new issue
Block a user