1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 13:09:22 +00:00

Move positions handling to its own file.

This commit is contained in:
Bruno Haible
2003-02-13 12:11:01 +00:00
parent 810fef43ae
commit ee135115f5
12 changed files with 445 additions and 325 deletions

View File

@@ -1,5 +1,20 @@
2002-11-17 Bruno Haible <bruno@clisp.org> 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. * src/options.h (POSITIONS): New enum value.
(Positions::Positions): New copy constructor. (Positions::Positions): New copy constructor.
(Positions::operator=, Positions::contains, Position::add, (Positions::operator=, Positions::contains, Position::add,

View File

@@ -61,7 +61,7 @@ SHELL = /bin/sh
VPATH = $(srcdir) 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 input.o bool-array.o hash-table.o search.o output.o main.o
LIBS = ../lib/libgp.a @GPERF_LIBM@ LIBS = ../lib/libgp.a @GPERF_LIBM@
CPPFLAGS = -I. -I$(srcdir)/../lib CPPFLAGS = -I. -I$(srcdir)/../lib
@@ -85,20 +85,23 @@ $(TARGETPROG): $(OBJECTS)
# Dependencies. # Dependencies.
CONFIG_H = config.h CONFIG_H = config.h
VERSION_H = version.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_H = keyword.h keyword.icc
KEYWORD_LIST_H = keyword-list.h keyword-list.icc $(KEYWORD_H) KEYWORD_LIST_H = keyword-list.h keyword-list.icc $(KEYWORD_H)
INPUT_H = input.h $(KEYWORD_LIST_H) INPUT_H = input.h $(KEYWORD_LIST_H)
BOOL_ARRAY_H = bool-array.h bool-array.icc $(OPTIONS_H) BOOL_ARRAY_H = bool-array.h bool-array.icc $(OPTIONS_H)
HASH_TABLE_H = hash-table.h $(KEYWORD_H) HASH_TABLE_H = hash-table.h $(KEYWORD_H)
SEARCH_H = search.h $(KEYWORD_LIST_H) $(OPTIONS_H) $(BOOL_ARRAY_H) SEARCH_H = search.h $(KEYWORD_LIST_H) $(POSITIONS_H) $(BOOL_ARRAY_H)
OUTPUT_H = output.h $(KEYWORD_LIST_H) $(OPTIONS_H) OUTPUT_H = output.h $(KEYWORD_LIST_H) $(POSITIONS_H)
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
positions.o : positions.cc $(POSITIONS_H)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/positions.cc
options.o : options.cc $(OPTIONS_H) $(VERSION_H) options.o : options.cc $(OPTIONS_H) $(VERSION_H)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/options.cc $(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 $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/keyword.cc
keyword-list.o : keyword-list.cc $(KEYWORD_LIST_H) keyword-list.o : keyword-list.cc $(KEYWORD_LIST_H)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/keyword-list.cc $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(srcdir)/keyword-list.cc

View File

@@ -26,7 +26,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "options.h" #include "positions.h"
/* --------------------------- KeywordExt class --------------------------- */ /* --------------------------- KeywordExt class --------------------------- */

View File

@@ -26,7 +26,7 @@
#ifndef keyword_h #ifndef keyword_h
#define keyword_h 1 #define keyword_h 1
/* Class defined in "options.h". */ /* Class defined in "positions.h". */
class Positions; class Positions;
/* 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. */

View File

@@ -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__ #ifndef __OPTIMIZE__

View File

@@ -30,6 +30,7 @@
#define options_h 1 #define options_h 1
#include <stdio.h> #include <stdio.h>
#include "positions.h"
/* Enumeration of the possible boolean options. */ /* Enumeration of the possible boolean options. */
@@ -103,79 +104,6 @@ enum Option_Type
SEVENBIT = 1 << 21 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 manager for gperf program Options. */
class Options class Options

View File

@@ -21,120 +21,6 @@
If not, write to the Free Software Foundation, Inc., If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 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 ----------------------------- */ /* ----------------------------- Class Options ----------------------------- */
/* Tests a given boolean option. Returns true if set, false otherwise. */ /* Tests a given boolean option. Returns true if set, false otherwise. */

View File

@@ -27,7 +27,7 @@
#define output_h 1 #define output_h 1
#include "keyword-list.h" #include "keyword-list.h"
#include "options.h" #include "positions.h"
/* OSF/1 cxx needs these forward declarations. */ /* OSF/1 cxx needs these forward declarations. */
struct Output_Constants; struct Output_Constants;

167
src/positions.cc Normal file
View File

@@ -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__ */

111
src/positions.h Normal file
View File

@@ -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

139
src/positions.icc Normal file
View File

@@ -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);
}

View File

@@ -27,7 +27,7 @@
#define search_h 1 #define search_h 1
#include "keyword-list.h" #include "keyword-list.h"
#include "options.h" #include "positions.h"
#include "bool-array.h" #include "bool-array.h"
class Search class Search