[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[groff] 07/47: [libbib, libgroff, indxbib]: Slightly refactor.
From: |
G. Branden Robinson |
Subject: |
[groff] 07/47: [libbib, libgroff, indxbib]: Slightly refactor. |
Date: |
Tue, 11 Jan 2022 06:33:14 -0500 (EST) |
gbranden pushed a commit to branch master
in repository groff.
commit 4c7a3396375b76c0d45483ba1cf3d22508a7d2fd
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Wed Jan 5 09:58:54 2022 +1100
[libbib, libgroff, indxbib]: Slightly refactor.
Also, it's silly to test even numbers > 2 for primality. Stop.
* src/include/lib.h: Include `stdbool.h` since this header file is used
for both C and C++ code.
* src/include/lib.h (is_prime):
* src/libs/libgroff/prime.cpp (is_prime): Boolify. Return Boolean
instead of integer literals and demote return type to `bool`. Include
`assert.h` and add assertion to reject mathematically offensive input.
* src/libs/libbib/index.cpp (index_search_item::read_common_words_file):
Test only odd numbers for primality.
* src/utils/indxbib/indxbib.cpp (main): Same. Since the number comes
from user input, make it (a potential hash table size) odd first.
Also add editor aid comments.
---
ChangeLog | 22 ++++++++++++++++++++++
src/include/lib.h | 4 +++-
src/libs/libbib/index.cpp | 2 +-
src/libs/libgroff/prime.cpp | 22 +++++++++++++++-------
src/utils/indxbib/indxbib.cpp | 22 +++++++++++++++++-----
5 files changed, 58 insertions(+), 14 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 82b5aae6..6cfdb898 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2022-01-05 G. Branden Robinson <g.branden.robinson@gmail.com>
+
+ [libbib, libgroff, indxbib]: Slightly refactor.
+
+ Also, it's silly to test even numbers > 2 for primality. Stop.
+
+ * src/include/lib.h: Include `stdbool.h` since this header file
+ is used for both C and C++ code.
+
+ * src/include/lib.h (is_prime):
+ * src/libs/libgroff/prime.cpp (is_prime): Boolify. Return
+ Boolean instead of integer literals and demote return type to
+ `bool`. Include `assert.h` and add assertion to reject
+ mathematically offensive input.
+
+ * src/libs/libbib/index.cpp
+ (index_search_item::read_common_words_file): Test only odd
+ numbers for primality.
+ * src/utils/indxbib/indxbib.cpp (main): Same. Since the number
+ comes from user input, make it (a potential hash table size) odd
+ first.
+
2022-01-04 G. Branden Robinson <g.branden.robinson@gmail.com>
[man]: Internationalize and localize.
diff --git a/src/include/lib.h b/src/include/lib.h
index a9ca8b48..ae43cee3 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -53,8 +53,10 @@ extern "C" {
#define getlocale(category) ((void)(category), (char *)"C")
#endif /* !HAVE_SETLOCALE */
+#include <stdbool.h>
+
char *strsave(const char *s);
-int is_prime(unsigned);
+bool is_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 5b716ee9..3fc9b941 100644
--- a/src/libs/libbib/index.cpp
+++ b/src/libs/libbib/index.cpp
@@ -615,7 +615,7 @@ void index_search_item::read_common_words_file()
}
common_words_table_size = 2*header.common + 1;
while (!is_prime(common_words_table_size))
- common_words_table_size++;
+ common_words_table_size += 2;
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 28485137..96001acc 100644
--- a/src/libs/libgroff/prime.cpp
+++ b/src/libs/libgroff/prime.cpp
@@ -18,29 +18,37 @@ internet at <http://www.gnu.org/licenses/gpl-2.0.txt>. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+#include <assert.h>
#include <math.h>
-int is_prime(unsigned n)
+bool is_prime(unsigned n)
{
+ assert(n > 1);
if (n <= 3)
- return 1;
+ return true;
if (!(n & 1))
- return 0;
+ return false;
if (n % 3 == 0)
- return 0;
+ return false;
unsigned lim = unsigned(sqrt((double)n));
unsigned d = 5;
for (;;) {
if (d > lim)
break;
if (n % d == 0)
- return 0;
+ return false;
d += 2;
if (d > lim)
break;
if (n % d == 0)
- return 0;
+ return false;
d += 4;
}
- return 1;
+ return true;
}
+
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:
diff --git a/src/utils/indxbib/indxbib.cpp b/src/utils/indxbib/indxbib.cpp
index b9e08021..1ccd2f0b 100644
--- a/src/utils/indxbib/indxbib.cpp
+++ b/src/utils/indxbib/indxbib.cpp
@@ -142,11 +142,17 @@ int main(int argc, char **argv)
foption = optarg;
break;
case 'h':
- check_integer_arg('h', optarg, 1, &hash_table_size);
- if (!is_prime(hash_table_size)) {
- while (!is_prime(++hash_table_size))
- ;
- warning("%1 not prime: using %2 instead", optarg, hash_table_size);
+ {
+ 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;
+ if (hash_table_size != requested_hash_table_size)
+ warning("requested hash table size %1 is not prime: using %2"
+ " instead", optarg, hash_table_size);
}
break;
case 'i':
@@ -786,3 +792,9 @@ void cleanup()
}
}
+
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [groff] 07/47: [libbib, libgroff, indxbib]: Slightly refactor.,
G. Branden Robinson <=