1 /* Inline Functions for positions.{h,cc}.
3 Copyright (C) 1989-1998, 2000, 2002 Free Software Foundation, Inc.
4 Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
5 and Bruno Haible <bruno@clisp.org>.
7 This file is part of GNU GPERF.
9 GNU GPERF is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU GPERF is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING.
21 If not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
27 /* ---------------------------- Class Positions ---------------------------- */
32 Positions::Positions ()
39 Positions::Positions (int pos1)
47 Positions::Positions (int pos1, int pos2)
55 /* Copy constructor. */
58 Positions::Positions (const Positions& src)
59 : _useall (src._useall),
62 memcpy (_positions, src._positions, _size * sizeof (_positions[0]));
65 /* Assignment operator. */
68 Positions::operator= (const Positions& src)
70 _useall = src._useall;
72 memcpy (_positions, src._positions, _size * sizeof (_positions[0]));
79 Positions::is_useall () const
85 Positions::operator[] (unsigned int index) const
87 return _positions[index];
91 Positions::get_size () const
99 Positions::set_useall (bool useall)
104 /* The positions are 0, 1, ..., MAX_KEY_POS-1, in descending order. */
106 int *ptr = _positions;
107 for (int i = MAX_KEY_POS - 1; i >= 0; i--)
113 Positions::pointer ()
119 Positions::set_size (unsigned int size)
124 /* Sorts the array in reverse order.
125 Returns true if there are no duplicates, false otherwise. */
133 bool duplicate_free = true;
134 int *base = _positions;
135 unsigned int len = _size;
137 for (unsigned int i = 1; i < len; i++)
142 for (j = i, tmp = base[j]; j > 0 && tmp >= base[j - 1]; j--)
143 if ((base[j] = base[j - 1]) == tmp) /* oh no, a duplicate!!! */
144 duplicate_free = false;
149 return duplicate_free;
152 /* Creates an iterator, returning the positions in descending order. */
153 INLINE PositionIterator
154 Positions::iterator () const
156 return PositionIterator (*this);
159 /* Creates an iterator, returning the positions in descending order,
160 that apply to strings of length <= maxlen. */
161 INLINE PositionIterator
162 Positions::iterator (int maxlen) const
164 return PositionIterator (*this, maxlen);
167 /* Creates an iterator, returning the positions in ascending order. */
168 INLINE PositionReverseIterator
169 Positions::reviterator () const
171 return PositionReverseIterator (*this);
174 /* Creates an iterator, returning the positions in ascending order,
175 that apply to strings of length <= maxlen. */
176 INLINE PositionReverseIterator
177 Positions::reviterator (int maxlen) const
179 return PositionReverseIterator (*this, maxlen);
182 /* ------------------------- Class PositionIterator ------------------------ */
184 /* Initializes an iterator through POSITIONS. */
186 PositionIterator::PositionIterator (Positions const& positions)
192 /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen. */
194 PositionIterator::PositionIterator (Positions const& positions, int maxlen)
197 if (positions._useall)
198 _index = (maxlen <= Positions::MAX_KEY_POS ? Positions::MAX_KEY_POS - maxlen : 0);
203 index < positions._size && positions._positions[index] >= maxlen;
210 /* Retrieves the next position, or EOS past the end. */
212 PositionIterator::next ()
214 return (_index < _set._size ? _set._positions[_index++] : EOS);
217 /* Returns the number of remaining positions, i.e. how often next() will
218 return a value != EOS. */
220 PositionIterator::remaining () const
222 return _set._size - _index;
225 /* Copy constructor. */
227 PositionIterator::PositionIterator (const PositionIterator& src)
233 /* --------------------- Class PositionReverseIterator --------------------- */
235 /* Initializes an iterator through POSITIONS. */
237 PositionReverseIterator::PositionReverseIterator (Positions const& positions)
244 /* Initializes an iterator through POSITIONS, ignoring positions >= maxlen. */
246 PositionReverseIterator::PositionReverseIterator (Positions const& positions, int maxlen)
250 if (positions._useall)
251 _minindex = (maxlen <= Positions::MAX_KEY_POS ? Positions::MAX_KEY_POS - maxlen : 0);
256 index < positions._size && positions._positions[index] >= maxlen;
263 /* Retrieves the next position, or EOS past the end. */
265 PositionReverseIterator::next ()
267 return (_index > _minindex ? _set._positions[--_index] : EOS);
270 /* Returns the number of remaining positions, i.e. how often next() will
271 return a value != EOS. */
273 PositionReverseIterator::remaining () const
275 return _index - _minindex;
278 /* Copy constructor. */
280 PositionReverseIterator::PositionReverseIterator (const PositionReverseIterator& src)
283 _minindex (src._minindex)