162Fermer164
Pen^2Le 12/10/2007 à 18:03
int fast_and_perfect_hash(const char *str, int length)  // works only with HASH_TABLE_SIZE = 256 or 512 or 1024 
{   // very good Daniel Julius Bernstein's algorithm 
    // speed optimized by Pollux
    // unrolled by Thibaut for more speed 
    // generates very uniform hash tables even with non prime modulos 
 
    register unsigned int hash = 5381; // DJB Hash 
    register unsigned int hash5 = 0; 
    register const char   *s; 
    register int          i; 
 
 
    s = str-sizeof(char); 
    i= (length / 4)+sizeof(char); 
 
    while (--i > 0) { 
        hash5+= hash;  hash+= *++s; 
        hash5+= hash;  hash+= *++s; 
        hash5+= hash;  hash+= *++s; 
        hash5+= hash;  hash+= *++s; 
    } 
 

    i= (length % 4)+sizeof(char); 
    while (--i > 0) { 
        hash5+= hash; 
        hash+= *++s; 
    } 
 
    return (hash + (hash5<<5)) % HASH_TABLE_SIZE; 
}