From 0e0614b5add70db580cd4852b88dac22a2b70dfc Mon Sep 17 00:00:00 2001 From: Ted Dunning Date: Sun, 6 Aug 2017 13:49:10 -0700 Subject: [PATCH] Closes #90 - makes MergingDigest be consistent on serialization. --- RELEASE-NOTES.md | 7 +++++++ .../java/com/tdunning/math/stats/AVLTreeDigest.java | 2 +- .../java/com/tdunning/math/stats/MergingDigest.java | 12 ++++-------- .../java/com/tdunning/math/stats/TDigestTest.java | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 RELEASE-NOTES.md diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md new file mode 100644 index 00000000..7b1f2a18 --- /dev/null +++ b/RELEASE-NOTES.md @@ -0,0 +1,7 @@ +In release 3.2 + +The following issues are fixed in this release + +[Issue #90](https://github.com/tdunning/t-digest/issues/90) Serialization for MergingDigest + +[Issue #92](https://github.com/tdunning/t-digest/issues/92) Serialization for AVLTreeDigest diff --git a/src/main/java/com/tdunning/math/stats/AVLTreeDigest.java b/src/main/java/com/tdunning/math/stats/AVLTreeDigest.java index 2a56276f..a0824dc2 100644 --- a/src/main/java/com/tdunning/math/stats/AVLTreeDigest.java +++ b/src/main/java/com/tdunning/math/stats/AVLTreeDigest.java @@ -315,7 +315,7 @@ public double compression() { */ @Override public int byteSize() { - return 4 + 8 * 2 + 8 + 4 + summary.size() * 12; + return 32 + summary.size() * 12; } /** diff --git a/src/main/java/com/tdunning/math/stats/MergingDigest.java b/src/main/java/com/tdunning/math/stats/MergingDigest.java index 8315cee5..68aa8d3e 100644 --- a/src/main/java/com/tdunning/math/stats/MergingDigest.java +++ b/src/main/java/com/tdunning/math/stats/MergingDigest.java @@ -753,7 +753,7 @@ public int byteSize() { compress(); // format code, compression(float), buffer-size(int), temp-size(int), #centroids-1(int), // then two doubles per centroid - return lastUsedCell * 16 + 36; + return lastUsedCell * 16 + 32; } @Override @@ -780,14 +780,10 @@ public void asBytes(ByteBuffer buf) { buf.putInt(Encoding.VERBOSE_ENCODING.code); buf.putDouble(min); buf.putDouble(max); - buf.putFloat((float) compression); - buf.putInt(mean.length); - buf.putInt(tempMean.length); + buf.putDouble(compression); buf.putInt(lastUsedCell); for (int i = 0; i < lastUsedCell; i++) { buf.putDouble(weight[i]); - } - for (int i = 0; i < lastUsedCell; i++) { buf.putDouble(mean[i]); } } @@ -814,11 +810,11 @@ public static MergingDigest fromBytes(ByteBuffer buf) { if (encoding == Encoding.VERBOSE_ENCODING.code) { double min = buf.getDouble(); double max = buf.getDouble(); - double compression = buf.getFloat(); + double compression = buf.getDouble(); int n = buf.getInt(); MergingDigest r = new MergingDigest(compression); r.setMinMax(min, max); - r.lastUsedCell = buf.getInt(); + r.lastUsedCell = n; for (int i = 0; i < n; i++) { r.weight[i] = buf.getDouble(); r.mean[i] = buf.getDouble(); diff --git a/src/test/java/com/tdunning/math/stats/TDigestTest.java b/src/test/java/com/tdunning/math/stats/TDigestTest.java index 9a482b62..8fe29d11 100644 --- a/src/test/java/com/tdunning/math/stats/TDigestTest.java +++ b/src/test/java/com/tdunning/math/stats/TDigestTest.java @@ -612,7 +612,7 @@ public double nextDouble() { @Test public void testSerialization() { Random gen = getRandom(); - final double compression = randomDouble() * 100; + final double compression = 20 + randomDouble() * 100; TDigest dist = factory(compression).create(); for (int i = 0; i < 100000; i++) { double x = gen.nextDouble();