//使用FastPfor算法将排序了的int数组进行压缩,注意:target数组必须是排序后的数组 public static int[] fastPforEncoder(int[] uncompressed) { var codec = new SkippableIntegratedComposition(new IntegratedBinaryPacking(), new IntegratedVariableByte()); var compressed = new int[uncompressed.Length + 1024]; var inputoffset = new IntWrapper(0); var outputoffset = new IntWrapper(1); codec.headlessCompress(uncompressed, inputoffset, uncompressed.Length, compressed, outputoffset, new IntWrapper(0)); compressed[0] = uncompressed.Length; compressed = Arrays.copyOf(compressed, outputoffset.intValue()); return(compressed); }
public void basicExampleHeadless() { int[] data = new int[2342351]; Console.WriteLine("Compressing " + data.Length + " integers in one go using the headless approach"); // data should be sorted for best // results for (int k = 0; k < data.Length; ++k) { data[k] = k; } // Very important: the data is in sorted order!!! If not, you // will get very poor compression with IntegratedBinaryPacking, // you should use another CODEC. // next we compose a CODEC. Most of the processing // will be done with binary packing, and leftovers will // be processed using variable byte SkippableIntegratedComposition codec = new SkippableIntegratedComposition(new IntegratedBinaryPacking(), new IntegratedVariableByte()); // output vector should be large enough... int[] compressed = new int[data.Length + 1024]; // compressed might not be large enough in some cases // if you get java.lang.ArrayIndexOutOfBoundsException, try // allocating more memory /** * * compressing * */ IntWrapper inputoffset = new IntWrapper(0); IntWrapper outputoffset = new IntWrapper(1); compressed[0] = data.Length; // we manually store how many integers we codec.headlessCompress(data, inputoffset, data.Length, compressed, outputoffset, new IntWrapper(0)); // got it! // inputoffset should be at data.Length but outputoffset tells // us where we are... Console.WriteLine( "compressed from " + data.Length * 4 / 1024 + "KB to " + outputoffset.intValue() * 4 / 1024 + "KB"); // we can repack the data: (optional) compressed = Arrays.copyOf(compressed, outputoffset.intValue()); /** * * now uncompressing * */ int howmany = compressed[0];// we manually stored the number of // compressed integers int[] recovered = new int[howmany]; IntWrapper recoffset = new IntWrapper(0); codec.headlessUncompress(compressed, new IntWrapper(1), compressed.Length, recovered, recoffset, howmany, new IntWrapper(0)); if (Arrays.equals(data, recovered)) { Console.WriteLine("data is recovered without loss"); } else { throw new Exception("bug"); // could use assert } Console.WriteLine(); }