mirror of
https://git.savannah.gnu.org/git/gperf.git
synced 2025-12-02 21:19:24 +00:00
Move positions handling to its own file.
This commit is contained in:
15
ChangeLog
15
ChangeLog
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "options.h"
|
||||
#include "positions.h"
|
||||
|
||||
|
||||
/* --------------------------- KeywordExt class --------------------------- */
|
||||
|
||||
@@ -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. */
|
||||
|
||||
129
src/options.cc
129
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__
|
||||
|
||||
@@ -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
|
||||
|
||||
114
src/options.icc
114
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. */
|
||||
|
||||
@@ -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;
|
||||
|
||||
167
src/positions.cc
Normal file
167
src/positions.cc
Normal 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
111
src/positions.h
Normal 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
139
src/positions.icc
Normal 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);
|
||||
}
|
||||
@@ -27,7 +27,7 @@
|
||||
#define search_h 1
|
||||
|
||||
#include "keyword-list.h"
|
||||
#include "options.h"
|
||||
#include "positions.h"
|
||||
#include "bool-array.h"
|
||||
|
||||
class Search
|
||||
|
||||
Reference in New Issue
Block a user