[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] src/: ceil_prime(): Add function to get the lowest prime not sma
From: |
Alejandro Colomar |
Subject: |
[PATCH] src/: ceil_prime(): Add function to get the lowest prime not smaller than n |
Date: |
Wed, 13 Mar 2024 10:53:55 +0100 |
And use it where the same logic was being open-coded.
Cc: "G. Branden Robinson" <branden@debian.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
Hi Branden,
Now this looks better. After this change, it seems to me more clear
that using 1 as the minimum is fine, since it only results in a warning,
and not in an error, so I've kept it.
Cheers,
Alex
src/include/lib.h | 2 +-
src/libs/libbib/index.cpp | 4 +---
src/libs/libgroff/prime.cpp | 15 ++++++++++++++-
src/utils/indxbib/indxbib.cpp | 6 +-----
4 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/src/include/lib.h b/src/include/lib.h
index 0f4ed458e..b6c98caee 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -56,7 +56,7 @@ extern "C" {
#include <stdbool.h>
char *strsave(const char *s);
-bool is_prime(unsigned);
+unsigned ceil_prime(unsigned);
double groff_hypot(double, double);
#include <stdio.h>
diff --git a/src/libs/libbib/index.cpp b/src/libs/libbib/index.cpp
index 18701a157..53088397a 100644
--- a/src/libs/libbib/index.cpp
+++ b/src/libs/libbib/index.cpp
@@ -611,9 +611,7 @@ void index_search_item::read_common_words_file()
error("can't open '%1': %2", common_words_file, strerror(errno));
return;
}
- common_words_table_size = 2*header.common + 1;
- while (!is_prime(common_words_table_size))
- common_words_table_size += 2;
+ common_words_table_size = ceil_prime(2 * header.common);
common_words_table = new char *[common_words_table_size];
for (int i = 0; i < common_words_table_size; i++)
common_words_table[i] = 0;
diff --git a/src/libs/libgroff/prime.cpp b/src/libs/libgroff/prime.cpp
index b1e73470a..0113b5b71 100644
--- a/src/libs/libgroff/prime.cpp
+++ b/src/libs/libgroff/prime.cpp
@@ -22,7 +22,20 @@ internet at <http://www.gnu.org/licenses/gpl-2.0.txt>. */
#include <assert.h>
#include <math.h>
-bool is_prime(unsigned n)
+static bool is_prime(unsigned);
+
+unsigned ceil_prime(unsigned n)
+{
+ if (n > 2 && n % 2 == 0)
+ n++;
+
+ while (!is_prime(n))
+ n += 2;
+
+ return n;
+}
+
+static bool is_prime(unsigned n)
{
assert(n > 1);
if (n <= 3)
diff --git a/src/utils/indxbib/indxbib.cpp b/src/utils/indxbib/indxbib.cpp
index 956b85700..42bf97060 100644
--- a/src/utils/indxbib/indxbib.cpp
+++ b/src/utils/indxbib/indxbib.cpp
@@ -148,11 +148,7 @@ int main(int argc, char **argv)
{
int requested_hash_table_size;
check_integer_arg('h', optarg, 1, &requested_hash_table_size);
- hash_table_size = requested_hash_table_size;
- if ((hash_table_size > 2) && (hash_table_size % 2) == 0)
- hash_table_size++;
- while (!is_prime(hash_table_size))
- hash_table_size += 2;
+ hash_table_size = ceil_prime(requested_hash_table_size);
if (hash_table_size != requested_hash_table_size)
warning("requested hash table size %1 is not prime: using %2"
" instead", optarg, hash_table_size);
--
2.43.0
signature.asc
Description: PGP signature
- [PATCH] src/: ceil_prime(): Add function to get the lowest prime not smaller than n,
Alejandro Colomar <=
- [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, Alejandro Colomar, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, G. Branden Robinson, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, G. Branden Robinson, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, Alejandro Colomar, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, G. Branden Robinson, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, Alejandro Colomar, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, James K. Lowden, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, G. Branden Robinson, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, James K. Lowden, 2024/03/13
- Re: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, Alejandro Colomar, 2024/03/13