[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[groff] 01/12: [troff]: Trivially refactor dict implementation.
From: |
G. Branden Robinson |
Subject: |
[groff] 01/12: [troff]: Trivially refactor dict implementation. |
Date: |
Sun, 5 Nov 2023 11:12:16 -0500 (EST) |
gbranden pushed a commit to branch master
in repository groff.
commit 9c251e82330685115c8da8c39fa23c8448424962
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Sat Nov 4 10:39:51 2023 -0500
[troff]: Trivially refactor dict implementation.
* src/roff/troff/dictionary.h (class dictionary_iterator):
(class object_dictionary_iterator):
(object_dictionary_iterator::get): Demote return type of `get()` from
`int` to `bool`.
(class object_dictionary): Demote return type of `alias()` from `int`
to `bool`.
(class object): Rename member variable `rcount` to `refcount`.
* src/roff/troff/dictionary.cpp (is_good_size):
(dictionary_iterator::get):
(object_dictionary::alias): Demote return type from `int` to `bool`.
Update return value literals.
(object::object):
(object::add_reference):
(object::remove_reference): Rename member variable `rcount` to
`refcount`.
Also fix cosmetic nits.
* Update editor aid comments; drop old style Emacs file-local variable
setting.
* Use spaces around binary operators.
* Use parentheses in complex expressions.
* Strip trailing whitespace from lines.
* Annotate null pointers with `nullptr` comment to ease any future
transition to C++11, which defines it as a keyword. (In truth, likely
this dictionary implementation would go away in favor of the STL.)
* Drop redundant and (seemingly) stale comments.
* Drop parameter names from prototypes, in keeping with the
Stroustrup-style C++ used in most of groff. In my opinion, the
`rename()` and `alias()` member functions seen here are good
counterexamples of that practice. If a programmer is supposed to be
able to learn an API by reading its header file--a claim tirelessly
repeated by generations of Unix/C console jockeys--the omission of
parameter names means one can only guess at the meanings of two
parameters of the same type. On the other hand, the notion that C++
is a language that maintains a boundary between specification and
implementation is one that died (or should have) decades ago, thanks
largely to templating. Perhaps I am bringing my Ada fanboy bias into
the matter once again.
---
ChangeLog | 20 +++++++++++++
src/roff/troff/dictionary.cpp | 67 +++++++++++++++++++++++--------------------
src/roff/troff/dictionary.h | 35 +++++++++++-----------
3 files changed, 75 insertions(+), 47 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index dea574c20..4c5aa8496 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2023-11-04 G. Branden Robinson <g.branden.robinson@gmail.com>
+
+ [troff]: Trivially refactor dictionary implementation.
+
+ * src/roff/troff/dictionary.h (class dictionary_iterator):
+ (class object_dictionary_iterator):
+ (object_dictionary_iterator::get): Demote return type of `get()`
+ from `int` to `bool`.
+ (class object_dictionary): Demote return type of `alias()` from
+ `int` to `bool`.
+ (class object): Rename member variable `rcount` to `refcount`.
+ * src/roff/troff/dictionary.cpp (is_good_size):
+ (dictionary_iterator::get):
+ (object_dictionary::alias): Demote return type from `int` to
+ `bool`. Update return value literals.
+ (object::object):
+ (object::add_reference):
+ (object::remove_reference): Rename member variable `rcount` to
+ `refcount`.
+
2023-11-03 G. Branden Robinson <g.branden.robinson@gmail.com>
[troff]: Implement new `phw` request.
diff --git a/src/roff/troff/dictionary.cpp b/src/roff/troff/dictionary.cpp
index 08afbd34b..6146751e4 100644
--- a/src/roff/troff/dictionary.cpp
+++ b/src/roff/troff/dictionary.cpp
@@ -1,4 +1,3 @@
-// -*- C++ -*-
/* Copyright (C) 1989-2020 Free Software Foundation, Inc.
Written by James Clark (jjc@jclark.com)
@@ -23,17 +22,17 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
// is 'p' a good size for a hash table
-static int is_good_size(unsigned int p)
+static bool is_good_size(unsigned int p)
{
const unsigned int SMALL = 10;
unsigned int i;
- for (i = 2; i <= p/2; i++)
+ for (i = 2; i <= (p / 2); i++)
if (p % i == 0)
- return 0;
+ return false;
for (i = 0x100; i != 0; i <<= 8)
- if (i % p <= SMALL || i % p > p - SMALL)
- return 0;
- return 1;
+ if ((i % p) <= SMALL || (i % p) > (p - SMALL))
+ return false;
+ return true;
}
dictionary::dictionary(int n) : size(n), used(0), threshold(0.5), factor(1.5)
@@ -47,11 +46,11 @@ dictionary::dictionary(int n) : size(n), used(0),
threshold(0.5), factor(1.5)
void *dictionary::lookup(symbol s, void *v)
{
int i;
- for (i = int(s.hash() % size);
- table[i].v != 0;
+ for (i = int(s.hash() % size);
+ table[i].v != 0 /* nullptr */;
i == 0 ? i = size - 1: --i)
if (s == table[i].s) {
- if (v != 0) {
+ if (v != 0 /* nullptr */) {
void *temp = table[i].v;
table[i].v = v;
return temp;
@@ -59,62 +58,62 @@ void *dictionary::lookup(symbol s, void *v)
else
return table[i].v;
}
- if (v == 0)
- return 0;
+ if (v == 0 /* nullptr */)
+ return 0 /* nullptr */;
++used;
table[i].v = v;
table[i].s = s;
if ((double)used/(double)size >= threshold || used + 1 >= size) {
int old_size = size;
- size = int(size*factor);
+ size = int(size * factor);
while (!is_good_size(size))
++size;
association *old_table = table;
table = new association[size];
used = 0;
for (i = 0; i < old_size; i++)
- if (old_table[i].v != 0)
+ if (old_table[i].v != 0 /* nullptr */)
(void)lookup(old_table[i].s, old_table[i].v);
delete[] old_table;
}
- return 0;
+ return 0 /* nullptr */;
}
void *dictionary::lookup(const char *p)
{
symbol s(p, MUST_ALREADY_EXIST);
if (s.is_null())
- return 0;
+ return 0 /* nullptr */;
else
return lookup(s);
}
// see Knuth, Sorting and Searching, p527, Algorithm R
-
+
void *dictionary::remove(symbol s)
{
// this relies on the fact that we are using linear probing
int i;
for (i = int(s.hash() % size);
- table[i].v != 0 && s != table[i].s;
+ table[i].v != 0 /* nullptr */ && s != table[i].s;
i == 0 ? i = size - 1: --i)
;
void *p = table[i].v;
- while (table[i].v != 0) {
- table[i].v = 0;
+ while (table[i].v != 0 /* nullptr */) {
+ table[i].v = 0 /* nullptr */;
int j = i;
int r;
do {
--i;
if (i < 0)
i = size - 1;
- if (table[i].v == 0)
+ if (table[i].v == 0 /* nullptr */)
break;
r = int(table[i].s.hash() % size);
} while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
table[j] = table[i];
}
- if (p != 0)
+ if (p != 0 /* nullptr */)
--used;
return p;
}
@@ -123,16 +122,16 @@ dictionary_iterator::dictionary_iterator(dictionary &d) :
dict(&d), i(0)
{
}
-int dictionary_iterator::get(symbol *sp, void **vp)
+bool dictionary_iterator::get(symbol *sp, void **vp)
{
for (; i < dict->size; i++)
if (dict->table[i].v) {
*sp = dict->table[i].s;
*vp = dict->table[i].v;
i++;
- return 1;
+ return true;
}
- return 0;
+ return false;
}
object_dictionary_iterator::object_dictionary_iterator(object_dictionary &od)
@@ -140,7 +139,7 @@
object_dictionary_iterator::object_dictionary_iterator(object_dictionary &od)
{
}
-object::object() : rcount(0)
+object::object() : refcount(0)
{
}
@@ -150,12 +149,12 @@ object::~object()
void object::add_reference()
{
- rcount += 1;
+ refcount += 1;
}
void object::remove_reference()
{
- if (--rcount == 0)
+ if (--refcount == 0)
delete this;
}
@@ -195,7 +194,7 @@ void object_dictionary::remove(symbol nm)
// Return non-zero if oldnm was defined.
-int object_dictionary::alias(symbol newnm, symbol oldnm)
+bool object_dictionary::alias(symbol newnm, symbol oldnm)
{
object *obj = (object *)d.lookup(oldnm);
if (obj) {
@@ -203,7 +202,13 @@ int object_dictionary::alias(symbol newnm, symbol oldnm)
obj = (object *)d.lookup(newnm, obj);
if (obj)
obj->remove_reference();
- return 1;
+ return true;
}
- return 0;
+ return false;
}
+
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:
diff --git a/src/roff/troff/dictionary.h b/src/roff/troff/dictionary.h
index 71c4f3f46..2f19f90ac 100644
--- a/src/roff/troff/dictionary.h
+++ b/src/roff/troff/dictionary.h
@@ -1,4 +1,3 @@
-// -*- C++ -*-
/* Copyright (C) 1989-2020 Free Software Foundation, Inc.
Written by James Clark (jjc@jclark.com)
@@ -18,14 +17,13 @@ You should have received a copy of the GNU General Public
License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
-
-// there is no distinction between name with no value and name with NULL value
-// null names are not permitted (they will be ignored).
+// There is no distinction between a name with no value and a name with
+// a 0 (nullptr) value. Null names are not permitted; they are ignored.
struct association {
symbol s;
void *v;
- association() : v(0) {}
+ association() : v(0 /* nullptr */) {}
};
class dictionary;
@@ -35,7 +33,7 @@ class dictionary_iterator {
int i;
public:
dictionary_iterator(dictionary &);
- int get(symbol *, void **);
+ bool get(symbol *, void **);
};
class dictionary {
@@ -47,15 +45,14 @@ class dictionary {
void rehash(int);
public:
dictionary(int);
- void *lookup(symbol s, void *v=0); // returns value associated with key
+ void *lookup(symbol s, void *v = 0 /* nullptr */);
void *lookup(const char *);
- // if second parameter not NULL, value will be replaced
void *remove(symbol);
friend class dictionary_iterator;
};
class object {
- int rcount;
+ int refcount;
public:
object();
virtual ~object();
@@ -69,23 +66,29 @@ class object_dictionary_iterator {
dictionary_iterator di;
public:
object_dictionary_iterator(object_dictionary &);
- int get(symbol *, object **);
+ bool get(symbol *, object **);
};
class object_dictionary {
dictionary d;
public:
object_dictionary(int);
- object *lookup(symbol nm);
- void define(symbol nm, object *obj);
- void rename(symbol oldnm, symbol newnm);
- void remove(symbol nm);
- int alias(symbol newnm, symbol oldnm);
+ object *lookup(symbol);
+ void define(symbol, object *);
+ void rename(symbol, symbol);
+ void remove(symbol);
+ bool alias(symbol, symbol);
friend class object_dictionary_iterator;
};
-inline int object_dictionary_iterator::get(symbol *sp, object **op)
+inline bool object_dictionary_iterator::get(symbol *sp, object **op)
{
return di.get(sp, (void **)op);
}
+
+// 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] 01/12: [troff]: Trivially refactor dict implementation.,
G. Branden Robinson <=