169Fermer171
ThibautLe 12/10/2007 à 18:26
Oui, ta version est plus pertinente smile Je l'ai adoptée, en mettant à ma sauce :

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
    // some other speed optimizations by Pen^2
    // 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;

    i= (unsigned)length / 4;
    while (i--) {
        hash5+= hash;  hash+= *s++;
        hash5+= hash;  hash+= *s++;
        hash5+= hash;  hash+= *s++;
        hash5+= hash;  hash+= *s++;
    }

    i= (unsigned)length % 4;
    while (i--) {
        hash5+= hash;
        hash+= *s++;
    }

    return (hash + (hash5<<5)) % HASH_TABLE_SIZE;
}
Comme tu le vois, je préfère laisser *s++ car il me semble que sur 68k cela correspondra à un add (an)+,dn alors que *++s risque de produire un code moins rapide, non ?