You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2013/10/13 10:21:33 UTC

svn commit: r1531627 - in /lucene/dev/trunk/lucene: CHANGES.txt core/src/java/org/apache/lucene/util/FixedBitSet.java core/src/test/org/apache/lucene/util/TestFixedBitSet.java

Author: shaie
Date: Sun Oct 13 08:21:32 2013
New Revision: 1531627

URL: http://svn.apache.org/r1531627
Log:
LUCENE-5277: Modify FixedBitSet copy constructor to take numBits to allow grow/shrink the new bitset

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1531627&r1=1531626&r2=1531627&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Sun Oct 13 08:21:32 2013
@@ -146,6 +146,10 @@ API Changes:
 * LUCENE-5275: Change AttributeSource.toString() to display the current
   state of attributes. (Robert Muir)
 
+* LUCENE-5277: Modify FixedBitSet copy constructor to take an additional
+  numBits parameter to allow growing/shrinking the copied bitset. You can
+  use FixedBitSet.clone() if you only need to clone the bitset. (Shai Erera)
+
 Optimizations
 
 * LUCENE-5225: The ToParentBlockJoinQuery only keeps tracks of the the child

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java?rev=1531627&r1=1531626&r2=1531627&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java Sun Oct 13 08:21:32 2013
@@ -35,7 +35,6 @@ import org.apache.lucene.search.DocIdSet
  *
  * @lucene.internal
  **/
-
 public final class FixedBitSet extends DocIdSet implements Bits {
   private final long[] bits;
   private final int numBits;
@@ -63,14 +62,18 @@ public final class FixedBitSet extends D
     }
     this.numBits = numBits;
     this.bits = storedBits;
-  }      
+  }
   
-  /** Makes full copy. */
-  public FixedBitSet(FixedBitSet other) {
-    bits = new long[other.wordLength];
-    System.arraycopy(other.bits, 0, bits, 0, other.wordLength);
-    numBits = other.numBits;
-    wordLength = other.wordLength;
+  /**
+   * Makes a full copy of the bits, while allowing to expand/shrink the bitset.
+   * If {@code numBits < other.numBits}, then only the first {@code numBits}
+   * are copied from other.
+   */
+  public FixedBitSet(FixedBitSet other, int numBits) {
+    wordLength = bits2words(numBits);
+    bits = new long[wordLength];
+    System.arraycopy(other.bits, 0, bits, 0, Math.min(other.wordLength, wordLength));
+    this.numBits = numBits;
   }
 
   @Override
@@ -403,7 +406,7 @@ public final class FixedBitSet extends D
 
   @Override
   public FixedBitSet clone() {
-    return new FixedBitSet(this);
+    return new FixedBitSet(this, numBits);
   }
 
   /** returns true if both sets have the same bits set */

Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java?rev=1531627&r1=1531626&r2=1531627&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java Sun Oct 13 08:21:32 2013
@@ -328,7 +328,50 @@ public class TestFixedBitSet extends Bas
     
     checkNextSetBitArray(new int[0], setBits.length + random().nextInt(10));
   }
-}
-
+  
+  public void testGrow() {
+    FixedBitSet bits = new FixedBitSet(5);
+    bits.set(1);
+    bits.set(4);
+    
+    FixedBitSet newBits = new FixedBitSet(bits, 8); // grow within the word
+    assertTrue(newBits.get(1));
+    assertTrue(newBits.get(4));
 
+    newBits = new FixedBitSet(bits, 72); // grow beyond one word
+    assertTrue(newBits.get(1));
+    assertTrue(newBits.get(4));
+  }
+  
+  public void testShrink() {
+    FixedBitSet bits = new FixedBitSet(72);
+    bits.set(1);
+    bits.set(4);
+    bits.set(69);
+    
+    FixedBitSet newBits = new FixedBitSet(bits, 66); // shrink within the word
+    assertTrue(newBits.get(1));
+    assertTrue(newBits.get(4));
+    boolean hitError = true;
+    try {
+      newBits.get(69);
+      hitError = false;
+    } catch (AssertionError e) {
+      hitError = true;
+    }
+    assertTrue(hitError);
 
+    newBits = new FixedBitSet(bits, 8); // shrink beyond one word
+    assertTrue(newBits.get(1));
+    assertTrue(newBits.get(4));
+    hitError = true;
+    try {
+      newBits.get(69);
+      hitError = false;
+    } catch (AssertionError e) {
+      hitError = true;
+    }
+    assertTrue(hitError);
+  }
+  
+}