1. 程式人生 > >10個經典的字串hash函式的C程式碼實現

10個經典的字串hash函式的C程式碼實現

unsigned int RSHash(char* str, unsigned int len)   
{   
   unsigned int b    = 378551;   
   unsigned int a    = 63689;   
   unsigned int hash = 0;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = hash * a + (*str);   
      a    = a * b;   
   }   
   return hash;   
}   
/* End Of RS Hash Function */  
  
unsigned int JSHash(char* str, unsigned int len)   
{   
   unsigned int hash = 1315423911;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash ^= ((hash << 5) + (*str) + (hash >> 2));   
   }   
   return hash;   
}   
/* End Of JS Hash Function */  
  
unsigned int PJWHash(char* str, unsigned int len)   
{   
   const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);   
   const unsigned int ThreeQuarters     = (unsigned int)((BitsInUnsignedInt  * 3) / 4);   
   const unsigned int OneEighth         = (unsigned int)(BitsInUnsignedInt / 8);   
   const unsigned int HighBits          = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);   
   unsigned int hash              = 0;   
   unsigned int test              = 0;   
   unsigned int i                 = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = (hash << OneEighth) + (*str);   
      if((test = hash & HighBits)  != 0)   
      {   
         hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));   
      }   
   }   
   return hash;   
}   
/* End Of  P. J. Weinberger Hash Function */  
  
unsigned int ELFHash(char* str, unsigned int len)   
{   
   unsigned int hash = 0;   
   unsigned int x    = 0;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = (hash << 4) + (*str);   
      if((x = hash & 0xF0000000L) != 0)   
      {   
         hash ^= (x >> 24);   
      }   
      hash &= ~x;   
   }   
   return hash;   
}   
/* End Of ELF Hash Function */  
  
unsigned int BKDRHash(char* str, unsigned int len)   
{   
   unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */  
   unsigned int hash = 0;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = (hash * seed) + (*str);   
   }   
   return hash;   
}   
/* End Of BKDR Hash Function */  
  
unsigned int SDBMHash(char* str, unsigned int len)   
{   
   unsigned int hash = 0;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = (*str) + (hash << 6) + (hash << 16) - hash;   
   }   
   return hash;   
}   
/* End Of SDBM Hash Function */  
  
unsigned int DJBHash(char* str, unsigned int len)   
{   
   unsigned int hash = 5381;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = ((hash << 5) + hash) + (*str);   
   }   
   return hash;   
}   
/* End Of DJB Hash Function */  
  
unsigned int DEKHash(char* str, unsigned int len)   
{   
   unsigned int hash = len;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);   
   }   
   return hash;   
}   
/* End Of DEK Hash Function */  
  
unsigned int BPHash(char* str, unsigned int len)   
{   
   unsigned int hash = 0;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash = hash << 7 ^ (*str);   
   }   
   return hash;   
}   
/* End Of BP Hash Function */  
  
unsigned int FNVHash(char* str, unsigned int len)   
{   
   const unsigned int fnv_prime = 0x811C9DC5;   
   unsigned int hash      = 0;   
   unsigned int i         = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash *= fnv_prime;   
      hash ^= (*str);   
   }   
   return hash;   
}   
/* End Of FNV Hash Function */  
  
unsigned int APHash(char* str, unsigned int len)   
{   
   unsigned int hash = 0xAAAAAAAA;   
   unsigned int i    = 0;   
   for(i = 0; i < len; str++, i++)   
   {   
      hash ^= ((i & 1) == 0) ? (  (hash <<  7) ^ (*str) * (hash >> 3)) :   
                               (~((hash << 11) + (*str) ^ (hash >> 5)));   
   }   
   return hash;   
}   
/* End Of AP Hash Function */ 

-------------------------------------------------------------------------------------------------------------------------------------------------------------

幾種經典的Hash演算法的實現(原始碼)

●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;
}


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);
}


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函式

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;
}



相關推薦

10經典字串hash函式C程式碼實現

unsigned int RSHash(char* str, unsigned int len) { unsigned int b = 378551; unsigned int a = 63689; unsigned i

C語言10經典小程式——小白必備!

網上有很多的人說程式設計有多麼多麼無聊。。。。So Boring ! 。。。其實小編想說:不要管別人怎麼說,別人說什麼,做你自己喜歡做的事就好。堅持下來,你會發現程式設計的樂趣的。。。。當然,如果你覺得學習程式語言很痛苦,堅持了一段時間後無果,南無果斷放棄未必不是一個好的選擇。。。。哈哈哈哈。。。

C庫提供了三讀取字串函式:gets( ) fgets( ) scanf( )。

C庫提供了三個讀取字串的函式:gets( )  fgets( )  scanf( )。 gets()---get string 從系統的標準輸入裝置(通常是鍵盤)獲得一個字串。因為字串沒有預定的長度,所以gets()需要知道輸入何時結束。解決辦法是在讀字串直到遇到一個換行符(/n),按回車鍵可以產生這個字元

合併字串中連續的多空格的C程式碼實現

1.問題描述 將某一字串中連續出現的多個空格合併為一個空格,如果合併之後的字串的首尾有空格,則將其去掉。 例如,“ This is a string! ”是一個包含多個空格的字串,要求其變成“This is a string!”的形式。 2.C程式碼實

10經典的Java面試題集合

支持 獲得 equal 效率 可用 ash 很快 鍵值對 shm 1.Java的HashMap是如何工作的? HashMap是一個針對數據結構的鍵值,每個鍵都會有相應的值,關鍵是識別這樣的值。 HashMap 基於 hashing 原理,我們通過 put ()和 g

字串替換兩特定字元之間內容的程式碼實現

今天一個同事寫字串的替換問題,提交程式碼的實現邏輯太過麻煩,於是追問是從網上拷貝下來的,我在網上一搜,果然一大堆這種實現方法,真的是太浪費正則表示式了,拖慢速度,浪費了強大的string類。 下面寫一下我的程式碼實現,只為了讓程式碼更加簡潔: String str="local/{yyy

資料結構經典例題解析C/C++程式碼實現(二)

第一題 題目 編一C程式,它能把讀入的整數依次插入到一個初始為空的二叉排序樹中,一直讀到-9999為止(-9999不插入該二叉排序樹)。輸出該二叉排序樹的前序序列、後序序列及葉結點的個數。(輸入時,兩個相鄰的整數用空格隔開)。 解析 這個程式碼可以參考二叉樹

資料結構經典例題解析C/C++程式碼實現(一)

考研需要吧,雖然挺基礎的,但是還是要練習下的,而且,還可以幫助一些其他同樣需要這些程式碼的朋友。 實現最基礎的資料結構建議是用C語言,這樣子很多細節都可以很好地把握,當然,如果用STL可以簡單地實現,那麼我也會實現一下。 第一題 題目 編一C程式,它能根據讀入的資

沒想通的關於複製建構函式C++程式碼: A obj=func()

class A { public: A(int x=0):i(x) { cout << "Normal Contructor: " << i << endl; } ~A() {

單執行緒實現同時監聽多埠(windows平臺c++程式碼

前言   多年前開發了一套網路庫,底層實現採用IOCP(完成埠)。該庫已在公司多個程式中應用;經過多次修改,長時間檢驗,已經非常穩定高效。 最近把以前的程式碼梳理了一下,又加進了一些新的思路。程式碼結構更加合理,效能也有所提升。打算將該庫一些的知識點寫出來,以供參考。 服務端要在多個埠監聽,這種場合並不多見。

今天定小目標,用C語言實現三子棋的玩法。裡面有精彩情景故事幫助你更快理解程式碼內容,不進來了解一下嗎?(內附程式碼

  如標題所示,今天我們要用C語言來實現三子棋的遊戲。相信大家都玩過這個遊戲。我們來回憶一下游戲步驟。   一、今天你在家裡看書,你的朋友小紅邀請你和她一起玩三子棋。這時你有兩個選擇。     1.接受她的邀請,在玩遊戲的同手,促進你們的感情。     0.殘忍

常見字串處理函式實現原理

字串是一種常見的資料結構,對字串的處理又可以十分靈活,所以在實際開發,尤其是非數值處理中,字串的應用非常廣泛。雖然很多字串操作都封裝在了函式庫裡,應用程式可以直接通過呼叫庫函式來實現字串處理,然而對於開發者而言,若能瞭解其底層實現原理,對於應用程式設計而言還是大有裨益的。

LeetCode 21.合併兩有序連結串列 C++程式碼實現

題目描述: 將兩個有序連結串列合併為一個新的有序連結串列並返回。新連結串列是通過拼接給定的兩個連結串列的所有節點組成的。  示例: 輸入:1->2->4, 1->3->4 輸出:1->1->2->3->4->4

字串的所有組合數(分冶法+遞迴)c++程式碼實現

題目:輸入一個字串,求字元的所有組合。例如輸入字串abc,則它的組合有a、b、c、ab、ac、bc、abc。當交換字串中的兩個字元時,雖然能得到兩個不同的排列,但卻是同一組合。下面假設字串中所有字元都不相同。如果輸入n個字元,則這n個字元能構成長度為1的組合、長度為2的組合、

高斯模糊函式 c 程式碼

注:程式碼來源於 http://hi.baidu.com/iceboy_/blog/item/729b79cae4744a18be09e6e9.html ,本人只是稍走修改,測試過,能使用。模糊後的資料保存於 傳入的data中。  int gaussBlur:(int *d

經典Top-K問題最優解決辦法以及C++程式碼實現

問題描述:Top-K問題是一個十分經典的問題,一般有以下兩種方式來描述問題:在10億的數字裡,找出其中最大的100個數;或者在一個包含n個整數的陣列中,找出最大的100個數。    前邊兩種問題描述稍有區別,但都是說的Top-K問題,前一種描述方式是說這裡也許沒有足夠的空間儲

12小球稱重量問題(C程式碼)

12個外表一模一樣的小球,其中有一個重量和其他11個不同,怎樣只用3次天平將這個小球找出來? /*author:cifry*/ /*platform:VC++6.0*/ #include<stdio.h> #include<stdarg.h> #in

字串hash函式(hashCode的生成)

非常好的外文網站!!!: General Purpose Hash Function Algorithms 最終結果: 1.  BKDRHash 2. Blizzard hash ************** Java 版:

一個字串擷取函式c語言

剛開始學習c語言,標準庫中總是有很多函式沒有,string.h中的字串處理函式好像不是很多,在做棧的例子用需要用到一個字串按位置擷取的函式,就自己寫一個,超簡單。 char* substring(char* ch,int pos,int length) { char

適用的字串hash函式

下面是綜合情況比較好的兩個字串hash函式,就當做一個筆記吧: unsigned int BKDRHash(char *str) { unsigned int seed = 131; // 31 131 1313 13131 131313 etc... unsigned