-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc++] Refactor __next_prime to be const #157421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
#include <__utility/swap.h> | ||
#include <__utility/try_key_extraction.h> | ||
#include <limits> | ||
#include <stdexcept> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
|
@@ -72,7 +73,28 @@ struct __is_hash_value_type : false_type {}; | |
template <class _One> | ||
struct __is_hash_value_type<_One> : __is_hash_value_type_imp<__remove_cvref_t<_One> > {}; | ||
|
||
_LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t __n); | ||
_LIBCPP_HIDE_FROM_ABI inline void __check_for_overflow(size_t __n) { | ||
if _LIBCPP_CONSTEXPR (sizeof(size_t) == 4) { | ||
if (__n > 0xFFFFFFFB) | ||
std::__throw_overflow_error("__next_prime overflow"); | ||
} else { | ||
if (__n > 0xFFFFFFFFFFFFFFC5ull) | ||
std::__throw_overflow_error("__next_prime overflow"); | ||
} | ||
} | ||
|
||
#if _LIBCPP_AVAILABILITY_HAS_NEXT_PRIME_IMPL | ||
[[__gnu__::__const__]] _LIBCPP_EXPORTED_FROM_ABI size_t __next_prime_impl(size_t) _NOEXCEPT; | ||
|
||
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline size_t __get_next_prime(size_t __n) { | ||
__check_for_overflow(__n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this isn't necessary for ADL, but let's qualify these calls with |
||
return __next_prime_impl(__n); | ||
} | ||
#else | ||
_LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t); | ||
|
||
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline size_t __get_next_prime(size_t __n) { return __next_prime(__n); } | ||
#endif | ||
|
||
template <class _NodePtr> | ||
struct __hash_node_base { | ||
|
@@ -1764,15 +1786,15 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __n) _LIBCPP_D | |
if (__n == 1) | ||
__n = 2; | ||
else if (__n & (__n - 1)) | ||
__n = std::__next_prime(__n); | ||
__n = std::__get_next_prime(__n); | ||
size_type __bc = bucket_count(); | ||
if (__n > __bc) | ||
__do_rehash<_UniqueKeys>(__n); | ||
else if (__n < __bc) { | ||
__n = std::max<size_type>( | ||
__n, | ||
std::__is_hash_power2(__bc) ? std::__next_hash_pow2(size_t(__math::ceil(float(size()) / max_load_factor()))) | ||
: std::__next_prime(size_t(__math::ceil(float(size()) / max_load_factor())))); | ||
: std::__get_next_prime(size_t(__math::ceil(float(size()) / max_load_factor())))); | ||
if (__n < __bc) | ||
__do_rehash<_UniqueKeys>(__n); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,17 +51,7 @@ const unsigned indices[] = { | |
// are fewer potential primes to search, and fewer potential primes to divide | ||
// against. | ||
|
||
inline void __check_for_overflow(size_t N) { | ||
if constexpr (sizeof(size_t) == 4) { | ||
if (N > 0xFFFFFFFB) | ||
std::__throw_overflow_error("__next_prime overflow"); | ||
} else { | ||
if (N > 0xFFFFFFFFFFFFFFC5ull) | ||
std::__throw_overflow_error("__next_prime overflow"); | ||
} | ||
} | ||
|
||
size_t __next_prime(size_t n) { | ||
size_t __next_prime_impl(size_t n) noexcept { | ||
const size_t L = 210; | ||
const size_t N = sizeof(small_primes) / sizeof(small_primes[0]); | ||
// If n is small enough, search in small_primes | ||
|
@@ -446,4 +436,6 @@ size_t __next_prime(size_t n) { | |
} | ||
} | ||
|
||
_LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t n) { return __get_next_prime(n); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a comment explaining that this is only provided for backwards compatibility. |
||
|
||
_LIBCPP_END_NAMESPACE_STD |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
|
||
// <__hash_table> | ||
|
||
// size_t __next_prime(size_t n); | ||
// size_t __get_next_prime(size_t n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the name of the test change? |
||
|
||
// If n == 0, return 0, else return the lowest prime greater than or equal to n | ||
|
||
|
@@ -36,9 +36,9 @@ bool is_prime(std::size_t n) { | |
} | ||
|
||
int main(int, char**) { | ||
assert(std::__next_prime(0) == 0); | ||
assert(std::__get_next_prime(0) == 0); | ||
for (std::size_t n = 1; n <= 100000; ++n) { | ||
std::size_t p = std::__next_prime(n); | ||
std::size_t p = std::__get_next_prime(n); | ||
assert(p >= n); | ||
for (std::size_t i = n; i < p; ++i) | ||
assert(!is_prime(i)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like a short comment explaining what this does and why there's also
__next_prime
would be helpful. This seems confusing for someone not familiar with this patch. Maybe just explain that a new__next_prime_impl
was introduce to allow inlining the__check_overflow
call and this dance deals with backwards compatibility.