Apache Lucene open-source search software
LUCENE-2216: disregard trailing zeros in OpenBitSet.hashCode
Yonik Seeley83b144f362f5
diff --git a/CHANGES.txt b/CHANGES.txtindex a7994487969..93fd4d386a7 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -94,6 +94,9 @@ Bug fixes* LUCENE-2182: DEFAULT_ATTRIBUTE_FACTORY was failing to loadimplementation class when interface was loaded by a differentclass loader. (Uwe Schindler, reported on java-user by Ahmed El-dawy)++* LUCENE-2216: OpenBitSet.hashCode returned different hash codes for+ sets that only differed by trailing zeros. (Dawid Weiss, yonik)New featuresdiff --git a/src/java/org/apache/lucene/util/OpenBitSet.java b/src/java/org/apache/lucene/util/OpenBitSet.javaindex 1300bdf820d..ebead411533 100644--- a/src/java/org/apache/lucene/util/OpenBitSet.java+++ b/src/java/org/apache/lucene/util/OpenBitSet.java@@ -802,12 +802,16 @@ public class OpenBitSet extends DocIdSet implements Cloneable, Serializable {@Overridepublic int hashCode() {- long h = 0x98761234; // something non-zero for length==0- for (int i = bits.length; --i>=0;) {+ // Start with a zero hash and use a mix that results in zero if the input is zero.+ // This effectively truncates trailing zeros without an explicit check.+ long h = 0;+ for (int i = bits.length; --i>=0;) {h ^= bits[i];h = (h << 1) | (h >>> 63); // rotate left}- return (int)((h>>32) ^ h); // fold leftmost bits into right+ // fold leftmost bits into right and add a constant to prevent+ // empty sets from returning 0, which is too common.+ return (int)((h>>32) ^ h) + 0x98761234;}}diff --git a/src/test/org/apache/lucene/util/TestOpenBitSet.java b/src/test/org/apache/lucene/util/TestOpenBitSet.javaindex 67551c8f6e0..d556de89751 100644--- a/src/test/org/apache/lucene/util/TestOpenBitSet.java+++ b/src/test/org/apache/lucene/util/TestOpenBitSet.java@@ -230,7 +230,14 @@ public class TestOpenBitSet extends LuceneTestCase {}}-+ public void testHashCodeEquals() {+ OpenBitSet bs1 = new OpenBitSet(200);+ OpenBitSet bs2 = new OpenBitSet(64);+ bs1.set(3);+ bs2.set(3);+ assertEquals(bs1, bs2);+ assertEquals(bs1.hashCode(), bs2.hashCode());+ }}