Ria*_*iaD 17 c++ templates compile-time template-meta-programming c++11
我需要在编译时检查一些整数素数(将布尔值作为模板参数).
我写的代码做得很好:
#include <type_traits>
namespace impl {
template <int n, long long i>
struct PrimeChecker {
typedef typename std::conditional<
(i * i > n),
std::true_type,
typename std::conditional<
n % i == 0,
std::false_type,
typename PrimeChecker<n, (i * i > n ) ? -1 : i + 1>::type
>::type
>::type type;
};
template <int n>
struct PrimeChecker<n, -1> {
typedef void type;
};
} // namespace impl
template<int n>
struct IsPrime {
typedef typename impl::PrimeChecker<n, 2>::type type;
};
template<>
struct IsPrime<1> : public std::false_type {
};
Run Code Online (Sandbox Code Playgroud)
它适用于~1000000的数字,并且10 9的错误失败
prog.cpp:15:23: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘struct impl::PrimeChecker<1000000000, 901ll>’
>::type type;
^
prog.cpp:15:23: recursively required from ‘struct impl::PrimeChecker<1000000000, 3ll>’
prog.cpp:15:23: required from ‘struct impl::PrimeChecker<1000000000, 2ll>’
prog.cpp:24:54: required from ‘struct IsPrime<1000000000>’
prog.cpp:32:41: required from here
Run Code Online (Sandbox Code Playgroud)
我无法增加深度限制.是否有可能减少我使用的深度?
我想要实现:我需要在编译时检查是否为常量素数而不更改具有模板深度限制900和constexpr深度限制512的编译字符串.(我的g ++默认).它适用于所有正int32或至少适用于最高10 9 +9的数字
Cas*_*sey 12
您可以通过使用分而治之算法将范围分成两半来将空间要求从线性更改为对数.这种方法使用分而治之,只测试奇数因素(Live at Coliru):
namespace detail {
using std::size_t;
constexpr size_t mid(size_t low, size_t high) {
return (low + high) / 2;
}
// precondition: low*low <= n, high*high > n.
constexpr size_t ceilsqrt(size_t n, size_t low, size_t high) {
return low + 1 >= high
? high
: (mid(low, high) * mid(low, high) == n)
? mid(low, high)
: (mid(low, high) * mid(low, high) < n)
? ceilsqrt(n, mid(low, high), high)
: ceilsqrt(n, low, mid(low, high));
}
// returns ceiling(sqrt(n))
constexpr size_t ceilsqrt(size_t n) {
return n < 3
? n
: ceilsqrt(n, 1, size_t(1) << (std::numeric_limits<size_t>::digits / 2));
}
// returns true if n is divisible by an odd integer in
// [2 * low + 1, 2 * high + 1).
constexpr bool find_factor(size_t n, size_t low, size_t high)
{
return low + 1 >= high
? (n % (2 * low + 1)) == 0
: (find_factor(n, low, mid(low, high))
|| find_factor(n, mid(low, high), high));
}
}
constexpr bool is_prime(std::size_t n)
{
using detail::find_factor;
using detail::ceilsqrt;
return n > 1
&& (n == 2
|| (n % 2 == 1
&& (n == 3
|| !find_factor(n, 1, (ceilsqrt(n) + 1) / 2))));
}
Run Code Online (Sandbox Code Playgroud)
编辑:使用编译时sqrt将搜索空间限制为上限(sqrt(n)),而不是n/2.现在可以在Coliru上is_prime(100000007)根据需要计算(并且is_prime(1000000000039ULL)就此而言)而不会爆炸.
对于可怕的格式化道歉,我仍然没有找到适合C++ 11折磨的constexpr子语言的舒适风格.
编辑:清理代码:用另一个函数替换宏,将实现细节移动到细节命名空间,从Pablo的答案中窃取缩进样式.
这是我对此的尝试.使用constexpr和米勒 - 拉宾素数测试的确定性变体,数字高达4,759,123,141(应覆盖所有uint32s,但您可以轻松更改引物检查器设置以覆盖更大的范围)
#include <cstdint>
constexpr uint64_t ct_mod_sqr(uint64_t a, uint64_t m)
{
return (a * a) % m;
}
constexpr uint64_t ct_mod_pow(uint64_t a, uint64_t n, uint64_t m)
{
return (n == 0)
? 1
: (ct_mod_sqr(ct_mod_pow(a, n/2, m), m) * ((n & 1) ? a : 1)) % m;
}
constexpr bool pass_prime_check_impl(uint64_t x, uint32_t n1, uint32_t s1)
{
return (s1 == 0)
? false
: (x == 1)
? false
: (x == n1)
? true
: pass_prime_check_impl(ct_mod_sqr(x, n1 + 1), n1, s1 - 1)
;
}
constexpr bool pass_prime_check_impl(uint32_t a, uint32_t n1, uint32_t s1, uint32_t d, uint64_t x)
{
return (x == 1) || (x == n1)
? true
: pass_prime_check_impl(ct_mod_sqr(x, n1 + 1), n1, s1)
;
}
constexpr bool pass_prime_check_impl(uint32_t a, uint32_t n1, uint32_t s1, uint32_t d)
{
return pass_prime_check_impl(a, n1, s1, d,
ct_mod_pow(a, d, n1 + 1));
}
constexpr bool pass_prime_check_impl(uint32_t n, uint32_t a)
{
return pass_prime_check_impl(a, n - 1,
__builtin_ctz(n - 1) - 1,
(n - 1) >> __builtin_ctz(n - 1));
}
constexpr bool pass_prime_check(uint32_t n, uint32_t p)
{
return (n == p)
? true
: pass_prime_check_impl(n, p);
}
constexpr bool is_prime(uint32_t n)
{
return (n == 2)
? true
: (n % 2 == 0)
? false
: (pass_prime_check(n, 2) &&
pass_prime_check(n, 7) &&
pass_prime_check(n, 61))
;
}
int main()
{
static_assert(is_prime(100000007), "100000007 is a prime!");
static_assert(is_prime(1000000007), "1000000007 is a prime!");
static_assert(is_prime(1000000009), "1000000009 is a prime!");
static_assert(!is_prime(1000000011), "1000000011 is not a prime!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2150 次 |
| 最近记录: |