From b2f347c92d518f180f88644e576dc9502ef37422 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Fri, 11 Apr 2014 14:56:29 +0000 Subject: [PATCH] Extract the zig-zag encoding/decoding routines we have to utility methods. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1586669 13f79535-47bb-0310-9956-ffa450edef68 --- .../CompressingStoredFieldsIndexReader.java | 10 +++----- .../CompressingStoredFieldsIndexWriter.java | 18 ++++++------- .../java/org/apache/lucene/util/BitUtil.java | 25 +++++++++++++++++++ .../packed/AbstractBlockPackedWriter.java | 4 --- .../lucene/util/packed/BlockPackedReader.java | 2 +- .../packed/BlockPackedReaderIterator.java | 5 +--- .../lucene/util/packed/BlockPackedWriter.java | 2 ++ .../packed/MonotonicBlockPackedReader.java | 2 +- .../packed/MonotonicBlockPackedWriter.java | 2 ++ 9 files changed, 44 insertions(+), 26 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.java index 819d8f6e08f..4f9a51ab3bb 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.java @@ -17,6 +17,8 @@ package org.apache.lucene.codecs.compressing; * limitations under the License. */ +import static org.apache.lucene.util.BitUtil.zigZagDecode; + import java.io.IOException; import java.util.Arrays; @@ -33,10 +35,6 @@ import org.apache.lucene.util.packed.PackedInts; */ public final class CompressingStoredFieldsIndexReader implements Cloneable { - static long moveLowOrderBitToSign(long n) { - return ((n >>> 1) ^ -(n & 1)); - } - final int maxDoc; final int[] docBases; final long[] startPointers; @@ -122,13 +120,13 @@ public final class CompressingStoredFieldsIndexReader implements Cloneable { private int relativeDocBase(int block, int relativeChunk) { final int expected = avgChunkDocs[block] * relativeChunk; - final long delta = moveLowOrderBitToSign(docBasesDeltas[block].get(relativeChunk)); + final long delta = zigZagDecode(docBasesDeltas[block].get(relativeChunk)); return expected + (int) delta; } private long relativeStartPointer(int block, int relativeChunk) { final long expected = avgChunkSizes[block] * relativeChunk; - final long delta = moveLowOrderBitToSign(startPointersDeltas[block].get(relativeChunk)); + final long delta = zigZagDecode(startPointersDeltas[block].get(relativeChunk)); return expected + delta; } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexWriter.java index ea82e15e9ad..74d5658fdeb 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexWriter.java @@ -17,6 +17,8 @@ package org.apache.lucene.codecs.compressing; * limitations under the License. */ +import static org.apache.lucene.util.BitUtil.zigZagEncode; + import java.io.Closeable; import java.io.IOException; @@ -72,10 +74,6 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable { static final int BLOCK_SIZE = 1024; // number of chunks to serialize at once - static long moveSignToLowOrderBit(long n) { - return (n >> 63) ^ (n << 1); - } - final IndexOutput fieldsIndexOut; int totalDocs; int blockDocs; @@ -124,7 +122,7 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable { long maxDelta = 0; for (int i = 0; i < blockChunks; ++i) { final int delta = docBase - avgChunkDocs * i; - maxDelta |= moveSignToLowOrderBit(delta); + maxDelta |= zigZagEncode(delta); docBase += docBaseDeltas[i]; } @@ -135,8 +133,8 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable { docBase = 0; for (int i = 0; i < blockChunks; ++i) { final long delta = docBase - avgChunkDocs * i; - assert PackedInts.bitsRequired(moveSignToLowOrderBit(delta)) <= writer.bitsPerValue(); - writer.add(moveSignToLowOrderBit(delta)); + assert PackedInts.bitsRequired(zigZagEncode(delta)) <= writer.bitsPerValue(); + writer.add(zigZagEncode(delta)); docBase += docBaseDeltas[i]; } writer.finish(); @@ -155,7 +153,7 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable { for (int i = 0; i < blockChunks; ++i) { startPointer += startPointerDeltas[i]; final long delta = startPointer - avgChunkSize * i; - maxDelta |= moveSignToLowOrderBit(delta); + maxDelta |= zigZagEncode(delta); } final int bitsPerStartPointer = PackedInts.bitsRequired(maxDelta); @@ -166,8 +164,8 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable { for (int i = 0; i < blockChunks; ++i) { startPointer += startPointerDeltas[i]; final long delta = startPointer - avgChunkSize * i; - assert PackedInts.bitsRequired(moveSignToLowOrderBit(delta)) <= writer.bitsPerValue(); - writer.add(moveSignToLowOrderBit(delta)); + assert PackedInts.bitsRequired(zigZagEncode(delta)) <= writer.bitsPerValue(); + writer.add(zigZagEncode(delta)); } writer.finish(); } diff --git a/lucene/core/src/java/org/apache/lucene/util/BitUtil.java b/lucene/core/src/java/org/apache/lucene/util/BitUtil.java index 3cb8bf4285a..bd2ffe7b5cb 100644 --- a/lucene/core/src/java/org/apache/lucene/util/BitUtil.java +++ b/lucene/core/src/java/org/apache/lucene/util/BitUtil.java @@ -187,4 +187,29 @@ public final class BitUtil { return v; } + /** Same as {@link #zigZagEncode(long)} but on integers. */ + public static int zigZagEncode(int i) { + return (i >> 31) ^ (i << 1); + } + + /** + * Zig-zag + * encode the provided long. Assuming the input is a signed long whose + * absolute value can be stored on n bits, the returned value will + * be an unsigned long that can be stored on n+1 bits. + */ + public static long zigZagEncode(long l) { + return (l >> 63) ^ (l << 1); + } + + /** Decode an int previously encoded with {@link #zigZagEncode(int)}. */ + public static int zigZagDecode(int i) { + return ((i >>> 1) ^ -(i & 1)); + } + + /** Decode a long previously encoded with {@link #zigZagEncode(long)}. */ + public static long zigZagDecode(long l) { + return ((l >>> 1) ^ -(l & 1)); + } + } diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java b/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java index 67c8d4bf6b1..8fc9a161a93 100644 --- a/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java +++ b/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java @@ -31,10 +31,6 @@ abstract class AbstractBlockPackedWriter { static final int MIN_VALUE_EQUALS_0 = 1 << 0; static final int BPV_SHIFT = 1; - static long zigZagEncode(long n) { - return (n >> 63) ^ (n << 1); - } - // same as DataOutput.writeVLong but accepts negative values static void writeVLong(DataOutput out, long i) throws IOException { int k = 0; diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.java b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.java index 32dfc60c8cc..d6c670f06bd 100644 --- a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.java +++ b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.java @@ -17,12 +17,12 @@ package org.apache.lucene.util.packed; * limitations under the License. */ +import static org.apache.lucene.util.BitUtil.zigZagDecode; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.BPV_SHIFT; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_VALUE_EQUALS_0; import static org.apache.lucene.util.packed.BlockPackedReaderIterator.readVLong; -import static org.apache.lucene.util.packed.BlockPackedReaderIterator.zigZagDecode; import static org.apache.lucene.util.packed.PackedInts.checkBlockSize; import static org.apache.lucene.util.packed.PackedInts.numBlocks; diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReaderIterator.java b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReaderIterator.java index eec6314dd26..2a7af3f26e2 100644 --- a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReaderIterator.java +++ b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReaderIterator.java @@ -17,6 +17,7 @@ package org.apache.lucene.util.packed; * limitations under the License. */ +import static org.apache.lucene.util.BitUtil.zigZagDecode; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.BPV_SHIFT; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE; @@ -38,10 +39,6 @@ import org.apache.lucene.util.LongsRef; */ public final class BlockPackedReaderIterator { - static long zigZagDecode(long n) { - return ((n >>> 1) ^ -(n & 1)); - } - // same as DataInput.readVLong but supports negative values static long readVLong(DataInput in) throws IOException { byte b = in.readByte(); diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedWriter.java b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedWriter.java index 6c39b87f98c..b65a70c4e40 100644 --- a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedWriter.java +++ b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedWriter.java @@ -17,6 +17,8 @@ package org.apache.lucene.util.packed; * limitations under the License. */ +import static org.apache.lucene.util.BitUtil.zigZagEncode; + import java.io.IOException; import org.apache.lucene.store.DataOutput; diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java index 9915c45dffd..fd76e4d5096 100644 --- a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java +++ b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java @@ -17,9 +17,9 @@ package org.apache.lucene.util.packed; * limitations under the License. */ +import static org.apache.lucene.util.BitUtil.zigZagDecode; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE; import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE; -import static org.apache.lucene.util.packed.BlockPackedReaderIterator.zigZagDecode; import static org.apache.lucene.util.packed.PackedInts.checkBlockSize; import static org.apache.lucene.util.packed.PackedInts.numBlocks; diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.java b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.java index a56d76fc19d..a875be9cc1b 100644 --- a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.java +++ b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.java @@ -17,6 +17,8 @@ package org.apache.lucene.util.packed; * limitations under the License. */ +import static org.apache.lucene.util.BitUtil.zigZagEncode; + import java.io.IOException; import org.apache.lucene.store.DataOutput;