Blame src/positions.h

Packit b27855
/* This may look like C code, but it is really -*- C++ -*- */
Packit b27855
Packit b27855
/* A set of byte positions.
Packit b27855
Packit b27855
   Copyright (C) 1989-1998, 2000, 2002, 2005 Free Software Foundation, Inc.
Packit b27855
   Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
Packit b27855
   and Bruno Haible <bruno@clisp.org>.
Packit b27855
Packit b27855
   This file is part of GNU GPERF.
Packit b27855
Packit b27855
   This program is free software: you can redistribute it and/or modify
Packit b27855
   it under the terms of the GNU General Public License as published by
Packit b27855
   the Free Software Foundation; either version 3 of the License, or
Packit b27855
   (at your option) any later version.
Packit b27855
Packit b27855
   This program is distributed in the hope that it will be useful,
Packit b27855
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit b27855
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit b27855
   GNU General Public License for more details.
Packit b27855
Packit b27855
   You should have received a copy of the GNU General Public License
Packit b27855
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit b27855
Packit b27855
#ifndef positions_h
Packit b27855
#define positions_h 1
Packit b27855
Packit b27855
/* Classes defined below.  */
Packit b27855
class PositionIterator;
Packit b27855
class PositionReverseIterator;
Packit b27855
Packit b27855
/* This class denotes a set of byte positions, used to access a keyword.  */
Packit b27855
Packit b27855
class Positions
Packit b27855
{
Packit b27855
  friend class PositionIterator;
Packit b27855
  friend class PositionReverseIterator;
Packit b27855
public:
Packit b27855
  /* Denotes the last char of a keyword, depending on the keyword's length.  */
Packit b27855
  enum {                LASTCHAR = -1 };
Packit b27855
Packit b27855
  /* Maximum key position specifiable by the user, 1-based.
Packit b27855
     Note that MAX_KEY_POS-1 must fit into the element type of _positions[],
Packit b27855
     below.  */
Packit b27855
  enum {                MAX_KEY_POS = 255 };
Packit b27855
Packit b27855
  /* Maximum possible size.  Since duplicates are eliminated and the possible
Packit b27855
     0-based positions are -1 .. MAX_KEY_POS-1, this is:  */
Packit b27855
  enum {                MAX_SIZE = MAX_KEY_POS + 1 };
Packit b27855
Packit b27855
  /* Constructors.  */
Packit b27855
                        Positions ();
Packit b27855
                        Positions (int pos1);
Packit b27855
                        Positions (int pos1, int pos2);
Packit b27855
Packit b27855
  /* Copy constructor.  */
Packit b27855
                        Positions (const Positions& src);
Packit b27855
Packit b27855
  /* Assignment operator.  */
Packit b27855
  Positions&            operator= (const Positions& src);
Packit b27855
Packit b27855
  /* Accessors.  */
Packit b27855
  bool                  is_useall () const;
Packit b27855
  int                   operator[] (unsigned int index) const;
Packit b27855
  unsigned int          get_size () const;
Packit b27855
Packit b27855
  /* Write access.  */
Packit b27855
  void                  set_useall (bool useall);
Packit b27855
  int *                 pointer ();
Packit b27855
  void                  set_size (unsigned int size);
Packit b27855
Packit b27855
  /* Sorts the array in reverse order.
Packit b27855
     Returns true if there are no duplicates, false otherwise.  */
Packit b27855
  bool                  sort ();
Packit b27855
Packit b27855
  /* Creates an iterator, returning the positions in descending order.  */
Packit b27855
  PositionIterator      iterator () const;
Packit b27855
  /* Creates an iterator, returning the positions in descending order,
Packit b27855
     that apply to strings of length <= maxlen.  */
Packit b27855
  PositionIterator      iterator (int maxlen) const;
Packit b27855
  /* Creates an iterator, returning the positions in ascending order.  */
Packit b27855
  PositionReverseIterator reviterator () const;
Packit b27855
  /* Creates an iterator, returning the positions in ascending order,
Packit b27855
     that apply to strings of length <= maxlen.  */
Packit b27855
  PositionReverseIterator reviterator (int maxlen) const;
Packit b27855
Packit b27855
  /* Set operations.  Assumes the array is in reverse order.  */
Packit b27855
  bool                  contains (int pos) const;
Packit b27855
  void                  add (int pos);
Packit b27855
  void                  remove (int pos);
Packit b27855
Packit b27855
  /* Output in external syntax.  */
Packit b27855
  void                  print () const;
Packit b27855
Packit b27855
private:
Packit b27855
  /* The special case denoted by '*'.  */
Packit b27855
  bool                  _useall;
Packit b27855
  /* Number of positions.  */
Packit b27855
  unsigned int          _size;
Packit b27855
  /* Array of positions.  0 for the first char, 1 for the second char etc.,
Packit b27855
     LASTCHAR for the last char.  */
Packit b27855
  int                   _positions[MAX_SIZE];
Packit b27855
};
Packit b27855
Packit b27855
/* This class denotes an iterator through a set of byte positions.  */
Packit b27855
Packit b27855
class PositionIterator
Packit b27855
{
Packit b27855
  friend class Positions;
Packit b27855
public:
Packit b27855
  /* Copy constructor.  */
Packit b27855
                        PositionIterator (const PositionIterator& src);
Packit b27855
Packit b27855
  /* End of iteration marker.  */
Packit b27855
  enum {                EOS = -2 };
Packit b27855
Packit b27855
  /* Retrieves the next position, or EOS past the end.  */
Packit b27855
  int                   next ();
Packit b27855
Packit b27855
  /* Returns the number of remaining positions, i.e. how often next() will
Packit b27855
     return a value != EOS.  */
Packit b27855
  unsigned int          remaining () const;
Packit b27855
Packit b27855
private:
Packit b27855
  /* Initializes an iterator through POSITIONS.  */
Packit b27855
                        PositionIterator (Positions const& positions);
Packit b27855
  /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen.  */
Packit b27855
                        PositionIterator (Positions const& positions, int maxlen);
Packit b27855
Packit b27855
  const Positions&      _set;
Packit b27855
  unsigned int          _index;
Packit b27855
};
Packit b27855
Packit b27855
/* This class denotes an iterator in reverse direction through a set of
Packit b27855
   byte positions.  */
Packit b27855
Packit b27855
class PositionReverseIterator
Packit b27855
{
Packit b27855
  friend class Positions;
Packit b27855
public:
Packit b27855
  /* Copy constructor.  */
Packit b27855
                        PositionReverseIterator (const PositionReverseIterator& src);
Packit b27855
Packit b27855
  /* End of iteration marker.  */
Packit b27855
  enum {                EOS = -2 };
Packit b27855
Packit b27855
  /* Retrieves the next position, or EOS past the end.  */
Packit b27855
  int                   next ();
Packit b27855
Packit b27855
  /* Returns the number of remaining positions, i.e. how often next() will
Packit b27855
     return a value != EOS.  */
Packit b27855
  unsigned int          remaining () const;
Packit b27855
Packit b27855
private:
Packit b27855
  /* Initializes an iterator through POSITIONS.  */
Packit b27855
                        PositionReverseIterator (Positions const& positions);
Packit b27855
  /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen.  */
Packit b27855
                        PositionReverseIterator (Positions const& positions, int maxlen);
Packit b27855
Packit b27855
  const Positions&      _set;
Packit b27855
  unsigned int          _index;
Packit b27855
  unsigned int          _minindex;
Packit b27855
};
Packit b27855
Packit b27855
#ifdef __OPTIMIZE__
Packit b27855
Packit b27855
#include <string.h>
Packit b27855
#define INLINE inline
Packit b27855
#include "positions.icc"
Packit b27855
#undef INLINE
Packit b27855
Packit b27855
#endif
Packit b27855
Packit b27855
#endif