RS hash program

Can someone tell me the operating principle or algorithm for the RS string hash algorithm? I need it, but can’t find it on Google. Please help me with the algorithm, at least I would implement it myself.

+3
source share
3 answers

Do you mean the hash algorithm for strings in the Sedgewick format ?

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

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

+6
source

The Python implementation is as follows:

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
0
source

The C ++ implementation is as follows:

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
source

All Articles