/// <summary> /// Write a block of data (<c>For</c> 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> internal void WriteBlock(int[] data, byte[] encoded, IndexOutput @out) { if (IsAllEqual(data)) { @out.WriteByte((byte)(sbyte)ALL_VALUES_EQUAL); @out.WriteVInt32(data[0]); return; } int numBits = BitsRequired(data); if (Debugging.AssertsEnabled) { Debugging.Assert(numBits > 0 && numBits <= 32, numBits.ToString); } PackedInt32s.IEncoder encoder = encoders[numBits]; int iters = iterations[numBits]; if (Debugging.AssertsEnabled) { Debugging.Assert(iters * encoder.ByteValueCount >= Lucene41PostingsFormat.BLOCK_SIZE); } int encodedSize = encodedSizes[numBits]; if (Debugging.AssertsEnabled) { Debugging.Assert(iters * encoder.ByteBlockCount >= encodedSize); } @out.WriteByte((byte)numBits); encoder.Encode(data, 0, encoded, 0, iters); @out.WriteBytes(encoded, encodedSize); }
public override int Set(int index, long[] arr, int off, int len) { Debug.Assert(len > 0, "len must be > 0 (got " + len + ")"); Debug.Assert(index >= 0 && index < m_valueCount); len = Math.Min(len, m_valueCount - index); Debug.Assert(off + len <= arr.Length); int originalIndex = index; PackedInt32s.IEncoder encoder = BulkOperation.Of(PackedInt32s.Format.PACKED, m_bitsPerValue); // go to the next block where the value does not span across two blocks int offsetInBlocks = index % encoder.Int64ValueCount; if (offsetInBlocks != 0) { for (int i = offsetInBlocks; i < encoder.Int64ValueCount && len > 0; ++i) { Set(index++, arr[off++]); --len; } if (len == 0) { return(index - originalIndex); } } // bulk set Debug.Assert(index % encoder.Int64ValueCount == 0); int blockIndex = (int)((ulong)((long)index * m_bitsPerValue) >> BLOCK_BITS); Debug.Assert((((long)index * m_bitsPerValue) & MOD_MASK) == 0); int iterations = len / encoder.Int64ValueCount; encoder.Encode(arr, off, blocks, blockIndex, iterations); int setValues = iterations * encoder.Int64ValueCount; index += setValues; len -= setValues; Debug.Assert(len >= 0); if (index > originalIndex) { // stay at the block boundary return(index - originalIndex); } else { // no progress so far => already at a block boundary but no full block to get Debug.Assert(index == originalIndex); return(base.Set(index, arr, off, len)); } }
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++] = buffer[i].TripleShift(bitsPerValue); buffer[i] &= mask; } } if (Debugging.AssertsEnabled) { Debugging.Assert(ex == numExceptions); } Arrays.Fill(exceptions, numExceptions, BLOCK_SIZE, 0); } if (bitsPerValue > 0) { PackedInt32s.IEncoder encoder = PackedInt32s.GetEncoder(PackedInt32s.Format.PACKED, PackedInt32s.VERSION_CURRENT, bitsPerValue); int numIterations = ITERATIONS[bitsPerValue]; encoder.Encode(buffer, 0, data.Bytes, data.Length, numIterations); data.Length += encoder.ByteBlockCount * numIterations; } if (numExceptions > 0) { if (Debugging.AssertsEnabled) { Debugging.Assert(bitsPerException > 0); } data.WriteByte((byte)numExceptions); data.WriteByte((byte)bitsPerException); PackedInt32s.IEncoder encoder = PackedInt32s.GetEncoder(PackedInt32s.Format.PACKED, PackedInt32s.VERSION_CURRENT, bitsPerException); int numIterations = (numExceptions + encoder.ByteValueCount - 1) / encoder.ByteValueCount; encoder.Encode(exceptions, 0, data.Bytes, data.Length, numIterations); data.Length += (int)PackedInt32s.Format.PACKED.ByteCount(PackedInt32s.VERSION_CURRENT, numExceptions, bitsPerException); for (int i = 0; i < numExceptions; ++i) { data.WriteByte((byte)exceptionIndices[i]); } } }
protected void WriteValues(int bitsRequired) { PackedInt32s.IEncoder encoder = PackedInt32s.GetEncoder(PackedInt32s.Format.PACKED, PackedInt32s.VERSION_CURRENT, bitsRequired); int iterations = m_values.Length / encoder.ByteValueCount; int blockSize = encoder.ByteBlockCount * iterations; if (m_blocks == null || m_blocks.Length < blockSize) { m_blocks = new byte[blockSize]; } if (m_off < m_values.Length) { Arrays.Fill(m_values, m_off, m_values.Length, 0L); } encoder.Encode(m_values, 0, m_blocks, 0, iterations); int blockCount = (int)PackedInt32s.Format.PACKED.ByteCount(PackedInt32s.VERSION_CURRENT, m_off, bitsRequired); m_out.WriteBytes(m_blocks, blockCount); }