常用的字符串哈希函數

  1. /* A Simple Hash Function */  
  2. unsigned int Simple_hash(char *str)  
  3. {  
  4.     register unsigned int hash;  
  5.     register unsigned char *p;  
  6.   
  7.     for(hash = 0, p = (unsigned char *)str; *p ; p++)  
  8.         hash = 31 * hash + *p;  
  9.   
  10.     return (hash & 0x7FFFFFFF);  
  11. }  
  12.   
  13. /* RS Hash Function */  
  14. unsigned int RS_hash(char *str)  
  15. {  
  16.          unsigned int b = 378551;  
  17.          unsigned int a = 63689;  
  18.          unsigned int hash = 0;  
  19.   
  20.          while (*str)  
  21.          {  
  22.                  hash = hash * a + (*str++);  
  23.                  a *= b;  
  24.          }  
  25.   
  26.          return (hash & 0x7FFFFFFF);  
  27. }  
  28.   
  29. /* JS Hash Function */  
  30. unsigned int JS_hash(char *str)  
  31. {  
  32.          unsigned int hash = 1315423911;  
  33.   
  34.          while (*str)  
  35.          {  
  36.                  hash ^= ((hash << 5) + (*str++) + (hash >> 2));  
  37.          }  
  38.           
  39.          return (hash & 0x7FFFFFFF);  
  40. }  
  41.   
  42. /* P. J. Weinberger Hash Function */  
  43. unsigned int PJW_hash(char *str)  
  44. {  
  45.          unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);  
  46.          unsigned int ThreeQuarters     = (unsigned int)((BitsInUnignedInt   * 3) / 4);  
  47.          unsigned int OneEighth         = (unsigned int)(BitsInUnignedInt / 8);  
  48.   
  49.          unsigned int HighBits          = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);  
  50.          unsigned int hash              = 0;  
  51.          unsigned int test              = 0;  
  52.   
  53.          while (*str)  
  54.          {  
  55.                  hash = (hash << OneEighth) + (*str++);  
  56.                  if ((test = hash & HighBits) != 0)  
  57.                  {  
  58.                          hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));  
  59.                  }  
  60.          }  
  61.   
  62.          return (hash & 0x7FFFFFFF);  
  63. }  
  64.   
  65. /* ELF Hash Function */  
  66. unsigned int ELF_hash(char *str)  
  67. {  
  68.          unsigned int hash = 0;  
  69.          unsigned int x     = 0;  
  70.   
  71.          while (*str)  
  72.          {  
  73.                  hash = (hash << 4) + (*str++);  
  74.                  if ((x = hash & 0xF0000000L) != 0)  
  75.                  {  
  76.                          hash ^= (x >> 24);  
  77.                          hash &= ~x;  
  78.                  }  
  79.          }  
  80.   
  81.          return (hash & 0x7FFFFFFF);  
  82. }  
  83.   
  84. /* BKDR Hash Function */  
  85. unsigned int BKDR_hash(char *str)  
  86. {  
  87.          unsigned int seed = 131; // 31 131 1313 13131 131313 etc..  
  88.          unsigned int hash = 0;  
  89.   
  90.          while (*str)  
  91.          {  
  92.                  hash = hash * seed + (*str++);  
  93.          }  
  94.   
  95.          return (hash & 0x7FFFFFFF);  
  96. }  
  97.   
  98. /* SDBM Hash Function */  
  99. unsigned int SDBM_hash(char *str)  
  100. {  
  101.          unsigned int hash = 0;  
  102.   
  103.          while (*str)  
  104.          {  
  105.                  hash = (*str++) + (hash << 6) + (hash << 16) - hash;  
  106.          }  
  107.   
  108.          return (hash & 0x7FFFFFFF);  
  109. }  
  110.   
  111. /* DJB Hash Function */  
  112. unsigned int DJB_hash(char *str)  
  113. {  
  114.          unsigned int hash = 5381;  
  115.   
  116.          while (*str)  
  117.          {  
  118.                  hash += (hash << 5) + (*str++);  
  119.          }  
  120.   
  121.          return (hash & 0x7FFFFFFF);  
  122. }  
  123.   
  124. /* AP Hash Function */  
  125. unsigned int AP_hash(char *str)  
  126. {  
  127.          unsigned int hash = 0;  
  128.          int i;  
  129.          for (i=0; *str; i++)  
  130.          {  
  131.                  if ((i & 1) == 0)  
  132.                  {  
  133.                          hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));  
  134.                  }  
  135.                  else  
  136.                  {  
  137.                          hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));  
  138.                  }  
  139.          }  
  140.   
  141.          return (hash & 0x7FFFFFFF);  
  142. }  
  143.   
  144. /* CRC Hash Function */  
  145. unsigned int CRC_hash(char *str)  
  146. {  
  147.     unsigned int        nleft   = strlen(str);  
  148.     unsigned long long  sum     = 0;  
  149.     unsigned short int *w       = (unsigned short int *)str;  
  150.     unsigned short int  answer  = 0;  
  151.   
  152.     /* 
  153.      * Our algorithm is simple, using a 32 bit accumulator (sum), we add 
  154.      * sequential 16 bit words to it, and at the end, fold back all the 
  155.      * carry bits from the top 16 bits into the lower 16 bits. 
  156.      */  
  157.     while ( nleft > 1 ) {  
  158.         sum += *w++;  
  159.         nleft -= 2;  
  160.     }  
  161.     /* 
  162.      * mop up an odd byte, if necessary 
  163.      */  
  164.     if ( 1 == nleft ) {  
  165.         *( unsigned char * )( &answer ) = *( unsigned char * )w ;  
  166.         sum += answer;  
  167.     }  
  168.     /* 
  169.      * add back carry outs from top 16 bits to low 16 bits 
  170.      * add hi 16 to low 16 
  171.      */  
  172.     sum = ( sum >> 16 ) + ( sum & 0xFFFF );  
  173.     /* add carry */  
  174.     sum += ( sum >> 16 );  
  175.     /* truncate to 16 bits */  
  176.     answer = ~sum;  
  177.   
  178.     return (answer & 0xFFFFFFFF);  
  179. }  

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