58Fermer60
JackosKingLe 06/05/2008 à 16:06
Je l'écrirais plutot comme cela, mais ca dépend des gouts.
A noter que ton code n'est compatible qu'à partir de C99


/*
\name fastAndPerfectHash
very good Daniel Julius Bernstein's algorithm 
speed optimized by Pollux 
unrolled by Thibaut Barthelemy for more speed 
many other speed optimizations by Julien Monville 
generates very uniform hash tables even with non prime modulos 
\arg pStr_ : Chaine ...
\arg length_ : longueur de chaine (A quoi ca sert?)
\pre HASH_TABLE_SIZE = 256 or 512 or 1024 and length_ <= pStr_'s length
\post nothing
*/
int fastAndPerfectHash(const char *pStr_, unsigned int length_){  
  REQUIRE(    (HASH_TABLE_SIZE == 256 ||  HASH_TABLE_SIZE  == 512 || HASH_TABLE_SIZE == 1024)
           && lenght_ <= strnlen(pStr_, length_));
    { 
      register unsigned int hash  = 5381; // DJB Hash 
      register unsigned int hash5 = 0; 
      register int i; 
 
 
      i = length_ / 4; 
      while (i--){ 
        hash5 += hash;   hash += *pStr_++; 
        hash5 += hash;   hash += *pStr_++; 
        hash5 += hash;   hash += *pStr_++; 
        hash5 += hash;   hash += *pStr_++; 
      } 
 
      i = length_ & 3;
      while (i--){ 
        hash5 += hash;   hash  += *pStr_++; 
      } 
  
      return (hash + (hash5 << 5) ) & (HASH_TABLE_SIZE - 1); 
    }
}