USG_PG3/usr/source/lexgen2/move.c

# include "../lexgen1/ldefs.c"
# define hsize 3019
# define prime1 3001
# define prime2 3011
struct {int bbkey; char  *bbdata;} hashtbl[hsize];
int tsize hsize;
int mbuff[2];
move(state,ch,ex,len)
{
	int key, i, p1, p2, q1;
	char *cp;
	key = (state<<CWIDTH) + ch;
p1 = key%prime1;
p2 = key%prime2;
if (p1<0) p1 = -p1;
if (p2<0) p2 = -p2;
if (p2==0) p2=17;
for(i=q1=0; i<hsize; i++)
	{
	if (hashtbl[p1].bbkey==0)
		{ /* new item */
		if (ex ==0)
			{
			if(q1) p1=q1;
			hashtbl[p1].bbkey = key;
			hashtbl[p1].bbdata = 0;
			return(&hashtbl[p1].bbdata);
			}
		else
			return(0);
		}
	else if (hashtbl[p1].bbkey== -1)
		{
		if (q1==0) q1=p1;
		}
	else
	if (hashtbl[p1].bbkey == key)
		if (ex>0)
			{
			if (ptr(cp= hashtbl[p1].bbdata))
				return(cp);
			mbuff[0] = cp;
			return(mbuff-1);
			}
		else if (ex==0)
			{
			return(&hashtbl[p1].bbdata);
			}
		else
			{
			if (ptr(hashtbl[p1].bbdata))
			cfree(hashtbl[p1].bbdata,0,0);
			hashtbl[p1].bbkey= -1;
			}
	p1 = (p1+p2)%hsize;
	}
error("hash table full state %d",state);
}