hashpjw.c

来自「一个c语言写做的编译器的源码」· C语言 代码 · 共 29 行

C
29
字号
/*@A (C) 1992 Allen I. Holub                                                */
#include <tools/debug.h>
#include <tools/hash.h>		/* for prototypes only */

#define NBITS_IN_UNSIGNED	(      NBITS(unsigned int)	  )
#if (0 UNIX(+1))
#define SEVENTY_FIVE_PERCENT 	((int)( (NBITS_IN_UNSIGNED * 75 ) /100  ))
#define TWELVE_PERCENT 	((int)( (NBITS_IN_UNSIGNED * 125) /1000 ))
#else
#define SEVENTY_FIVE_PERCENT 	((int)( NBITS_IN_UNSIGNED * .75  ))
#define TWELVE_PERCENT 		((int)( NBITS_IN_UNSIGNED * .125 ))
#endif
#define HIGH_BITS		( ~( (unsigned)(~0) >> TWELVE_PERCENT)  )

unsigned hash_pjw( name )
unsigned char	*name;
{
    unsigned h = 0;			/* Hash value */
    unsigned g;

    for(; *name ; ++name )
    {
	h = (h << TWELVE_PERCENT) + *name ;
	if( g = h & HIGH_BITS )
	    h = (h ^ (g >> SEVENTY_FIVE_PERCENT)) & ~HIGH_BITS ;
    }
    return h;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?