Skip to content

Commit 9492f7d

Browse files
committed
Fix for fast-pack#41
1 parent 2f28fc1 commit 9492f7d

5 files changed

Lines changed: 100 additions & 5 deletions

File tree

src/main/java/me/lemire/integercompression/SkippableComposition.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public SkippableComposition(SkippableIntegerCODEC f1,
3838
public void headlessCompress(int[] in, IntWrapper inpos, int inlength, int[] out,
3939
IntWrapper outpos) {
4040
int init = inpos.get();
41+
int outposInit = outpos.get();
4142
F1.headlessCompress(in, inpos, inlength, out, outpos);
43+
if (outpos.get() == outposInit) {
44+
out[outposInit] = 0;
45+
outpos.increment();
46+
}
4247
inlength -= inpos.get() - init;
4348
F2.headlessCompress(in, inpos, inlength, out, outpos);
4449
}
@@ -48,6 +53,9 @@ public void headlessUncompress(int[] in, IntWrapper inpos, int inlength, int[] o
4853
IntWrapper outpos, int num) {
4954
int init = inpos.get();
5055
F1.headlessUncompress(in, inpos, inlength, out, outpos, num);
56+
if (inpos.get() == init) {
57+
inpos.increment();
58+
}
5159
inlength -= inpos.get() - init;
5260
num -= outpos.get();
5361
F2.headlessUncompress(in, inpos, inlength, out, outpos, num);

src/main/java/me/lemire/integercompression/differential/IntegratedBinaryPacking.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package me.lemire.integercompression.differential;
99

10+
import me.lemire.integercompression.BitPacking;
1011
import me.lemire.integercompression.IntWrapper;
1112
import me.lemire.integercompression.Util;
1213

@@ -83,6 +84,7 @@ public void headlessCompress(int[] in, IntWrapper inpos, int inlength,
8384
if (inlength == 0)
8485
return;
8586
int tmpoutpos = outpos.get();
87+
8688
int initoffset = initvalue.get();
8789
initvalue.set(in[inpos.get()+inlength -1]);
8890
int s = inpos.get();

src/main/java/me/lemire/integercompression/differential/IntegratedVariableByte.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,22 @@ public void headlessUncompress(int[] in, IntWrapper inpos, int inlength,
229229
int[] out, IntWrapper outpos, int num, IntWrapper initvalue) {
230230
int s = 0;
231231
int val = 0;
232+
232233
int p = inpos.get();
233234
int initoffset = initvalue.get();
234235
int tmpoutpos = outpos.get();
235236
int finaloutpos = num + tmpoutpos;
236237
for (int v = 0, shift = 0; tmpoutpos < finaloutpos;) {
238+
237239
val = in[p];
238-
int c = val >>> s;
240+
int c = (byte) (val >>> s);
239241
s += 8;
240242
p += s>>5;
241243
s = s & 31;
242244
v += ((c & 127) << shift);
245+
243246
if ((c & 128) == 128) {
247+
244248
out[tmpoutpos++] = (initoffset = initoffset + v);
245249
v = 0;
246250
shift = 0;

src/main/java/me/lemire/integercompression/differential/SkippableIntegratedComposition.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ public void headlessCompress(int[] in, IntWrapper inpos, int inlength,
4949
if (inlength == 0)
5050
return;
5151
final int init = inpos.get();
52+
int outposInit = outpos.get();
53+
5254
F1.headlessCompress(in, inpos, inlength, out, outpos, initvalue);
53-
if (outpos.get() == 0) {
54-
out[0] = 0;
55+
if (outpos.get() == outposInit) {
56+
out[outposInit] = 0;
5557
outpos.increment();
5658
}
5759
inlength -= inpos.get() - init;
@@ -65,7 +67,11 @@ public void headlessUncompress(int[] in, IntWrapper inpos, int inlength,
6567
return;
6668
int init = inpos.get();
6769
F1.headlessUncompress(in, inpos, inlength, out, outpos,num,initvalue);
70+
if (inpos.get() == init) {
71+
inpos.increment();
72+
}
6873
inlength -= inpos.get() - init;
74+
6975
num -= outpos.get();
7076
F2.headlessUncompress(in, inpos, inlength, out, outpos,num,initvalue);
7177
}

src/test/java/me/lemire/integercompression/AdhocTest.java

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,90 @@
11
package me.lemire.integercompression;
22

3+
import org.junit.Assert;
34
import org.junit.Test;
5+
6+
import me.lemire.integercompression.differential.*;
7+
48
import static me.lemire.integercompression.TestUtils.*;
59

10+
import java.util.Arrays;
11+
612
/**
713
* Collection of adhoc tests.
814
*/
915
@SuppressWarnings({ "static-method" })
10-
public class AdhocTest
11-
{
16+
public class AdhocTest {
17+
18+
19+
/**
20+
*
21+
*/
22+
@Test
23+
public void testIssue29() {
24+
for(int x = 0; x < 64; x++) {
25+
int[] a = {2, 3, 4, 5};
26+
int[] b = new int[90];
27+
int[] c = new int[a.length];
28+
IntegerCODEC codec = new Composition(new BinaryPacking(), new VariableByte());
29+
30+
IntWrapper aOffset = new IntWrapper(0);
31+
IntWrapper bOffset = new IntWrapper(x);
32+
codec.compress(a, aOffset, a.length, b, bOffset);
33+
int len = bOffset.get() - x;
34+
bOffset.set(x);
35+
IntWrapper cOffset = new IntWrapper(0);
36+
codec.uncompress(b, bOffset, len, c, cOffset);
37+
Assert.assertArrayEquals(a,c);
38+
}
39+
}
40+
41+
/**
42+
*
43+
*/
44+
@Test
45+
public void testIssue29b() {
46+
for(int x = 0; x < 64; x++) {
47+
int[] a = {2, 3, 4, 5};
48+
int[] b = new int[90];
49+
int[] c = new int[a.length];
50+
SkippableIntegerCODEC codec = new SkippableComposition(new BinaryPacking(), new VariableByte());
51+
IntWrapper aOffset = new IntWrapper(0);
52+
IntWrapper bOffset = new IntWrapper(x);
53+
codec.headlessCompress(a, aOffset, a.length, b, bOffset);
54+
int len = bOffset.get() - x;
55+
bOffset.set(x);
56+
IntWrapper cOffset = new IntWrapper(0);
57+
codec.headlessUncompress(b, bOffset, len, c, cOffset, a.length);
58+
Assert.assertArrayEquals(a,c);
59+
}
60+
}
61+
1262

63+
/**
64+
*
65+
*/
66+
@Test
67+
public void testIssue41() {
68+
for (int x = 0; x < 64; x++) {
69+
int[] a = { 2, 3, 4, 5 };
70+
int[] b = new int[90];
71+
int[] c = new int[a.length];
72+
SkippableIntegratedIntegerCODEC codec = new SkippableIntegratedComposition(new IntegratedBinaryPacking(),
73+
new IntegratedVariableByte());
74+
IntWrapper aOffset = new IntWrapper(0);
75+
IntWrapper bOffset = new IntWrapper(x);
76+
IntWrapper initValue = new IntWrapper(0);
77+
78+
codec.headlessCompress(a, aOffset, a.length, b, bOffset, initValue);
79+
int len = bOffset.get() - x;
80+
bOffset.set(x);
81+
IntWrapper cOffset = new IntWrapper(0);
82+
initValue = new IntWrapper(0);
83+
codec.headlessUncompress(b, bOffset, len, c, cOffset, a.length, initValue);
84+
Assert.assertArrayEquals(a, c);
85+
}
86+
}
87+
1388
/**
1489
* a test
1590
*/

0 commit comments

Comments
 (0)