/** Collect documents from a bitset. */
  private List<Document> collectDocuments(List<Document> l, BitSet bitset) {
    if (l == null) {
      l = Lists.newArrayListWithCapacity((int) bitset.cardinality());
    }

    final BitSetIterator i = bitset.iterator();
    for (int d = i.nextSetBit(); d >= 0; d = i.nextSetBit()) {
      l.add(documents.get(d));
    }
    return l;
  }
  @Override
  public int encodedSize() {
    /*
     * The number of bytes goes as follows:
     *
     * you need at least as many bits as the highest 1-bit in the bitSet(equivalent
     *  to bitSet.length()). Because each set bit will have an additional 2-bit "type delimiter"
     *  set afterwords, we need to have 3 bits for every set bit, but 1 for every non-set bit
     *
     * This is equivalent to length()+2*numSetBits().
     *
     * we have 4 available bits in the header, and 7 bits in each subsequent byte (we use a continuation
     * bit).
     */
    int numBits = (int) (bitSet.length() + 2 * bitSet.cardinality());
    int numBytes = 1;
    numBits -= 4;
    if (numBits > 0) {
      numBytes += numBits / 7;
      if (numBits % 7 != 0) numBytes++;
    }

    return numBytes;
  }
 @Override
 public int cardinality() {
   return (int) bitSet.cardinality();
 }