115Fermer117
Kevin KoflerLe 12/10/2007 à 00:17
Pour la fonction donnée par Godzil, il y a écrit PRIME pour le modulo, donc c'est normal que la performance soit pourrie si tu utilises une puissance de 2, qui aux dernière nouvelles n'était forcément pas un nombre premier (sauf 2, qui est une très mauvaise taille pour une table de hachage gni)...

Pour la fonction de Java, voici la version de OpenJDK:
    /**
     * Returns a hash code for this string. The hash code for a
     * <code>String</code> object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
     * <i>i</i>th character of the string, <code>n</code> is the length of
     * the string, and <code>^</code> indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0) {
            int off = offset;
            char val[] = value;
            int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }

(https://openjdk.dev.java.net/source/browse/openjdk/jdk/trunk/j2se/src/share/classes/java/lang/String.java?rev=249&view=markup).