#include "hash.h"

/*
 * hash() takes a null-terminated word and a maximum value and returns
 * an appropriate hash value (two-byte integer) for the word
 */

short int hash(word, max)
char *word;
short int max;
{
     int hashval = 0;
     char *ptr = word;
     int letter;
     int shift = 0;

     /*
      * Convert each character to a 5 bit number between 3 and 28.
      * Xor these into a 32 bit integer in the following pattern:
      *
      * --------------------------------
      *        4444433333222221111100000   <-- first 5 bits go in 00000
      *      9999988888777776666655555
      *    4444433333222221111100000
      *       9999988888777776666655555
      *     4444433333222221111100000
      *
      * This value is taken modulo max and the 16 bit result is returned.
      */
     while (letter = *ptr++) {
	  hashval ^= (letter - '>') << shift;
	  if((shift += 5) >= 22) shift -= 22;
     }
     
     return((short) (hashval % (int) max));
}
