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

Remove the global vectors. Move them to the class Search.

This commit is contained in:
Bruno Haible
2002-11-28 12:56:08 +00:00
parent aca3f4abae
commit c73312f0fd
10 changed files with 65 additions and 96 deletions

View File

@@ -1,5 +1,31 @@
2002-11-02 Bruno Haible <bruno@clisp.org> 2002-11-02 Bruno Haible <bruno@clisp.org>
* 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.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/search,cc: New file, combines src/key-list.cc, src/gen-perf.cc.
* src/key-list.h: Remove file. * src/key-list.h: Remove file.

View File

@@ -62,7 +62,7 @@ SHELL = /bin/sh
VPATH = $(srcdir) VPATH = $(srcdir)
OBJECTS = options.o main.o \ 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 keyword.o keyword-list.o output.o input.o search.o
LIBS = ../lib/libgp.a @GPERF_LIBM@ LIBS = ../lib/libgp.a @GPERF_LIBM@
CPPFLAGS = -I. -I$(srcdir)/../lib CPPFLAGS = -I. -I$(srcdir)/../lib
@@ -86,7 +86,6 @@ $(TARGETPROG): $(OBJECTS)
# Dependencies. # Dependencies.
CONFIG_H = config.h CONFIG_H = config.h
VERSION_H = version.h VERSION_H = version.h
VECTORS_H = vectors.h
READ_LINE_H = read-line.h read-line.icc READ_LINE_H = read-line.h read-line.icc
OPTIONS_H = options.h options.icc OPTIONS_H = options.h options.icc
HASH_TABLE_H = hash-table.h 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 $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/options.cc
read-line.o : read-line.cc $(READ_LINE_H) $(OPTIONS_H) read-line.o : read-line.cc $(READ_LINE_H) $(OPTIONS_H)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/read-line.cc $(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) version.o : version.cc $(VERSION_H)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/version.cc $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/version.cc
keyword.o : keyword.cc keyword.h keyword.o : keyword.cc keyword.h

View File

@@ -26,8 +26,6 @@
#ifndef keyword_h #ifndef keyword_h
#define keyword_h 1 #define keyword_h 1
#include "vectors.h"
/* An instance of this class is a keyword, as specified in the input file. */ /* An instance of this class is a keyword, as specified in the input file. */
struct Keyword struct Keyword
{ {
@@ -37,7 +35,7 @@ struct Keyword
/* Data members defined immediately by the input file. */ /* Data members defined immediately by the input file. */
/* The keyword as a string, possibly containing NUL bytes. */ /* The keyword as a string, possibly containing NUL bytes. */
const char *const _allchars; const char *const _allchars;
const int _allchars_length; int const _allchars_length;
/* Additional stuff seen on the same line of the input file. */ /* Additional stuff seen on the same line of the input file. */
const char *const _rest; const char *const _rest;
}; };

View File

@@ -51,7 +51,6 @@ main (int argc, char *argv[])
/* Initialize the key word list. */ /* Initialize the key word list. */
KeywordExt_Factory factory; KeywordExt_Factory factory;
Input inputter (&factory); Input inputter (&factory);
Vectors::ALPHA_SIZE = (option[SEVENBIT] ? 128 : 256);
inputter.read_keys (); inputter.read_keys ();
/* We can cast the keyword list to KeywordExt_List* because its list /* We can cast the keyword list to KeywordExt_List* because its list
elements were created by KeywordExt_Factory. */ elements were created by KeywordExt_Factory. */
@@ -72,7 +71,9 @@ main (int argc, char *argv[])
searcher._total_duplicates, searcher._total_duplicates,
searcher._max_key_len, searcher._max_key_len,
searcher._min_key_len, searcher._min_key_len,
&searcher); searcher._alpha_size,
searcher._occurrences,
searcher._asso_values);
outputter.output (); outputter.output ();
/* Check for write error on stdout. */ /* Check for write error on stdout. */

View File

@@ -70,12 +70,14 @@ Output::Output (KeywordExt_List *head, const char *array_type,
const char *return_type, const char *struct_tag, const char *return_type, const char *struct_tag,
bool additional_code, const char *include_src, bool additional_code, const char *include_src,
int total_keys, int total_duplicates, int max_key_len, 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), : _head (head), _array_type (array_type), _return_type (return_type),
_struct_tag (struct_tag), _additional_code (additional_code), _struct_tag (struct_tag), _additional_code (additional_code),
_include_src (include_src), _total_keys (total_keys), _include_src (include_src), _total_keys (total_keys),
_total_duplicates (total_duplicates), _max_key_len (max_key_len), _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, const_readonly_array,
smallest_integral_type (_max_hash_value + 1)); 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) if (count > 0)
printf (","); printf (",");
if (!(count % max_column)) if (!(count % max_column))
printf ("\n "); printf ("\n ");
printf ("%*d", field_width, 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" printf ("\n"

View File

@@ -27,7 +27,6 @@
#define output_h 1 #define output_h 1
#include "keyword-list.h" #include "keyword-list.h"
#include "vectors.h"
/* OSF/1 cxx needs these forward declarations. */ /* OSF/1 cxx needs these forward declarations. */
struct Output_Constants; struct Output_Constants;
@@ -36,7 +35,7 @@ struct Output_Compare;
class Output class Output
{ {
public: 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 (); void output ();
private: private:
void compute_min_max (); void compute_min_max ();
@@ -75,7 +74,12 @@ private:
int _min_hash_value; int _min_hash_value;
/* Maximum hash value for all keywords. */ /* Maximum hash value for all keywords. */
int _max_hash_value; 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 #endif

View File

@@ -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))) #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) 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 void
Search::prepare () Search::prepare ()
{ {
@@ -529,7 +531,7 @@ Search::optimize ()
{ {
srand (reinterpret_cast<long>(time (0))); srand (reinterpret_cast<long>(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); _asso_values[i] = (rand () & asso_value_max - 1);
} }
else else
@@ -537,7 +539,7 @@ Search::optimize ()
int asso_value = option.get_initial_asso_value (); int asso_value = option.get_initial_asso_value ();
if (asso_value) /* Initialize array if user requests non-zero default. */ 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; _asso_values[i] = asso_value & get_asso_max () - 1;
} }
_max_hash_value = max_key_length () + get_asso_max () * get_max_keysig_size (); _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"); 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]) if (_occurrences[i])
fprintf (stderr, "asso_values[%c] = %6d, occurrences[%c] = %6d\n", fprintf (stderr, "asso_values[%c] = %6d, occurrences[%c] = %6d\n",
i, _asso_values[i], i, _occurrences[i]); i, _asso_values[i], i, _occurrences[i]);

View File

@@ -27,10 +27,9 @@
#define search_h 1 #define search_h 1
#include "keyword-list.h" #include "keyword-list.h"
#include "vectors.h"
#include "bool-array.h" #include "bool-array.h"
class Search : public Vectors class Search
{ {
public: public:
Search (KeywordExt_List *list); Search (KeywordExt_List *list);
@@ -40,16 +39,16 @@ private:
void prepare (); void prepare ();
KeywordExt_List * merge (KeywordExt_List *list1, KeywordExt_List *list2); KeywordExt_List * merge (KeywordExt_List *list1, KeywordExt_List *list2);
KeywordExt_List * merge_sort (KeywordExt_List *head); KeywordExt_List * merge_sort (KeywordExt_List *head);
static int get_occurrence (KeywordExt *ptr); int get_occurrence (KeywordExt *ptr);
static void set_determined (KeywordExt *ptr); void set_determined (KeywordExt *ptr);
static bool already_determined (KeywordExt *ptr); bool already_determined (KeywordExt *ptr);
void reorder (); void reorder ();
int keyword_list_length (); int keyword_list_length ();
int max_key_length (); int max_key_length ();
int get_max_keysig_size (); 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 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); bool affects_prev (char c, KeywordExt *curr);
void change (KeywordExt *prior, KeywordExt *curr); void change (KeywordExt *prior, KeywordExt *curr);
void sort (); void sort ();
@@ -59,11 +58,17 @@ public:
int _total_duplicates; /* Total number of duplicate hash values. */ int _total_duplicates; /* Total number of duplicate hash values. */
int _max_key_len; /* Maximum length of the longest keyword. */ int _max_key_len; /* Maximum length of the longest keyword. */
int _min_key_len; /* Minimum length of the shortest 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: private:
int _list_len; /* Length of head's Key_List, not counting duplicates. */ int _list_len; /* Length of head's Key_List, not counting duplicates. */
bool _occurrence_sort; /* True if sorting by occurrence. */ bool _occurrence_sort; /* True if sorting by occurrence. */
bool _hash_sort; /* True if sorting by hash value. */ 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 _num_done; /* Number of keywords processed without a collision. */
int _fewest_collisions; /* Records fewest # of collisions for asso value. */ int _fewest_collisions; /* Records fewest # of collisions for asso value. */
int _max_hash_value; /* Maximum possible hash value. */ int _max_hash_value; /* Maximum possible hash value. */

View File

@@ -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 <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>.
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];

View File

@@ -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 <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>.
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