From 1d9259c7e73adac94b0c2978ae30011ebbe154a7 Mon Sep 17 00:00:00 2001 From: wissaaamm <172216098+wissaaamm@users.noreply.github.com> Date: Sun, 29 Nov 2026 15:21:21 +0200 Subject: [PATCH] Fix LongAs2IntsCodec decoding of highly compressed blocks --- .../longcompression/LongAs2IntsCodec.java | 4 +-- .../longcompression/TestLongAs2IntsCodec.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/lemire/longcompression/LongAs2IntsCodec.java b/src/main/java/me/lemire/longcompression/LongAs2IntsCodec.java index 35c1166..0510b56 100644 --- a/src/main/java/me/lemire/longcompression/LongAs2IntsCodec.java +++ b/src/main/java/me/lemire/longcompression/LongAs2IntsCodec.java @@ -134,8 +134,8 @@ public void uncompress(long[] in, IntWrapper inpos, int inlength, long[] out, In highPart = !highPart; } - // TODO What would be a relevant buffer size? - int[] buffer = new int[inlength * 16]; + // A packed block can expand beyond a fixed multiple of its compressed size. + int[] buffer = new int[out.length - outpos.get()]; IntWrapper highPartsOutPosition = new IntWrapper(); highPartsCodec.uncompress(compressedHighParts, diff --git a/src/test/java/me/lemire/longcompression/TestLongAs2IntsCodec.java b/src/test/java/me/lemire/longcompression/TestLongAs2IntsCodec.java index bddff2a..00dc452 100644 --- a/src/test/java/me/lemire/longcompression/TestLongAs2IntsCodec.java +++ b/src/test/java/me/lemire/longcompression/TestLongAs2IntsCodec.java @@ -7,6 +7,14 @@ package me.lemire.longcompression; +import java.util.Arrays; + +import me.lemire.integercompression.BinaryPacking; +import me.lemire.integercompression.Composition; +import me.lemire.integercompression.IntWrapper; +import me.lemire.integercompression.IntegerCODEC; +import me.lemire.integercompression.VariableByte; + import org.junit.Assert; import org.junit.Test; @@ -28,4 +36,26 @@ public void testCodec_intermediateHighPowerOfTwo() { Assert.assertEquals(3, LongTestUtils.compress((LongCODEC) codec, new long[] { 1L << 42 }).length); } + @Test + public void testHighlyCompressedRoundTrip() { + IntegerCODEC packing = new Composition(new BinaryPacking(), new VariableByte()); + LongCODEC packedCodec = new LongAs2IntsCodec(packing, packing); + long[] original = new long[1024]; + long[] compressed = new long[original.length]; + IntWrapper compressedEnd = new IntWrapper(2); + packedCodec.compress(original, new IntWrapper(), original.length, compressed, compressedEnd); + + long[] decoded = new long[original.length + 2]; + Arrays.fill(decoded, -1); + IntWrapper compressedStart = new IntWrapper(2); + IntWrapper decodedEnd = new IntWrapper(2); + packedCodec.uncompress(compressed, compressedStart, compressedEnd.get() - 2, decoded, decodedEnd); + + Assert.assertEquals(compressedEnd.get(), compressedStart.get()); + Assert.assertEquals(decoded.length, decodedEnd.get()); + Assert.assertEquals(-1, decoded[0]); + Assert.assertEquals(-1, decoded[1]); + Assert.assertArrayEquals(original, Arrays.copyOfRange(decoded, 2, decoded.length)); + } + }