groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not


From: Alejandro Colomar
Subject: [PATCH v2] src/: ceil_prime(): Add function to get the lowest prime not smaller than n
Date: Wed, 13 Mar 2024 11:21:55 +0100

And use it where the same logic was being open-coded.

While at it, fix the logic, which was incorrect in the open-coded call
sites, since for an input of 1, it produced 3, but the first prime is 2.

Also, since this is a library function, let's behave well for an input
of 0, and return also the first prime, 2.

Cc: "G. Branden Robinson" <branden@debian.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
Range-diff against v1:
1:  124bbe1ae ! 1:  9432f7f87 src/: ceil_prime(): Add function to get the 
lowest prime not smaller than n
    @@ Commit message
     
         And use it where the same logic was being open-coded.
     
    +    While at it, fix the logic, which was incorrect in the open-coded call
    +    sites, since for an input of 1, it produced 3, but the first prime is 
2.
    +
    +    Also, since this is a library function, let's behave well for an input
    +    of 0, and return also the first prime, 2.
    +
    +    Cc: "G. Branden Robinson" <branden@debian.org>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## src/include/lib.h ##
    @@ src/libs/libgroff/prime.cpp: internet at 
<http://www.gnu.org/licenses/gpl-2.0.tx
     +
     +unsigned ceil_prime(unsigned n)
     +{
    -+  if (n > 2 && n % 2 == 0)
    ++  if (n <= 2)
    ++    return 2;
    ++
    ++  if (n % 2 == 0)
     +    n++;
     +
     +  while (!is_prime(n))

 src/include/lib.h             |  2 +-
 src/libs/libbib/index.cpp     |  4 +---
 src/libs/libgroff/prime.cpp   | 18 +++++++++++++++++-
 src/utils/indxbib/indxbib.cpp |  6 +-----
 4 files changed, 20 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..485f7b95d 100644
--- a/src/libs/libgroff/prime.cpp
+++ b/src/libs/libgroff/prime.cpp
@@ -22,7 +22,23 @@ 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)
+    return 2;
+
+  if (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

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]