diff options
author | Bruno Haible <bruno@clisp.org> | 2003-02-13 12:11:01 +0000 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2003-02-13 12:11:01 +0000 |
commit | ee135115f5da5bcfe6626a08fb91891d2aa2ee86 (patch) | |
tree | eb0f8ff0556019c6c282548d7de0e99582785c50 | |
parent | 810fef43aebd9cd1d58d9a6a412c49835d3e8471 (diff) | |
download | gperf-ee135115f5da5bcfe6626a08fb91891d2aa2ee86.tar.gz |
Move positions handling to its own file.
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | src/Makefile.in | 13 | ||||
-rw-r--r-- | src/keyword.cc | 2 | ||||
-rw-r--r-- | src/keyword.h | 2 | ||||
-rw-r--r-- | src/options.cc | 129 | ||||
-rw-r--r-- | src/options.h | 74 | ||||
-rw-r--r-- | src/options.icc | 114 | ||||
-rw-r--r-- | src/output.h | 2 | ||||
-rw-r--r-- | src/positions.cc | 167 | ||||
-rw-r--r-- | src/positions.h | 111 | ||||
-rw-r--r-- | src/positions.icc | 139 | ||||
-rw-r--r-- | src/search.h | 2 |
12 files changed, 445 insertions, 325 deletions
@@ -1,5 +1,20 @@ 2002-11-17 Bruno Haible <bruno@clisp.org> + * src/positions.h: New file, extracted from options.h. + * src/positions.icc: New file, extracted from options.icc. + * src/positions.cc: New file, extracted from options.cc. + * src/options.h: Include positions.h. Move classes Positions and + PositionsIterator away. + * src/options.icc: Move classes Positions and PositionsIterator away. + * src/options.cc: Move class Positions away. + * src/keyword.cc: Include positions.h instead of options.h. + * src/output.h: Include positions.h instead of options.h. + * src/search.h: Include positions.h instead of options.h. + * src/Makefile.in (OBJECTS): Add positions.o. + (POSITIONS_H): New variable. + (OPTIONS_H, SEARCH_H, OUTPUT_H, keyword.o): Use it. + (positions.o): New rule. + * src/options.h (POSITIONS): New enum value. (Positions::Positions): New copy constructor. (Positions::operator=, Positions::contains, Position::add, diff --git a/src/Makefile.in b/src/Makefile.in index d959696..8774371 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -61,7 +61,7 @@ SHELL = /bin/sh VPATH = $(srcdir) -OBJECTS = version.o options.o keyword.o keyword-list.o \ +OBJECTS = version.o positions.o options.o keyword.o keyword-list.o \ input.o bool-array.o hash-table.o search.o output.o main.o LIBS = ../lib/libgp.a @GPERF_LIBM@ CPPFLAGS = -I. -I$(srcdir)/../lib @@ -85,20 +85,23 @@ $(TARGETPROG): $(OBJECTS) # Dependencies. CONFIG_H = config.h VERSION_H = version.h -OPTIONS_H = options.h options.icc +POSITIONS_H = positions.h positions.icc +OPTIONS_H = options.h options.icc $(POSITIONS_H) KEYWORD_H = keyword.h keyword.icc KEYWORD_LIST_H = keyword-list.h keyword-list.icc $(KEYWORD_H) INPUT_H = input.h $(KEYWORD_LIST_H) BOOL_ARRAY_H = bool-array.h bool-array.icc $(OPTIONS_H) HASH_TABLE_H = hash-table.h $(KEYWORD_H) -SEARCH_H = search.h $(KEYWORD_LIST_H) $(OPTIONS_H) $(BOOL_ARRAY_H) -OUTPUT_H = output.h $(KEYWORD_LIST_H) $(OPTIONS_H) +SEARCH_H = search.h $(KEYWORD_LIST_H) $(POSITIONS_H) $(BOOL_ARRAY_H) +OUTPUT_H = output.h $(KEYWORD_LIST_H) $(POSITIONS_H) version.o : version.cc $(VERSION_H) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/version.cc +positions.o : positions.cc $(POSITIONS_H) + $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/positions.cc options.o : options.cc $(OPTIONS_H) $(VERSION_H) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/options.cc -keyword.o : keyword.cc $(KEYWORD_H) $(OPTIONS_H) +keyword.o : keyword.cc $(KEYWORD_H) $(POSITIONS_H) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/keyword.cc keyword-list.o : keyword-list.cc $(KEYWORD_LIST_H) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/keyword-list.cc diff --git a/src/keyword.cc b/src/keyword.cc index 73608d0..7ed112f 100644 --- a/src/keyword.cc +++ b/src/keyword.cc @@ -26,7 +26,7 @@ #include <stddef.h> #include <stdio.h> #include <stdlib.h> -#include "options.h" +#include "positions.h" /* --------------------------- KeywordExt class --------------------------- */ diff --git a/src/keyword.h b/src/keyword.h index b2f0a75..4f0b509 100644 --- a/src/keyword.h +++ b/src/keyword.h @@ -26,7 +26,7 @@ #ifndef keyword_h #define keyword_h 1 -/* Class defined in "options.h". */ +/* Class defined in "positions.h". */ class Positions; /* An instance of this class is a keyword, as specified in the input file. */ diff --git a/src/options.cc b/src/options.cc index 885504e..d7f670d 100644 --- a/src/options.cc +++ b/src/options.cc @@ -940,135 +940,6 @@ Options::parse_options (int argc, char *argv[]) } } -/* ---------------------------- Class Positions ---------------------------- */ - -/* Set operations. Assumes the array is in reverse order. */ - -bool -Positions::contains (int pos) const -{ - unsigned int count = _size; - const unsigned char *p = _positions + _size - 1; - - for (; count > 0; p--, count--) - { - if (*p == pos) - return true; - if (*p > pos) - break; - } - return false; -} - -void -Positions::add (int pos) -{ - unsigned int count = _size; - - if (count == MAX_KEY_POS + 1) - { - fprintf (stderr, "Positions::add internal error: overflow\n"); - exit (1); - } - - unsigned char *p = _positions + _size - 1; - - for (; count > 0; p--, count--) - { - if (*p == pos) - { - fprintf (stderr, "Positions::add internal error: duplicate\n"); - exit (1); - } - if (*p > pos) - break; - p[1] = p[0]; - } - p[1] = pos; - _size++; -} - -void -Positions::remove (int pos) -{ - unsigned int count = _size; - if (count > 0) - { - unsigned char *p = _positions + _size - 1; - - if (*p == pos) - { - _size--; - return; - } - if (*p < pos) - { - unsigned char prev = *p; - - for (;;) - { - p--; - count--; - if (count == 0) - break; - if (*p == pos) - { - *p = prev; - _size--; - return; - } - if (*p > pos) - break; - unsigned char curr = *p; - *p = prev; - prev = curr; - } - } - } - fprintf (stderr, "Positions::remove internal error: not found\n"); - exit (1); -} - -/* Output in external syntax. */ -void -Positions::print () const -{ - bool first = true; - bool seen_LASTCHAR = false; - unsigned int count = _size; - const unsigned char *p = _positions + _size - 1; - - for (; count > 0; p--, count--) - { - if (*p == LASTCHAR) - seen_LASTCHAR = true; - else - { - if (!first) - printf (","); - printf ("%d", *p); - if (count > 0 && p[-1] == *p + 1) - { - printf ("-"); - do - { - p--; - count--; - } - while (count > 0 && p[-1] == *p + 1); - printf ("%d", *p); - } - first = false; - } - } - if (seen_LASTCHAR) - { - if (!first) - printf (","); - printf ("$"); - } -} - /* ------------------------------------------------------------------------- */ #ifndef __OPTIMIZE__ diff --git a/src/options.h b/src/options.h index ec32c97..6053b45 100644 --- a/src/options.h +++ b/src/options.h @@ -30,6 +30,7 @@ #define options_h 1 #include <stdio.h> +#include "positions.h" /* Enumeration of the possible boolean options. */ @@ -103,79 +104,6 @@ enum Option_Type SEVENBIT = 1 << 21 }; -/* This class denotes a set of key positions. */ - -class Positions -{ - friend class PositionIterator; -public: - /* Denotes the last char of a keyword, depending on the keyword's length. */ - static const int LASTCHAR = 0; - - /* Maximum key position specifiable by the user. - Note that this must fit into the element type of _positions[], below. */ - static const int MAX_KEY_POS = 255; - - /* Constructors. */ - Positions (); - Positions (int pos1); - Positions (int pos1, int pos2); - - /* Copy constructor. */ - Positions (const Positions& src); - - /* Assignment operator. */ - Positions& operator= (const Positions& src); - - /* Accessors. */ - int operator[] (unsigned int index) const; - unsigned int get_size () const; - - /* Write access. */ - unsigned char * pointer (); - void set_size (unsigned int size); - - /* Sorts the array in reverse order. - Returns true if there are no duplicates, false otherwise. */ - bool sort (); - - /* Set operations. Assumes the array is in reverse order. */ - bool contains (int pos) const; - void add (int pos); - void remove (int pos); - - /* Output in external syntax. */ - void print () const; - -private: - /* Number of positions. */ - unsigned int _size; - /* Array of positions. 1 for the first char, 2 for the second char etc., - LASTCHAR for the last char. - Note that since duplicates are eliminated, the maximum possible size - is MAX_KEY_POS + 1. */ - unsigned char _positions[MAX_KEY_POS + 1]; -}; - -/* This class denotes an iterator through a set of key positions. */ - -class PositionIterator -{ -public: - /* Initializes an iterator through POSITIONS. */ - PositionIterator (Positions const& positions); - - /* End of iteration marker. */ - static const int EOS = -1; - - /* Retrieves the next position, or EOS past the end. */ - int next (); - -private: - const Positions& _set; - unsigned int _index; -}; - /* Class manager for gperf program Options. */ class Options diff --git a/src/options.icc b/src/options.icc index 444c849..c84768a 100644 --- a/src/options.icc +++ b/src/options.icc @@ -21,120 +21,6 @@ If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* ---------------------------- Class Positions ---------------------------- */ - -/* Constructors. */ - -INLINE -Positions::Positions () - : _size (0) -{ -} - -INLINE -Positions::Positions (int pos1) - : _size (1) -{ - _positions[0] = pos1; -} - -INLINE -Positions::Positions (int pos1, int pos2) - : _size (2) -{ - _positions[0] = pos1; - _positions[1] = pos2; -} - -/* Copy constructor. */ - -INLINE -Positions::Positions (const Positions& src) - : _size (src._size) -{ - memcpy (_positions, src._positions, _size * sizeof (_positions[0])); -} - -/* Assignment operator. */ - -INLINE Positions& -Positions::operator= (const Positions& src) -{ - _size = src._size; - memcpy (_positions, src._positions, _size * sizeof (_positions[0])); - return *this; -} - -/* Accessors. */ - -INLINE int -Positions::operator[] (unsigned int index) const -{ - return _positions[index]; -} - -INLINE unsigned int -Positions::get_size () const -{ - return _size; -} - -/* Write access. */ - -INLINE unsigned char * -Positions::pointer () -{ - return _positions; -} - -INLINE void -Positions::set_size (unsigned int size) -{ - _size = size; -} - -/* Sorts the array in reverse order. - Returns true if there are no duplicates, false otherwise. */ -INLINE bool -Positions::sort () -{ - /* Bubble sort. */ - bool duplicate_free = true; - unsigned char *base = _positions; - unsigned int len = _size; - - for (unsigned int i = 1; i < len; i++) - { - unsigned int j; - int tmp; - - for (j = i, tmp = base[j]; j > 0 && tmp >= base[j - 1]; j--) - if ((base[j] = base[j - 1]) == tmp) /* oh no, a duplicate!!! */ - duplicate_free = false; - - base[j] = tmp; - } - - return duplicate_free; -} - -/* ------------------------- Class PositionIterator ------------------------ */ - -/* Initializes an iterator through POSITIONS. */ -INLINE -PositionIterator::PositionIterator (Positions const& positions) - : _set (positions), - _index (0) -{ -} - -/* Retrieves the next position, or EOS past the end. */ -INLINE int -PositionIterator::next () -{ - return (_index < _set._size ? _set._positions[_index++] : EOS); -} - /* ----------------------------- Class Options ----------------------------- */ /* Tests a given boolean option. Returns true if set, false otherwise. */ diff --git a/src/output.h b/src/output.h index e141645..5b747fd 100644 --- a/src/output.h +++ b/src/output.h @@ -27,7 +27,7 @@ #define output_h 1 #include "keyword-list.h" -#include "options.h" +#include "positions.h" /* OSF/1 cxx needs these forward declarations. */ struct Output_Constants; diff --git a/src/positions.cc b/src/positions.cc new file mode 100644 index 0000000..f02ff67 --- /dev/null +++ b/src/positions.cc @@ -0,0 +1,167 @@ +/* A set of byte positions. + Copyright (C) 1989-1998, 2000, 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. */ + +/* Specification. */ +#include "positions.h" + +#include <stdio.h> +#include <stdlib.h> /* declares exit() */ +#include <string.h> + +/* ---------------------------- Class Positions ---------------------------- */ + +/* Set operations. Assumes the array is in reverse order. */ + +bool +Positions::contains (int pos) const +{ + unsigned int count = _size; + const unsigned char *p = _positions + _size - 1; + + for (; count > 0; p--, count--) + { + if (*p == pos) + return true; + if (*p > pos) + break; + } + return false; +} + +void +Positions::add (int pos) +{ + unsigned int count = _size; + + if (count == MAX_KEY_POS + 1) + { + fprintf (stderr, "Positions::add internal error: overflow\n"); + exit (1); + } + + unsigned char *p = _positions + _size - 1; + + for (; count > 0; p--, count--) + { + if (*p == pos) + { + fprintf (stderr, "Positions::add internal error: duplicate\n"); + exit (1); + } + if (*p > pos) + break; + p[1] = p[0]; + } + p[1] = pos; + _size++; +} + +void +Positions::remove (int pos) +{ + unsigned int count = _size; + if (count > 0) + { + unsigned char *p = _positions + _size - 1; + + if (*p == pos) + { + _size--; + return; + } + if (*p < pos) + { + unsigned char prev = *p; + + for (;;) + { + p--; + count--; + if (count == 0) + break; + if (*p == pos) + { + *p = prev; + _size--; + return; + } + if (*p > pos) + break; + unsigned char curr = *p; + *p = prev; + prev = curr; + } + } + } + fprintf (stderr, "Positions::remove internal error: not found\n"); + exit (1); +} + +/* Output in external syntax. */ +void +Positions::print () const +{ + bool first = true; + bool seen_LASTCHAR = false; + unsigned int count = _size; + const unsigned char *p = _positions + _size - 1; + + for (; count > 0; p--, count--) + { + if (*p == LASTCHAR) + seen_LASTCHAR = true; + else + { + if (!first) + printf (","); + printf ("%d", *p); + if (count > 0 && p[-1] == *p + 1) + { + printf ("-"); + do + { + p--; + count--; + } + while (count > 0 && p[-1] == *p + 1); + printf ("%d", *p); + } + first = false; + } + } + if (seen_LASTCHAR) + { + if (!first) + printf (","); + printf ("$"); + } +} + +/* ------------------------------------------------------------------------- */ + +#ifndef __OPTIMIZE__ + +#define INLINE /* not inline */ +#include "positions.icc" +#undef INLINE + +#endif /* not defined __OPTIMIZE__ */ diff --git a/src/positions.h b/src/positions.h new file mode 100644 index 0000000..78f57c5 --- /dev/null +++ b/src/positions.h @@ -0,0 +1,111 @@ +/* This may look like C code, but it is really -*- C++ -*- */ + +/* A set of byte positions. + + Copyright (C) 1989-1998, 2000, 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 positions_h +#define positions_h 1 + +/* This class denotes a set of byte positions, used to access a keyword. */ + +class Positions +{ + friend class PositionIterator; +public: + /* Denotes the last char of a keyword, depending on the keyword's length. */ + static const int LASTCHAR = 0; + + /* Maximum key position specifiable by the user. + Note that this must fit into the element type of _positions[], below. */ + static const int MAX_KEY_POS = 255; + + /* Constructors. */ + Positions (); + Positions (int pos1); + Positions (int pos1, int pos2); + + /* Copy constructor. */ + Positions (const Positions& src); + + /* Assignment operator. */ + Positions& operator= (const Positions& src); + + /* Accessors. */ + int operator[] (unsigned int index) const; + unsigned int get_size () const; + + /* Write access. */ + unsigned char * pointer (); + void set_size (unsigned int size); + + /* Sorts the array in reverse order. + Returns true if there are no duplicates, false otherwise. */ + bool sort (); + + /* Set operations. Assumes the array is in reverse order. */ + bool contains (int pos) const; + void add (int pos); + void remove (int pos); + + /* Output in external syntax. */ + void print () const; + +private: + /* Number of positions. */ + unsigned int _size; + /* Array of positions. 1 for the first char, 2 for the second char etc., + LASTCHAR for the last char. + Note that since duplicates are eliminated, the maximum possible size + is MAX_KEY_POS + 1. */ + unsigned char _positions[MAX_KEY_POS + 1]; +}; + +/* This class denotes an iterator through a set of byte positions. */ + +class PositionIterator +{ +public: + /* Initializes an iterator through POSITIONS. */ + PositionIterator (Positions const& positions); + + /* End of iteration marker. */ + static const int EOS = -1; + + /* Retrieves the next position, or EOS past the end. */ + int next (); + +private: + const Positions& _set; + unsigned int _index; +}; + +#ifdef __OPTIMIZE__ + +#include <string.h> +#define INLINE inline +#include "positions.icc" +#undef INLINE + +#endif + +#endif diff --git a/src/positions.icc b/src/positions.icc new file mode 100644 index 0000000..12e9938 --- /dev/null +++ b/src/positions.icc @@ -0,0 +1,139 @@ +/* Inline Functions for positions.{h,cc}. + + Copyright (C) 1989-1998, 2000, 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. */ + +// This needs: +//#include <string.h> + +/* ---------------------------- Class Positions ---------------------------- */ + +/* Constructors. */ + +INLINE +Positions::Positions () + : _size (0) +{ +} + +INLINE +Positions::Positions (int pos1) + : _size (1) +{ + _positions[0] = pos1; +} + +INLINE +Positions::Positions (int pos1, int pos2) + : _size (2) +{ + _positions[0] = pos1; + _positions[1] = pos2; +} + +/* Copy constructor. */ + +INLINE +Positions::Positions (const Positions& src) + : _size (src._size) +{ + memcpy (_positions, src._positions, _size * sizeof (_positions[0])); +} + +/* Assignment operator. */ + +INLINE Positions& +Positions::operator= (const Positions& src) +{ + _size = src._size; + memcpy (_positions, src._positions, _size * sizeof (_positions[0])); + return *this; +} + +/* Accessors. */ + +INLINE int +Positions::operator[] (unsigned int index) const +{ + return _positions[index]; +} + +INLINE unsigned int +Positions::get_size () const +{ + return _size; +} + +/* Write access. */ + +INLINE unsigned char * +Positions::pointer () +{ + return _positions; +} + +INLINE void +Positions::set_size (unsigned int size) +{ + _size = size; +} + +/* Sorts the array in reverse order. + Returns true if there are no duplicates, false otherwise. */ +INLINE bool +Positions::sort () +{ + /* Bubble sort. */ + bool duplicate_free = true; + unsigned char *base = _positions; + unsigned int len = _size; + + for (unsigned int i = 1; i < len; i++) + { + unsigned int j; + int tmp; + + for (j = i, tmp = base[j]; j > 0 && tmp >= base[j - 1]; j--) + if ((base[j] = base[j - 1]) == tmp) /* oh no, a duplicate!!! */ + duplicate_free = false; + + base[j] = tmp; + } + + return duplicate_free; +} + +/* ------------------------- Class PositionIterator ------------------------ */ + +/* Initializes an iterator through POSITIONS. */ +INLINE +PositionIterator::PositionIterator (Positions const& positions) + : _set (positions), + _index (0) +{ +} + +/* Retrieves the next position, or EOS past the end. */ +INLINE int +PositionIterator::next () +{ + return (_index < _set._size ? _set._positions[_index++] : EOS); +} diff --git a/src/search.h b/src/search.h index 0d67458..363c9b3 100644 --- a/src/search.h +++ b/src/search.h @@ -27,7 +27,7 @@ #define search_h 1 #include "keyword-list.h" -#include "options.h" +#include "positions.h" #include "bool-array.h" class Search |