/// <summary> /// Write a block of data (<code>For</code> format). /// </summary> /// <param name="data"> the data to write </param> /// <param name="encoded"> a buffer to use to encode data </param> /// <param name="out"> the destination output </param> /// <exception cref="IOException"> If there is a low-level I/O error </exception> public void WriteBlock(int[] data, byte[] encoded, IndexOutput @out) { if (IsAllEqual(data)) { @out.WriteByte((byte)(sbyte)ALL_VALUES_EQUAL); @out.WriteVInt(data[0]); return; } int numBits = BitsRequired(data); Debug.Assert(numBits > 0 && numBits <= 32, numBits.ToString()); PackedInts.Encoder encoder = Encoders[numBits]; int iters = Iterations[numBits]; Debug.Assert(iters * encoder.ByteValueCount() >= Lucene41PostingsFormat.BLOCK_SIZE); int encodedSize = EncodedSizes[numBits]; Debug.Assert(iters * encoder.ByteBlockCount() >= encodedSize); @out.WriteByte((byte)numBits); encoder.Encode(data, 0, encoded, 0, iters); @out.WriteBytes(encoded, encodedSize); }
protected internal void WriteValues(int bitsRequired) { PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, bitsRequired); int iterations = Values.Length / encoder.ByteValueCount(); int blockSize = encoder.ByteBlockCount() * iterations; if (Blocks == null || Blocks.Length < blockSize) { Blocks = new byte[blockSize]; } if (Off < Values.Length) { Arrays.Fill(Values, Off, Values.Length, 0L); } encoder.Encode(Values, 0, Blocks, 0, iterations); int blockCount = (int)PackedInts.Format.PACKED.ByteCount(PackedInts.VERSION_CURRENT, Off, bitsRequired); @out.WriteBytes(Blocks, blockCount); }
internal virtual void PforEncode() { if (NumExceptions > 0) { int mask = (1 << BitsPerValue) - 1; int ex = 0; for (int i = 0; i < BufferSize; ++i) { if (Buffer[i] > mask) { ExceptionIndices[ex] = i; Exceptions[ex++] = (int)((uint)Buffer[i] >> BitsPerValue); Buffer[i] &= mask; } } Debug.Assert(ex == NumExceptions); Arrays.Fill(Exceptions, NumExceptions, BLOCK_SIZE, 0); } if (BitsPerValue > 0) { PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, BitsPerValue); int numIterations = ITERATIONS[BitsPerValue]; encoder.Encode(Buffer, 0, Data.Bytes, Data.Length, numIterations); Data.Length += encoder.ByteBlockCount() * numIterations; } if (NumExceptions > 0) { Debug.Assert(BitsPerException > 0); Data.WriteByte((sbyte)NumExceptions); Data.WriteByte((sbyte)BitsPerException); PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, BitsPerException); int numIterations = (NumExceptions + encoder.ByteValueCount() - 1) / encoder.ByteValueCount(); encoder.Encode(Exceptions, 0, Data.Bytes, Data.Length, numIterations); Data.Length += (int)PackedInts.Format.PACKED.ByteCount(PackedInts.VERSION_CURRENT, NumExceptions, BitsPerException); for (int i = 0; i < NumExceptions; ++i) { Data.WriteByte((sbyte)ExceptionIndices[i]); } } }