FileDocCategorySizeDatePackage
BitVector.javaAPI DocApache Lucene 1.4.34591Tue Mar 30 00:48:06 BST 2004org.apache.lucene.util

BitVector

public final class BitVector extends Object
Optimized implementation of a vector of bits. This is more-or-less like java.util.BitSet, but also includes the following:
  • a count() method, which efficiently computes the number of one bits;
  • optimized read from and write to disk;
  • inlinable get() method;
author
Doug Cutting
version
$Id: BitVector.java,v 1.4 2004/03/29 22:48:05 cutting Exp $

Fields Summary
private byte[]
bits
private int
size
private int
count
private static final byte[]
BYTE_COUNTS
Constructors Summary
public BitVector(int n)
Constructs a vector capable of holding n bits.


           
     
    size = n;
    bits = new byte[(size >> 3) + 1];
  
public BitVector(Directory d, String name)
Constructs a bit vector from the file name in Directory d, as written by the {@link #write} method.

    InputStream input = d.openFile(name);
    try {
      size = input.readInt();			  // read size
      count = input.readInt();			  // read count
      bits = new byte[(size >> 3) + 1];		  // allocate bits
      input.readBytes(bits, 0, bits.length);	  // read bits
    } finally {
      input.close();
    }
  
Methods Summary
public final voidclear(int bit)
Sets the value of bit to zero.

    bits[bit >> 3] &= ~(1 << (bit & 7));
    count = -1;
  
public final intcount()
Returns the total number of one bits in this vector. This is efficiently computed and cached, so that, if the vector is not changed, no recomputation is done for repeated calls.

    // if the vector has been modified
    if (count == -1) {
      int c = 0;
      int end = bits.length;
      for (int i = 0; i < end; i++)
        c += BYTE_COUNTS[bits[i] & 0xFF];	  // sum bits per byte
      count = c;
    }
    return count;
  
public final booleanget(int bit)
Returns true if bit is one and false if it is zero.

    return (bits[bit >> 3] & (1 << (bit & 7))) != 0;
  
public final voidset(int bit)
Sets the value of bit to one.

    bits[bit >> 3] |= 1 << (bit & 7);
    count = -1;
  
public final intsize()
Returns the number of bits in this vector. This is also one greater than the number of the largest valid bit number.

    return size;
  
public final voidwrite(org.apache.lucene.store.Directory d, java.lang.String name)
Writes this vector to the file name in Directory d, in a format that can be read by the constructor {@link #BitVector(Directory, String)}.



                                 
           
    OutputStream output = d.createFile(name);
    try {
      output.writeInt(size());			  // write size
      output.writeInt(count());			  // write count
      output.writeBytes(bits, bits.length);	  // write bits
    } finally {
      output.close();
    }