lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r900030 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/util/OpenBitSet.java src/test/org/apache/lucene/util/TestOpenBitSet.java
Date Sat, 16 Jan 2010 22:13:03 GMT
Author: yonik
Date: Sat Jan 16 22:13:02 2010
New Revision: 900030

URL: http://svn.apache.org/viewvc?rev=900030&view=rev
Log:
LUCENE-2216: disregard trailing zeros in OpenBitSet.hashCode

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/util/OpenBitSet.java
    lucene/java/trunk/src/test/org/apache/lucene/util/TestOpenBitSet.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=900030&r1=900029&r2=900030&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Sat Jan 16 22:13:02 2010
@@ -94,6 +94,9 @@
 * LUCENE-2182: DEFAULT_ATTRIBUTE_FACTORY was failing to load
   implementation class when interface was loaded by a different
   class 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 features
 

Modified: lucene/java/trunk/src/java/org/apache/lucene/util/OpenBitSet.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/util/OpenBitSet.java?rev=900030&r1=900029&r2=900030&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/util/OpenBitSet.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/util/OpenBitSet.java Sat Jan 16 22:13:02
2010
@@ -802,12 +802,16 @@
 
   @Override
   public 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;
   }
 
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/util/TestOpenBitSet.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/util/TestOpenBitSet.java?rev=900030&r1=900029&r2=900030&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/util/TestOpenBitSet.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/util/TestOpenBitSet.java Sat Jan 16 22:13:02
2010
@@ -230,7 +230,14 @@
     }
   }
 
-  
+  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());
+  } 
 }
 
 



Mime
View raw message