1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 21:19:24 +00:00

Move positions handling to its own file.

This commit is contained in:
Bruno Haible
2003-02-13 12:11:01 +00:00
parent 810fef43ae
commit ee135115f5
12 changed files with 445 additions and 325 deletions

View File

@@ -940,135 +940,6 @@ Options::parse_options (int argc, char *argv[])
}
}
/* ---------------------------- Class Positions ---------------------------- */
/* Set operations. Assumes the array is in reverse order. */
bool
Positions::contains (int pos) const
{
unsigned int count = _size;
const unsigned char *p = _positions + _size - 1;
for (; count > 0; p--, count--)
{
if (*p == pos)
return true;
if (*p > pos)
break;
}
return false;
}
void
Positions::add (int pos)
{
unsigned int count = _size;
if (count == MAX_KEY_POS + 1)
{
fprintf (stderr, "Positions::add internal error: overflow\n");
exit (1);
}
unsigned char *p = _positions + _size - 1;
for (; count > 0; p--, count--)
{
if (*p == pos)
{
fprintf (stderr, "Positions::add internal error: duplicate\n");
exit (1);
}
if (*p > pos)
break;
p[1] = p[0];
}
p[1] = pos;
_size++;
}
void
Positions::remove (int pos)
{
unsigned int count = _size;
if (count > 0)
{
unsigned char *p = _positions + _size - 1;
if (*p == pos)
{
_size--;
return;
}
if (*p < pos)
{
unsigned char prev = *p;
for (;;)
{
p--;
count--;
if (count == 0)
break;
if (*p == pos)
{
*p = prev;
_size--;
return;
}
if (*p > pos)
break;
unsigned char curr = *p;
*p = prev;
prev = curr;
}
}
}
fprintf (stderr, "Positions::remove internal error: not found\n");
exit (1);
}
/* Output in external syntax. */
void
Positions::print () const
{
bool first = true;
bool seen_LASTCHAR = false;
unsigned int count = _size;
const unsigned char *p = _positions + _size - 1;
for (; count > 0; p--, count--)
{
if (*p == LASTCHAR)
seen_LASTCHAR = true;
else
{
if (!first)
printf (",");
printf ("%d", *p);
if (count > 0 && p[-1] == *p + 1)
{
printf ("-");
do
{
p--;
count--;
}
while (count > 0 && p[-1] == *p + 1);
printf ("%d", *p);
}
first = false;
}
}
if (seen_LASTCHAR)
{
if (!first)
printf (",");
printf ("$");
}
}
/* ------------------------------------------------------------------------- */
#ifndef __OPTIMIZE__