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 This program 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 3 of the License, or
14 (at your option) any later version.
15
16 This program 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. If not, see <http://www.gnu.org/licenses/>. */
23
24#ifndef positions_h
25#define positions_h 1
26
27/* Classes defined below. */
28class PositionIterator;
29class PositionReverseIterator;
30
31/* This class denotes a set of byte positions, used to access a keyword. */
32
33class Positions
34{
35 friend class PositionIterator;
36 friend class PositionReverseIterator;
37public:
38 /* Denotes the last char of a keyword, depending on the keyword's length. */
39 enum { LASTCHAR = -1 };
40
41 /* Maximum key position specifiable by the user, 1-based.
42 Note that MAX_KEY_POS-1 must fit into the element type of _positions[],
43 below. */
44 enum { MAX_KEY_POS = 255 };
45
46 /* Maximum possible size. Since duplicates are eliminated and the possible
47 0-based positions are -1 .. MAX_KEY_POS-1, this is: */
48 enum { MAX_SIZE = MAX_KEY_POS + 1 };
49
50 /* Constructors. */
51 Positions ();
52 Positions (int pos1);
53 Positions (int pos1, int pos2);
54
55 /* Copy constructor. */
56 Positions (const Positions& src);
57
58 /* Assignment operator. */
59 Positions& operator= (const Positions& src);
60
61 /* Accessors. */
62 bool is_useall () const;
63 int operator[] (unsigned int index) const;
64 unsigned int get_size () const;
65
66 /* Write access. */
67 void set_useall (bool useall);
68 int * pointer ();
69 void set_size (unsigned int size);
70
71 /* Sorts the array in reverse order.
72 Returns true if there are no duplicates, false otherwise. */
73 bool sort ();
74
75 /* Creates an iterator, returning the positions in descending order. */
76 PositionIterator iterator () const;
77 /* Creates an iterator, returning the positions in descending order,
78 that apply to strings of length <= maxlen. */
79 PositionIterator iterator (int maxlen) const;
80 /* Creates an iterator, returning the positions in ascending order. */
81 PositionReverseIterator reviterator () const;
82 /* Creates an iterator, returning the positions in ascending order,
83 that apply to strings of length <= maxlen. */
84 PositionReverseIterator reviterator (int maxlen) const;
85
86 /* Set operations. Assumes the array is in reverse order. */
87 bool contains (int pos) const;
88 void add (int pos);
89 void remove (int pos);
90
91 /* Output in external syntax. */
92 void print () const;
93
94private:
95 /* The special case denoted by '*'. */
96 bool _useall;
97 /* Number of positions. */
98 unsigned int _size;
99 /* Array of positions. 0 for the first char, 1 for the second char etc.,
100 LASTCHAR for the last char. */
101 int _positions[MAX_SIZE];
102};
103
104/* This class denotes an iterator through a set of byte positions. */
105
106class PositionIterator
107{
108 friend class Positions;
109public:
110 /* Copy constructor. */
111 PositionIterator (const PositionIterator& src);
112
113 /* End of iteration marker. */
114 enum { EOS = -2 };
115
116 /* Retrieves the next position, or EOS past the end. */
117 int next ();
118
119 /* Returns the number of remaining positions, i.e. how often next() will
120 return a value != EOS. */
121 unsigned int remaining () const;
122
123private:
124 /* Initializes an iterator through POSITIONS. */
125 PositionIterator (Positions const& positions);
126 /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen. */
127 PositionIterator (Positions const& positions, int maxlen);
128
129 const Positions& _set;
130 unsigned int _index;
131};
132
133/* This class denotes an iterator in reverse direction through a set of
134 byte positions. */
135
136class PositionReverseIterator
137{
138 friend class Positions;
139public:
140 /* Copy constructor. */
141 PositionReverseIterator (const PositionReverseIterator& src);
142
143 /* End of iteration marker. */
144 enum { EOS = -2 };
145
146 /* Retrieves the next position, or EOS past the end. */
147 int next ();
148
149 /* Returns the number of remaining positions, i.e. how often next() will
150 return a value != EOS. */
151 unsigned int remaining () const;
152
153private:
154 /* Initializes an iterator through POSITIONS. */
155 PositionReverseIterator (Positions const& positions);
156 /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen. */
157 PositionReverseIterator (Positions const& positions, int maxlen);
158
159 const Positions& _set;
160 unsigned int _index;
161 unsigned int _minindex;
162};
163
164#ifdef __OPTIMIZE__
165
166#include <string.h>
167#define INLINE inline
168#include "positions.icc"
169#undef INLINE
170
171#endif
172
173#endif
174