# include "../lexgen1/ldefs.c"
int stt[30];
struct {int ns; int st[10];};
subset()
{
int i, c, d, *p, ni, l;
int sn, v, w, ons, flag, boundary;
int kk, nnew, *np, k2, ki;
boundary = nstate;
for(i=1; i<=nstate; i++)
	{
	for(c=0; c<NCH; c++)
		{
		if ( (p= move(i,c,1,0))== 0)
			continue;
		if (p[1] == 0 || p[2] == 0)
			continue;
		ni = 0;
		while (kk = *++p)
			{
			if (kk < boundary)
				{
				if (notin(stt, ni, kk))
					stt[ni++] = kk;
				}
			else
				{
				np = new(kk,0);
				if (nnew = *np++)
					while(nnew--)
						if(notin(stt,ni,k2 = *np++))
							stt[ni++]= k2;
				}
			}
		ons= nstate;
		sn =  stset(ni);
		move(i,c,-1,0);
		addmove(i,c,sn);
		for (l=0; l<ni; l++)
			{
			w = stt[l];
			if (p= stops(w,1))
			while (v= *p++)
				addstop(sn, v);
			}
		if (ons <nstate) /* really new state made */

		{
		for (d=0; d<NCH; d++)
			{
			for(flag=l=0; l<ni-1; l++)
				{
				if (p=move(stt[l],d,1,0))
					{
					flag = 1;
					while (v = *++p)
						{
						addmove(sn,d,v);
						}
					}
				}
			if (flag)
				{
				if (p=move(stt[ni-1],d,1,0))
				while (v= *++p)
					addmove(sn,d,v);
				}
			}
		v = stt[ni-1];
		other[nstate] = ngo[v] ? v: 0;
		}

		}
	}
}
stset(n)
{
extern icomp(), exch();
int nst, *ps, *pt, pi;
int i;
sort (n, icomp, exch);
for (nst=nstate; nst>0 && new(nst,2)->ns > 0; nst--)
	{
	if (new(nst,2)->ns != n)
		continue;
	ps = new(nst,2)->st;
	pt = stt;
	pi=n;
	while(*ps++ == *pt++)
		if (--pi == 0)
			return(nst);
	}
new(++nstate, (n+1)*sizeof(n))->ns = n;
ps = new(nstate,0)->st;
for(pi=0; pi<n; pi++)
	ps[pi] = stt[pi];
return(nstate);
}
icomp(i,j)
{
int k,l;
k = stt[i]; l = stt[j];
if (ngo[k] != ngo[l])
	return(ngo[k] < ngo[l]);
return(k>l);
}
exch(i,j)
{
int t;
t=stt[i];
stt[i] = stt[j];
stt[j] = t;
}
notin(arr, len, i)
	int *arr;
{
while (len--)
	if (*arr++ == i)
		return(0);
return(1);
}
