forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntCompressor.java
More file actions
65 lines (57 loc) · 1.97 KB
/
IntCompressor.java
File metadata and controls
65 lines (57 loc) · 1.97 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
package me.lemire.integercompression;
import java.util.Arrays;
/**
* This is a convenience class that wraps a codec to provide
* a "friendly" API.
*
*/
public class IntCompressor {
SkippableIntegerCODEC codec;
/**
* Constructor wrapping a codec.
*
* @param c the underlying codec
*/
public IntCompressor(SkippableIntegerCODEC c) {
codec = c;
}
/**
* Constructor with default codec.
*/
public IntCompressor() {
codec = new SkippableComposition(new BinaryPacking(),
new VariableByte());
}
/**
* Compress an array and returns the compressed result as a new array.
*
* @param input array to be compressed
* @return compressed array
*/
public int[] compress(int[] input) {
int maxCompressedLength = codec.maxHeadlessCompressedLength(new IntWrapper(0), input.length);
int[] compressed = new int[maxCompressedLength + 1]; // +1 to store the length of the input
// Store at index=0 the length of the input, hence enabling .headlessCompress
compressed[0] = input.length;
IntWrapper outpos = new IntWrapper(1);
codec.headlessCompress(input, new IntWrapper(0), input.length, compressed, outpos);
compressed = Arrays.copyOf(compressed,outpos.intValue());
return compressed;
}
/**
* Uncompress an array and returns the uncompressed result as a new array.
*
* @param compressed compressed array
* @return uncompressed array
*/
public int[] uncompress(int[] compressed) {
// Read at index=0 the length of the input, hence enabling .headlessUncompress
int[] decompressed = new int[compressed[0]];
IntWrapper inpos = new IntWrapper(1);
codec.headlessUncompress(compressed, inpos,
compressed.length - inpos.intValue(),
decompressed, new IntWrapper(0),
decompressed.length);
return decompressed;
}
}