1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 21:19:24 +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

@@ -234,7 +234,7 @@ Options::long_usage (FILE * stream) const
" in relationship to the number of keys, e.g. a value\n"
" of 3 means \"allow the maximum associated value to\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"
" the number of input keys\". A larger table should\n"
" decrease the time required for an unsuccessful\n"
@@ -481,7 +481,7 @@ Options::~Options ()
"\ninitializer suffix = %s"
"\nasso_values iterations = %d"
"\njump value = %d"
"\nhash table size multiplier = %d"
"\nhash table size multiplier = %g"
"\ninitial associated value = %d"
"\ndelimiters = %s"
"\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. */
{
if (abs (_size_multiple = atoi (/*getopt*/optarg)) > 50)
fprintf (stderr, "%d is excessive, did you really mean this?! (try `%s --help' for help)\n", _size_multiple, program_name);
float numerator;
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;
}
case 'S': /* Generate switch statement output, rather than lookup table. */

View File

@@ -155,7 +155,7 @@ public:
void set_total_switches (int total_switches);
/* 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. */
const char * get_function_name () const;
@@ -235,7 +235,7 @@ private:
int _total_switches;
/* Factor by which to multiply the generated table's size. */
int _size_multiple;
float _size_multiple;
/* Names used for generated lookup function. */
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. */
INLINE int
INLINE float
Options::get_size_multiple () const
{
return _size_multiple;

View File

@@ -802,16 +802,11 @@ Search::get_max_keysig_size () const
void
Search::prepare_asso_values ()
{
int size_multiple = option.get_size_multiple ();
int non_linked_length = keyword_list_length ();
int asso_value_max;
if (size_multiple == 0)
asso_value_max = non_linked_length;
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;
asso_value_max =
static_cast<int>(non_linked_length * option.get_size_multiple());
/* 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
being odd, it guarantees that Search::try_asso_value() will iterate