me.lemire.integercompression.IntCompressor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JavaFastPFOR Show documentation
Show all versions of JavaFastPFOR Show documentation
It is a library to compress and uncompress arrays of integers
very fast. The assumption is that most (but not all) values in
your array use less than 32 bits.
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 [] compressed = new int[input.length+1024];
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) {
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;
}
}