1228060Sbapt/* This may look like C code, but it is really -*- C++ -*- */ 2228060Sbapt 3228060Sbapt/* A set of byte positions. 4228060Sbapt 5228060Sbapt Copyright (C) 1989-1998, 2000, 2002, 2005 Free Software Foundation, Inc. 6228060Sbapt Written by Douglas C. Schmidt <schmidt@ics.uci.edu> 7228060Sbapt and Bruno Haible <bruno@clisp.org>. 8228060Sbapt 9228060Sbapt This file is part of GNU GPERF. 10228060Sbapt 11228060Sbapt GNU GPERF is free software; you can redistribute it and/or modify 12228060Sbapt it under the terms of the GNU General Public License as published by 13228060Sbapt the Free Software Foundation; either version 2, or (at your option) 14228060Sbapt any later version. 15228060Sbapt 16228060Sbapt GNU GPERF is distributed in the hope that it will be useful, 17228060Sbapt but WITHOUT ANY WARRANTY; without even the implied warranty of 18228060Sbapt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19228060Sbapt GNU General Public License for more details. 20228060Sbapt 21228060Sbapt You should have received a copy of the GNU General Public License 22228060Sbapt along with this program; see the file COPYING. 23228060Sbapt If not, write to the Free Software Foundation, Inc., 24228060Sbapt 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 25228060Sbapt 26228060Sbapt#ifndef positions_h 27228060Sbapt#define positions_h 1 28228060Sbapt 29228060Sbapt/* Classes defined below. */ 30228060Sbaptclass PositionIterator; 31228060Sbaptclass PositionReverseIterator; 32228060Sbapt 33228060Sbapt/* This class denotes a set of byte positions, used to access a keyword. */ 34228060Sbapt 35228060Sbaptclass Positions 36228060Sbapt{ 37228060Sbapt friend class PositionIterator; 38228060Sbapt friend class PositionReverseIterator; 39228060Sbaptpublic: 40228060Sbapt /* Denotes the last char of a keyword, depending on the keyword's length. */ 41228060Sbapt enum { LASTCHAR = -1 }; 42228060Sbapt 43228060Sbapt /* Maximum key position specifiable by the user, 1-based. 44228060Sbapt Note that MAX_KEY_POS-1 must fit into the element type of _positions[], 45228060Sbapt below. */ 46228060Sbapt enum { MAX_KEY_POS = 255 }; 47228060Sbapt 48228060Sbapt /* Maximum possible size. Since duplicates are eliminated and the possible 49228060Sbapt 0-based positions are -1 .. MAX_KEY_POS-1, this is: */ 50228060Sbapt enum { MAX_SIZE = MAX_KEY_POS + 1 }; 51228060Sbapt 52228060Sbapt /* Constructors. */ 53228060Sbapt Positions (); 54228060Sbapt Positions (int pos1); 55228060Sbapt Positions (int pos1, int pos2); 56228060Sbapt 57228060Sbapt /* Copy constructor. */ 58228060Sbapt Positions (const Positions& src); 59228060Sbapt 60228060Sbapt /* Assignment operator. */ 61228060Sbapt Positions& operator= (const Positions& src); 62228060Sbapt 63228060Sbapt /* Accessors. */ 64228060Sbapt bool is_useall () const; 65228060Sbapt int operator[] (unsigned int index) const; 66228060Sbapt unsigned int get_size () const; 67228060Sbapt 68228060Sbapt /* Write access. */ 69228060Sbapt void set_useall (bool useall); 70228060Sbapt int * pointer (); 71228060Sbapt void set_size (unsigned int size); 72228060Sbapt 73228060Sbapt /* Sorts the array in reverse order. 74228060Sbapt Returns true if there are no duplicates, false otherwise. */ 75228060Sbapt bool sort (); 76228060Sbapt 77228060Sbapt /* Creates an iterator, returning the positions in descending order. */ 78228060Sbapt PositionIterator iterator () const; 79228060Sbapt /* Creates an iterator, returning the positions in descending order, 80228060Sbapt that apply to strings of length <= maxlen. */ 81228060Sbapt PositionIterator iterator (int maxlen) const; 82228060Sbapt /* Creates an iterator, returning the positions in ascending order. */ 83228060Sbapt PositionReverseIterator reviterator () const; 84228060Sbapt /* Creates an iterator, returning the positions in ascending order, 85228060Sbapt that apply to strings of length <= maxlen. */ 86228060Sbapt PositionReverseIterator reviterator (int maxlen) const; 87228060Sbapt 88228060Sbapt /* Set operations. Assumes the array is in reverse order. */ 89228060Sbapt bool contains (int pos) const; 90228060Sbapt void add (int pos); 91228060Sbapt void remove (int pos); 92228060Sbapt 93228060Sbapt /* Output in external syntax. */ 94228060Sbapt void print () const; 95228060Sbapt 96228060Sbaptprivate: 97228060Sbapt /* The special case denoted by '*'. */ 98228060Sbapt bool _useall; 99228060Sbapt /* Number of positions. */ 100228060Sbapt unsigned int _size; 101228060Sbapt /* Array of positions. 0 for the first char, 1 for the second char etc., 102228060Sbapt LASTCHAR for the last char. */ 103228060Sbapt int _positions[MAX_SIZE]; 104228060Sbapt}; 105228060Sbapt 106228060Sbapt/* This class denotes an iterator through a set of byte positions. */ 107228060Sbapt 108228060Sbaptclass PositionIterator 109228060Sbapt{ 110228060Sbapt friend class Positions; 111228060Sbaptpublic: 112228060Sbapt /* Copy constructor. */ 113228060Sbapt PositionIterator (const PositionIterator& src); 114228060Sbapt 115228060Sbapt /* End of iteration marker. */ 116228060Sbapt enum { EOS = -2 }; 117228060Sbapt 118228060Sbapt /* Retrieves the next position, or EOS past the end. */ 119228060Sbapt int next (); 120228060Sbapt 121228060Sbapt /* Returns the number of remaining positions, i.e. how often next() will 122228060Sbapt return a value != EOS. */ 123228060Sbapt unsigned int remaining () const; 124228060Sbapt 125228060Sbaptprivate: 126228060Sbapt /* Initializes an iterator through POSITIONS. */ 127228060Sbapt PositionIterator (Positions const& positions); 128228060Sbapt /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen. */ 129228060Sbapt PositionIterator (Positions const& positions, int maxlen); 130228060Sbapt 131228060Sbapt const Positions& _set; 132228060Sbapt unsigned int _index; 133228060Sbapt}; 134228060Sbapt 135228060Sbapt/* This class denotes an iterator in reverse direction through a set of 136228060Sbapt byte positions. */ 137228060Sbapt 138228060Sbaptclass PositionReverseIterator 139228060Sbapt{ 140228060Sbapt friend class Positions; 141228060Sbaptpublic: 142228060Sbapt /* Copy constructor. */ 143228060Sbapt PositionReverseIterator (const PositionReverseIterator& src); 144228060Sbapt 145228060Sbapt /* End of iteration marker. */ 146228060Sbapt enum { EOS = -2 }; 147228060Sbapt 148228060Sbapt /* Retrieves the next position, or EOS past the end. */ 149228060Sbapt int next (); 150228060Sbapt 151228060Sbapt /* Returns the number of remaining positions, i.e. how often next() will 152228060Sbapt return a value != EOS. */ 153228060Sbapt unsigned int remaining () const; 154228060Sbapt 155228060Sbaptprivate: 156228060Sbapt /* Initializes an iterator through POSITIONS. */ 157228060Sbapt PositionReverseIterator (Positions const& positions); 158228060Sbapt /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen. */ 159228060Sbapt PositionReverseIterator (Positions const& positions, int maxlen); 160228060Sbapt 161228060Sbapt const Positions& _set; 162228060Sbapt unsigned int _index; 163228060Sbapt unsigned int _minindex; 164228060Sbapt}; 165228060Sbapt 166228060Sbapt#ifdef __OPTIMIZE__ 167228060Sbapt 168228060Sbapt#include <string.h> 169228060Sbapt#define INLINE inline 170228060Sbapt#include "positions.icc" 171228060Sbapt#undef INLINE 172228060Sbapt 173228060Sbapt#endif 174228060Sbapt 175228060Sbapt#endif 176