while (lo < hi)
{
ptrdiff_t mid = (lo + hi) >> 1;
- if (s->elems[mid].index > p.index)
+ if (s->elems[mid].index < p.index)
lo = mid + 1;
else if (s->elems[mid].index == p.index)
{
m->nelem = 0;
while (i < s1->nelem || j < s2->nelem)
if (! (j < s2->nelem)
- || (i < s1->nelem && s1->elems[i].index >= s2->elems[j].index))
+ || (i < s1->nelem && s1->elems[i].index <= s2->elems[j].index))
{
unsigned int c = ((i < s1->nelem && j < s2->nelem
&& s1->elems[i].index == s2->elems[j].index)
while (lo < hi)
{
size_t mid = (lo + hi) >> 1;
- if (s->elems[mid].index > del)
+ if (s->elems[mid].index < del)
lo = mid + 1;
else if (s->elems[mid].index == del)
{
/* Array allocated to hold position sets. */
position *posalloc = xnmalloc (d->nleaves, 2 * sizeof *posalloc);
/* Firstpos and lastpos elements. */
- position *firstpos = posalloc + d->nleaves;
+ position *firstpos = posalloc;
position *lastpos = firstpos + d->nleaves;
/* Stack for element counts and nullable flags. */
of every element in the lastpos. */
{
position_set tmp;
+ tmp.elems = firstpos - stk[-1].nfirstpos;
tmp.nelem = stk[-1].nfirstpos;
- tmp.elems = firstpos;
- position *pos = lastpos;
+ position *pos = lastpos - stk[-1].nlastpos;
for (size_t j = 0; j < stk[-1].nlastpos; j++)
{
merge (&tmp, &d->follows[pos[j].index], &merged);
{
position_set tmp;
tmp.nelem = stk[-1].nfirstpos;
- tmp.elems = firstpos;
- position *pos = lastpos + stk[-1].nlastpos;
+ tmp.elems = firstpos - stk[-1].nfirstpos;
+ position *pos = lastpos - stk[-1].nlastpos - stk[-2].nlastpos;
for (size_t j = 0; j < stk[-2].nlastpos; j++)
{
merge (&tmp, &d->follows[pos[j].index], &merged);
if (stk[-2].nullable)
stk[-2].nfirstpos += stk[-1].nfirstpos;
else
- firstpos += stk[-1].nfirstpos;
+ firstpos -= stk[-1].nfirstpos;
/* The lastpos of a CAT node is the lastpos of the second argument,
union that of the first argument if the second is nullable. */
stk[-2].nlastpos += stk[-1].nlastpos;
else
{
- position *pos = lastpos + stk[-2].nlastpos;
- for (size_t j = stk[-1].nlastpos; j-- > 0;)
- pos[j] = lastpos[j];
- lastpos += stk[-2].nlastpos;
+ position *pos = lastpos - stk[-1].nlastpos - stk[-2].nlastpos;
+ for (size_t j = 0; j < stk[-1].nlastpos; j++)
+ pos[j] = pos[j + stk[-2].nlastpos];
+ lastpos -= stk[-2].nlastpos;
stk[-2].nlastpos = stk[-1].nlastpos;
}
stk->nfirstpos = stk->nlastpos = 1;
stk++;
- --firstpos, --lastpos;
firstpos->index = lastpos->index = i;
firstpos->constraint = lastpos->constraint = NO_CONSTRAINT;
+ firstpos++, lastpos++;
break;
}
fprintf (stderr,
stk[-1].nullable ? " nullable: yes\n" : " nullable: no\n");
fprintf (stderr, " firstpos:");
- for (size_t j = stk[-1].nfirstpos; j-- > 0;)
+ for (size_t j = 0; j < stk[-1].nfirstpos; j++)
{
- fprintf (stderr, " %zu:", firstpos[j].index);
- prtok (d->tokens[firstpos[j].index]);
+ fprintf (stderr, " %zu:", firstpos[j - stk[-1].nfirstpos].index);
+ prtok (d->tokens[firstpos[j - stk[-1].nfirstpos].index]);
}
fprintf (stderr, "\n lastpos:");
- for (size_t j = stk[-1].nlastpos; j-- > 0;)
+ for (size_t j = 0; j < stk[-1].nlastpos; j++)
{
- fprintf (stderr, " %zu:", lastpos[j].index);
- prtok (d->tokens[lastpos[j].index]);
+ fprintf (stderr, " %zu:", lastpos[j - stk[-1].nlastpos].index);
+ prtok (d->tokens[lastpos[j - stk[-1].nlastpos].index]);
}
putc ('\n', stderr);
#endif
fprintf (stderr, "follows(%zu:", i);
prtok (d->tokens[i]);
fprintf (stderr, "):");
- for (size_t j = d->follows[i].nelem; j-- > 0;)
+ for (size_t j = 0; j < d->follows[i].nelem; j++)
{
fprintf (stderr, " %zu:", d->follows[i].elems[j].index);
prtok (d->tokens[d->follows[i].elems[j].index]);