开发者

RS hashing program

Can anyone please tell me the working principle or开发者_开发知识库 algorithm for the RS string hashing algorithm? I need it, but cannot find on google. Please help me with the algorithm atleast, i would implement it on my own.


Do you mean Robert Sedgewick's string hashing algorithm?

uint a = 63689, uint b = 378551
foreach ( byte x ; bytes ) {
    value = value * a + x;
    a *= b;
}
return value;

(quoted from from http://pallas.telperion.info/d/hash/).


Python implementation is as below:

def RSHash(key):
    a = 63689
    b = 378551
    hash = 0
    for i in range(len(key)):
        hash = hash * a + ord(key[i])
        a = a * b
return hash


C++ Implementation is as below:

unsigned int RSHash(const std::string& str)
{
   unsigned int b    = 378551;
   unsigned int a    = 63689;
   unsigned int hash = 0;

   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = hash * a + str[i];
      a    = a * b;
   }

   return hash;
}
/* End Of RS Hash Function */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜