1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 21:19:24 +00:00

Use const where possible.

This commit is contained in:
Bruno Haible
2003-01-13 19:57:56 +00:00
parent 3a01fe39b2
commit 741d34fb07
10 changed files with 86 additions and 52 deletions

View File

@@ -1,5 +1,37 @@
2002-11-03 Bruno Haible <bruno@clisp.org> 2002-11-03 Bruno Haible <bruno@clisp.org>
* src/hash-table.h (Hash_Table::_ignore_length, Hash_Table::equal):
Declare as const.
* src/hash-table.cc (Hash_Table::equal): Declare as const.
* src/input.h (Input::_factory): Declare as const.
* src/keyword-list.h (Keyword_List::first, KeywordExt_List::first):
Declare as const.
* src/keyword-list.icc (Keyword_List::first, KeywordExt_List::first):
Declare as const.
* src/output.h (Output::num_hash_values, Output::output_constants,
Output::output_hash_function, Output::output_keylength_table,
Output::output_keyword_table, Output::output_lookup_array,
Output::output_lookup_tables, Output::output_lookup_function_body,
Output::output_lookup_function, Output::_array_type,
Output::_additional_code, Output::_include_src, Output::_total_keys,
Output::_total_duplicates, Output::_max_key_len, Output::_min_key_len):
Declare as const.
* src/output.cc (Output::num_hash_values, Output::output_constants,
Output::output_hash_function, Output::output_keylength_table,
Output::output_keyword_table, Output::output_lookup_array,
Output::output_lookup_tables, Output::output_lookup_function_body,
Output::output_lookup_function): Declare as const.
* src/search.h (Search::merge, Search::merge_sort,
Search::compute_occurrence, Search::already_determined,
Search::keyword_list_length, Search::max_key_length,
Search::get_max_keysig_size, Search::compute_hash,
Search::sort_by_occurrence): Declare as const.
* src/search.cc (Search::merge, Search::merge_sort,
Search::compute_occurrence, Search::already_determined,
Search::keyword_list_length, Search::max_key_length,
Search::get_max_keysig_size, Search::compute_hash,
Search::sort_by_occurrence): Declare as const.
* src/output.cc (Output::output): Set char_to_index to a cast in all * src/output.cc (Output::output): Set char_to_index to a cast in all
cases. Avoids gcc warnings on the generated code. cases. Avoids gcc warnings on the generated code.

View File

@@ -121,7 +121,7 @@ Hash_Table::~Hash_Table ()
/* Compares two items. */ /* Compares two items. */
inline bool inline bool
Hash_Table::equal (KeywordExt *item1, KeywordExt *item2) Hash_Table::equal (KeywordExt *item1, KeywordExt *item2) const
{ {
return item1->_selchars_length == item2->_selchars_length return item1->_selchars_length == item2->_selchars_length
&& memcmp (item1->_selchars, item2->_selchars, item2->_selchars_length) && memcmp (item1->_selchars, item2->_selchars, item2->_selchars_length)

View File

@@ -53,12 +53,12 @@ private:
/* log2(_size). */ /* log2(_size). */
unsigned int _log_size; unsigned int _log_size;
/* A detail of the comparison function. */ /* A detail of the comparison function. */
bool _ignore_length; bool const _ignore_length;
/* Statistics: Number of collisions so far. */ /* Statistics: Number of collisions so far. */
unsigned int _collisions; unsigned int _collisions;
/* Compares two items. */ /* Compares two items. */
bool equal (KeywordExt *item1, KeywordExt *item2); bool equal (KeywordExt *item1, KeywordExt *item2) const;
}; };
#endif #endif

View File

@@ -48,7 +48,7 @@ public:
const char * _struct_tag; /* Shorthand for user-defined struct tag type. */ const char * _struct_tag; /* Shorthand for user-defined struct tag type. */
const char * _include_src; /* C source code to be included verbatim. */ const char * _include_src; /* C source code to be included verbatim. */
bool _additional_code; /* True if any additional C code is included. */ bool _additional_code; /* True if any additional C code is included. */
Keyword_Factory * _factory; /* Creates the keywords. */ Keyword_Factory * const _factory; /* Creates the keywords. */
Keyword_List * _head; /* Points to the head of the linked list. */ Keyword_List * _head; /* Points to the head of the linked list. */
private: private:
Keyword_List * parse_line (const char *line, const char *delimiters); Keyword_List * parse_line (const char *line, const char *delimiters);

View File

@@ -35,7 +35,7 @@ public:
Keyword_List (Keyword *car); Keyword_List (Keyword *car);
/* Access to first element of list. */ /* Access to first element of list. */
Keyword * first (); Keyword * first () const;
/* Access to next element of list. */ /* Access to next element of list. */
Keyword_List *& rest (); Keyword_List *& rest ();
@@ -52,7 +52,7 @@ public:
KeywordExt_List (KeywordExt *car); KeywordExt_List (KeywordExt *car);
/* Access to first element of list. */ /* Access to first element of list. */
KeywordExt * first (); KeywordExt * first () const;
/* Access to next element of list. */ /* Access to next element of list. */
KeywordExt_List *& rest (); KeywordExt_List *& rest ();
}; };

View File

@@ -24,7 +24,7 @@
/* Access to first element of list. */ /* Access to first element of list. */
INLINE Keyword * INLINE Keyword *
Keyword_List::first () Keyword_List::first () const
{ {
return _car; return _car;
} }
@@ -40,7 +40,7 @@ Keyword_List::rest ()
/* Access to first element of list. */ /* Access to first element of list. */
INLINE KeywordExt * INLINE KeywordExt *
KeywordExt_List::first () KeywordExt_List::first () const
{ {
return static_cast<KeywordExt*>(_car); return static_cast<KeywordExt*>(_car);
} }

View File

@@ -119,7 +119,7 @@ Output::compute_min_max ()
/* Returns the number of different hash values. */ /* Returns the number of different hash values. */
int int
Output::num_hash_values () Output::num_hash_values () const
{ {
/* Since the list is already sorted by hash value we can count the /* Since the list is already sorted by hash value we can count the
different hash values in a single pass through the list. */ different hash values in a single pass through the list. */
@@ -216,7 +216,7 @@ void Output_Enum::output_end ()
/* Outputs the maximum and minimum hash values etc. */ /* Outputs the maximum and minimum hash values etc. */
void void
Output::output_constants (struct Output_Constants& style) Output::output_constants (struct Output_Constants& style) const
{ {
style.output_start (); style.output_start ();
style.output_item ("TOTAL_KEYWORDS", _total_keys); style.output_item ("TOTAL_KEYWORDS", _total_keys);
@@ -430,7 +430,7 @@ void Output_Compare_Memcmp::output_comparison (const Output_Expr& expr1,
unsigned int <hash> (const char *str, unsigned int len). */ unsigned int <hash> (const char *str, unsigned int len). */
void void
Output::output_hash_function () Output::output_hash_function () const
{ {
/* Output the function's head. */ /* Output the function's head. */
if (option[CPLUSPLUS]) if (option[CPLUSPLUS])
@@ -610,7 +610,7 @@ Output::output_hash_function ()
Only called if option[LENTABLE]. */ Only called if option[LENTABLE]. */
void void
Output::output_keylength_table () Output::output_keylength_table () const
{ {
const int columns = 14; const int columns = 14;
const char * const indent = option[GLOBAL] ? "" : " "; const char * const indent = option[GLOBAL] ? "" : " ";
@@ -732,7 +732,7 @@ output_keyword_blank_entries (int count, const char *indent)
/* Prints out the array containing the keywords for the hash function. */ /* Prints out the array containing the keywords for the hash function. */
void void
Output::output_keyword_table () Output::output_keyword_table () const
{ {
const char *indent = option[GLOBAL] ? "" : " "; const char *indent = option[GLOBAL] ? "" : " ";
int index; int index;
@@ -793,7 +793,7 @@ Output::output_keyword_table ()
the smaller, contiguous range of the keyword table. */ the smaller, contiguous range of the keyword table. */
void void
Output::output_lookup_array () Output::output_lookup_array () const
{ {
if (option[DUP]) if (option[DUP])
{ {
@@ -956,7 +956,7 @@ Output::output_lookup_array ()
/* Generate all the tables needed for the lookup function. */ /* Generate all the tables needed for the lookup function. */
void void
Output::output_lookup_tables () Output::output_lookup_tables () const
{ {
if (option[SWITCH]) if (option[SWITCH])
{ {
@@ -1138,7 +1138,7 @@ output_switches (KeywordExt_List *list, int num_switches, int size, int min_hash
/* Generates C code to perform the keyword lookup. */ /* Generates C code to perform the keyword lookup. */
void void
Output::output_lookup_function_body (const Output_Compare& comparison) Output::output_lookup_function_body (const Output_Compare& comparison) const
{ {
printf (" if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)\n" printf (" if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)\n"
" {\n" " {\n"
@@ -1385,7 +1385,7 @@ Output::output_lookup_function_body (const Output_Compare& comparison)
/* Generates C code for the lookup function. */ /* Generates C code for the lookup function. */
void void
Output::output_lookup_function () Output::output_lookup_function () const
{ {
/* Output the function's head. */ /* Output the function's head. */
if (option[KRC] | option[C] | option[ANSIC]) if (option[KRC] | option[C] | option[ANSIC])

View File

@@ -59,56 +59,56 @@ private:
void compute_min_max (); void compute_min_max ();
/* Returns the number of different hash values. */ /* Returns the number of different hash values. */
int num_hash_values (); int num_hash_values () const;
/* Outputs the maximum and minimum hash values etc. */ /* Outputs the maximum and minimum hash values etc. */
void output_constants (struct Output_Constants&); void output_constants (struct Output_Constants&) const;
/* Generates C code for the hash function that returns the /* Generates C code for the hash function that returns the
proper encoding for each keyword. */ proper encoding for each keyword. */
void output_hash_function (); void output_hash_function () const;
/* Prints out a table of keyword lengths, for use with the /* Prints out a table of keyword lengths, for use with the
comparison code in generated function 'in_word_set'. */ comparison code in generated function 'in_word_set'. */
void output_keylength_table (); void output_keylength_table () const;
/* Prints out the array containing the keywords for the hash function. */ /* Prints out the array containing the keywords for the hash function. */
void output_keyword_table (); void output_keyword_table () const;
/* Generates the large, sparse table that maps hash values into /* Generates the large, sparse table that maps hash values into
the smaller, contiguous range of the keyword table. */ the smaller, contiguous range of the keyword table. */
void output_lookup_array (); void output_lookup_array () const;
/* Generate all the tables needed for the lookup function. */ /* Generate all the tables needed for the lookup function. */
void output_lookup_tables (); void output_lookup_tables () const;
/* Generates C code to perform the keyword lookup. */ /* Generates C code to perform the keyword lookup. */
void output_lookup_function_body (const struct Output_Compare&); void output_lookup_function_body (const struct Output_Compare&) const;
/* Generates C code for the lookup function. */ /* Generates C code for the lookup function. */
void output_lookup_function (); void output_lookup_function () const;
/* Linked list of keywords. */ /* Linked list of keywords. */
KeywordExt_List * _head; KeywordExt_List * _head;
/* Pointer to the type for word list. */ /* Pointer to the type for word list. */
const char * _array_type; const char * const _array_type;
/* Pointer to return type for lookup function. */ /* Pointer to return type for lookup function. */
const char * _return_type; const char * _return_type;
/* Shorthand for user-defined struct tag type. */ /* Shorthand for user-defined struct tag type. */
const char * _struct_tag; const char * _struct_tag;
/* True if any additional C code is included. */ /* True if any additional C code is included. */
bool _additional_code; bool const _additional_code;
/* C source code to be included verbatim. */ /* C source code to be included verbatim. */
const char * _include_src; const char * const _include_src;
/* Total number of keys, counting duplicates. */ /* Total number of keys, counting duplicates. */
int _total_keys; int const _total_keys;
/* Total number of duplicate hash values. */ /* Total number of duplicate hash values. */
int _total_duplicates; int const _total_duplicates;
/* Maximum length of the longest keyword. */ /* Maximum length of the longest keyword. */
int _max_key_len; int const _max_key_len;
/* Minimum length of the shortest keyword. */ /* Minimum length of the shortest keyword. */
int _min_key_len; int const _min_key_len;
/* Minimum hash value for all keywords. */ /* Minimum hash value for all keywords. */
int _min_hash_value; int _min_hash_value;
/* Maximum hash value for all keywords. */ /* Maximum hash value for all keywords. */

View File

@@ -161,7 +161,7 @@ Search::prepare ()
every comparison. */ every comparison. */
KeywordExt_List * KeywordExt_List *
Search::merge (KeywordExt_List *list1, KeywordExt_List *list2) Search::merge (KeywordExt_List *list1, KeywordExt_List *list2) const
{ {
KeywordExt_List *result; KeywordExt_List *result;
KeywordExt_List **resultp = &result; KeywordExt_List **resultp = &result;
@@ -199,7 +199,7 @@ Search::merge (KeywordExt_List *list1, KeywordExt_List *list2)
is set to true. */ is set to true. */
KeywordExt_List * KeywordExt_List *
Search::merge_sort (KeywordExt_List *head) Search::merge_sort (KeywordExt_List *head) const
{ {
if (!head || !head->rest()) if (!head || !head->rest())
/* List of length 0 or 1. Nothing to do. */ /* List of length 0 or 1. Nothing to do. */
@@ -239,7 +239,7 @@ Search::merge_sort (KeywordExt_List *head)
in other keywords have a low occurrence sum. */ in other keywords have a low occurrence sum. */
inline int inline int
Search::compute_occurrence (KeywordExt *ptr) Search::compute_occurrence (KeywordExt *ptr) const
{ {
int value = 0; int value = 0;
@@ -276,7 +276,7 @@ Search::set_determined (KeywordExt *keyword)
Returns true if the keyword's selected characters are all determined. */ Returns true if the keyword's selected characters are all determined. */
inline bool inline bool
Search::already_determined (KeywordExt *keyword) Search::already_determined (KeywordExt *keyword) const
{ {
const unsigned char *p = keyword->_selchars; const unsigned char *p = keyword->_selchars;
unsigned int i = keyword->_selchars_length; unsigned int i = keyword->_selchars_length;
@@ -363,7 +363,7 @@ Search::reorder ()
/* Returns the length of keyword list. */ /* Returns the length of keyword list. */
int int
Search::keyword_list_length () Search::keyword_list_length () const
{ {
return _list_len; return _list_len;
} }
@@ -371,7 +371,7 @@ Search::keyword_list_length ()
/* Returns the maximum length of keywords. */ /* Returns the maximum length of keywords. */
int int
Search::max_key_length () Search::max_key_length () const
{ {
return _max_key_len; return _max_key_len;
} }
@@ -379,7 +379,7 @@ Search::max_key_length ()
/* Returns the number of key positions. */ /* Returns the number of key positions. */
int int
Search::get_max_keysig_size () Search::get_max_keysig_size () const
{ {
return option[ALLCHARS] ? _max_key_len : option.get_max_keysig_size (); return option[ALLCHARS] ? _max_key_len : option.get_max_keysig_size ();
} }
@@ -454,7 +454,7 @@ Search::init_asso_values ()
This is called very frequently, and needs to be fast! */ This is called very frequently, and needs to be fast! */
inline int inline int
Search::compute_hash (KeywordExt *keyword) Search::compute_hash (KeywordExt *keyword) const
{ {
int sum = option[NOLENGTH] ? 0 : keyword->_allchars_length; int sum = option[NOLENGTH] ? 0 : keyword->_allchars_length;
@@ -519,7 +519,7 @@ compute_disjoint_union (const unsigned char *set_1, int size_1,
/* Sorts the given set in increasing frequency of _occurrences[]. */ /* Sorts the given set in increasing frequency of _occurrences[]. */
inline void inline void
Search::sort_by_occurrence (unsigned char *set, int len) Search::sort_by_occurrence (unsigned char *set, int len) const
{ {
/* Use bubble sort, since the set is typically short. */ /* Use bubble sort, since the set is typically short. */
for (int i = 1; i < len; i++) for (int i = 1; i < len; i++)
@@ -706,7 +706,7 @@ Search::find_asso_values ()
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* Sorts the keys by hash value. */ /* Sorts the keyword list by hash value. */
void void
Search::sort () Search::sort ()

View File

@@ -39,28 +39,28 @@ private:
void prepare (); void prepare ();
/* Merges two sorted lists together to form one sorted list. */ /* Merges two sorted lists together to form one sorted list. */
KeywordExt_List * merge (KeywordExt_List *list1, KeywordExt_List *list2); KeywordExt_List * merge (KeywordExt_List *list1, KeywordExt_List *list2) const;
/* Sorts a list using the recursive merge sort algorithm. */ /* Sorts a list using the recursive merge sort algorithm. */
KeywordExt_List * merge_sort (KeywordExt_List *head); KeywordExt_List * merge_sort (KeywordExt_List *head) const;
/* Computes the sum of occurrences of the _selchars of a keyword. */ /* Computes the sum of occurrences of the _selchars of a keyword. */
int compute_occurrence (KeywordExt *ptr); int compute_occurrence (KeywordExt *ptr) const;
/* Auxiliary functions used by Search::reorder(). */ /* Auxiliary functions used by Search::reorder(). */
void clear_determined (); void clear_determined ();
void set_determined (KeywordExt *keyword); void set_determined (KeywordExt *keyword);
bool already_determined (KeywordExt *keyword); bool already_determined (KeywordExt *keyword) const;
/* Reorders the keyword list so as to minimize search times. */ /* Reorders the keyword list so as to minimize search times. */
void reorder (); void reorder ();
/* Returns the length of keyword list. */ /* Returns the length of keyword list. */
int keyword_list_length (); int keyword_list_length () const;
/* Returns the maximum length of keywords. */ /* Returns the maximum length of keywords. */
int max_key_length (); int max_key_length () const;
/* Returns the number of key positions. */ /* Returns the number of key positions. */
int get_max_keysig_size (); int get_max_keysig_size () const;
/* Initializes the asso_values[] related parameters and put a first guess /* Initializes the asso_values[] related parameters and put a first guess
into asso_values[]. */ into asso_values[]. */
@@ -68,10 +68,10 @@ private:
/* Computes a keyword's hash value, relative to the current _asso_values[], /* Computes a keyword's hash value, relative to the current _asso_values[],
and stores it in keyword->_hash_value. */ and stores it in keyword->_hash_value. */
int compute_hash (KeywordExt *keyword); int compute_hash (KeywordExt *keyword) const;
/* Sorts the given set in increasing frequency of _occurrences[]. */ /* Sorts the given set in increasing frequency of _occurrences[]. */
void sort_by_occurrence (unsigned char *set, int len); void sort_by_occurrence (unsigned char *set, int len) const;
/* Tries various other values for _asso_values[c]. */ /* Tries various other values for _asso_values[c]. */
bool try_asso_value (unsigned char c, KeywordExt *curr, int iterations); bool try_asso_value (unsigned char c, KeywordExt *curr, int iterations);
@@ -83,7 +83,9 @@ private:
/* Finds good _asso_values[]. */ /* Finds good _asso_values[]. */
void find_asso_values (); void find_asso_values ();
/* Sorts the keyword list by hash value. */
void sort (); void sort ();
public: public:
/* Linked list of keywords. */ /* Linked list of keywords. */