State **i2s = new State*[nstates];
for (size_t i = 0; i < nstates; ++i)
{
- i2s[i] = dfa.states[i] ? new State : NULL;
+ i2s[i] = new State;
}
State **p = &head;
for (size_t i = 0; i < nstates; ++i)
{
+ dfa_state_t *t = dfa.states[i];
State *s = i2s[i];
- if (s)
+
+ ++nStates;
+ *p = s;
+ p = &s->next;
+
+ s->isPreCtxt = t->ctx;
+ s->rule = t->rule;
+ s->fill = fill[i];
+ s->go.span = allocate<Span>(nchars);
+ uint32_t j = 0;
+ for (uint32_t c = 0; c < nchars; ++j)
{
- ++nStates;
-
- *p = s;
- p = &s->next;
-
- dfa_state_t *t = dfa.states[i];
- s->isPreCtxt = t->ctx;
- s->rule = t->rule;
- s->fill = fill[i];
- s->go.span = allocate<Span>(nchars);
- uint32_t j = 0;
- for (uint32_t c = 0; c < nchars; ++j)
- {
- const size_t to = t->arcs[c];
- for (;++c < nchars && t->arcs[c] == to;);
- s->go.span[j].to = to == dfa_t::NIL ? NULL : i2s[to];
- s->go.span[j].ub = charset[c];
- }
- s->go.nSpans = j;
+ const size_t to = t->arcs[c];
+ for (;++c < nchars && t->arcs[c] == to;);
+ s->go.span[j].to = to == dfa_t::NIL ? NULL : i2s[to];
+ s->go.span[j].ub = charset[c];
}
+ s->go.nSpans = j;
}
*p = NULL;
break;
}
+ size_t *compact = new size_t[count];
+ for (size_t i = 0, j = 0; i < count; ++i)
+ {
+ if (i == part[i])
+ {
+ compact[i] = j++;
+ }
+ }
+
+ size_t new_count = 0;
for (size_t i = 0; i < count; ++i)
{
+ dfa_state_t *s = dfa.states[i];
if (i == part[i])
{
- size_t *arcs = dfa.states[i]->arcs;
+ size_t *arcs = s->arcs;
for (size_t c = 0; c < dfa.nchars; ++c)
{
if (arcs[c] != dfa_t::NIL)
{
- arcs[c] = part[arcs[c]];
+ arcs[c] = compact[part[arcs[c]]];
}
}
+ dfa.states[new_count++] = s;
}
else
{
- delete dfa.states[i];
- dfa.states[i] = NULL;
+ delete s;
}
}
+ dfa.states.resize(new_count);
+ delete[] compact;
delete[] part;
}