1/* This may look like C code, but it is really -*- C++ -*- */
2
3/* A set of byte positions.
4
5   Copyright (C) 1989-1998, 2000, 2002, 2005 Free Software Foundation, Inc.
6   Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
7   and Bruno Haible <bruno@clisp.org>.
8
9   This file is part of GNU GPERF.
10
11   GNU GPERF is free software; you can redistribute it and/or modify
12   it under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2, or (at your option)
14   any later version.
15
16   GNU GPERF is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20
21   You should have received a copy of the GNU General Public License
22   along with this program; see the file COPYING.
23   If not, write to the Free Software Foundation, Inc.,
24   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
25
26#ifndef positions_h
27#define positions_h 1
28
29/* Classes defined below.  */
30class PositionIterator;
31class PositionReverseIterator;
32
33/* This class denotes a set of byte positions, used to access a keyword.  */
34
35class Positions
36{
37  friend class PositionIterator;
38  friend class PositionReverseIterator;
39public:
40  /* Denotes the last char of a keyword, depending on the keyword's length.  */
41  enum {                LASTCHAR = -1 };
42
43  /* Maximum key position specifiable by the user, 1-based.
44     Note that MAX_KEY_POS-1 must fit into the element type of _positions[],
45     below.  */
46  enum {                MAX_KEY_POS = 255 };
47
48  /* Maximum possible size.  Since duplicates are eliminated and the possible
49     0-based positions are -1 .. MAX_KEY_POS-1, this is:  */
50  enum {                MAX_SIZE = MAX_KEY_POS + 1 };
51
52  /* Constructors.  */
53                        Positions ();
54                        Positions (int pos1);
55                        Positions (int pos1, int pos2);
56
57  /* Copy constructor.  */
58                        Positions (const Positions& src);
59
60  /* Assignment operator.  */
61  Positions&            operator= (const Positions& src);
62
63  /* Accessors.  */
64  bool                  is_useall () const;
65  int                   operator[] (unsigned int index) const;
66  unsigned int          get_size () const;
67
68  /* Write access.  */
69  void                  set_useall (bool useall);
70  int *                 pointer ();
71  void                  set_size (unsigned int size);
72
73  /* Sorts the array in reverse order.
74     Returns true if there are no duplicates, false otherwise.  */
75  bool                  sort ();
76
77  /* Creates an iterator, returning the positions in descending order.  */
78  PositionIterator      iterator () const;
79  /* Creates an iterator, returning the positions in descending order,
80     that apply to strings of length <= maxlen.  */
81  PositionIterator      iterator (int maxlen) const;
82  /* Creates an iterator, returning the positions in ascending order.  */
83  PositionReverseIterator reviterator () const;
84  /* Creates an iterator, returning the positions in ascending order,
85     that apply to strings of length <= maxlen.  */
86  PositionReverseIterator reviterator (int maxlen) const;
87
88  /* Set operations.  Assumes the array is in reverse order.  */
89  bool                  contains (int pos) const;
90  void                  add (int pos);
91  void                  remove (int pos);
92
93  /* Output in external syntax.  */
94  void                  print () const;
95
96private:
97  /* The special case denoted by '*'.  */
98  bool                  _useall;
99  /* Number of positions.  */
100  unsigned int          _size;
101  /* Array of positions.  0 for the first char, 1 for the second char etc.,
102     LASTCHAR for the last char.  */
103  int                   _positions[MAX_SIZE];
104};
105
106/* This class denotes an iterator through a set of byte positions.  */
107
108class PositionIterator
109{
110  friend class Positions;
111public:
112  /* Copy constructor.  */
113                        PositionIterator (const PositionIterator& src);
114
115  /* End of iteration marker.  */
116  enum {                EOS = -2 };
117
118  /* Retrieves the next position, or EOS past the end.  */
119  int                   next ();
120
121  /* Returns the number of remaining positions, i.e. how often next() will
122     return a value != EOS.  */
123  unsigned int          remaining () const;
124
125private:
126  /* Initializes an iterator through POSITIONS.  */
127                        PositionIterator (Positions const& positions);
128  /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen.  */
129                        PositionIterator (Positions const& positions, int maxlen);
130
131  const Positions&      _set;
132  unsigned int          _index;
133};
134
135/* This class denotes an iterator in reverse direction through a set of
136   byte positions.  */
137
138class PositionReverseIterator
139{
140  friend class Positions;
141public:
142  /* Copy constructor.  */
143                        PositionReverseIterator (const PositionReverseIterator& src);
144
145  /* End of iteration marker.  */
146  enum {                EOS = -2 };
147
148  /* Retrieves the next position, or EOS past the end.  */
149  int                   next ();
150
151  /* Returns the number of remaining positions, i.e. how often next() will
152     return a value != EOS.  */
153  unsigned int          remaining () const;
154
155private:
156  /* Initializes an iterator through POSITIONS.  */
157                        PositionReverseIterator (Positions const& positions);
158  /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen.  */
159                        PositionReverseIterator (Positions const& positions, int maxlen);
160
161  const Positions&      _set;
162  unsigned int          _index;
163  unsigned int          _minindex;
164};
165
166#ifdef __OPTIMIZE__
167
168#include <string.h>
169#define INLINE inline
170#include "positions.icc"
171#undef INLINE
172
173#endif
174
175#endif
176