/// <summary> /// read as a d-gaps list </summary> private void ReadSetDgaps(IndexInput input) { Size_Renamed = input.ReadInt(); // (re)read size Count_Renamed = input.ReadInt(); // read count Bits = new byte[GetNumBytes(Size_Renamed)]; // allocate bits int last = 0; int n = Count(); while (n > 0) { last += input.ReadVInt(); 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_Renamed == -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_Renamed = c; } Debug.Assert(Count_Renamed <= Size_Renamed, "count=" + Count_Renamed + " size=" + Size_Renamed); return(Count_Renamed); }
/// <summary> /// Write as a d-gaps list </summary> private void WriteClearedDgaps(IndexOutput output) { 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] != unchecked ((byte)0xff)) { output.WriteVInt(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_Renamed & 7)))); } } }
/// <summary> /// read as a d-gaps cleared bits list </summary> private void ReadClearedDgaps(IndexInput input) { Size_Renamed = input.ReadInt(); // (re)read size Count_Renamed = input.ReadInt(); // read count Bits = new byte[GetNumBytes(Size_Renamed)]; // allocate bits for (int i = 0; i < Bits.Length; ++i) { Bits[i] = 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]); Debug.Assert(numCleared >= 0 || (last == (Bits.Length - 1) && numCleared == -(8 - (Size_Renamed & 7)))); } }
internal virtual void AddWord(int wordNum, byte word) { Debug.Assert(wordNum > lastWordNum); Debug.Assert(word != 0); if (!reverse) { if (lastWordNum == -1) { clean = 2 + wordNum; // special case for the 1st sequence dirtyWords.WriteByte(word); } else { switch (wordNum - lastWordNum) { case 1: if (word == 0xFF && (byte)dirtyWords.Bytes[dirtyWords.Length - 1] == 0xFF) { --dirtyWords.Length; WriteSequence(); reverse = true; clean = 2; } else { dirtyWords.WriteByte(word); } break; case 2: dirtyWords.WriteByte(0); dirtyWords.WriteByte(word); break; default: WriteSequence(); clean = wordNum - lastWordNum - 1; dirtyWords.WriteByte(word); break; } } } else { Debug.Assert(lastWordNum >= 0); switch (wordNum - lastWordNum) { case 1: if (word == 0xFF) { if (dirtyWords.Length == 0) { ++clean; } else if ((byte)dirtyWords.Bytes[dirtyWords.Length - 1] == 0xFF) { --dirtyWords.Length; WriteSequence(); clean = 2; } else { dirtyWords.WriteByte(word); } } else { dirtyWords.WriteByte(word); } break; case 2: dirtyWords.WriteByte(0); dirtyWords.WriteByte(word); break; default: WriteSequence(); reverse = false; clean = wordNum - lastWordNum - 1; dirtyWords.WriteByte(word); break; } } lastWordNum = wordNum; cardinality += BitUtil.BitCount(word); }