/* ============================================================================ *
		        hints.c - functions for computing hint bits
						       Written by Talin.
						   © 1991 The Dreamers Guild
 * ============================================================================ */

#include "database.h"

#define NUMSYMBOLS		27						/* number of distinct symbols	*/
#define OTHER_SYMBOL	26						/* any non-alpha character		*/

UBYTE hint_table[NUMSYMBOLS*NUMSYMBOLS] = {
	 0, 0,-1,-1, 0, 1,-1, 1, 2, 2, 2,-1,-1,-1, 2,-1, 2,-1,-1,-1, 3, 3, 4, 4, 5, 5, 5,
	 5, 5, 5, 5,-1, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9,10,10,10,
	-1,10,10,10,-1,10,10,-1,11,11,11,12,12,12,-1,12,12,13,13,-1,14,14,14,14,14,14,14,
	15,15,15,15,-1,15,15,15,-1,16,16,16,16,16,17,17,17,17,18,18,18,18,18,18,19,19,19,
	-1,19,-1,-1,-1,19,20,20,20,20,20,-1,-1,-1,21,21,21,-1,-1,-1,21,22,22,-1,23,23,23,
	23,23,23,23,24,25,25,25,26,26,26,26,26,26,-1,26,26,27,27,27,28,28,28,28,28,28,28,
	28,28,28,29,-1,29,29,29,30,30,30,30,30,30,31,31,31,32,32,32,32,33,33,33,33,33,33,
	-1,33,33,33,-1,33,33,33,-1,33,33,33,33,33,34,34,34,34,34,35,35,35,35,35,35,35,35,
	36,36,-1,37,-1,38,39,39,39,39,39,-1,-1,-1,-1,40,40,40,-1,-1,40,41,41,41,41,42,42,
	42,42,42,42,42,42,42,42,42,42,42,42,42,42,43,43,43,43,43,43,43,43,43,43,43,43,43,
	43,43,43,43,44,44,44,44,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,
	-1,46,46,46,-1,46,47,47,-1,47,47,-1,47,47,-1,47,47,47,48,48,49,49,49,49,50,50,50,
	-1,50,50,50,-1,50,50,50,51,51,51,52,52,52,-1,-1,52,52,53,53,53,53,53,53,53,53,53,
	-1,53,-1,-1,-1,54,-1,54,-1,54,54,54,54,55,-1,55,55,55,-1,-1,55,56,56,56,56,56,56,
	56,57,57,58,58,-1,59,59,59,59,59,-1,-1,-1,60,61,61,-1,62,-1,-1,62,-1,62,62,62,62,
	63,63,63,63,-1,63,63,64,65,65,65,-1,65,65,-1,66,66,-1,66,67,67,67,67,67,67,67,67,
	67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,68,68,68,68,68,68,68,
	-1,68,68,69,-1,69,69,69,-1,69,70,70,71,71,-1,71,71,72,-1,-1,73,73,73,73,74,74,74,
	74,75,75,75,-1,75,75,-1,-1,75,76,76,76,76,-1,-1,76,76,-1,-1,77,77,77,77,78,78,78,
	-1,78,78,78,-1,78,78,-1,-1,78,78,78,79,79,-1,79,79,-1,80,81,82,82,82,82,82,83,83,
	83,83,84,84,84,84,85,85,85,85,85,-1,86,-1,86,86,86,-1,-1,-1,86,86,86,86,86,86,86,
	87,87,87,87,-1,87,87,87,88,88,88,88,88,88,88,88,88,88,88,88,88,88,88,88,88,88,88,
	89,89,89,89,-1,89,89,90,-1,90,90,90,90,90,91,91,91,91,91,91,91,91,91,91,91,91,91,
	91,91,91,91,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,93,93,93,
	93,93,93,93,93,93,93,93,93,93,93,93,93,93,94,94,94,94,95,95,95,95,95,95,95,95,95,
	95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,
	95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,
};

	/* REM: also convert foreign symbols */
	/* REM: Mac uses a different character set -- we'll need to handle the
		problem of ISO latin characters.
	*/

convert_symbol(int c)
{	if (isalpha(c)) return (toupper(c) - 'A');
	else return OTHER_SYMBOL;
}

compute_hints(char *buffer,int length,Hint *h)
{	WORD				c1,
						c2;

		/* REM: Should convert this to assembly. */

	zero(h,sizeof *h);

	c1 = c2 = OTHER_SYMBOL;

	while (length--)
	{	c2 = convert_symbol(*buffer++);
		if (c1 != OTHER_SYMBOL && c2 != OTHER_SYMBOL)
		{	int bit = hint_table[c1 * NUMSYMBOLS + c2];
			if (bit < HINTSIZE)
			{	*h[bit >> 5] |= (1 << (bit & 31));
			}
		}
		c1 = c2;
	}
}
