private RunLength.Long getValueStats(long[] values) { long[] copy = Platform.copyOfLong(values, values.length); Arrays.sort(copy); RunLength.Long rl = new RunLength.Long(); for (int i = 0; i < copy.length; i++) { long l = copy[i]; rl.add(l); } return rl; } }
public void compress(long[] values, ByteBuffer bb) { RunLength.Long rl = getValueStats(values); int[] counts = rl.getCounts(); long[] keys = rl.getValues(); VLC vlc = buildCodes(counts, values.length / 10); int[] codes = vlc.getCodes(); int[] codeSizes = vlc.getCodeSizes(); bb.putInt(codes.length); for (int i = 0; i < codes.length; i++) { bb.put((byte) codeSizes[i]); bb.putShort((short) (codes[i] >>> 16)); bb.putLong(keys[i]); } BitWriter br = new BitWriter(bb); for (int j = 0; j < values.length; j++) { long l = values[j]; for (int i = 0; i < keys.length; i++) if (keys[i] == l) { vlc.writeVLC(br, i); if (codes[i] == 0xf) br.writeNBit(16, i); } } br.flush(); }
public void serialize(ByteBuffer bb) { ByteBuffer dup = bb.duplicate(); int[] counts = getCounts(); long[] values = getValues(); NIOUtils.skip(bb, 4); int recCount = 0; for (int i = 0; i < counts.length; i++, recCount++) { int count = counts[i]; while (count >= 0x100) { bb.put((byte) 0xff); bb.putLong(values[i]); ++recCount; count -= 0x100; } bb.put((byte) (count - 1)); bb.putLong(values[i]); } dup.putInt(recCount); }
public long[] flattern() { int[] counts = getCounts(); int total = 0; for (int i = 0; i < counts.length; i++) { total += counts[i]; } long[] values = getValues(); long[] result = new long[total]; for (int i = 0, ind = 0; i < counts.length; i++) { for (int j = 0; j < counts[i]; j++, ind++) result[ind] = values[i]; } return result; } }
public long[] getValues() { finish(); return values.toArray(); }
public int[] getCounts() { finish(); return counts.toArray(); }
public static RunLength.Long parse(ByteBuffer bb) { RunLength.Long rl = new RunLength.Long(); int recCount = bb.getInt(); for (int i = 0; i < recCount; i++) { int count = (bb.get() & 0xff) + 1; long value = bb.getLong(); rl.counts.add(count); rl.values.add(value); } return rl; }