From c73312f0fd69fe47b957af8e91d45738f4a6c371 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 28 Nov 2002 12:56:08 +0000 Subject: [PATCH] Remove the global vectors. Move them to the class Search. --- ChangeLog | 26 ++++++++++++++++++++++++++ src/Makefile.in | 5 +---- src/keyword.h | 4 +--- src/main.cc | 5 +++-- src/output.cc | 10 ++++++---- src/output.h | 10 +++++++--- src/search.cc | 14 ++++++++------ src/search.h | 21 +++++++++++++-------- src/vectors.cc | 27 --------------------------- src/vectors.h | 39 --------------------------------------- 10 files changed, 65 insertions(+), 96 deletions(-) delete mode 100644 src/vectors.cc delete mode 100644 src/vectors.h diff --git a/ChangeLog b/ChangeLog index 282526e..d4c495b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,31 @@ 2002-11-02 Bruno Haible + * src/vectors.h: Remove file. + * src/vectors.cc: Remove file. + * src/search.h: Don't include vectors.h. + (Search): Don't inherit from Vectors. New fields _alpha_size, + _occurrences, _asso_values. + (Search::_determined, Search::get_occurrence, Search::set_determined, + Search::already_determined, Search::hash, Search::sort_set): Make + nonstatic. + * src/search.cc (Search::Search): Initialize _alpha_size, _occurrences, + _asso_values, _determined. + (Search::optimize, Search::~Search): Update. + * src/output.h: Don't include vectors.h. + (Output): Remove field _v. New fields _alpha_size, _occurrences, + _asso_values. + (Output::Output): Replace Vectors* argument with alpha_size, + occurrences, asso_values. + * src/output.cc (Output::Output): Replace Vectors* argument with + alpha_size, occurrences, asso_values. + (Output::output_hash_function): Update. + * src/main.cc (main): Don't set Vectors::ALPHA_SIZE. + Pass _alpha_size, _occurrences, _asso_values from Search to Output. + * src/keyword.h: Don't include vectors.h. + * src/Makefile.in (OBJECTS): Remove vectors.o. + (VECTORS_H): Remove variable. + (vectors.o): Remove rule. + * src/search.h: New file, combines src/key-list.h, src/gen-perf.h. * src/search,cc: New file, combines src/key-list.cc, src/gen-perf.cc. * src/key-list.h: Remove file. diff --git a/src/Makefile.in b/src/Makefile.in index 7b2a50d..68be761 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -62,7 +62,7 @@ SHELL = /bin/sh VPATH = $(srcdir) OBJECTS = options.o main.o \ - hash-table.o bool-array.o read-line.o vectors.o version.o \ + hash-table.o bool-array.o read-line.o version.o \ keyword.o keyword-list.o output.o input.o search.o LIBS = ../lib/libgp.a @GPERF_LIBM@ CPPFLAGS = -I. -I$(srcdir)/../lib @@ -86,7 +86,6 @@ $(TARGETPROG): $(OBJECTS) # Dependencies. CONFIG_H = config.h VERSION_H = version.h -VECTORS_H = vectors.h READ_LINE_H = read-line.h read-line.icc OPTIONS_H = options.h options.icc HASH_TABLE_H = hash-table.h @@ -102,8 +101,6 @@ options.o : options.cc $(OPTIONS_H) $(VECTORS_H) $(VERSION_H) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/options.cc read-line.o : read-line.cc $(READ_LINE_H) $(OPTIONS_H) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/read-line.cc -vectors.o : vectors.cc $(VECTORS_H) - $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/vectors.cc version.o : version.cc $(VERSION_H) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/version.cc keyword.o : keyword.cc keyword.h diff --git a/src/keyword.h b/src/keyword.h index d78cb0c..705c481 100644 --- a/src/keyword.h +++ b/src/keyword.h @@ -26,8 +26,6 @@ #ifndef keyword_h #define keyword_h 1 -#include "vectors.h" - /* An instance of this class is a keyword, as specified in the input file. */ struct Keyword { @@ -37,7 +35,7 @@ struct Keyword /* Data members defined immediately by the input file. */ /* The keyword as a string, possibly containing NUL bytes. */ const char *const _allchars; - const int _allchars_length; + int const _allchars_length; /* Additional stuff seen on the same line of the input file. */ const char *const _rest; }; diff --git a/src/main.cc b/src/main.cc index f73bd7e..78e966f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -51,7 +51,6 @@ main (int argc, char *argv[]) /* Initialize the key word list. */ KeywordExt_Factory factory; Input inputter (&factory); - Vectors::ALPHA_SIZE = (option[SEVENBIT] ? 128 : 256); inputter.read_keys (); /* We can cast the keyword list to KeywordExt_List* because its list elements were created by KeywordExt_Factory. */ @@ -72,7 +71,9 @@ main (int argc, char *argv[]) searcher._total_duplicates, searcher._max_key_len, searcher._min_key_len, - &searcher); + searcher._alpha_size, + searcher._occurrences, + searcher._asso_values); outputter.output (); /* Check for write error on stdout. */ diff --git a/src/output.cc b/src/output.cc index d3c99b0..26344c2 100644 --- a/src/output.cc +++ b/src/output.cc @@ -70,12 +70,14 @@ Output::Output (KeywordExt_List *head, const char *array_type, const char *return_type, const char *struct_tag, bool additional_code, const char *include_src, int total_keys, int total_duplicates, int max_key_len, - int min_key_len, Vectors *v) + int min_key_len, int alpha_size, const int *occurrences, + const int *asso_values) : _head (head), _array_type (array_type), _return_type (return_type), _struct_tag (struct_tag), _additional_code (additional_code), _include_src (include_src), _total_keys (total_keys), _total_duplicates (total_duplicates), _max_key_len (max_key_len), - _min_key_len (min_key_len), _v (v) + _min_key_len (min_key_len), _alpha_size (alpha_size), + _occurrences (occurrences), _asso_values (asso_values) { } @@ -453,14 +455,14 @@ Output::output_hash_function () const_readonly_array, smallest_integral_type (_max_hash_value + 1)); - for (int count = 0; count < _v->ALPHA_SIZE; count++) + for (int count = 0; count < _alpha_size; count++) { if (count > 0) printf (","); if (!(count % max_column)) printf ("\n "); printf ("%*d", field_width, - _v->_occurrences[count] ? _v->_asso_values[count] : _max_hash_value + 1); + _occurrences[count] ? _asso_values[count] : _max_hash_value + 1); } printf ("\n" diff --git a/src/output.h b/src/output.h index 4dd5ee4..b705cb4 100644 --- a/src/output.h +++ b/src/output.h @@ -27,7 +27,6 @@ #define output_h 1 #include "keyword-list.h" -#include "vectors.h" /* OSF/1 cxx needs these forward declarations. */ struct Output_Constants; @@ -36,7 +35,7 @@ struct Output_Compare; class Output { public: - Output (KeywordExt_List *head, const char *array_type, const char *return_type, const char *struct_tag, bool additional_code, const char *include_src, int total_keys, int total_duplicates, int max_key_len, int min_key_len, Vectors *v); + Output (KeywordExt_List *head, const char *array_type, const char *return_type, const char *struct_tag, bool additional_code, const char *include_src, int total_keys, int total_duplicates, int max_key_len, int min_key_len, int alpha_size, const int *occurrences, const int *asso_values); void output (); private: void compute_min_max (); @@ -75,7 +74,12 @@ private: int _min_hash_value; /* Maximum hash value for all keywords. */ int _max_hash_value; - Vectors * _v; + /* Size of alphabet. */ + int const _alpha_size; + /* Counts occurrences of each key set character. */ + const int * const _occurrences; + /* Value associated with each character. */ + const int * const _asso_values; }; #endif diff --git a/src/search.cc b/src/search.cc index 0a16055..7f70bf3 100644 --- a/src/search.cc +++ b/src/search.cc @@ -36,12 +36,14 @@ static const int TABLE_MULTIPLE = 10; #define POW(X) ((!X)?1:(X-=1,X|=X>>1,X|=X>>2,X|=X>>4,X|=X>>8,X|=X>>16,(++X))) Search::Search (KeywordExt_List *list) - : _head (list) + : _head (list), + _alpha_size (option[SEVENBIT] ? 128 : 256), + _occurrences (new int[_alpha_size]), + _asso_values (new int[_alpha_size]), + _determined (new bool[_alpha_size]) { } -bool Search::_determined[MAX_ALPHA_SIZE]; - void Search::prepare () { @@ -529,7 +531,7 @@ Search::optimize () { srand (reinterpret_cast(time (0))); - for (int i = 0; i < ALPHA_SIZE; i++) + for (int i = 0; i < _alpha_size; i++) _asso_values[i] = (rand () & asso_value_max - 1); } else @@ -537,7 +539,7 @@ Search::optimize () int asso_value = option.get_initial_asso_value (); if (asso_value) /* Initialize array if user requests non-zero default. */ - for (int i = ALPHA_SIZE - 1; i >= 0; i--) + for (int i = _alpha_size - 1; i >= 0; i--) _asso_values[i] = asso_value & get_asso_max () - 1; } _max_hash_value = max_key_length () + get_asso_max () * get_max_keysig_size (); @@ -603,7 +605,7 @@ Search::~Search () { fprintf (stderr, "\ndumping occurrence and associated values tables\n"); - for (int i = 0; i < ALPHA_SIZE; i++) + for (int i = 0; i < _alpha_size; i++) if (_occurrences[i]) fprintf (stderr, "asso_values[%c] = %6d, occurrences[%c] = %6d\n", i, _asso_values[i], i, _occurrences[i]); diff --git a/src/search.h b/src/search.h index 4e94bc2..ef83da4 100644 --- a/src/search.h +++ b/src/search.h @@ -27,10 +27,9 @@ #define search_h 1 #include "keyword-list.h" -#include "vectors.h" #include "bool-array.h" -class Search : public Vectors +class Search { public: Search (KeywordExt_List *list); @@ -40,16 +39,16 @@ private: void prepare (); KeywordExt_List * merge (KeywordExt_List *list1, KeywordExt_List *list2); KeywordExt_List * merge_sort (KeywordExt_List *head); - static int get_occurrence (KeywordExt *ptr); - static void set_determined (KeywordExt *ptr); - static bool already_determined (KeywordExt *ptr); + int get_occurrence (KeywordExt *ptr); + void set_determined (KeywordExt *ptr); + bool already_determined (KeywordExt *ptr); void reorder (); int keyword_list_length (); int max_key_length (); int get_max_keysig_size (); - static int hash (KeywordExt *key_node); + int hash (KeywordExt *key_node); static int compute_disjoint_union (const char *set_1, int size_1, const char *set_2, int size_2, char *set_3); - static void sort_set (char *union_set, int len); + void sort_set (char *union_set, int len); bool affects_prev (char c, KeywordExt *curr); void change (KeywordExt *prior, KeywordExt *curr); void sort (); @@ -59,11 +58,17 @@ public: int _total_duplicates; /* Total number of duplicate hash values. */ int _max_key_len; /* Maximum length of the longest keyword. */ int _min_key_len; /* Minimum length of the shortest keyword. */ + /* Size of alphabet. */ + int const _alpha_size; + /* Counts occurrences of each key set character. */ + int * const _occurrences; + /* Value associated with each character. */ + int * const _asso_values; private: int _list_len; /* Length of head's Key_List, not counting duplicates. */ bool _occurrence_sort; /* True if sorting by occurrence. */ bool _hash_sort; /* True if sorting by hash value. */ - static bool _determined[MAX_ALPHA_SIZE]; /* Used in function reorder, below. */ + bool * const _determined; /* Used in function reorder, below. */ int _num_done; /* Number of keywords processed without a collision. */ int _fewest_collisions; /* Records fewest # of collisions for asso value. */ int _max_hash_value; /* Maximum possible hash value. */ diff --git a/src/vectors.cc b/src/vectors.cc deleted file mode 100644 index 6d2ff3b..0000000 --- a/src/vectors.cc +++ /dev/null @@ -1,27 +0,0 @@ -/* Static class data members that are shared between several classes. - Copyright (C) 1989-1998, 2002 Free Software Foundation, Inc. - Written by Douglas C. Schmidt - and Bruno Haible . - - This file is part of GNU GPERF. - - GNU GPERF is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. - - GNU GPERF is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; see the file COPYING. - If not, write to the Free Software Foundation, Inc., - 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#include "vectors.h" - -int Vectors::ALPHA_SIZE; -int Vectors::_occurrences[MAX_ALPHA_SIZE]; -int Vectors::_asso_values[MAX_ALPHA_SIZE]; diff --git a/src/vectors.h b/src/vectors.h deleted file mode 100644 index d32b9da..0000000 --- a/src/vectors.h +++ /dev/null @@ -1,39 +0,0 @@ -/* This may look like C code, but it is really -*- C++ -*- */ - -/* Static class data members that are shared between several classes via - inheritance. - - Copyright (C) 1989-1998, 2002 Free Software Foundation, Inc. - Written by Douglas C. Schmidt - and Bruno Haible . - - This file is part of GNU GPERF. - - GNU GPERF is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. - - GNU GPERF is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; see the file COPYING. - If not, write to the Free Software Foundation, Inc., - 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#ifndef vectors_h -#define vectors_h 1 - -static const int MAX_ALPHA_SIZE = 256; - -struct Vectors -{ - static int ALPHA_SIZE; /* Size of alphabet. */ - static int _occurrences[MAX_ALPHA_SIZE]; /* Counts occurrences of each key set character. */ - static int _asso_values[MAX_ALPHA_SIZE]; /* Value associated with each character. */ -}; - -#endif