1 /* This may look like C code, but it is really -*- C++ -*- */
3 /* A set of byte positions.
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>.
9 This file is part of GNU GPERF.
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)
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.
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. */
29 /* Classes defined below. */
30 class PositionIterator;
31 class PositionReverseIterator;
33 /* This class denotes a set of byte positions, used to access a keyword. */
37 friend class PositionIterator;
38 friend class PositionReverseIterator;
40 /* Denotes the last char of a keyword, depending on the keyword's length. */
41 enum { LASTCHAR = -1 };
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[],
46 enum { MAX_KEY_POS = 255 };
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 };
55 Positions (int pos1, int pos2);
57 /* Copy constructor. */
58 Positions (const Positions& src);
60 /* Assignment operator. */
61 Positions& operator= (const Positions& src);
64 bool is_useall () const;
65 int operator[] (unsigned int index) const;
66 unsigned int get_size () const;
69 void set_useall (bool useall);
71 void set_size (unsigned int size);
73 /* Sorts the array in reverse order.
74 Returns true if there are no duplicates, false otherwise. */
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;
88 /* Set operations. Assumes the array is in reverse order. */
89 bool contains (int pos) const;
91 void remove (int pos);
93 /* Output in external syntax. */
97 /* The special case denoted by '*'. */
99 /* Number of positions. */
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];
106 /* This class denotes an iterator through a set of byte positions. */
108 class PositionIterator
110 friend class Positions;
112 /* Copy constructor. */
113 PositionIterator (const PositionIterator& src);
115 /* End of iteration marker. */
118 /* Retrieves the next position, or EOS past the end. */
121 /* Returns the number of remaining positions, i.e. how often next() will
122 return a value != EOS. */
123 unsigned int remaining () const;
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);
131 const Positions& _set;
135 /* This class denotes an iterator in reverse direction through a set of
138 class PositionReverseIterator
140 friend class Positions;
142 /* Copy constructor. */
143 PositionReverseIterator (const PositionReverseIterator& src);
145 /* End of iteration marker. */
148 /* Retrieves the next position, or EOS past the end. */
151 /* Returns the number of remaining positions, i.e. how often next() will
152 return a value != EOS. */
153 unsigned int remaining () const;
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);
161 const Positions& _set;
163 unsigned int _minindex;
169 #define INLINE inline
170 #include "positions.icc"