|
| 1 | +// Copyright (C) 2022 Intel Corporation |
| 2 | + |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import me.lemire.integercompression.FastPFOR; |
| 7 | +import me.lemire.integercompression.IntWrapper; |
| 8 | +import me.lemire.integercompression.Composition; |
| 9 | +import me.lemire.integercompression.IntegerCODEC; |
| 10 | +import me.lemire.integercompression.VariableByte; |
| 11 | +import me.lemire.integercompression.vector.VectorFastPFOR; |
| 12 | + |
| 13 | +public class Example { |
| 14 | + public static void main(String[] args) { |
| 15 | + if (args.length == 0) |
| 16 | + throw new IllegalArgumentException(); |
| 17 | + |
| 18 | + // pass 0 for Vector compressor , non-zero for default compressor |
| 19 | + int compressorToUse = Integer.parseInt(args[0]); |
| 20 | + |
| 21 | + final int N = 1310720; |
| 22 | + int[] data = new int[N]; |
| 23 | + |
| 24 | + // 2-bit data |
| 25 | + for (int k = 0; k < N; k += 1) |
| 26 | + data[k] = 3; |
| 27 | + |
| 28 | + // a few large values |
| 29 | + for (int k = 0; k < N; k += 5) |
| 30 | + data[k] = 100; |
| 31 | + for (int k = 0; k < N; k += 533) |
| 32 | + data[k] = 10000; |
| 33 | + |
| 34 | + int[] compressed = new int[N + 1024]; |
| 35 | + |
| 36 | + IntegerCODEC codec = new Composition( |
| 37 | + compressorToUse == 0 ? new VectorFastPFOR() : new FastPFOR(), |
| 38 | + new VariableByte()); |
| 39 | + |
| 40 | + IntWrapper inputoffset = new IntWrapper(0); |
| 41 | + IntWrapper outputoffset = new IntWrapper(0); |
| 42 | + |
| 43 | + codec.compress(data, inputoffset, data.length, compressed, outputoffset); |
| 44 | + |
| 45 | + System.out.println("compressed unsorted integers from " + |
| 46 | + data.length * 4 / 1024 + "KB to " + |
| 47 | + outputoffset.intValue() * 4 / 1024 + "KB"); |
| 48 | + |
| 49 | + compressed = Arrays.copyOf(compressed, outputoffset.intValue()); |
| 50 | + |
| 51 | + int[] recovered = new int[N]; |
| 52 | + IntWrapper recoffset = new IntWrapper(0); |
| 53 | + |
| 54 | + codec.uncompress(compressed, new IntWrapper(0), compressed.length, |
| 55 | + recovered, recoffset); |
| 56 | + |
| 57 | + System.out.println("compressed length = " + compressed.length + |
| 58 | + ", uncompressed length = " + recoffset.intValue()); |
| 59 | + |
| 60 | + if (Arrays.equals(data, recovered)) |
| 61 | + System.out.println("data is recovered without loss"); |
| 62 | + else |
| 63 | + throw new RuntimeException("bug"); // could use assert |
| 64 | + |
| 65 | + System.out.println(); |
| 66 | + } |
| 67 | +} |
0 commit comments