gbranden pushed a commit to branch master
in repository groff.
commit 52dde3693d47d1484b4e95a96f181cff45a5dd5b
Author: Alejandro Colomar <[email protected]>
AuthorDate: Sat Mar 16 13:34:53 2024 +0100
[libgroff,libbib,indxbib]: Add, use ceil_prime().
* src/include/lib.h:
* src/libs/libgroff/prime.cpp (ceil_prime): Add function to get the
lowest prime not less than n. 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. A recent commit started
rejecting 1 earlier, so this bug was now impossible to trigger, but
remained there.
Also, since this is a library function, let's behave well for an input
of 0, which is mathematically fine, and return also the first prime,
2.
* src/libs/libbib/index.cpp (index_search_item::read_common_words_file):
* src/utils/indxbib/indxbib.cpp (main): And use it where the same logic
was being open-coded.
Fixes: 4c7a3396375b ("[libbib, libgroff, indxbib]: Slightly refactor.")
Link: <https://savannah.gnu.org/bugs/?65452>
Link: <https://lists.gnu.org/archive/html/groff/2024-03/msg00065.html>
Cc: "G. Branden Robinson" <[email protected]>
Cc: Dave Kemper <[email protected]>
Cc: "James K. Lowden" <[email protected]>
Cc: Colin Watson <[email protected]>
Cc: Werner LEMBERG <[email protected]>
Cc: James Clark <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
---
ChangeLog | 21 +++++++++++++++++++++
src/include/lib.h | 2 +-
src/libs/libbib/index.cpp | 4 +---
src/libs/libgroff/prime.cpp | 18 +++++++++++++++++-
src/utils/indxbib/indxbib.cpp | 6 +-----
5 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 395816d31..110e4e8c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+2024-03-16 Alejandro Colomar <[email protected]>
+
+ [libgroff,libbib,indxbib]: Add, use `ceil_prime()`.
+
+ * src/include/lib.h:
+ * src/libs/libgroff/prime.cpp (ceil_prime): Add function to get
+ the lowest prime not less than n. 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. A recent
+ commit started rejecting 1 earlier, so this bug was now
+ impossible to trigger, but remained there.
+
+ Also, since this is a library function, let's behave well for an
+ input of 0, which is mathematically fine, and return also the
+ first prime, 2.
+
+ * src/libs/libbib/index.cpp
+ (index_search_item::read_common_words_file):
+ * src/utils/indxbib/indxbib.cpp (main): And use it where the
+ same logic was being open-coded.
+
2024-03-16 Alejandro Colomar <[email protected]>
* src/devices/grolbp/lbp.cpp (main): Remove bogus (and
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 a3ac8eb6e..b15bd8187 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, 2, &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);
_______________________________________________
Groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit