/*
 *  smoke16/src/hashpjw.c
 *
 *  This file is in the public domain.
 */

#include <smoke16/hashpjw.h>
#define EOS '\0'

/* hashpjw returns an integer by hashing the character string s */
/* hashpjw was obtained from the March 1986 edition of Compilers
   by Aho, Sethi and Ullman; the algorithm was developed
   by P. J. Weinberger for his C compiler. */
/* tested 12/31/1993 - BS */
/* filenames changed to lowercase & far pointers removed, also added
   conditional DEBUG compilation directives for debug purposes.
   compiled to object file successfully 1/21/1995 - BS */

int hashpjw(char *instring)
{
   char *p;
   unsigned long int h, g;

#ifdef DEBUG
   printf("hashpjw: entry with word |%Fs| \n", instring );
#endif
   h = 0;
   for ( p = instring; *p != EOS; p = p+1 ) {
      h = (h << 4) + (unsigned long int)(*p);
      if ((g = h&0xf0000000L) != 0) {
	 h = h ^ (g >> 24);
	 h = h ^ g;
      }
   }
#ifdef DEBUG
   printf( "hashpjw: returns %d \n", h % HASHSIZE );
#endif
   return (int)(h % HASHSIZE);
}

