From 741d34fb0701ce8d2784fd210f2b5284b3496a91 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 13 Jan 2003 19:57:56 +0000 Subject: [PATCH] Use const where possible. --- ChangeLog | 32 ++++++++++++++++++++++++++++++++ src/hash-table.cc | 2 +- src/hash-table.h | 4 ++-- src/input.h | 2 +- src/keyword-list.h | 4 ++-- src/keyword-list.icc | 4 ++-- src/output.cc | 18 +++++++++--------- src/output.h | 32 ++++++++++++++++---------------- src/search.cc | 20 ++++++++++---------- src/search.h | 20 +++++++++++--------- 10 files changed, 86 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index 288294d..ddabf85 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,37 @@ 2002-11-03 Bruno Haible + * 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 cases. Avoids gcc warnings on the generated code. diff --git a/src/hash-table.cc b/src/hash-table.cc index 3b8c139..a4d2a1b 100644 --- a/src/hash-table.cc +++ b/src/hash-table.cc @@ -121,7 +121,7 @@ Hash_Table::~Hash_Table () /* Compares two items. */ inline bool -Hash_Table::equal (KeywordExt *item1, KeywordExt *item2) +Hash_Table::equal (KeywordExt *item1, KeywordExt *item2) const { return item1->_selchars_length == item2->_selchars_length && memcmp (item1->_selchars, item2->_selchars, item2->_selchars_length) diff --git a/src/hash-table.h b/src/hash-table.h index 4de8466..22ad955 100644 --- a/src/hash-table.h +++ b/src/hash-table.h @@ -53,12 +53,12 @@ private: /* log2(_size). */ unsigned int _log_size; /* A detail of the comparison function. */ - bool _ignore_length; + bool const _ignore_length; /* Statistics: Number of collisions so far. */ unsigned int _collisions; /* Compares two items. */ - bool equal (KeywordExt *item1, KeywordExt *item2); + bool equal (KeywordExt *item1, KeywordExt *item2) const; }; #endif diff --git a/src/input.h b/src/input.h index fa7be68..14b0e1e 100644 --- a/src/input.h +++ b/src/input.h @@ -48,7 +48,7 @@ public: const char * _struct_tag; /* Shorthand for user-defined struct tag type. */ const char * _include_src; /* C source code to be included verbatim. */ 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. */ private: Keyword_List * parse_line (const char *line, const char *delimiters); diff --git a/src/keyword-list.h b/src/keyword-list.h index 6cf0c33..09f4988 100644 --- a/src/keyword-list.h +++ b/src/keyword-list.h @@ -35,7 +35,7 @@ public: Keyword_List (Keyword *car); /* Access to first element of list. */ - Keyword * first (); + Keyword * first () const; /* Access to next element of list. */ Keyword_List *& rest (); @@ -52,7 +52,7 @@ public: KeywordExt_List (KeywordExt *car); /* Access to first element of list. */ - KeywordExt * first (); + KeywordExt * first () const; /* Access to next element of list. */ KeywordExt_List *& rest (); }; diff --git a/src/keyword-list.icc b/src/keyword-list.icc index bfecb2c..233e2d3 100644 --- a/src/keyword-list.icc +++ b/src/keyword-list.icc @@ -24,7 +24,7 @@ /* Access to first element of list. */ INLINE Keyword * -Keyword_List::first () +Keyword_List::first () const { return _car; } @@ -40,7 +40,7 @@ Keyword_List::rest () /* Access to first element of list. */ INLINE KeywordExt * -KeywordExt_List::first () +KeywordExt_List::first () const { return static_cast(_car); } diff --git a/src/output.cc b/src/output.cc index 3001689..eac1d57 100644 --- a/src/output.cc +++ b/src/output.cc @@ -119,7 +119,7 @@ Output::compute_min_max () /* Returns the number of different hash values. */ int -Output::num_hash_values () +Output::num_hash_values () const { /* Since the list is already sorted by hash value we can count the 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. */ void -Output::output_constants (struct Output_Constants& style) +Output::output_constants (struct Output_Constants& style) const { style.output_start (); style.output_item ("TOTAL_KEYWORDS", _total_keys); @@ -430,7 +430,7 @@ void Output_Compare_Memcmp::output_comparison (const Output_Expr& expr1, unsigned int (const char *str, unsigned int len). */ void -Output::output_hash_function () +Output::output_hash_function () const { /* Output the function's head. */ if (option[CPLUSPLUS]) @@ -610,7 +610,7 @@ Output::output_hash_function () Only called if option[LENTABLE]. */ void -Output::output_keylength_table () +Output::output_keylength_table () const { const int columns = 14; 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. */ void -Output::output_keyword_table () +Output::output_keyword_table () const { const char *indent = option[GLOBAL] ? "" : " "; int index; @@ -793,7 +793,7 @@ Output::output_keyword_table () the smaller, contiguous range of the keyword table. */ void -Output::output_lookup_array () +Output::output_lookup_array () const { if (option[DUP]) { @@ -956,7 +956,7 @@ Output::output_lookup_array () /* Generate all the tables needed for the lookup function. */ void -Output::output_lookup_tables () +Output::output_lookup_tables () const { 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. */ 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" " {\n" @@ -1385,7 +1385,7 @@ Output::output_lookup_function_body (const Output_Compare& comparison) /* Generates C code for the lookup function. */ void -Output::output_lookup_function () +Output::output_lookup_function () const { /* Output the function's head. */ if (option[KRC] | option[C] | option[ANSIC]) diff --git a/src/output.h b/src/output.h index 74006cc..57506c4 100644 --- a/src/output.h +++ b/src/output.h @@ -59,56 +59,56 @@ private: void compute_min_max (); /* Returns the number of different hash values. */ - int num_hash_values (); + int num_hash_values () const; /* 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 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 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. */ - void output_keyword_table (); + void output_keyword_table () const; /* Generates the large, sparse table that maps hash values into 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. */ - void output_lookup_tables (); + void output_lookup_tables () const; /* 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. */ - void output_lookup_function (); + void output_lookup_function () const; /* Linked list of keywords. */ KeywordExt_List * _head; /* Pointer to the type for word list. */ - const char * _array_type; + const char * const _array_type; /* Pointer to return type for lookup function. */ const char * _return_type; /* Shorthand for user-defined struct tag type. */ const char * _struct_tag; /* True if any additional C code is included. */ - bool _additional_code; + bool const _additional_code; /* C source code to be included verbatim. */ - const char * _include_src; + const char * const _include_src; /* Total number of keys, counting duplicates. */ - int _total_keys; + int const _total_keys; /* Total number of duplicate hash values. */ - int _total_duplicates; + int const _total_duplicates; /* Maximum length of the longest keyword. */ - int _max_key_len; + int const _max_key_len; /* Minimum length of the shortest keyword. */ - int _min_key_len; + int const _min_key_len; /* Minimum hash value for all keywords. */ int _min_hash_value; /* Maximum hash value for all keywords. */ diff --git a/src/search.cc b/src/search.cc index c0cdd86..7e58f4b 100644 --- a/src/search.cc +++ b/src/search.cc @@ -161,7 +161,7 @@ Search::prepare () every comparison. */ KeywordExt_List * -Search::merge (KeywordExt_List *list1, KeywordExt_List *list2) +Search::merge (KeywordExt_List *list1, KeywordExt_List *list2) const { KeywordExt_List *result; KeywordExt_List **resultp = &result; @@ -199,7 +199,7 @@ Search::merge (KeywordExt_List *list1, KeywordExt_List *list2) is set to true. */ KeywordExt_List * -Search::merge_sort (KeywordExt_List *head) +Search::merge_sort (KeywordExt_List *head) const { if (!head || !head->rest()) /* 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. */ inline int -Search::compute_occurrence (KeywordExt *ptr) +Search::compute_occurrence (KeywordExt *ptr) const { int value = 0; @@ -276,7 +276,7 @@ Search::set_determined (KeywordExt *keyword) Returns true if the keyword's selected characters are all determined. */ inline bool -Search::already_determined (KeywordExt *keyword) +Search::already_determined (KeywordExt *keyword) const { const unsigned char *p = keyword->_selchars; unsigned int i = keyword->_selchars_length; @@ -363,7 +363,7 @@ Search::reorder () /* Returns the length of keyword list. */ int -Search::keyword_list_length () +Search::keyword_list_length () const { return _list_len; } @@ -371,7 +371,7 @@ Search::keyword_list_length () /* Returns the maximum length of keywords. */ int -Search::max_key_length () +Search::max_key_length () const { return _max_key_len; } @@ -379,7 +379,7 @@ Search::max_key_length () /* Returns the number of key positions. */ int -Search::get_max_keysig_size () +Search::get_max_keysig_size () const { 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! */ inline int -Search::compute_hash (KeywordExt *keyword) +Search::compute_hash (KeywordExt *keyword) const { 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[]. */ 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. */ 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 Search::sort () diff --git a/src/search.h b/src/search.h index 51fbd4f..540887c 100644 --- a/src/search.h +++ b/src/search.h @@ -39,28 +39,28 @@ private: void prepare (); /* 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. */ - 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. */ - int compute_occurrence (KeywordExt *ptr); + int compute_occurrence (KeywordExt *ptr) const; /* Auxiliary functions used by Search::reorder(). */ void clear_determined (); 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. */ void reorder (); /* Returns the length of keyword list. */ - int keyword_list_length (); + int keyword_list_length () const; /* Returns the maximum length of keywords. */ - int max_key_length (); + int max_key_length () const; /* 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 into asso_values[]. */ @@ -68,10 +68,10 @@ private: /* Computes a keyword's hash value, relative to the current _asso_values[], 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[]. */ - 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]. */ bool try_asso_value (unsigned char c, KeywordExt *curr, int iterations); @@ -83,7 +83,9 @@ private: /* Finds good _asso_values[]. */ void find_asso_values (); + /* Sorts the keyword list by hash value. */ void sort (); + public: /* Linked list of keywords. */