Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6586631

Use toLongArray in the specification of BitSet.hashCode

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 7
    • core-libs
    • b20
    • generic
    • generic
    • Verified

        BitSet.hashCode()'s slightly bizarre specification can be made more normal
        by using the new public toLongArray method.


             /**
        - * Returns a hash code value for this bit set. The hash code
        - * depends only on which bits have been set within this
        - * {@code BitSet}. The algorithm used to compute it may
        - * be described as follows.
        - *
        - * <p>Suppose the bits in the {@code BitSet} were to be stored
        - * in an array of {@code long} integers called, say,
        - * {@code words}, in such a manner that bit {@code k} is
        - * set in the {@code BitSet} (for nonnegative values of
        - * {@code k}) if and only if the expression
        - * <pre>{@code ((k>>6) < words.length) && ((words[k>>6] & (1L << (bit & 0x3F))) != 0)}</pre>
        - * is true. Then the following definition of the {@code hashCode}
        - * method would be a correct implementation of the actual algorithm:
        + * Returns the hash code value for this bit set. The hash code depends
        + * only on which bits are set within this {@code BitSet}.
        + *
        + * <p>The hash code is defined to be the result of the following
        + * calculation:
              * <pre> {@code
              * public int hashCode() {
              * long h = 1234;
        - * for (int i = words.length; --i >= 0; ) {
        + * long[] words = toLongArray();
        + * for (int i = words.length; --i >= 0; )
              * h ^= words[i] * (i + 1);
        - * }
              * return (int)((h >> 32) ^ h);
              * }}</pre>
        - * Note that the hash code values change if the set of bits is altered.
        + * Note that the hash code changes if the set of bits is altered.
              *
        - * @return a hash code value for this bit set
        + * @return the hash code value for this bit set
              */
             public int hashCode() {
        BitSet.hashCode()'s slightly bizarre specification can be made more normal
        by using the new public toLongArray method.


             /**
        - * Returns a hash code value for this bit set. The hash code
        - * depends only on which bits have been set within this
        - * {@code BitSet}. The algorithm used to compute it may
        - * be described as follows.
        - *
        - * <p>Suppose the bits in the {@code BitSet} were to be stored
        - * in an array of {@code long} integers called, say,
        - * {@code words}, in such a manner that bit {@code k} is
        - * set in the {@code BitSet} (for nonnegative values of
        - * {@code k}) if and only if the expression
        - * <pre>{@code ((k>>6) < words.length) && ((words[k>>6] & (1L << (bit & 0x3F))) != 0)}</pre>
        - * is true. Then the following definition of the {@code hashCode}
        - * method would be a correct implementation of the actual algorithm:
        + * Returns the hash code value for this bit set. The hash code depends
        + * only on which bits are set within this {@code BitSet}.
        + *
        + * <p>The hash code is defined to be the result of the following
        + * calculation:
              * <pre> {@code
              * public int hashCode() {
              * long h = 1234;
        - * for (int i = words.length; --i >= 0; ) {
        + * long[] words = toLongArray();
        + * for (int i = words.length; --i >= 0; )
              * h ^= words[i] * (i + 1);
        - * }
              * return (int)((h >> 32) ^ h);
              * }}</pre>
        - * Note that the hash code values change if the set of bits is altered.
        + * Note that the hash code changes if the set of bits is altered.
              *
        - * @return a hash code value for this bit set
        + * @return the hash code value for this bit set
              */
             public int hashCode() {

              martin Martin Buchholz
              martin Martin Buchholz
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: