/// <summary> /// For testing </summary> public 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); }
/// <summary> /// Read as a d-gaps list. </summary> private void ReadSetDgaps(IndexInput input) { size = input.ReadInt32(); // (re)read size count = input.ReadInt32(); // read count bits = new byte[GetNumBytes(size)]; // allocate bits int last = 0; int n = Count(); while (n > 0) { last += input.ReadVInt32(); bits[last] = input.ReadByte(); n -= BitUtil.BitCount(bits[last]); Debug.Assert(n >= 0); } }
/// <summary> /// 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. /// </summary> public 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; } Debug.Assert(count <= size, "count=" + count + " size=" + size); return(count); }
/// <summary> /// 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. /// </summary> public int Count() // LUCENENET TODO: API - make into a property { // 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; } if (Debugging.AssertsEnabled) { Debugging.Assert(count <= size, "count={0} size={1}", count, size); } return(count); }
/// <summary> /// Write as a d-gaps list. </summary> private void WriteClearedDgaps(IndexOutput output) { output.WriteInt32(-1); // mark using d-gaps output.WriteInt32(Length); // write size output.WriteInt32(Count()); // write count int last = 0; int numCleared = Length - Count(); for (int i = 0; i < bits.Length && numCleared > 0; i++) { if (bits[i] != 0xff) { output.WriteVInt32(i - last); output.WriteByte(bits[i]); last = i; numCleared -= (8 - BitUtil.BitCount(bits[i])); Debug.Assert(numCleared >= 0 || (i == (bits.Length - 1) && numCleared == -(8 - (size & 7)))); } } }
/// <summary> /// Read as a d-gaps cleared bits list. </summary> private void ReadClearedDgaps(IndexInput input) { size = input.ReadInt32(); // (re)read size count = input.ReadInt32(); // read count bits = new byte[GetNumBytes(size)]; // allocate bits for (int i = 0; i < bits.Length; ++i) { bits[i] = 0xff; } ClearUnusedBits(); int last = 0; int numCleared = Length - Count(); while (numCleared > 0) { last += input.ReadVInt32(); bits[last] = input.ReadByte(); numCleared -= 8 - BitUtil.BitCount(bits[last]); Debug.Assert(numCleared >= 0 || (last == (bits.Length - 1) && numCleared == -(8 - (size & 7)))); } }