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
72 lines (64 loc) · 2.19 KB
/
Copy pathIntCompressor.java
File metadata and controls
72 lines (64 loc) · 2.19 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
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
* @throws UncompressibleInputException if the data is too poorly compressible
*/
public int[] compress(int[] input) {
int[] compressed = new int[input.length + input.length / 100 + 1024];
// Store at index=0 the length of the input, hence enabling .headlessCompress
compressed[0] = input.length;
IntWrapper outpos = new IntWrapper(1);
try {
codec.headlessCompress(input, new IntWrapper(0),
input.length, compressed, outpos);
} catch (IndexOutOfBoundsException ioebe) {
throw new
UncompressibleInputException("Your input is too poorly compressible "
+ "with the current codec : "+codec);
}
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;
}
}