若干經典的字符串哈希函數

//  RS Hash Function 
 unsigned  int  RSHash( char   * str)
  {
        unsigned  int  b  =   378551 ;
        unsigned  int  a  =   63689 ;
        unsigned  int  hash  =   0 ;

         while  ( * str)
          {
                hash  =  hash  *  a  +  ( * str ++ );
                a  *=  b;
        } 
 
         return  (hash  &   0x7FFFFFFF );

 
 //  JS Hash Function 
 unsigned  int  JSHash( char   * str)
  {
        unsigned  int  hash  =   1315423911 ;

         while  ( * str)
          {
                hash  ^=  ((hash  <<   5 )  +  ( * str ++ )  +  (hash  >>   2 ));
        } 
 
         return  (hash  &   0x7FFFFFFF );

 
 //  P. J. Weinberger Hash Function 
 unsigned  int  PJWHash( char   * str)
  {
        unsigned  int  BitsInUnignedInt  =  (unsigned  int )( sizeof (unsigned  int )  * 
 8 );
        unsigned  int  ThreeQuarters     =  (unsigned  int )((BitsInUnignedInt   *   3 )
  /   4 );
        unsigned  int  OneEighth         =  (unsigned  int )(BitsInUnignedInt  /   8 );

        unsigned  int  HighBits          =  (unsigned  int )( 0xFFFFFFFF )  <<  (BitsInU
nignedInt  -  OneEighth);
        unsigned  int  hash              =   0 ;
        unsigned  int  test              =   0 ;

         while  ( * str)
          {
                hash  =  (hash  <<  OneEighth)  +  ( * str ++ );
                 if  ((test  =  hash  &  HighBits)  !=   0 )
                  {
                        hash  =  ((hash  ^  (test  >>  ThreeQuarters))  &  ( ~ HighBits)
);
                } 
        } 
 
         return  (hash  &   0x7FFFFFFF );

 
 //  ELF Hash Function 
 unsigned  int  ELFHash( char   * str)
  {
        unsigned  int  hash  =   0 ;
        unsigned  int  x     =   0 ;

         while  ( * str)
          {
                hash  =  (hash  <<   4 )  +  ( * str ++ );
                 if  ((x  =  hash  &   0xF0000000L )  !=   0 )
                  {
                        hash  ^=  (x  >>   24 );
                        hash  &=   ~ x;
                } 
        } 
 
         return  (hash  &   0x7FFFFFFF );

 
 //  BKDR Hash Function 
 unsigned  int  BKDRHash( char   * str)
  {
        unsigned  int  seed  =   131 ;  //  31 131 1313 13131 131313 etc.. 
         unsigned  int  hash  =   0 ;

         while  ( * str)
          {
                hash  =  hash  *  seed  +  ( * str ++ );
        } 
 
         return  (hash  &   0x7FFFFFFF );

 
 //  SDBM Hash Function 
 unsigned  int  SDBMHash( char   * str)
  {
        unsigned  int  hash  =   0 ;

         while  ( * str)
          {
                hash  =  ( * str ++ )  +  (hash  <<   6 )  +  (hash  <<   16 )  -  hash;
        } 
 
         return  (hash  &   0x7FFFFFFF );

 
 //  DJB Hash Function 
 unsigned  int  DJBHash( char   * str)
  {
        unsigned  int  hash  =   5381 ;

         while  ( * str)
          {
                hash  +=  (hash  <<   5 )  +  ( * str ++ );
        } 
 
         return  (hash  &   0x7FFFFFFF );

 
 //  AP Hash Function 
 unsigned  int  APHash( char   * str)
  {
        unsigned  int  hash  =   0 ;
         int  i;

         for  (i = 0 ;  * str; i ++ )
          {
                 if  ((i  &   1 )  ==   0 )
                  {
                        hash  ^=  ((hash  <<   7 )  ^  ( * str ++ )  ^  (hash  >>   3 ));
                } 
                 else 
                   {
                        hash  ^=  ( ~ ((hash  <<   11 )  ^  ( * str ++ )  ^  (hash  >>   5 )));
                } 
        } 
 
         return  (hash  &   0x7FFFFFFF );

 比較經典的字符串hash就這些了吧,"ELF Hash Function" <-這個比較常用. 

字符串hash算法比較

1 概述
鏈表查找的時間效率爲O(N),二分法爲log2N,B+ Tree爲log2N,但Hash鏈表查找的時間效率爲O(1)。
設 計高效算法往往需要使用Hash鏈表,常數級的查找速度是任何別的算法無法比擬的,Hash鏈表的構造和衝突的不同實現方法對效率當然有一定的影響,然 而Hash函數是Hash鏈表最核心的部分,本文嘗試分析一些經典軟件中使用到的字符串Hash函數在執行效率、離散性、空間利用率等方面的性能問題。
 
2 經典字符串Hash函數介紹
作者閱讀過大量經典軟件原代碼,下面分別介紹幾個經典軟件中出現的字符串Hash函數。
2.1 PHP中出現的字符串Hash函數
static unsigned long hashpjw(char *arKey, unsigned int nKeyLength)
{
unsigned long h = 0, g;
char *arEnd=arKey+nKeyLength;

while (arKey < arEnd) {
h = (h << 4) + *arKey++;
if ((g = (h & 0xF0000000))) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
}
2.2 OpenSSL中出現的字符串Hash函數
unsigned long lh_strhash(char *str)
{
int i,l;
unsigned long ret=0;
unsigned short *s;

if (str == NULL) return(0);
l=(strlen(str)+1)/2;
s=(unsigned short *)str;
for (i=0; i
ret^=(s[i]<<(i&0x0f));
return(ret);
} */

/* The following hash seems to work very well on normal text strings
* no collisions on /usr/dict/words and it distributes on %2^n quite
* well, not as good as MD5, but still good.
*/
unsigned long lh_strhash(const char *c)
{
unsigned long ret=0;
long n;
unsigned long v;
int r;

if ((c == NULL) || (*c == '/0'))
return(ret);
/*
unsigned char b[16];
MD5(c,strlen(c),b);
return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
*/

n=0x100;
while (*c)
{
v=n|(*c);
n+=0x100;
r= (int)((v>>2)^v)&0x0f;
ret=(ret(32-r));
ret&=0xFFFFFFFFL;
ret^=v*v;
c++;
}
return((ret>>16)^ret);
}
在下面的測量過程中我們分別將上面的兩個函數標記爲OpenSSL_Hash1和OpenSSL_Hash2,至於上面的實現中使用MD5算法的實現函數我們不作測試。
2.3 MySql中出現的字符串Hash函數
#ifndef NEW_HASH_FUNCTION

/* Calc hashvalue for a key */

static uint calc_hashnr(const byte *key,uint length)
{
register uint nr=1, nr2=4;
while (length--)
{
nr^= (((nr & 63)+nr2)*((uint) (uchar) *key++))+ (nr << 8);
nr2+=3;
}
return((uint) nr);
}

/* Calc hashvalue for a key, case indepenently */

static uint calc_hashnr_caseup(const byte *key,uint length)
{
register uint nr=1, nr2=4;
while (length--)
{
nr^= (((nr & 63)+nr2)*((uint) (uchar) toupper(*key++)))+ (nr << 8);
nr2+=3;
}
return((uint) nr);
}

#else

/*
* Fowler/Noll/Vo hash
*
* The basis of the hash algorithm was taken from an idea sent by email to the
* IEEE Posix P1003.2 mailing list from Phong Vo ([email protected]) and
* Glenn Fowler ([email protected]). Landon Curt Noll ([email protected])
* later improved on their algorithm.
*
* The magic is in the interesting relationship between the special prime
* 16777619 (2^24 + 403) and 2^32 and 2^8.
*
* This hash produces the fewest collisions of any function that we've seen so
* far, and works well on both numbers and strings.
*/

uint calc_hashnr(const byte *key, uint len)
{
const byte *end=key+len;
uint hash;
for (hash = 0; key < end; key++)
{
hash *= 16777619;
hash ^= (uint) *(uchar*) key;
}
return (hash);
}

uint calc_hashnr_caseup(const byte *key, uint len)
{
const byte *end=key+len;
uint hash;
for (hash = 0; key < end; key++)
{
hash *= 16777619;
hash ^= (uint) (uchar) toupper(*key);
}
return (hash);
}

#endif
Mysql中對字符串Hash函數還區分了大小寫,我們的測試中使用不區分大小寫的字符串Hash函數,另外我們將上面的兩個函數分別記爲MYSQL_Hash1和MYSQL_Hash2。
2.4 另一個經驗字符串Hash函數
unsigned int hash(char *str)
{
register unsigned int h;
register unsigned char *p;

for(h=0, p = (unsigned char *)str; *p ; p++)
h = 31 * h + *p;

return h;
}
3 測試及結果
3.1 測試說明
從上面給出的經典字符串Hash函數中可以看出,有的涉及到字符串大小敏感問題,我們的測試中只考慮字符串大小寫敏感的函數,另外在上面的函數中有的函數 需要長度參數,有的不需要長度參數,這對函數本身的效率有一定的影響,我們的測試中將對函數稍微作一點修改,全部使用長度參數,並將函數內部出現的計算長 度代碼刪除。
我們用來作測試用的Hash鏈表採用經典的拉鍊法解決衝突,另外我們採用靜態分配桶(Hash鏈表長度)的方法來構造Hash鏈表,這主要是爲了簡化我們的實現,並不影響我們的測試結果。
測試文本採用單詞表,測試過程中從一個輸入文件中讀取全部不重複單詞構造一個Hash表,測試內容分別是函數總調用次數、函數總調用時間、最大拉鍊長度、 平均拉鍊長度、桶利用率(使用過的桶所佔的比率),其中函數總調用次數是指Hash函數被調用的總次數,爲了測試出函數執行時間,該值在測試過程中作了一 定的放大,函數總調用時間是指Hash函數總的執行時間,最大拉鍊長度是指使用拉鍊法構造鏈表過程中出現的最大拉鍊長度,平均拉鍊長度指拉鍊的平均長度。
測試過程中使用的機器配置如下:
PIII600筆記本,128M內存,windows 2000 server操作系統。
3.2 測試結果
以下分別是對兩個不同文本文件中的全部不重複單詞構造Hash鏈表的測試結果,測試結果中函數調用次數放大了100倍,相應的函數調用時間也放大了100倍。

從上表可以看出,這些經典軟件雖然構造字符串Hash函數的方法不同,但是它們的效率都是不錯的,相互之間差距很小,讀者可以參考實際情況從其中借鑑使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章