forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkippableComposition.java
More file actions
79 lines (71 loc) · 2.53 KB
/
SkippableComposition.java
File metadata and controls
79 lines (71 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* This is code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
package me.lemire.integercompression;
/**
* Helper class to compose schemes.
*
* @author Daniel Lemire
*/
public class SkippableComposition implements SkippableIntegerCODEC {
SkippableIntegerCODEC F1, F2;
/**
* Compose a scheme from a first one (f1) and a second one (f2). The first
* one is called first and then the second one tries to compress whatever
* remains from the first run.
*
* By convention, the first scheme should be such that if, during decoding,
* a 32-bit zero is first encountered, then there is no output.
*
* @param f1
* first codec
* @param f2
* second codec
*/
public SkippableComposition(SkippableIntegerCODEC f1,
SkippableIntegerCODEC f2) {
F1 = f1;
F2 = f2;
}
@Override
public void headlessCompress(int[] in, IntWrapper inpos, int inlength, int[] out,
IntWrapper outpos) {
int init = inpos.get();
int outposInit = outpos.get();
F1.headlessCompress(in, inpos, inlength, out, outpos);
if (outpos.get() == outposInit) {
out[outposInit] = 0;
outpos.increment();
}
inlength -= inpos.get() - init;
F2.headlessCompress(in, inpos, inlength, out, outpos);
}
@Override
public void headlessUncompress(int[] in, IntWrapper inpos, int inlength, int[] out,
IntWrapper outpos, int num) {
int init = inpos.get();
F1.headlessUncompress(in, inpos, inlength, out, outpos, num);
if (inpos.get() == init) {
inpos.increment();
}
inlength -= inpos.get() - init;
num -= outpos.get();
F2.headlessUncompress(in, inpos, inlength, out, outpos, num);
}
@Override
public int maxHeadlessCompressedLength(IntWrapper compressedPositions, int inlength) {
int init = compressedPositions.get();
int maxLength = F1.maxHeadlessCompressedLength(compressedPositions, inlength);
maxLength += 1; // Add +1 for the potential F2 header. Question: is this header actually needed in the headless version?
inlength -= compressedPositions.get() - init;
maxLength += F2.maxHeadlessCompressedLength(compressedPositions, inlength);
return maxLength;
}
@Override
public String toString() {
return F1.toString() + "+" + F2.toString();
}
}