/** For testing */ final int getRecomputedCount() { int c = 0; int end = bits.length; for (int i = 0; i < end; i++) { c += BitUtil.bitCount(bits[i]); // sum bits per byte } return c; }
/** 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. */ final int count() { // if the vector has been modified if (count == -1) { int c = 0; int end = bits.length; for (int i = 0; i < end; i++) { c += BitUtil.bitCount(bits[i]); // sum bits per byte } count = c; } assert count <= size: "count=" + count + " size=" + size; return count; }
/** read as a d-gaps list */ private void readSetDgaps(IndexInput input) throws IOException { size = input.readInt(); // (re)read size count = input.readInt(); // read count bits = new byte[getNumBytes(size)]; // allocate bits int last=0; int n = count(); while (n>0) { last += input.readVInt(); bits[last] = input.readByte(); n -= BitUtil.bitCount(bits[last]); assert n >= 0; } }
/** read as a d-gaps cleared bits list */ private void readClearedDgaps(IndexInput input) throws IOException { size = input.readInt(); // (re)read size count = input.readInt(); // read count bits = new byte[getNumBytes(size)]; // allocate bits Arrays.fill(bits, (byte) 0xff); clearUnusedBits(); int last=0; int numCleared = size()-count(); while (numCleared>0) { last += input.readVInt(); bits[last] = input.readByte(); numCleared -= 8-BitUtil.bitCount(bits[last]); assert numCleared >= 0 || (last == (bits.length-1) && numCleared == -(8-(size&7))); } } }
/** Write as a d-gaps list */ private void writeClearedDgaps(IndexOutput output) throws IOException { output.writeInt(-1); // mark using d-gaps output.writeInt(size()); // write size output.writeInt(count()); // write count int last=0; int numCleared = size()-count(); for (int i=0; i<bits.length && numCleared>0; i++) { if (bits[i] != (byte) 0xff) { output.writeVInt(i-last); output.writeByte(bits[i]); last = i; numCleared -= (8-BitUtil.bitCount(bits[i])); assert numCleared >= 0 || (i == (bits.length-1) && numCleared == -(8-(size&7))); } } }