1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 13:09:22 +00:00

Make the option -s easier to use.

This commit is contained in:
Bruno Haible
2003-03-10 15:01:00 +00:00
parent 9492f0dad7
commit 6ba5486229
7 changed files with 70 additions and 25 deletions

View File

@@ -1,3 +1,20 @@
2002-12-07 Bruno Haible <bruno@clisp.org>
* src/options.h (Options::get_size_multiple): Change return type to
float.
(Options::_size_multiple): Change type to float.
* src/options.icc (Options::get_size_multiple): Change return type to
float.
* src/options.cc (Options::long_usage): Update description of option
-s.
(Options::~Options): Update.
(Options::parse_options): For option -s, accept a fraction.
* src/search.cc (Search::prepare_asso_values): Use get_size_multiple
as it is.
* tests/test-6.exp: Update.
* doc/gperf.texi (Algorithmic Details): Update description of option
-s.
2002-12-04 Bruno Haible <bruno@clisp.org> 2002-12-04 Bruno Haible <bruno@clisp.org>
Improve debugging output. Improve debugging output.

View File

@@ -1074,16 +1074,13 @@ table.
Affects the size of the generated hash table. The numeric argument for Affects the size of the generated hash table. The numeric argument for
this option indicates ``how many times larger or smaller'' the maximum this option indicates ``how many times larger or smaller'' the maximum
associated value range should be, in relationship to the number of keywords. associated value range should be, in relationship to the number of keywords.
If the @var{size-multiple} is negative the maximum associated value is It can be written as an integer, a floating-point number or a fraction.
calculated by @emph{dividing} the number of keywords by it. For For example, a value of 3 means ``allow the maximum associated value to be
example, a value of 3 means ``allow the maximum associated value to be about 3 times larger than the number of input keywords''.
about 3 times larger than the number of input keywords''. Conversely, a value of 1/3 means ``allow the maximum associated value to
be about 3 times smaller than the number of input keywords''. Values
Conversely, a value of -3 means ``allow the maximum associated value to smaller than 1 are useful for limiting the overall size of the generated hash
be about 3 times smaller than the number of input keywords''. Negative table, though the option @samp{-m} is better at this purpose.
values are useful for limiting the overall size of the generated hash
table, though this usually increases the number of duplicate hash
values.
If `generate switch' option @samp{-S} (or, equivalently, @samp{%switch}) is If `generate switch' option @samp{-S} (or, equivalently, @samp{%switch}) is
@emph{not} enabled, the maximum @emph{not} enabled, the maximum

View File

@@ -234,7 +234,7 @@ Options::long_usage (FILE * stream) const
" in relationship to the number of keys, e.g. a value\n" " in relationship to the number of keys, e.g. a value\n"
" of 3 means \"allow the maximum associated value to\n" " of 3 means \"allow the maximum associated value to\n"
" be about 3 times larger than the number of input\n" " be about 3 times larger than the number of input\n"
" keys\". Conversely, a value of -3 means \"make the\n" " keys\". Conversely, a value of 1/3 means \"make the\n"
" maximum associated value about 3 times smaller than\n" " maximum associated value about 3 times smaller than\n"
" the number of input keys\". A larger table should\n" " the number of input keys\". A larger table should\n"
" decrease the time required for an unsuccessful\n" " decrease the time required for an unsuccessful\n"
@@ -481,7 +481,7 @@ Options::~Options ()
"\ninitializer suffix = %s" "\ninitializer suffix = %s"
"\nasso_values iterations = %d" "\nasso_values iterations = %d"
"\njump value = %d" "\njump value = %d"
"\nhash table size multiplier = %d" "\nhash table size multiplier = %g"
"\ninitial associated value = %d" "\ninitial associated value = %d"
"\ndelimiters = %s" "\ndelimiters = %s"
"\nnumber of switch statements = %d\n", "\nnumber of switch statements = %d\n",
@@ -873,8 +873,44 @@ Options::parse_options (int argc, char *argv[])
} }
case 's': /* Range of associated values, determines size of final table. */ case 's': /* Range of associated values, determines size of final table. */
{ {
if (abs (_size_multiple = atoi (/*getopt*/optarg)) > 50) float numerator;
fprintf (stderr, "%d is excessive, did you really mean this?! (try `%s --help' for help)\n", _size_multiple, program_name); float denominator = 1;
bool invalid = false;
char *endptr;
numerator = strtod (/*getopt*/optarg, &endptr);
if (endptr == /*getopt*/optarg)
invalid = true;
else if (*endptr != '\0')
{
if (*endptr == '/')
{
char *denomptr = endptr + 1;
denominator = strtod (denomptr, &endptr);
if (endptr == denomptr || *endptr != '\0')
invalid = true;
}
else
invalid = true;
}
if (invalid)
{
fprintf (stderr, "Invalid value for option -s.\n");
short_usage (stderr);
exit (1);
}
_size_multiple = numerator / denominator;
/* Backward compatibility: -3 means 1/3. */
if (_size_multiple < 0)
_size_multiple = 1 / (-_size_multiple);
/* Catch stupid users. */
if (_size_multiple == 0)
_size_multiple = 1;
/* Warnings. */
if (_size_multiple > 50)
fprintf (stderr, "Size multiple %g is excessive, did you really mean this?! (try '%s --help' for help)\n", _size_multiple, program_name);
else if (_size_multiple < 0.01f)
fprintf (stderr, "Size multiple %g is extremely small, did you really mean this?! (try '%s --help' for help)\n", _size_multiple, program_name);
break; break;
} }
case 'S': /* Generate switch statement output, rather than lookup table. */ case 'S': /* Generate switch statement output, rather than lookup table. */

View File

@@ -155,7 +155,7 @@ public:
void set_total_switches (int total_switches); void set_total_switches (int total_switches);
/* Returns the factor by which to multiply the generated table's size. */ /* Returns the factor by which to multiply the generated table's size. */
int get_size_multiple () const; float get_size_multiple () const;
/* Returns the generated function name. */ /* Returns the generated function name. */
const char * get_function_name () const; const char * get_function_name () const;
@@ -235,7 +235,7 @@ private:
int _total_switches; int _total_switches;
/* Factor by which to multiply the generated table's size. */ /* Factor by which to multiply the generated table's size. */
int _size_multiple; float _size_multiple;
/* Names used for generated lookup function. */ /* Names used for generated lookup function. */
const char * _function_name; const char * _function_name;

View File

@@ -87,7 +87,7 @@ Options::get_total_switches () const
} }
/* Returns the factor by which to multiply the generated table's size. */ /* Returns the factor by which to multiply the generated table's size. */
INLINE int INLINE float
Options::get_size_multiple () const Options::get_size_multiple () const
{ {
return _size_multiple; return _size_multiple;

View File

@@ -802,16 +802,11 @@ Search::get_max_keysig_size () const
void void
Search::prepare_asso_values () Search::prepare_asso_values ()
{ {
int size_multiple = option.get_size_multiple ();
int non_linked_length = keyword_list_length (); int non_linked_length = keyword_list_length ();
int asso_value_max; int asso_value_max;
if (size_multiple == 0) asso_value_max =
asso_value_max = non_linked_length; static_cast<int>(non_linked_length * option.get_size_multiple());
else if (size_multiple > 0)
asso_value_max = non_linked_length * size_multiple;
else /* if (size_multiple < 0) */
asso_value_max = non_linked_length / -size_multiple;
/* Round up to the next power of two. This makes it easy to ensure /* Round up to the next power of two. This makes it easy to ensure
an _asso_value[c] is >= 0 and < asso_value_max. Also, the jump value an _asso_value[c] is >= 0 and < asso_value_max. Also, the jump value
being odd, it guarantees that Search::try_asso_value() will iterate being odd, it guarantees that Search::try_asso_value() will iterate

View File

@@ -117,7 +117,7 @@ Algorithm employed by gperf:
in relationship to the number of keys, e.g. a value in relationship to the number of keys, e.g. a value
of 3 means "allow the maximum associated value to of 3 means "allow the maximum associated value to
be about 3 times larger than the number of input be about 3 times larger than the number of input
keys". Conversely, a value of -3 means "make the keys". Conversely, a value of 1/3 means "make the
maximum associated value about 3 times smaller than maximum associated value about 3 times smaller than
the number of input keys". A larger table should the number of input keys". A larger table should
decrease the time required for an unsuccessful decrease the time required for an unsuccessful