You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by GitBox <gi...@apache.org> on 2022/03/30 00:42:57 UTC

[GitHub] [datasketches-java] AlexanderSaydakov commented on a change in pull request #390: Direct kll double

AlexanderSaydakov commented on a change in pull request #390:
URL: https://github.com/apache/datasketches-java/pull/390#discussion_r837996688



##########
File path: src/main/java/org/apache/datasketches/kll/package-info.java
##########
@@ -35,16 +35,17 @@
  *
  * <p>The <i>normalized rank</i> (<i>rank</i>) of any specific value is defined as its
  * <i>absolute rank</i> divided by <i>N</i>.
- * Thus, the <i>normalized rank</i> is a value between zero and one.
+ * Thus, the <i>normalized rank</i> is a value in the interval [0.0, 1.0), exclusive.

Review comment:
       this can be understood as if both sides are excluded, not looking at the brackets. and if we do look at the brackets, then the "exclusive" remark is redundant

##########
File path: src/test/java/org/apache/datasketches/kll/KllDirectFloatsSketchTest.java
##########
@@ -0,0 +1,594 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+//import static org.apache.datasketches.Util.getResourceBytes; //don't have matching numbers from C++
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.memory.DefaultMemoryRequestServer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class KllDirectFloatsSketchTest {
+
+  private static final double PMF_EPS_FOR_K_8 = 0.35; // PMF rank error (epsilon) for k=8
+  private static final double PMF_EPS_FOR_K_128 = 0.025; // PMF rank error (epsilon) for k=128
+  private static final double PMF_EPS_FOR_K_256 = 0.013; // PMF rank error (epsilon) for k=256
+  private static final double NUMERIC_NOISE_TOLERANCE = 1E-6;
+  private static final DefaultMemoryRequestServer memReqSvr = new DefaultMemoryRequestServer();
+
+  @Test
+  public void empty() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(Float.NaN); // this must not change anything
+    assertTrue(sketch.isEmpty());
+    assertEquals(sketch.getN(), 0);
+    assertEquals(sketch.getNumRetained(), 0);
+    assertTrue(Double.isNaN(sketch.getRank(0)));
+    assertTrue(Float.isNaN(sketch.getMinValue()));
+    assertTrue(Float.isNaN(sketch.getMaxValue()));
+    assertTrue(Float.isNaN(sketch.getQuantile(0.5)));
+    assertNull(sketch.getQuantiles(new double[] {0}));
+    assertNull(sketch.getPMF(new float[] {0}));
+    assertNotNull(sketch.toString(true, true));
+    assertNotNull(sketch.toString());
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantileInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantile(-1.0);
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantilesInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantiles(new double[] {2.0});
+  }
+
+  @Test
+  public void oneItem() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    assertFalse(sketch.isEmpty());
+    assertEquals(sketch.getN(), 1);
+    assertEquals(sketch.getNumRetained(), 1);
+    assertEquals(sketch.getRank(1), 0.0);
+    assertEquals(sketch.getRank(2), 1.0);
+    assertEquals(sketch.getMinValue(), 1f);
+    assertEquals(sketch.getMaxValue(), 1f);
+    assertEquals(sketch.getQuantile(0.5), 1f);
+  }
+
+  @Test
+  public void manyItemsEstimationMode() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1_000_000;
+
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getN(), n);
+
+    // test getRank
+    for (int i = 0; i < n; i++) {
+      final double trueRank = (double) i / n;
+      assertEquals(sketch.getRank(i), trueRank, PMF_EPS_FOR_K_256, "for value " + i);
+    }
+
+    // test getPMF
+    final double[] pmf = sketch.getPMF(new float[] {n / 2}); // split at median
+    assertEquals(pmf.length, 2);
+    assertEquals(pmf[0], 0.5, PMF_EPS_FOR_K_256);
+    assertEquals(pmf[1], 0.5, PMF_EPS_FOR_K_256);
+
+    assertEquals(sketch.getMinValue(), 0f); // min value is exact
+    assertEquals(sketch.getQuantile(0), 0f); // min value is exact
+    assertEquals(sketch.getMaxValue(), n - 1f); // max value is exact
+    assertEquals(sketch.getQuantile(1), n - 1f); // max value is exact
+
+    // check at every 0.1 percentage point
+    final double[] fractions = new double[1001];
+    final double[] reverseFractions = new double[1001]; // check that ordering doesn't matter
+    for (int i = 0; i <= 1000; i++) {
+      fractions[i] = (double) i / 1000;
+      reverseFractions[1000 - i] = fractions[i];
+    }
+    final float[] quantiles = sketch.getQuantiles(fractions);
+    final float[] reverseQuantiles = sketch.getQuantiles(reverseFractions);
+    double previousQuantile = 0;
+    for (int i = 0; i <= 1000; i++) {
+      final double quantile = sketch.getQuantile(fractions[i]);
+      assertEquals(quantile, quantiles[i]);
+      assertEquals(quantile, reverseQuantiles[1000 - i]);
+      assertTrue(previousQuantile <= quantile);
+      previousQuantile = quantile;
+    }
+  }
+
+  @Test
+  public void getRankGetCdfGetPmfConsistency() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1000;
+    final float[] values = new float[n];
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+      values[i] = i;
+    }
+    final double[] ranks = sketch.getCDF(values);
+    final double[] pmf = sketch.getPMF(values);
+    double sumPmf = 0;
+    for (int i = 0; i < n; i++) {
+      assertEquals(ranks[i], sketch.getRank(values[i]), NUMERIC_NOISE_TOLERANCE,
+          "rank vs CDF for value " + i);
+      sumPmf += pmf[i];
+      assertEquals(ranks[i], sumPmf, NUMERIC_NOISE_TOLERANCE, "CDF vs PMF for value " + i);
+    }
+    sumPmf += pmf[n];
+    assertEquals(sumPmf, 1.0, NUMERIC_NOISE_TOLERANCE);
+    assertEquals(ranks[n], 1.0, NUMERIC_NOISE_TOLERANCE);
+  }
+
+  @Test
+  public void merge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i * 1.0F);
+      sketch2.update((2 * n - i - 1) * 1.0F);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (n - 1) * 1.0);
+
+    assertEquals(sketch2.getMinValue(), n * 1.0);
+    assertEquals(sketch2.getMaxValue(), (2 * n - 1) * 1.0);
+
+    sketch1.merge(sketch2);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2L * n);
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (2 * n - 1) * 1.0F);
+    assertEquals(sketch1.getQuantile(0.5), n * 1.0F, n * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+      sketch2.update(2 * n - i - 1);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+
+    assertEquals(sketch2.getMinValue(), n);
+    assertEquals(sketch2.getMaxValue(), 2f * n - 1f);
+
+    assertTrue(sketch1.getNormalizedRankError(false) < sketch2.getNormalizedRankError(false));
+    assertTrue(sketch1.getNormalizedRankError(true) < sketch2.getNormalizedRankError(true));
+    sketch1.merge(sketch2);
+
+    // sketch1 must get "contaminated" by the lower K in sketch2
+    assertEquals(sketch1.getNormalizedRankError(false), sketch2.getNormalizedRankError(false));
+    assertEquals(sketch1.getNormalizedRankError(true), sketch2.getNormalizedRankError(true));
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2 * n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), 2f * n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n, n * PMF_EPS_FOR_K_128);
+  }
+
+  @Test
+  public void mergeEmptyLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+
+    // rank error should not be affected by a merge with an empty sketch with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+
+    //merge the other way
+    sketch2.merge(sketch1);
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeExactModeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+    sketch2.update(1);
+
+    // rank error should not be affected by a merge with a sketch in exact mode with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+  }
+
+  @Test
+  public void mergeMinMinValueFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    sketch1.update(1);
+    sketch2.update(2);
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1.0F);
+  }
+
+  @Test
+  public void mergeMinAndMaxFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(8, 0); //was 200
+    final KllDirectFloatsSketch sketch2 = getDFSketch(8, 0); //was 200
+    for (int i = 1; i <= 9; i++) { //was 1_000_000
+      sketch1.update(i);
+    }
+    //System.out.println(sketch1.toString(true, true));
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1F);
+    assertEquals(sketch2.getMaxValue(), 9F); //was 1_000_000
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooSmall() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(DEFAULT_M - 1, 0);
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooLarge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(MAX_K + 1, 0);
+  }
+
+  @Test
+  public void minK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(DEFAULT_M, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), DEFAULT_M);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_8);
+  }
+
+  @Test
+  public void maxK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(MAX_K, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), MAX_K);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void serializeDeserializeEmpty() { //compact serialize then heapify using KllDoublesSketch

Review comment:
       the comment seems to be wrong. Floats, not Doubles. I would suggest transferring that info into the test name like serializeDeserializeCompactFloats

##########
File path: src/test/java/org/apache/datasketches/kll/KllDirectFloatsSketchTest.java
##########
@@ -0,0 +1,594 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+//import static org.apache.datasketches.Util.getResourceBytes; //don't have matching numbers from C++
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.memory.DefaultMemoryRequestServer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class KllDirectFloatsSketchTest {
+
+  private static final double PMF_EPS_FOR_K_8 = 0.35; // PMF rank error (epsilon) for k=8
+  private static final double PMF_EPS_FOR_K_128 = 0.025; // PMF rank error (epsilon) for k=128
+  private static final double PMF_EPS_FOR_K_256 = 0.013; // PMF rank error (epsilon) for k=256
+  private static final double NUMERIC_NOISE_TOLERANCE = 1E-6;
+  private static final DefaultMemoryRequestServer memReqSvr = new DefaultMemoryRequestServer();
+
+  @Test
+  public void empty() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(Float.NaN); // this must not change anything
+    assertTrue(sketch.isEmpty());
+    assertEquals(sketch.getN(), 0);
+    assertEquals(sketch.getNumRetained(), 0);
+    assertTrue(Double.isNaN(sketch.getRank(0)));
+    assertTrue(Float.isNaN(sketch.getMinValue()));
+    assertTrue(Float.isNaN(sketch.getMaxValue()));
+    assertTrue(Float.isNaN(sketch.getQuantile(0.5)));
+    assertNull(sketch.getQuantiles(new double[] {0}));
+    assertNull(sketch.getPMF(new float[] {0}));
+    assertNotNull(sketch.toString(true, true));
+    assertNotNull(sketch.toString());
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantileInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantile(-1.0);
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantilesInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantiles(new double[] {2.0});
+  }
+
+  @Test
+  public void oneItem() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    assertFalse(sketch.isEmpty());
+    assertEquals(sketch.getN(), 1);
+    assertEquals(sketch.getNumRetained(), 1);
+    assertEquals(sketch.getRank(1), 0.0);
+    assertEquals(sketch.getRank(2), 1.0);
+    assertEquals(sketch.getMinValue(), 1f);
+    assertEquals(sketch.getMaxValue(), 1f);
+    assertEquals(sketch.getQuantile(0.5), 1f);
+  }
+
+  @Test
+  public void manyItemsEstimationMode() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1_000_000;
+
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getN(), n);
+
+    // test getRank
+    for (int i = 0; i < n; i++) {
+      final double trueRank = (double) i / n;
+      assertEquals(sketch.getRank(i), trueRank, PMF_EPS_FOR_K_256, "for value " + i);
+    }
+
+    // test getPMF
+    final double[] pmf = sketch.getPMF(new float[] {n / 2}); // split at median
+    assertEquals(pmf.length, 2);
+    assertEquals(pmf[0], 0.5, PMF_EPS_FOR_K_256);
+    assertEquals(pmf[1], 0.5, PMF_EPS_FOR_K_256);
+
+    assertEquals(sketch.getMinValue(), 0f); // min value is exact
+    assertEquals(sketch.getQuantile(0), 0f); // min value is exact
+    assertEquals(sketch.getMaxValue(), n - 1f); // max value is exact
+    assertEquals(sketch.getQuantile(1), n - 1f); // max value is exact
+
+    // check at every 0.1 percentage point
+    final double[] fractions = new double[1001];
+    final double[] reverseFractions = new double[1001]; // check that ordering doesn't matter
+    for (int i = 0; i <= 1000; i++) {
+      fractions[i] = (double) i / 1000;
+      reverseFractions[1000 - i] = fractions[i];
+    }
+    final float[] quantiles = sketch.getQuantiles(fractions);
+    final float[] reverseQuantiles = sketch.getQuantiles(reverseFractions);
+    double previousQuantile = 0;
+    for (int i = 0; i <= 1000; i++) {
+      final double quantile = sketch.getQuantile(fractions[i]);
+      assertEquals(quantile, quantiles[i]);
+      assertEquals(quantile, reverseQuantiles[1000 - i]);
+      assertTrue(previousQuantile <= quantile);
+      previousQuantile = quantile;
+    }
+  }
+
+  @Test
+  public void getRankGetCdfGetPmfConsistency() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1000;
+    final float[] values = new float[n];
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+      values[i] = i;
+    }
+    final double[] ranks = sketch.getCDF(values);
+    final double[] pmf = sketch.getPMF(values);
+    double sumPmf = 0;
+    for (int i = 0; i < n; i++) {
+      assertEquals(ranks[i], sketch.getRank(values[i]), NUMERIC_NOISE_TOLERANCE,
+          "rank vs CDF for value " + i);
+      sumPmf += pmf[i];
+      assertEquals(ranks[i], sumPmf, NUMERIC_NOISE_TOLERANCE, "CDF vs PMF for value " + i);
+    }
+    sumPmf += pmf[n];
+    assertEquals(sumPmf, 1.0, NUMERIC_NOISE_TOLERANCE);
+    assertEquals(ranks[n], 1.0, NUMERIC_NOISE_TOLERANCE);
+  }
+
+  @Test
+  public void merge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i * 1.0F);
+      sketch2.update((2 * n - i - 1) * 1.0F);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (n - 1) * 1.0);
+
+    assertEquals(sketch2.getMinValue(), n * 1.0);
+    assertEquals(sketch2.getMaxValue(), (2 * n - 1) * 1.0);
+
+    sketch1.merge(sketch2);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2L * n);
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (2 * n - 1) * 1.0F);
+    assertEquals(sketch1.getQuantile(0.5), n * 1.0F, n * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+      sketch2.update(2 * n - i - 1);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+
+    assertEquals(sketch2.getMinValue(), n);
+    assertEquals(sketch2.getMaxValue(), 2f * n - 1f);
+
+    assertTrue(sketch1.getNormalizedRankError(false) < sketch2.getNormalizedRankError(false));
+    assertTrue(sketch1.getNormalizedRankError(true) < sketch2.getNormalizedRankError(true));
+    sketch1.merge(sketch2);
+
+    // sketch1 must get "contaminated" by the lower K in sketch2
+    assertEquals(sketch1.getNormalizedRankError(false), sketch2.getNormalizedRankError(false));
+    assertEquals(sketch1.getNormalizedRankError(true), sketch2.getNormalizedRankError(true));
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2 * n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), 2f * n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n, n * PMF_EPS_FOR_K_128);
+  }
+
+  @Test
+  public void mergeEmptyLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+
+    // rank error should not be affected by a merge with an empty sketch with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+
+    //merge the other way
+    sketch2.merge(sketch1);
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeExactModeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+    sketch2.update(1);
+
+    // rank error should not be affected by a merge with a sketch in exact mode with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+  }
+
+  @Test
+  public void mergeMinMinValueFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    sketch1.update(1);
+    sketch2.update(2);
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1.0F);
+  }
+
+  @Test
+  public void mergeMinAndMaxFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(8, 0); //was 200
+    final KllDirectFloatsSketch sketch2 = getDFSketch(8, 0); //was 200
+    for (int i = 1; i <= 9; i++) { //was 1_000_000
+      sketch1.update(i);
+    }
+    //System.out.println(sketch1.toString(true, true));
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1F);
+    assertEquals(sketch2.getMaxValue(), 9F); //was 1_000_000
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooSmall() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(DEFAULT_M - 1, 0);
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooLarge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(MAX_K + 1, 0);
+  }
+
+  @Test
+  public void minK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(DEFAULT_M, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), DEFAULT_M);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_8);
+  }
+
+  @Test
+  public void maxK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(MAX_K, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), MAX_K);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void serializeDeserializeEmpty() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeEmpty2() { //updatable serialize then new (loaded) KllDirectDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toUpdatableByteArray();
+    final KllDirectFloatsSketch sketch2 =
+        KllDirectFloatsSketch.writableWrap(WritableMemory.writableWrap(bytes),memReqSvr);
+    assertEquals(bytes.length, sketch1.getCurrentUpdatableSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeOneItem() { //compact serialize then heapify using KllDoublesSketch

Review comment:
       and here

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectFloatsSketch.java
##########
@@ -0,0 +1,326 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.UPDATABLE_BIT_MASK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFamilyID;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFlags;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertM;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertPreInts;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSerVer;
+
+import org.apache.datasketches.Family;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+//Intentional

Review comment:
       not clear what is intentional

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectDoublesSketch.java
##########
@@ -0,0 +1,326 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.apache.datasketches.kll.KllPreambleUtil.DOUBLES_SKETCH_BIT_MASK;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.UPDATABLE_BIT_MASK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFamilyID;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFlags;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertM;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertPreInts;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSerVer;
+
+import org.apache.datasketches.Family;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class implements an off-heap doubles KllSketch via a WritableMemory instance of the sketch.
+ *
+ * <p>Please refer to the documentation in the package-info:<br>
+ * {@link org.apache.datasketches.kll}</p>
+ *
+ * @author Lee Rhodes, Kevin Lang
+ */
+public final class KllDirectDoublesSketch extends KllDirectSketch {
+
+  /**
+   * The actual constructor.
+   * @param wmem the current WritableMemory
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @param memVal the MemoryValadate object
+   */
+  private KllDirectDoublesSketch(final WritableMemory wmem, final MemoryRequestServer memReqSvr,
+      final MemoryValidate memVal) {
+    super(SketchType.DOUBLES_SKETCH, wmem, memReqSvr, memVal);
+  }
+
+  /**
+   * Wrap a sketch around the given source Memory containing sketch data that originated from
+   * this sketch.
+   * @param srcMem a WritableMemory that contains data.
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return instance of this sketch
+   */
+  public static KllDirectDoublesSketch writableWrap(final WritableMemory srcMem, final MemoryRequestServer memReqSvr) {
+    final MemoryValidate memVal = new MemoryValidate(srcMem);
+    return new KllDirectDoublesSketch(srcMem, memReqSvr, memVal);
+  }
+
+  /**
+   * Create a new instance of this sketch using default M.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param dstMem the given destination WritableMemory object for use by the sketch
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return a new instance of this sketch
+   */
+  public static KllDirectDoublesSketch newInstance(final int k, final WritableMemory dstMem,
+      final MemoryRequestServer memReqSvr) {
+    return newInstance(k, DEFAULT_M, dstMem, memReqSvr);
+  }
+
+  /**
+   * Create a new instance of this sketch.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param m parameter that controls the minimum level width.
+   * @param dstMem the given destination WritableMemory object for use by the sketch
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return a new instance of this sketch
+   */
+  public static KllDirectDoublesSketch newInstance(final int k, final int m, final WritableMemory dstMem,
+      final MemoryRequestServer memReqSvr) {
+    insertPreInts(dstMem, PREAMBLE_INTS_DOUBLE);
+    insertSerVer(dstMem, SERIAL_VERSION_UPDATABLE);
+    insertFamilyID(dstMem, Family.KLL.getID());
+    insertFlags(dstMem, DOUBLES_SKETCH_BIT_MASK | UPDATABLE_BIT_MASK);
+    insertK(dstMem, k);
+    insertM(dstMem, m);
+    insertN(dstMem, 0);
+    insertDyMinK(dstMem, k);

Review comment:
       I cannot guess from the name what this is

##########
File path: src/main/java/org/apache/datasketches/kll/KllDoublesSketch.java
##########
@@ -389,441 +271,81 @@ public double getQuantileLowerBound(final double fraction) {
    * @return an approximate rank of the given value
    */
   public double getRank(final double value) {
-    if (isEmpty()) { return Double.NaN; }
-    int level = 0;
-    int weight = 1;
-    long total = 0;
-    while (level < numLevels_) {
-      final int fromIndex = levels_[level];
-      final int toIndex = levels_[level + 1]; // exclusive
-      for (int i = fromIndex; i < toIndex; i++) {
-        if (items_[i] < value) {
-          total += weight;
-        } else if (level > 0 || isLevelZeroSorted_) {
-          break; // levels above 0 are sorted, no point comparing further
-        }
-      }
-      level++;
-      weight *= 2;
-    }
-    return (double) total / n_;
-  }
-
-  /**
-   * Returns the number of bytes this sketch would require to store.
-   * @return the number of bytes this sketch would require to store.
-   */
-  public int getSerializedSizeBytes() {
-    if (isEmpty()) { return N_LONG; }
-    return getSerializedSizeBytes(numLevels_, getNumRetained());
+    return getDoubleRank(value);
   }
 
   /**
    * @return the iterator for this class
    */
   public KllDoublesSketchIterator iterator() {
-    return new KllDoublesSketchIterator(items_, levels_, numLevels_);
+    return new KllDoublesSketchIterator(getDoubleItemsArray(), getLevelsArray(), getNumLevels());
   }
 
   /**
    * Merges another sketch into this one.
    * @param other sketch to merge into this one
    */
-  public void merge(final KllDoublesSketch other) {
-    if (other == null || other.isEmpty()) { return; }
-    if (m_ != other.m_) {
-      throw new SketchesArgumentException("incompatible M: " + m_ + " and " + other.m_);
-    }
-    final long finalN = n_ + other.n_;
-    //update this sketch with level0 items from the other sketch
-    for (int i = other.levels_[0]; i < other.levels_[1]; i++) {
-      update(other.items_[i]);
-    }
-    if (other.numLevels_ >= 2) { //now merge other levels if they exist
-      mergeHigherLevels(other, finalN);
-    }
-    //update min, max values, n
-    if (Double.isNaN(minValue_) || other.minValue_ < minValue_) { minValue_ = other.minValue_; }
-    if (Double.isNaN(maxValue_) || other.maxValue_ > maxValue_) { maxValue_ = other.maxValue_; }
-    n_ = finalN;
-
-    assert KllHelper.sumTheSampleWeights(numLevels_, levels_) == n_;
-    if (other.isEstimationMode()) {
-      minK_ = min(minK_, other.minK_);
-    }
-  }
-
-  @Override
-  public byte[] toByteArray() {
-    final byte[] bytes = new byte[getSerializedSizeBytes()];
-    final WritableMemory wmem = WritableMemory.writableWrap(bytes);
-    final boolean singleItem = n_ == 1;
-    final boolean empty = isEmpty();
-    //load the preamble
-    wmem.putByte(PREAMBLE_INTS_BYTE, (byte) (empty || singleItem ? PREAMBLE_INTS_EMPTY_SINGLE : PREAMBLE_INTS_DOUBLE));
-    wmem.putByte(SER_VER_BYTE, singleItem ? SERIAL_VERSION_SINGLE : SERIAL_VERSION);
-    wmem.putByte(FAMILY_BYTE, (byte) Family.KLL.getID());
-    final byte flags = (byte) (
-        (empty ? 1 << Flags.IS_EMPTY.ordinal() : 0)
-      | (isLevelZeroSorted_ ? 1 << Flags.IS_LEVEL_ZERO_SORTED.ordinal() : 0)
-      | (singleItem ? 1 << Flags.IS_SINGLE_ITEM.ordinal() : 0));
-    wmem.putByte(FLAGS_BYTE, flags);
-    wmem.putShort(K_SHORT, (short) k_);
-    wmem.putByte(M_BYTE, (byte) m_);
-    if (empty) { return bytes; }
-    //load data
-    int offset = DATA_START_SINGLE_ITEM;
-    if (!singleItem) {
-      wmem.putLong(N_LONG, n_);
-      wmem.putShort(MIN_K_SHORT, (short) minK_);
-      wmem.putByte(NUM_LEVELS_BYTE, (byte) numLevels_);
-      offset = DATA_START_DOUBLE;
-      // the last integer in levels_ is not serialized because it can be derived
-      final int len = levels_.length - 1;
-      wmem.putIntArray(offset, levels_, 0, len);
-      offset += len * Integer.BYTES;
-      wmem.putDouble(offset, minValue_);
-      offset += Double.BYTES;
-      wmem.putDouble(offset, maxValue_);
-      offset += Double.BYTES;
-    }
-    wmem.putDoubleArray(offset, items_, levels_[0], getNumRetained());
-    return bytes;
-  }
-
-  @Override
-  public String toString(final boolean withLevels, final boolean withData) {
-    final String epsPct = String.format("%.3f%%", getNormalizedRankError(false) * 100);
-    final String epsPMFPct = String.format("%.3f%%", getNormalizedRankError(true) * 100);
-    final StringBuilder sb = new StringBuilder();
-    sb.append(Util.LS).append("### KLL sketch summary:").append(Util.LS);
-    sb.append("   K                    : ").append(k_).append(Util.LS);
-    sb.append("   min K                : ").append(minK_).append(Util.LS);
-    sb.append("   M                    : ").append(m_).append(Util.LS);
-    sb.append("   N                    : ").append(n_).append(Util.LS);
-    sb.append("   Epsilon              : ").append(epsPct).append(Util.LS);
-    sb.append("   Epsison PMF          : ").append(epsPMFPct).append(Util.LS);
-    sb.append("   Empty                : ").append(isEmpty()).append(Util.LS);
-    sb.append("   Estimation Mode      : ").append(isEstimationMode()).append(Util.LS);
-    sb.append("   Levels               : ").append(numLevels_).append(Util.LS);
-    sb.append("   Level 0 Sorted       : ").append(isLevelZeroSorted_).append(Util.LS);
-    sb.append("   Capacity Items       : ").append(items_.length).append(Util.LS);
-    sb.append("   Retained Items       : ").append(getNumRetained()).append(Util.LS);
-    sb.append("   Storage Bytes        : ").append(getSerializedSizeBytes()).append(Util.LS);
-    sb.append("   Min Value            : ").append(minValue_).append(Util.LS);
-    sb.append("   Max Value            : ").append(maxValue_).append(Util.LS);
-    sb.append("### End sketch summary").append(Util.LS);
-
-    if (withLevels) {
-      sb.append("### KLL sketch levels:").append(Util.LS)
-      .append(" level, offset: nominal capacity, actual size").append(Util.LS);
-      for (int i = 0; i < numLevels_; i++) {
-        sb.append("   ").append(i).append(", ").append(levels_[i]).append(": ")
-        .append(KllHelper.levelCapacity(k_, numLevels_, i, m_))
-        .append(", ").append(currentLevelSize(i)).append(Util.LS);
-      }
-      sb.append("### End sketch levels").append(Util.LS);
-    }
-
-    if (withData) {
-      sb.append("### KLL sketch data {index, item}:").append(Util.LS);
-      if (levels_[0] > 0) {
-        sb.append(" Garbage:" + Util.LS);
-        for (int i = 0; i < levels_[0]; i++) {
-          if (items_[i] == 0.0f) { continue; }
-          sb.append("   ").append(i + ", ").append(items_[i]).append(Util.LS);
-        }
-      }
-      int level = 0;
-      while (level < numLevels_) {
-        final int fromIndex = levels_[level];
-        final int toIndex = levels_[level + 1]; // exclusive
-        if (fromIndex < toIndex) {
-          sb.append(" level[").append(level).append("]: offset: " + levels_[level] + " wt: " + (1 << level));
-          sb.append(Util.LS);
-        }
-        for (int i = fromIndex; i < toIndex; i++) {
-          sb.append("   ").append(i + ", ").append(items_[i]).append(Util.LS);
-        }
-        level++;
-      }
-      sb.append(" level[" + level + "]: offset: " + levels_[level] + " (Exclusive)");
-      sb.append(Util.LS);
-      sb.append("### End sketch data").append(Util.LS);
-    }
-
-    return sb.toString();
+  public void merge(final KllSketch other) {
+    if (other.isDirect()) { kllSketchThrow(35); }
+    if (!other.isDoublesSketch()) { kllSketchThrow(33); }
+    mergeDoubleImpl(other);
   }
 
   /**
    * Updates this sketch with the given data item.
    *
    * @param value an item from a stream of items. NaNs are ignored.
    */
-  public void update(final double value) {
-    if (Double.isNaN(value)) { return; }
-    if (isEmpty()) {
-      minValue_ = value;
-      maxValue_ = value;
-    } else {
-      if (value < minValue_) { minValue_ = value; }
-      if (value > maxValue_) { maxValue_ = value; }
-    }
-    if (levels_[0] == 0) {
-      compressWhileUpdating();
-    }
-    n_++;
-    isLevelZeroSorted_ = false;
-    final int nextPos = levels_[0] - 1;
-    assert levels_[0] >= 0;
-    levels_[0] = nextPos;
-    items_[nextPos] = value;
-  }
-
-  // Restricted Methods
-
-  private KllDoublesQuantileCalculator getQuantileCalculator() {
-    sortLevelZero(); // sort in the sketch to reuse if possible
-    return new KllDoublesQuantileCalculator(items_, levels_, numLevels_, n_);
-  }
-
-  private double[] getPmfOrCdf(final double[] splitPoints, final boolean isCdf) {
-    if (isEmpty()) { return null; }
-    KllDoublesHelper.validateDoubleValues(splitPoints);
-    final double[] buckets = new double[splitPoints.length + 1];
-    int level = 0;
-    int weight = 1;
-    while (level < numLevels_) {
-      final int fromIndex = levels_[level];
-      final int toIndex = levels_[level + 1]; // exclusive
-      if (level == 0 && !isLevelZeroSorted_) {
-        incrementBucketsUnsortedLevel(fromIndex, toIndex, weight, splitPoints, buckets);
-      } else {
-        incrementBucketsSortedLevel(fromIndex, toIndex, weight, splitPoints, buckets);
-      }
-      level++;
-      weight *= 2;
-    }
-    // normalize and, if CDF, convert to cumulative
-    if (isCdf) {
-      double subtotal = 0;
-      for (int i = 0; i < buckets.length; i++) {
-        subtotal += buckets[i];
-        buckets[i] = subtotal / n_;
-      }
-    } else {
-      for (int i = 0; i < buckets.length; i++) {
-        buckets[i] /= n_;
-      }
-    }
-    return buckets;
-  }
-
-  private void incrementBucketsUnsortedLevel(final int fromIndex, final int toIndex,
-      final int weight, final double[] splitPoints, final double[] buckets) {
-    for (int i = fromIndex; i < toIndex; i++) {
-      int j;
-      for (j = 0; j < splitPoints.length; j++) {
-        if (items_[i] < splitPoints[j]) {
-          break;
-        }
-      }
-      buckets[j] += weight;
-    }
-  }
-
-  private void incrementBucketsSortedLevel(final int fromIndex, final int toIndex,
-      final int weight, final double[] splitPoints, final double[] buckets) {
-    int i = fromIndex;
-    int j = 0;
-    while (i <  toIndex && j < splitPoints.length) {
-      if (items_[i] < splitPoints[j]) {
-        buckets[j] += weight; // this sample goes into this bucket
-        i++; // move on to next sample and see whether it also goes into this bucket
-      } else {
-        j++; // no more samples for this bucket
-      }
-    }
-    // now either i == toIndex (we are out of samples), or
-    // j == numSplitPoints (we are out of buckets, but there are more samples remaining)
-    // we only need to do something in the latter case
-    if (j == splitPoints.length) {
-      buckets[j] += weight * (toIndex - i);
-    }
-  }
-
-  // The following code is only valid in the special case of exactly reaching capacity while updating.
-  // It cannot be used while merging, while reducing k, or anything else.
-  private void compressWhileUpdating() {
-    final int level = findLevelToCompact();
-
-    // It is important to do add the new top level right here. Be aware that this operation
-    // grows the buffer and shifts the data and also the boundaries of the data and grows the
-    // levels array and increments numLevels_
-    if (level == numLevels_ - 1) {
-      addEmptyTopLevelToCompletelyFullSketch();
-    }
-
-    final int rawBeg = levels_[level];
-    final int rawLim = levels_[level + 1];
-    // +2 is OK because we already added a new top level if necessary
-    final int popAbove = levels_[level + 2] - rawLim;
-    final int rawPop = rawLim - rawBeg;
-    final boolean oddPop = isOdd(rawPop);
-    final int adjBeg = oddPop ? rawBeg + 1 : rawBeg;
-    final int adjPop = oddPop ? rawPop - 1 : rawPop;
-    final int halfAdjPop = adjPop / 2;
-
-    // level zero might not be sorted, so we must sort it if we wish to compact it
-    if (level == 0) {
-      Arrays.sort(items_, adjBeg, adjBeg + adjPop);
-    }
-    if (popAbove == 0) {
-      KllDoublesHelper.randomlyHalveUpDoubles(items_, adjBeg, adjPop, random);
-    } else {
-      KllDoublesHelper.randomlyHalveDownDoubles(items_, adjBeg, adjPop, random);
-      KllDoublesHelper.mergeSortedDoubleArrays(
-          items_, adjBeg, halfAdjPop,
-          items_, rawLim, popAbove,
-          items_, adjBeg + halfAdjPop);
-    }
-    levels_[level + 1] -= halfAdjPop;          // adjust boundaries of the level above
-    if (oddPop) {
-      levels_[level] = levels_[level + 1] - 1; // the current level now contains one item
-      items_[levels_[level]] = items_[rawBeg]; // namely this leftover guy
-    } else {
-      levels_[level] = levels_[level + 1];     // the current level is now empty
-    }
-
-    // verify that we freed up halfAdjPop array slots just below the current level
-    assert levels_[level] == rawBeg + halfAdjPop;
-
-    // finally, we need to shift up the data in the levels below
-    // so that the freed-up space can be used by level zero
-    if (level > 0) {
-      final int amount = rawBeg - levels_[0];
-      System.arraycopy(items_, levels_[0], items_, levels_[0] + halfAdjPop, amount);
-      for (int lvl = 0; lvl < level; lvl++) {
-        levels_[lvl] += halfAdjPop;
-      }
-    }
+  public void update(final double value) {  //possibly move proxy
+    updateDouble(value);
   }
 
-  private void addEmptyTopLevelToCompletelyFullSketch() {
-    final int curTotalCap = levels_[numLevels_];
-
-    // make sure that we are following a certain growth scheme
-    assert levels_[0] == 0; //definition of full
-    assert items_.length == curTotalCap;
-
-    // note that merging MIGHT over-grow levels_, in which case we might not have to grow it here
-    if (levels_.length < numLevels_ + 2) {
-      levels_ = KllHelper.growIntArray(levels_, numLevels_ + 2);
-    }
-
-    final int deltaCap = KllHelper.levelCapacity(k_, numLevels_ + 1, 0, m_);
-    final int newTotalCap = curTotalCap + deltaCap;
-
-    final double[] newBuf = new double[newTotalCap];
-
-    // copy (and shift) the current data into the new buffer
-    System.arraycopy(items_, levels_[0], newBuf, levels_[0] + deltaCap, curTotalCap);
-    items_ = newBuf;
-
-    // this loop includes the old "extra" index at the top
-    for (int i = 0; i <= numLevels_; i++) {
-      levels_[i] += deltaCap;
-    }
+  @Override //Used internally
+  double[] getDoubleItemsArray() { return doubleItems_; }
 
-    assert levels_[numLevels_] == newTotalCap;
+  @Override
+  double getDoubleItemsArrayAt(final int index) { return doubleItems_[index]; }
 
-    numLevels_++;
-    levels_[numLevels_] = newTotalCap; // initialize the new "extra" index at the top
-  }
+  @Override //Dummy

Review comment:
       what does this mean? it shouldn't be called? perhaps throw then?

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectSketch.java
##########
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.kll.KllPreambleUtil.extractDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractM;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractN;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
+import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class implements all the methods for the Direct (off-heap) sketches that are independent
+ * of the sketch type (float or double).
+ */
+abstract class KllDirectSketch extends KllSketch {
+  //All these members are constant for the life of this object. If the WritableMemory changes,

Review comment:
       should they be final then?

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectDoublesSketch.java
##########
@@ -0,0 +1,326 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.apache.datasketches.kll.KllPreambleUtil.DOUBLES_SKETCH_BIT_MASK;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.UPDATABLE_BIT_MASK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFamilyID;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFlags;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertM;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertPreInts;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSerVer;
+
+import org.apache.datasketches.Family;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class implements an off-heap doubles KllSketch via a WritableMemory instance of the sketch.
+ *
+ * <p>Please refer to the documentation in the package-info:<br>
+ * {@link org.apache.datasketches.kll}</p>
+ *
+ * @author Lee Rhodes, Kevin Lang
+ */
+public final class KllDirectDoublesSketch extends KllDirectSketch {
+
+  /**
+   * The actual constructor.
+   * @param wmem the current WritableMemory
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @param memVal the MemoryValadate object
+   */
+  private KllDirectDoublesSketch(final WritableMemory wmem, final MemoryRequestServer memReqSvr,
+      final MemoryValidate memVal) {
+    super(SketchType.DOUBLES_SKETCH, wmem, memReqSvr, memVal);
+  }
+
+  /**
+   * Wrap a sketch around the given source Memory containing sketch data that originated from
+   * this sketch.
+   * @param srcMem a WritableMemory that contains data.
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return instance of this sketch
+   */
+  public static KllDirectDoublesSketch writableWrap(final WritableMemory srcMem, final MemoryRequestServer memReqSvr) {
+    final MemoryValidate memVal = new MemoryValidate(srcMem);
+    return new KllDirectDoublesSketch(srcMem, memReqSvr, memVal);
+  }
+
+  /**
+   * Create a new instance of this sketch using default M.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param dstMem the given destination WritableMemory object for use by the sketch
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return a new instance of this sketch
+   */
+  public static KllDirectDoublesSketch newInstance(final int k, final WritableMemory dstMem,
+      final MemoryRequestServer memReqSvr) {
+    return newInstance(k, DEFAULT_M, dstMem, memReqSvr);
+  }
+
+  /**
+   * Create a new instance of this sketch.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param m parameter that controls the minimum level width.
+   * @param dstMem the given destination WritableMemory object for use by the sketch
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return a new instance of this sketch
+   */
+  public static KllDirectDoublesSketch newInstance(final int k, final int m, final WritableMemory dstMem,
+      final MemoryRequestServer memReqSvr) {
+    insertPreInts(dstMem, PREAMBLE_INTS_DOUBLE);
+    insertSerVer(dstMem, SERIAL_VERSION_UPDATABLE);
+    insertFamilyID(dstMem, Family.KLL.getID());
+    insertFlags(dstMem, DOUBLES_SKETCH_BIT_MASK | UPDATABLE_BIT_MASK);
+    insertK(dstMem, k);
+    insertM(dstMem, m);
+    insertN(dstMem, 0);
+    insertDyMinK(dstMem, k);
+    insertNumLevels(dstMem, 1);
+    int offset = DATA_START_ADR_DOUBLE;
+    dstMem.putIntArray(offset, new int[] {k, k}, 0, 2);
+    offset += 2 * Integer.BYTES;
+    dstMem.putDoubleArray(offset, new double[] {Double.NaN, Double.NaN}, 0, 2);
+    offset += 2 * Double.BYTES;
+    dstMem.putDoubleArray(offset, new double[k], 0, k);
+    final MemoryValidate memVal = new MemoryValidate(dstMem);
+    return new KllDirectDoublesSketch(dstMem, memReqSvr, memVal);
+  }
+
+  /**
+   * Returns an approximation to the Cumulative Distribution Function (CDF), which is the
+   * cumulative analog of the PMF, of the input stream given a set of splitPoint (values).
+   *
+   * <p>The resulting approximations have a probabilistic guarantee that can be obtained from the
+   * getNormalizedRankError(false) function.
+   *
+   * <p>If the sketch is empty this returns null.</p>
+   *
+   * @param splitPoints an array of <i>m</i> unique, monotonically increasing double values
+   * that divide the real number line into <i>m+1</i> consecutive disjoint intervals.
+   * The definition of an "interval" is inclusive of the left splitPoint (or minimum value) and
+   * exclusive of the right splitPoint, with the exception that the last interval will include
+   * the maximum value.
+   * It is not necessary to include either the min or max values in these split points.
+   *
+   * @return an array of m+1 double values on the interval [0.0, 1.0) exclusive,
+   * which are a consecutive approximation to the CDF of the input stream given the splitPoints.
+   * The value at array position j of the returned CDF array is the sum of the returned values
+   * in positions 0 through j of the returned PMF array.
+   */
+  public double[] getCDF(final double[] splitPoints) {
+    return getDoublesPmfOrCdf(splitPoints, true);
+  }
+
+  /**
+   * Returns the max value of the stream.
+   * If the sketch is empty this returns NaN.
+   *
+   * @return the max value of the stream
+   */
+  public double getMaxValue() {
+    return getMaxDoubleValue();
+  }
+
+  /**
+   * Returns the min value of the stream.
+   * If the sketch is empty this returns NaN.
+   *
+   * @return the min value of the stream
+   */
+  public double getMinValue() {
+    return getMinDoubleValue();
+  }
+
+  /**
+   * Returns an approximation to the Probability Mass Function (PMF) of the input stream
+   * given a set of splitPoints (values).
+   *
+   * <p>The resulting approximations have a probabilistic guarantee that can be obtained from the
+   * getNormalizedRankError(true) function.
+   *
+   * <p>If the sketch is empty this returns null.</p>
+   *
+   * @param splitPoints an array of <i>m</i> unique, monotonically increasing double values
+   * that divide the real number line into <i>m+1</i> consecutive disjoint intervals.
+   * The definition of an "interval" is inclusive of the left splitPoint (or minimum value) and
+   * exclusive of the right splitPoint, with the exception that the last interval will include
+   * the maximum value.
+   * It is not necessary to include either the min or max values in these split points.
+   *
+   * @return an array of m+1 doubles on the interval [0.0, 1.0) exclusive,
+   * each of which is an approximation to the fraction of the total input stream values
+   * (the mass) that fall into one of those intervals.
+   * The definition of an "interval" is inclusive of the left splitPoint and exclusive of the right
+   * splitPoint, with the exception that the last interval will include maximum value.
+   */
+  public double[] getPMF(final double[] splitPoints) {
+    return getDoublesPmfOrCdf(splitPoints, false);
+  }
+
+  /**
+   * Returns an approximation to the value of the data item
+   * that would be preceded by the given fraction of a hypothetical sorted
+   * version of the input stream so far.
+   *
+   * <p>We note that this method has a fairly large overhead (microseconds instead of nanoseconds)
+   * so it should not be called multiple times to get different quantiles from the same
+   * sketch. Instead use getQuantiles(), which pays the overhead only once.
+   *
+   * <p>If the sketch is empty this returns NaN.
+   *
+   * @param fraction the specified fractional position in the hypothetical sorted stream.
+   * These are also called normalized ranks or fractional ranks.
+   * If fraction = 0.0, the true minimum value of the stream is returned.
+   * If fraction = 1.0, the true maximum value of the stream is returned.
+   *
+   * @return the approximation to the value at the given fraction
+   */
+  public double getQuantile(final double fraction) {
+    return getDoublesQuantile(fraction);
+  }
+
+  /**
+   * Gets the lower bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%.
+   * @param fraction the given normalized rank as a fraction
+   * @return the lower bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%. Returns NaN if the sketch is empty.
+   */
+  public double getQuantileLowerBound(final double fraction) {
+    return getQuantile(max(0, fraction - KllHelper.getNormalizedRankError(getDyMinK(), false)));
+  }
+
+  /**
+   * This is a more efficient multiple-query version of getQuantile().
+   *
+   * <p>This returns an array that could have been generated by using getQuantile() with many
+   * different fractional ranks, but would be very inefficient.
+   * This method incurs the internal set-up overhead once and obtains multiple quantile values in
+   * a single query. It is strongly recommend that this method be used instead of multiple calls
+   * to getQuantile().
+   *
+   * <p>If the sketch is empty this returns null.
+   *
+   * @param fractions given array of fractional positions in the hypothetical sorted stream.
+   * These are also called normalized ranks or fractional ranks.
+   * These fractions must be in the interval [0.0, 1.0], inclusive.
+   *
+   * @return array of approximations to the given fractions in the same order as given fractions
+   * array.
+   */
+  public double[] getQuantiles(final double[] fractions) {
+    return getDoublesQuantiles(fractions);
+  }
+
+  /**
+   * This is also a more efficient multiple-query version of getQuantile() and allows the caller to
+   * specify the number of evenly spaced fractional ranks.
+   *
+   * <p>If the sketch is empty this returns null.
+   *
+   * @param numEvenlySpaced an integer that specifies the number of evenly spaced fractional ranks.
+   * This must be a positive integer greater than 0. A value of 1 will return the min value.
+   * A value of 2 will return the min and the max value. A value of 3 will return the min,
+   * the median and the max value, etc.
+   *
+   * @return array of approximations to the given fractions in the same order as given fractions
+   * array.
+   */
+  public double[] getQuantiles(final int numEvenlySpaced) {
+    if (isEmpty()) { return null; }
+    return getQuantiles(org.apache.datasketches.Util.evenlySpaced(0.0, 1.0, numEvenlySpaced));
+  }
+
+  /**
+   * Gets the upper bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%.
+   * @param fraction the given normalized rank as a fraction
+   * @return the upper bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%. Returns NaN if the sketch is empty.
+   */
+  public double getQuantileUpperBound(final double fraction) {
+    return getQuantile(min(1.0, fraction + KllHelper.getNormalizedRankError(getDyMinK(), false)));
+  }
+
+  /**
+   * Returns an approximation to the normalized (fractional) rank of the given value from 0 to 1,
+   * inclusive.
+   *
+   * <p>The resulting approximation has a probabilistic guarantee that can be obtained from the
+   * getNormalizedRankError(false) function.
+   *
+   * <p>If the sketch is empty this returns NaN.</p>
+   *
+   * @param value to be ranked
+   * @return an approximate rank of the given value
+   */
+  public double getRank(final double value) {
+    return getDoubleRank(value);
+  }
+
+  /**
+   * @return the iterator for this class
+   */
+  public KllDoublesSketchIterator iterator() {
+    return new KllDoublesSketchIterator(getDoubleItemsArray(), getLevelsArray(), getNumLevels());
+  }
+
+  /**
+   * Merges another sketch into this one.
+   * @param other sketch to merge into this one
+   */
+  public void merge(final KllSketch other) {
+    if (!other.isDirect()) { kllSketchThrow(32); }

Review comment:
       I think it would be better to use named static final values or enum instead of magic numbers

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectSketch.java
##########
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.kll.KllPreambleUtil.extractDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractM;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractN;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
+import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class implements all the methods for the Direct (off-heap) sketches that are independent
+ * of the sketch type (float or double).
+ */
+abstract class KllDirectSketch extends KllSketch {
+  //All these members are constant for the life of this object. If the WritableMemory changes,
+  // it may require rebuilding this class
+  final boolean updatable = true;
+  WritableMemory levelsArrUpdatable;
+  WritableMemory minMaxArrUpdatable;
+  WritableMemory itemsArrUpdatable;
+
+  /**
+   * For the direct sketches it is important that the methods implemented here are designed to work dynamically
+   * as the sketch grows off-heap.
+   * @param sketchType either DOUBLE_SKETCH or FLOAT_SKETCH
+   * @param wmem the current WritableMemory
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   */
+  KllDirectSketch(final SketchType sketchType, final WritableMemory wmem, final MemoryRequestServer memReqSvr,
+      final MemoryValidate memVal) {
+    super(sketchType, wmem, memReqSvr);
+    levelsArrUpdatable = memVal.levelsArrUpdatable;
+    minMaxArrUpdatable = memVal.minMaxArrUpdatable;
+    itemsArrUpdatable = memVal.itemsArrUpdatable;
+  }
+
+  @Override
+  public int getK() {
+    return extractK(wmem);
+  }
+
+  @Override
+  public int getM() {
+    return extractM(wmem);
+  }
+
+  @Override
+  public long getN() {
+    return extractN(wmem);
+  }
+
+  @Override
+  public byte[] toUpdatableByteArray() {
+    final int bytes = (int) wmem.getCapacity();
+    final byte[] byteArr = new byte[bytes];
+    wmem.getByteArray(0, byteArr, 0, bytes);
+    return byteArr;
+  }
+
+  @Override
+  double[] getDoubleItemsArray() {
+    if (sketchType == FLOATS_SKETCH) { return null; }

Review comment:
       if this is a logic error, shouldn't we throw here? otherwise a null check is needed at the calling location

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectFloatsSketch.java
##########
@@ -0,0 +1,326 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.UPDATABLE_BIT_MASK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFamilyID;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFlags;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertM;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertPreInts;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSerVer;
+
+import org.apache.datasketches.Family;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+//Intentional
+/**
+ * This class implements an off-heap floats KllSketch via a WritableMemory instance of the sketch.
+ *
+ * <p>Please refer to the documentation in the package-info:<br>
+ * {@link org.apache.datasketches.kll}</p>
+ *
+ * @author Lee Rhodes, Kevin Lang
+ */
+public final class KllDirectFloatsSketch extends KllDirectSketch {
+
+  /**
+   * The actual constructor
+   * @param wmem the current WritableMemory
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @param memVal the MemoryValadate object
+   */
+  private KllDirectFloatsSketch(final WritableMemory wmem, final MemoryRequestServer memReqSvr,
+      final MemoryValidate memVal) {
+   super(SketchType.FLOATS_SKETCH, wmem, memReqSvr, memVal);
+  }
+
+  /**
+   * Wrap a sketch around the given source Memory containing sketch data that originated from
+   * this sketch.
+   * @param srcMem a WritableMemory that contains data.
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return instance of this sketch
+   */
+  public static KllDirectFloatsSketch writableWrap(final WritableMemory srcMem, final MemoryRequestServer memReqSvr) {
+    final MemoryValidate memVal = new MemoryValidate(srcMem);
+    return new KllDirectFloatsSketch(srcMem, memReqSvr, memVal);
+  }
+
+  /**
+   * Create a new instance of this sketch using default M.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param dstMem the given destination WritableMemory object for use by the sketch
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return a new instance of this sketch
+   */
+  public static KllDirectFloatsSketch newInstance(final int k, final WritableMemory dstMem,
+      final MemoryRequestServer memReqSvr) {
+    return newInstance(k, DEFAULT_M, dstMem, memReqSvr);
+  }
+
+  /**
+   * Create a new instance of this sketch.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param m parameter that controls the minimum level width.
+   * @param dstMem the given destination WritableMemory object for use by the sketch
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   * @return a new instance of this sketch
+   */
+  public static KllDirectFloatsSketch newInstance(final int k, final int m, final WritableMemory dstMem,
+      final MemoryRequestServer memReqSvr) {
+    insertPreInts(dstMem, PREAMBLE_INTS_FLOAT);
+    insertSerVer(dstMem, SERIAL_VERSION_UPDATABLE);
+    insertFamilyID(dstMem, Family.KLL.getID());
+    insertFlags(dstMem, UPDATABLE_BIT_MASK);
+    insertK(dstMem, k);
+    insertM(dstMem, m);
+    insertN(dstMem, 0);
+    insertDyMinK(dstMem, k);
+    insertNumLevels(dstMem, 1);
+    int offset = DATA_START_ADR_FLOAT;
+    dstMem.putIntArray(offset, new int[] {k, k}, 0, 2);
+    offset += 2 * Integer.BYTES;
+    dstMem.putFloatArray(offset, new float[] {Float.NaN, Float.NaN}, 0, 2);
+    offset += 2 * Float.BYTES;
+    dstMem.putFloatArray(offset, new float[k], 0, k);
+    final MemoryValidate memVal = new MemoryValidate(dstMem);
+    return new KllDirectFloatsSketch(dstMem, memReqSvr, memVal);
+  }
+
+  /**
+   * Returns an approximation to the Cumulative Distribution Function (CDF), which is the
+   * cumulative analog of the PMF, of the input stream given a set of splitPoint (values).
+   *
+   * <p>The resulting approximations have a probabilistic guarantee that can be obtained from the
+   * getNormalizedRankError(false) function.
+   *
+   * <p>If the sketch is empty this returns null.</p>
+   *
+   * @param splitPoints an array of <i>m</i> unique, monotonically increasing float values
+   * that divide the real number line into <i>m+1</i> consecutive disjoint intervals.
+   * The definition of an "interval" is inclusive of the left splitPoint (or minimum value) and
+   * exclusive of the right splitPoint, with the exception that the last interval will include
+   * the maximum value.
+   * It is not necessary to include either the min or max values in these split points.
+   *
+   * @return an array of m+1 double values on the interval [0.0, 1.0) exclusive,
+   * which are a consecutive approximation to the CDF of the input stream given the splitPoints.
+   * The value at array position j of the returned CDF array is the sum of the returned values
+   * in positions 0 through j of the returned PMF array.
+   */
+  public double[] getCDF(final float[] splitPoints) {
+    return getFloatsPmfOrCdf(splitPoints, true);
+  }
+
+  /**
+   * Returns the max value of the stream.
+   * If the sketch is empty this returns NaN.
+   *
+   * @return the max value of the stream
+   */
+  public float getMaxValue() {
+    return getMaxFloatValue();
+  }
+
+  /**
+   * Returns the min value of the stream.
+   * If the sketch is empty this returns NaN.
+   *
+   * @return the min value of the stream
+   */
+  public float getMinValue() {
+    return getMinFloatValue();
+  }
+
+  /**
+   * Returns an approximation to the Probability Mass Function (PMF) of the input stream
+   * given a set of splitPoints (values).
+   *
+   * <p>The resulting approximations have a probabilistic guarantee that can be obtained from the
+   * getNormalizedRankError(true) function.
+   *
+   * <p>If the sketch is empty this returns null.</p>
+   *
+   * @param splitPoints an array of <i>m</i> unique, monotonically increasing float values
+   * that divide the real number line into <i>m+1</i> consecutive disjoint intervals.
+   * The definition of an "interval" is inclusive of the left splitPoint (or minimum value) and
+   * exclusive of the right splitPoint, with the exception that the last interval will include
+   * the maximum value.
+   * It is not necessary to include either the min or max values in these split points.
+   *
+   * @return an array of m+1 doubles on the interval [0.0, 1.0) exclusive,
+   * each of which is an approximation to the fraction of the total input stream values
+   * (the mass) that fall into one of those intervals.
+   * The definition of an "interval" is inclusive of the left splitPoint and exclusive of the right
+   * splitPoint, with the exception that the last interval will include maximum value.
+   */
+  public double[] getPMF(final float[] splitPoints) {
+    return getFloatsPmfOrCdf(splitPoints, false);
+  }
+
+  /**
+   * Returns an approximation to the value of the data item
+   * that would be preceded by the given fraction of a hypothetical sorted
+   * version of the input stream so far.
+   *
+   * <p>We note that this method has a fairly large overhead (microseconds instead of nanoseconds)
+   * so it should not be called multiple times to get different quantiles from the same
+   * sketch. Instead use getQuantiles(), which pays the overhead only once.
+   *
+   * <p>If the sketch is empty this returns NaN.
+   *
+   * @param fraction the specified fractional position in the hypothetical sorted stream.
+   * These are also called normalized ranks or fractional ranks.
+   * If fraction = 0.0, the true minimum value of the stream is returned.
+   * If fraction = 1.0, the true maximum value of the stream is returned.
+   *
+   * @return the approximation to the value at the given fraction
+   */
+  public float getQuantile(final double fraction) {
+    return getFloatsQuantile(fraction);
+  }
+
+  /**
+   * Gets the lower bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%.
+   * @param fraction the given normalized rank as a fraction
+   * @return the lower bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%. Returns NaN if the sketch is empty.
+   */
+  public float getQuantileLowerBound(final double fraction) {
+    return getQuantile(max(0, fraction - KllHelper.getNormalizedRankError(getDyMinK(), false)));
+  }
+
+  /**
+   * This is a more efficient multiple-query version of getQuantile().
+   *
+   * <p>This returns an array that could have been generated by using getQuantile() with many
+   * different fractional ranks, but would be very inefficient.
+   * This method incurs the internal set-up overhead once and obtains multiple quantile values in
+   * a single query. It is strongly recommend that this method be used instead of multiple calls
+   * to getQuantile().
+   *
+   * <p>If the sketch is empty this returns null.
+   *
+   * @param fractions given array of fractional positions in the hypothetical sorted stream.
+   * These are also called normalized ranks or fractional ranks.
+   * These fractions must be in the interval [0.0, 1.0], inclusive.
+   *
+   * @return array of approximations to the given fractions in the same order as given fractions
+   * array.
+   */
+  public float[] getQuantiles(final double[] fractions) {
+    return getFloatsQuantiles(fractions);
+  }
+
+  /**
+   * This is also a more efficient multiple-query version of getQuantile() and allows the caller to
+   * specify the number of evenly spaced fractional ranks.
+   *
+   * <p>If the sketch is empty this returns null.
+   *
+   * @param numEvenlySpaced an integer that specifies the number of evenly spaced fractional ranks.
+   * This must be a positive integer greater than 0. A value of 1 will return the min value.
+   * A value of 2 will return the min and the max value. A value of 3 will return the min,
+   * the median and the max value, etc.
+   *
+   * @return array of approximations to the given fractions in the same order as given fractions
+   * array.
+   */
+  public float[] getQuantiles(final int numEvenlySpaced) {
+    if (isEmpty()) { return null; }
+    return getQuantiles(org.apache.datasketches.Util.evenlySpaced(0.0, 1.0, numEvenlySpaced));
+  }
+
+  /**
+   * Gets the upper bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%.
+   * @param fraction the given normalized rank as a fraction
+   * @return the upper bound of the value interval in which the true quantile of the given rank
+   * exists with a confidence of at least 99%. Returns NaN if the sketch is empty.
+   */
+  public float getQuantileUpperBound(final double fraction) {
+    return getQuantile(min(1.0, fraction + KllHelper.getNormalizedRankError(getDyMinK(), false)));
+  }
+
+  /**
+   * Returns an approximation to the normalized (fractional) rank of the given value from 0 to 1,
+   * inclusive.
+   *
+   * <p>The resulting approximation has a probabilistic guarantee that can be obtained from the
+   * getNormalizedRankError(false) function.
+   *
+   * <p>If the sketch is empty this returns NaN.</p>
+   *
+   * @param value to be ranked
+   * @return an approximate rank of the given value
+   */
+  public double getRank(final float value) {
+    return getFloatRank(value);
+  }
+
+  /**
+   * @return the iterator for this class
+   */
+  public KllFloatsSketchIterator iterator() {
+    return new KllFloatsSketchIterator(getFloatItemsArray(), getLevelsArray(), getNumLevels());
+  }
+
+  /**
+   * Merges another sketch into this one.
+   * @param other sketch to merge into this one
+   */
+  public void merge(final KllSketch other) {
+    if (!other.isDirect()) { kllSketchThrow(32); }

Review comment:
       magic numbers

##########
File path: src/test/java/org/apache/datasketches/kll/KllDirectFloatsSketchTest.java
##########
@@ -0,0 +1,594 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+//import static org.apache.datasketches.Util.getResourceBytes; //don't have matching numbers from C++
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.memory.DefaultMemoryRequestServer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class KllDirectFloatsSketchTest {
+
+  private static final double PMF_EPS_FOR_K_8 = 0.35; // PMF rank error (epsilon) for k=8
+  private static final double PMF_EPS_FOR_K_128 = 0.025; // PMF rank error (epsilon) for k=128
+  private static final double PMF_EPS_FOR_K_256 = 0.013; // PMF rank error (epsilon) for k=256
+  private static final double NUMERIC_NOISE_TOLERANCE = 1E-6;
+  private static final DefaultMemoryRequestServer memReqSvr = new DefaultMemoryRequestServer();
+
+  @Test
+  public void empty() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(Float.NaN); // this must not change anything
+    assertTrue(sketch.isEmpty());
+    assertEquals(sketch.getN(), 0);
+    assertEquals(sketch.getNumRetained(), 0);
+    assertTrue(Double.isNaN(sketch.getRank(0)));
+    assertTrue(Float.isNaN(sketch.getMinValue()));
+    assertTrue(Float.isNaN(sketch.getMaxValue()));
+    assertTrue(Float.isNaN(sketch.getQuantile(0.5)));
+    assertNull(sketch.getQuantiles(new double[] {0}));
+    assertNull(sketch.getPMF(new float[] {0}));
+    assertNotNull(sketch.toString(true, true));
+    assertNotNull(sketch.toString());
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantileInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantile(-1.0);
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantilesInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantiles(new double[] {2.0});
+  }
+
+  @Test
+  public void oneItem() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    assertFalse(sketch.isEmpty());
+    assertEquals(sketch.getN(), 1);
+    assertEquals(sketch.getNumRetained(), 1);
+    assertEquals(sketch.getRank(1), 0.0);
+    assertEquals(sketch.getRank(2), 1.0);
+    assertEquals(sketch.getMinValue(), 1f);
+    assertEquals(sketch.getMaxValue(), 1f);
+    assertEquals(sketch.getQuantile(0.5), 1f);
+  }
+
+  @Test
+  public void manyItemsEstimationMode() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1_000_000;
+
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getN(), n);
+
+    // test getRank
+    for (int i = 0; i < n; i++) {
+      final double trueRank = (double) i / n;
+      assertEquals(sketch.getRank(i), trueRank, PMF_EPS_FOR_K_256, "for value " + i);
+    }
+
+    // test getPMF
+    final double[] pmf = sketch.getPMF(new float[] {n / 2}); // split at median
+    assertEquals(pmf.length, 2);
+    assertEquals(pmf[0], 0.5, PMF_EPS_FOR_K_256);
+    assertEquals(pmf[1], 0.5, PMF_EPS_FOR_K_256);
+
+    assertEquals(sketch.getMinValue(), 0f); // min value is exact
+    assertEquals(sketch.getQuantile(0), 0f); // min value is exact
+    assertEquals(sketch.getMaxValue(), n - 1f); // max value is exact
+    assertEquals(sketch.getQuantile(1), n - 1f); // max value is exact
+
+    // check at every 0.1 percentage point
+    final double[] fractions = new double[1001];
+    final double[] reverseFractions = new double[1001]; // check that ordering doesn't matter
+    for (int i = 0; i <= 1000; i++) {
+      fractions[i] = (double) i / 1000;
+      reverseFractions[1000 - i] = fractions[i];
+    }
+    final float[] quantiles = sketch.getQuantiles(fractions);
+    final float[] reverseQuantiles = sketch.getQuantiles(reverseFractions);
+    double previousQuantile = 0;
+    for (int i = 0; i <= 1000; i++) {
+      final double quantile = sketch.getQuantile(fractions[i]);
+      assertEquals(quantile, quantiles[i]);
+      assertEquals(quantile, reverseQuantiles[1000 - i]);
+      assertTrue(previousQuantile <= quantile);
+      previousQuantile = quantile;
+    }
+  }
+
+  @Test
+  public void getRankGetCdfGetPmfConsistency() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1000;
+    final float[] values = new float[n];
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+      values[i] = i;
+    }
+    final double[] ranks = sketch.getCDF(values);
+    final double[] pmf = sketch.getPMF(values);
+    double sumPmf = 0;
+    for (int i = 0; i < n; i++) {
+      assertEquals(ranks[i], sketch.getRank(values[i]), NUMERIC_NOISE_TOLERANCE,
+          "rank vs CDF for value " + i);
+      sumPmf += pmf[i];
+      assertEquals(ranks[i], sumPmf, NUMERIC_NOISE_TOLERANCE, "CDF vs PMF for value " + i);
+    }
+    sumPmf += pmf[n];
+    assertEquals(sumPmf, 1.0, NUMERIC_NOISE_TOLERANCE);
+    assertEquals(ranks[n], 1.0, NUMERIC_NOISE_TOLERANCE);
+  }
+
+  @Test
+  public void merge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i * 1.0F);
+      sketch2.update((2 * n - i - 1) * 1.0F);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (n - 1) * 1.0);
+
+    assertEquals(sketch2.getMinValue(), n * 1.0);
+    assertEquals(sketch2.getMaxValue(), (2 * n - 1) * 1.0);
+
+    sketch1.merge(sketch2);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2L * n);
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (2 * n - 1) * 1.0F);
+    assertEquals(sketch1.getQuantile(0.5), n * 1.0F, n * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+      sketch2.update(2 * n - i - 1);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+
+    assertEquals(sketch2.getMinValue(), n);
+    assertEquals(sketch2.getMaxValue(), 2f * n - 1f);
+
+    assertTrue(sketch1.getNormalizedRankError(false) < sketch2.getNormalizedRankError(false));
+    assertTrue(sketch1.getNormalizedRankError(true) < sketch2.getNormalizedRankError(true));
+    sketch1.merge(sketch2);
+
+    // sketch1 must get "contaminated" by the lower K in sketch2
+    assertEquals(sketch1.getNormalizedRankError(false), sketch2.getNormalizedRankError(false));
+    assertEquals(sketch1.getNormalizedRankError(true), sketch2.getNormalizedRankError(true));
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2 * n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), 2f * n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n, n * PMF_EPS_FOR_K_128);
+  }
+
+  @Test
+  public void mergeEmptyLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+
+    // rank error should not be affected by a merge with an empty sketch with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+
+    //merge the other way
+    sketch2.merge(sketch1);
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeExactModeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+    sketch2.update(1);
+
+    // rank error should not be affected by a merge with a sketch in exact mode with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+  }
+
+  @Test
+  public void mergeMinMinValueFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    sketch1.update(1);
+    sketch2.update(2);
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1.0F);
+  }
+
+  @Test
+  public void mergeMinAndMaxFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(8, 0); //was 200
+    final KllDirectFloatsSketch sketch2 = getDFSketch(8, 0); //was 200
+    for (int i = 1; i <= 9; i++) { //was 1_000_000
+      sketch1.update(i);
+    }
+    //System.out.println(sketch1.toString(true, true));
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1F);
+    assertEquals(sketch2.getMaxValue(), 9F); //was 1_000_000
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooSmall() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(DEFAULT_M - 1, 0);
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooLarge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(MAX_K + 1, 0);
+  }
+
+  @Test
+  public void minK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(DEFAULT_M, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), DEFAULT_M);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_8);
+  }
+
+  @Test
+  public void maxK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(MAX_K, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), MAX_K);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void serializeDeserializeEmpty() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeEmpty2() { //updatable serialize then new (loaded) KllDirectDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toUpdatableByteArray();
+    final KllDirectFloatsSketch sketch2 =
+        KllDirectFloatsSketch.writableWrap(WritableMemory.writableWrap(bytes),memReqSvr);
+    assertEquals(bytes.length, sketch1.getCurrentUpdatableSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeOneItem() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    sketch1.update(1);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertFalse(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), 1);
+    assertEquals(sketch2.getN(), 1);
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertFalse(Double.isNaN(sketch2.getMinValue()));
+    assertFalse(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), 8 + Float.BYTES);
+  }
+
+  @Test
+  public void serializeDeserializeOneItem2() { //updatable serialize then new (loaded) KllDirectDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    sketch1.update(1);
+    final byte[] bytes = sketch1.toUpdatableByteArray();
+    final KllDirectFloatsSketch sketch2 =
+        KllDirectFloatsSketch.writableWrap(WritableMemory.writableWrap(bytes),memReqSvr);
+    assertEquals(bytes.length, sketch1.getCurrentUpdatableSerializedSizeBytes());
+    assertFalse(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), 1);
+    assertEquals(sketch2.getN(), 1);
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertFalse(Double.isNaN(sketch2.getMinValue()));
+    assertFalse(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), 8 + Float.BYTES);
+  }
+
+  @Test
+  public void serializeDeserialize() { //compact serialize then heapify using KllDoublesSketch

Review comment:
       and again

##########
File path: src/main/java/org/apache/datasketches/kll/KllSketch.java
##########
@@ -0,0 +1,1634 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static java.lang.Math.abs;
+import static java.lang.Math.ceil;
+import static java.lang.Math.exp;
+import static java.lang.Math.log;
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static java.lang.Math.round;
+import static org.apache.datasketches.Util.isOdd;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_SINGLE_ITEM;
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.N_LONG_ADR;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_EMPTY_SINGLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_EMPTY_FULL;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_SINGLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDoubleSketchFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertEmptyFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFamilyID;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertM;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertPreInts;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSerVer;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSingleItemFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertUpdatableFlag;
+import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
+import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
+
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.datasketches.Family;
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.Util;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/*
+ * Sampled stream data (floats or doubles) is stored as an array or as part of a Memory object.
+ * This array is partitioned into sections called levels and the indices into the array of items
+ * are tracked by a small integer array called levels or levels array.
+ * The data for level i lies in positions levelsArray[i] through levelsArray[i + 1] - 1 inclusive.
+ * Hence, the levelsArray must contain (numLevels + 1) indices.
+ * The valid portion of items array is completely packed and sorted, except for level 0,
+ * which is filled from the top down. Any items below the index levelsArray[0] is garbage and will be
+ * overwritten by subsequent updates.
+ *
+ * Invariants:
+ * 1) After a compaction, or an update, or a merge, all levels are sorted except for level zero.
+ * 2) After a compaction, (sum of capacities) - (sum of items) >= 1,
+ *  so there is room for least 1 more item in level zero.
+ * 3) There are no gaps except at the bottom, so if levels_[0] = 0,
+ *  the sketch is exactly filled to capacity and must be compacted or the itemsArray and levelsArray
+ *  must be expanded to include more levels.
+ * 4) Sum of weights of all retained items == N.
+ * 5) Current total item capacity = itemsArray.length = levelsArray[numLevels].
+ */
+
+/**
+ * This class is the root of the KLL sketch class hierarchy. It includes the public API that is independent
+ * of either sketch type (float or double) and independent of whether the sketch is targeted for use on the
+ * heap or Direct (off-heap.
+ *
+ * <p>Please refer to the documentation in the package-info:<br>
+ * {@link org.apache.datasketches.kll}</p>
+ *
+ * @author Lee Rhodes, Kevin Lang
+ */
+public abstract class KllSketch {
+  static final Random random = new Random();
+  static final boolean compatible = true; //rank 0.0 and 1.0. compatible with classic Quantiles Sketch
+  //final int M = DEFAULT_M; // configured minimum buffer "width", default is 8.
+  SketchType sketchType;
+  WritableMemory wmem;
+  MemoryRequestServer memReqSvr;
+  boolean direct;
+
+  /**
+   *
+   * @param sketchType either DOUBLE_SKETCH or FLOAT_SKETCH
+   * @param wmem  the current WritableMemory or null
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   */
+  KllSketch(final SketchType sketchType, final WritableMemory wmem, final MemoryRequestServer memReqSvr) {
+   this.sketchType = sketchType;
+   this.wmem = wmem;
+   if (wmem != null) {
+     this.direct = true;
+     this.memReqSvr = memReqSvr;
+   } else {
+     this.direct = false;
+     this.memReqSvr = null;
+   }
+  }
+
+public enum SketchType { FLOATS_SKETCH, DOUBLES_SKETCH }
+
+  //Static methods
+
+  /**
+   * Gets the approximate value of <em>k</em> to use given epsilon, the normalized rank error.
+   * @param epsilon the normalized rank error between zero and one.
+   * @param pmf if true, this function returns the value of <em>k</em> assuming the input epsilon
+   * is the desired "double-sided" epsilon for the getPMF() function. Otherwise, this function
+   * returns the value of <em>k</em> assuming the input epsilon is the desired "single-sided"
+   * epsilon for all the other queries.
+   *
+   * <p>Please refer to the documentation in the package-info:<br>
+   * {@link org.apache.datasketches.kll}</p>
+   * @return the value of <i>k</i> given a value of epsilon.
+   */
+  // constants were derived as the best fit to 99 percentile empirically measured max error in
+  // thousands of trials
+  public static int getKFromEpsilon(final double epsilon, final boolean pmf) {
+    //Ensure that eps is >= than the lowest possible eps given MAX_K and pmf=false.
+    final double eps = max(epsilon, 4.7634E-5);
+    final double kdbl = pmf
+        ? exp(log(2.446 / eps) / 0.9433)
+        : exp(log(2.296 / eps) / 0.9723);
+    final double krnd = round(kdbl);
+    final double del = abs(krnd - kdbl);
+    final int k = (int) (del < 1E-6 ? krnd : ceil(kdbl));
+    return max(2, min(MAX_K, k));
+  }
+
+  /**
+   * Returns upper bound on the compact serialized size of a FloatsSketch given a parameter
+   * <em>k</em> and stream length. This method can be used if allocation of storage
+   * is necessary beforehand. This assumes the DEFAULT_M = 8 used in older sketches, it will not
+   * work with other values of m.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param n stream length
+   * @return upper bound on the compact serialized size
+   * @deprecated use {@link #getMaxSerializedSizeBytes(int, int, long, SketchType, boolean)} instead.
+   */
+  @Deprecated
+  public static int getMaxSerializedSizeBytes(final int k, final long n) {
+    final KllHelper.GrowthStats gStats =  KllHelper.getGrowthSchemeForGivenN(k, DEFAULT_M, n, FLOATS_SKETCH, false);
+    return gStats.compactBytes;
+  }
+
+  /**
+   * Returns upper bound on the serialized size of a KllSketch given the following parameters.
+   * @param k parameter that controls size of the sketch and accuracy of estimates
+   * @param m parameter that controls the smallest value of k, and the smallest level width.
+   * If in doubt, use the default value of 8.
+   * @param n stream length
+   * @param sketchType either DOUBLES_SKETCH or FLOATS_SKETCH
+   * @param updatable true if updatable form, otherwise the standard compact form.
+   * @return upper bound on the serialized size of a KllSketch.
+   */
+  public static int getMaxSerializedSizeBytes(final int k, final int m, final long n,
+      final SketchType sketchType, final boolean updatable) {
+    final KllHelper.GrowthStats gStats = KllHelper.getGrowthSchemeForGivenN(k, m, n, sketchType, false);
+    return updatable ? gStats.updatableBytes : gStats.compactBytes;
+  }
+
+  /**
+   * Gets the normalized rank error given k and pmf.
+   * Static method version of the <i>getNormalizedRankError(boolean)</i>.
+   * @param k the configuration parameter
+   * @param pmf if true, returns the "double-sided" normalized rank error for the getPMF() function.
+   * Otherwise, it is the "single-sided" normalized rank error for all the other queries.
+   * @return if pmf is true, the normalized rank error for the getPMF() function.
+   * Otherwise, it is the "single-sided" normalized rank error for all the other queries.
+   */
+  public static double getNormalizedRankError(final int k, final boolean pmf) {
+    return KllHelper.getNormalizedRankError(k, pmf);
+  }
+
+  /**
+   * Returns the current number of bytes this Sketch would require if serialized.
+   * @return the number of bytes this sketch would require if serialized.
+   */
+  public int getSerializedSizeBytes() {
+    return (direct)
+        ? getCurrentUpdatableSerializedSizeBytes()
+        : getCurrentCompactSerializedSizeBytes();
+  }
+
+  static int getSerializedSizeBytes(final int numLevels, final int numItems,
+      final SketchType sketchType, final boolean updatable) {
+    int levelsBytes = 0;
+    if (!updatable) {
+      if (numItems == 0) { return N_LONG_ADR; }
+      if (numItems == 1) {
+        return DATA_START_ADR_SINGLE_ITEM + (sketchType == DOUBLES_SKETCH ? Double.BYTES : Float.BYTES);
+      }
+      levelsBytes = numLevels * Integer.BYTES;
+    } else {
+      levelsBytes = (numLevels + 1) * Integer.BYTES;
+    }
+    if (sketchType == DOUBLES_SKETCH) {
+      return DATA_START_ADR_DOUBLE + levelsBytes + (numItems + 2) * Double.BYTES; //+2 is for min & max
+    } else {
+      return DATA_START_ADR_FLOAT + levelsBytes + (numItems + 2) * Float.BYTES;
+    }
+  }
+
+  final static boolean isCompatible() {
+    return compatible;
+  }
+
+  final static void kllSketchThrow(final int errNo) {
+    String msg = "";
+    switch (errNo) {
+      case 30: msg = "Given sketch Memory is immutable, cannot write."; break;
+      case 31: msg = "Given sketch Memory is immutable and incompatible."; break;
+      case 32: msg = "Given sketch must be of type Direct."; break;
+      case 33: msg = "Given sketch must be of type Double."; break;
+      case 34: msg = "Given sketch must be of type Float."; break;
+      case 35: msg = "Given sketch must not be of type Direct."; break;
+      default: msg = "Unknown error: errNo: " + errNo; break;
+    }
+    throw new SketchesArgumentException(msg);
+  }
+
+  //Public Non-static methods
+
+  /**
+   * Returns the current compact number of bytes this sketch would require to store.
+   * @return the current compact number of bytes this sketch would require to store.
+   */
+  public final int getCurrentCompactSerializedSizeBytes() {
+    return KllSketch.getSerializedSizeBytes(getNumLevels(), getNumRetained(), sketchType, false);
+  }
+
+  /**
+   * Returns the current updatable number of bytes this sketch would require to store.
+   * @return the current updatable number of bytes this sketch would require to store.
+   */
+  public final int getCurrentUpdatableSerializedSizeBytes() {
+    final int itemCap = KllHelper.computeTotalItemCapacity(getK(), getM(), getNumLevels());
+    return KllSketch.getSerializedSizeBytes(getNumLevels(), itemCap, sketchType, true);
+  }
+
+  /**
+   * Returns the user configured parameter k
+   * @return the user configured parameter k
+   */
+  public abstract int getK();
+
+  /**
+   * Returns the user configured parameter m
+   * @return the user configured parameter m
+   */
+  public abstract int getM();
+
+  /**
+   * Returns the length of the input stream in items.
+   * @return stream length
+   */
+  public abstract long getN();
+
+  /**
+   * Gets the approximate rank error of this sketch normalized as a fraction between zero and one.
+   * @param pmf if true, returns the "double-sided" normalized rank error for the getPMF() function.
+   * Otherwise, it is the "single-sided" normalized rank error for all the other queries.
+   * The epsilon value returned is a best fit to 99 percentile empirically measured max error in
+   * thousands of trials
+   * @return if pmf is true, returns the normalized rank error for the getPMF() function.
+   * Otherwise, it is the "single-sided" normalized rank error for all the other queries.
+   *
+   * <p>Please refer to the documentation in the package-info:<br>
+   * {@link org.apache.datasketches.kll}</p>
+   */
+  public final double getNormalizedRankError(final boolean pmf) {
+    return getNormalizedRankError(getDyMinK(), pmf);
+  }
+
+  /**
+   * Returns the number of retained items (samples) in the sketch.
+   * @return the number of retained items (samples) in the sketch
+   */
+  public final int getNumRetained() {
+    return getLevelsArray()[getNumLevels()] - getLevelsArray()[0];
+  }
+
+  /**
+   * This returns the WritableMemory for Direct type sketches,
+   * otherwise returns null.
+   * @return the WritableMemory for Direct type sketches, otherwise null.
+   */
+  public WritableMemory getWritableMemory() {
+    return wmem;
+  }
+
+  public final boolean isDirect() {
+    return direct;
+  }
+
+  /**
+   * Returns true if this sketch is empty.
+   * @return empty flag
+   */
+  public final boolean isEmpty() {
+    return getN() == 0;
+  }
+
+  /**
+   * Returns true if this sketch is in estimation mode.
+   * @return estimation mode flag
+   */
+  public final boolean isEstimationMode() {
+    return getNumLevels() > 1;
+  }
+
+  /**
+   * Returns serialized sketch in a compact byte array form.
+   * @return serialized sketch in a compact byte array form.
+   */
+  public byte[] toByteArray() {
+    return toCompactByteArrayImpl();
+  }
+
+  @Override
+  public final String toString() {
+    return toString(false, false);
+  }
+
+  /**
+   * Returns a summary of the sketch as a string.
+   * @param withLevels if true include information about levels
+   * @param withData if true include sketch data
+   * @return string representation of sketch summary
+   */
+  public String toString(final boolean withLevels, final boolean withData) {
+    return toStringImpl(withLevels, withData);
+  }
+
+  /**
+   * Returns serialized sketch in an updatable byte array form.
+   * @return serialized sketch in an updatable byte array form.
+   */
+  public byte[] toUpdatableByteArray() {
+    return toUpdatableByteArrayImpl();
+  }
+
+  //package-private non-static methods
+
+  final void buildHeapKllSketchFromMemory(final MemoryValidate memVal) {
+    final boolean doubleType = (sketchType == DOUBLES_SKETCH);
+    final boolean updatable = memVal.updatable;
+    setLevelZeroSorted(memVal.level0Sorted);
+    setN(memVal.n);
+    setDyMinK(memVal.dyMinK);
+    setNumLevels(memVal.numLevels);
+    final int[] myLevelsArr = new int[getNumLevels() + 1];
+
+    if (updatable) {
+      memVal.levelsArrUpdatable.getIntArray(0, myLevelsArr, 0, getNumLevels() + 1);
+      setLevelsArray(myLevelsArr);
+      if (doubleType) {
+        setMinDoubleValue(memVal.minMaxArrUpdatable.getDouble(0));
+        setMaxDoubleValue(memVal.minMaxArrUpdatable.getDouble(Double.BYTES));
+        final int itemsCap = (int)memVal.itemsArrUpdatable.getCapacity() / Double.BYTES;
+        final double[] myItemsArr = new double[itemsCap];
+        memVal.itemsArrUpdatable.getDoubleArray(0, myItemsArr, 0, itemsCap);
+        setDoubleItemsArray(myItemsArr);
+      } else { //float
+        setMinFloatValue(memVal.minMaxArrUpdatable.getFloat(0));
+        setMaxFloatValue(memVal.minMaxArrUpdatable.getFloat(Float.BYTES));
+        final int itemsCap = (int)memVal.itemsArrUpdatable.getCapacity() / Float.BYTES;
+        final float[] myItemsArr = new float[itemsCap];
+        memVal.itemsArrUpdatable.getFloatArray(0, myItemsArr, 0, itemsCap);
+        setFloatItemsArray(myItemsArr);
+      }
+    } else { //compact
+      memVal.levelsArrCompact.getIntArray(0, myLevelsArr, 0, getNumLevels() + 1);
+      setLevelsArray(myLevelsArr);
+      if (doubleType) {
+        setMinDoubleValue(memVal.minMaxArrCompact.getDouble(0));
+        setMaxDoubleValue(memVal.minMaxArrCompact.getDouble(Double.BYTES));
+        final int itemsCap = (int)memVal.itemsArrCompact.getCapacity() / Double.BYTES;
+        final double[] myItemsArr = new double[itemsCap];
+        memVal.itemsArrCompact.getDoubleArray(0, myItemsArr, 0, itemsCap);
+        setDoubleItemsArray(myItemsArr);
+      } else { //float
+        setMinFloatValue(memVal.minMaxArrCompact.getFloat(0));
+        setMaxFloatValue(memVal.minMaxArrCompact.getFloat(Float.BYTES));
+        final int itemsCap = (int)memVal.itemsArrCompact.getCapacity() / Float.BYTES;
+        final float[] myItemsArr = new float[itemsCap];
+        memVal.itemsArrCompact.getFloatArray(0, myItemsArr, 0, itemsCap);
+        setFloatItemsArray(myItemsArr);
+      }
+    }
+  }
+
+  /**
+   * @return full size of internal items array including garbage; for a floats sketch this will be null.
+   */
+  abstract double[] getDoubleItemsArray();
+
+  final double getDoubleRank(final double value) {
+    if (isEmpty()) { return Double.NaN; }
+    int level = 0;
+    int weight = 1;
+    long total = 0;
+    final double[] myDoubleItemsArr = getDoubleItemsArray();
+    final int[] myLevelsArr = getLevelsArray();
+    while (level < getNumLevels()) {
+      final int fromIndex = myLevelsArr[level];
+      final int toIndex = myLevelsArr[level + 1]; // exclusive
+      for (int i = fromIndex; i < toIndex; i++) {
+        if (myDoubleItemsArr[i] < value) {
+          total += weight;
+        } else if (level > 0 || isLevelZeroSorted()) {
+          break; // levels above 0 are sorted, no point comparing further
+        }
+      }
+      level++;
+      weight *= 2;
+    }
+    return (double) total / getN();
+  }
+
+  final double[] getDoublesPmfOrCdf(final double[] splitPoints, final boolean isCdf) {
+    if (isEmpty()) { return null; }
+    KllDoublesHelper.validateDoubleValues(splitPoints);
+    final double[] buckets = new double[splitPoints.length + 1];
+    final int myNumLevels = getNumLevels();
+    final int[] myLevelsArr = getLevelsArray();
+    int level = 0;
+    int weight = 1;
+    while (level < myNumLevels) {
+      final int fromIndex = myLevelsArr[level];
+      final int toIndex = myLevelsArr[level + 1]; // exclusive
+      if (level == 0 && !isLevelZeroSorted()) {
+        incrementDoublesBucketsUnsortedLevel(fromIndex, toIndex, weight, splitPoints, buckets);
+      } else {
+        incrementDoublesBucketsSortedLevel(fromIndex, toIndex, weight, splitPoints, buckets);
+      }
+      level++;
+      weight *= 2;
+    }
+    // normalize and, if CDF, convert to cumulative
+    if (isCdf) {
+      double subtotal = 0;
+      for (int i = 0; i < buckets.length; i++) {
+        subtotal += buckets[i];
+        buckets[i] = subtotal / getN();
+      }
+    } else {
+      for (int i = 0; i < buckets.length; i++) {
+        buckets[i] /= getN();
+      }
+    }
+    return buckets;
+  }
+
+  final double getDoublesQuantile(final double fraction) {
+    if (isEmpty()) { return Double.NaN; }
+    if (fraction < 0.0 || fraction > 1.0) {
+      throw new SketchesArgumentException("Fraction cannot be less than zero nor greater than 1.0");
+    }
+    if (isCompatible()) {
+      if (fraction == 0.0) { return getMinDoubleValue(); }
+      if (fraction == 1.0) { return getMaxDoubleValue(); }
+    }
+    final KllDoublesQuantileCalculator quant = getDoublesQuantileCalculator();
+    return quant.getQuantile(fraction);
+  }
+
+  final double[] getDoublesQuantiles(final double[] fractions) {
+    if (isEmpty()) { return null; }
+    KllDoublesQuantileCalculator quant = null;
+    final double[] quantiles = new double[fractions.length];
+    for (int i = 0; i < fractions.length; i++) {
+      final double fraction = fractions[i];
+      if (fraction < 0.0 || fraction > 1.0) {
+        throw new SketchesArgumentException("Fraction cannot be less than zero nor greater than 1.0");
+      }
+      if      (fraction == 0.0 && isCompatible()) { quantiles[i] = getMinDoubleValue(); }
+      else if (fraction == 1.0 && isCompatible()) { quantiles[i] = getMaxDoubleValue(); }
+      else {
+        if (quant == null) {
+          quant = getDoublesQuantileCalculator();
+        }
+        quantiles[i] = quant.getQuantile(fraction);
+      }
+    }
+    return quantiles;
+  }
+
+  abstract int getDyMinK();

Review comment:
       Not clear from the name what is this, and no doc

##########
File path: src/main/java/org/apache/datasketches/kll/MemoryValidate.java
##########
@@ -0,0 +1,338 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.Family.idToFamily;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_SINGLE_ITEM;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_EMPTY_SINGLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_EMPTY_FULL;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_SINGLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractDoubleSketchFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractEmptyFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractFamilyID;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractFlags;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractM;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractN;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractPreInts;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractSerVer;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractSingleItemFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractUpdatableFlag;
+
+import org.apache.datasketches.Family;
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.kll.KllPreambleUtil.Layout;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class performs all the error checking of an incoming Memory object and extracts the key fields in the process.
+ * This is used by all sketches that read or import Memory objects.
+ *
+ * @author lrhodes
+ *
+ */
+final class MemoryValidate {
+  // first 8 bytes
+  final int preInts; // = extractPreInts(srcMem);
+  final int serVer;
+  final int familyID;
+  final String famName;
+  final int flags;
+  boolean empty;
+  boolean singleItem;
+  final boolean level0Sorted;
+  final boolean doublesSketch;
+  final boolean updatable;
+  final int k;
+  final int m;
+  final int memCapacity;
+
+  Layout layout;
+  // depending on the layout, the next 8-16 bytes of the preamble, may be filled with assumed values.
+  // For example, if the layout is compact & empty, n = 0, if compact and single, n = 1, etc.
+  long n;
+  // next 4 bytes
+  int dyMinK;
+  int numLevels;
+  // derived
+  int capacityItems; //capacity of Items array for exporting and for Updatable form
+  int itemsRetained; //actual items retained in Compact form
+  int itemsArrStart;
+  int sketchBytes;
+  Memory levelsArrCompact; //if sk = empty or single, this is derived
+  Memory minMaxArrCompact; //if sk = empty or single, this is derived
+  Memory itemsArrCompact;  //if sk = empty or single, this is derived
+  WritableMemory levelsArrUpdatable;
+  WritableMemory minMaxArrUpdatable;
+  WritableMemory itemsArrUpdatable;
+
+  MemoryValidate(final Memory srcMem) {
+    memCapacity = (int) srcMem.getCapacity();
+    preInts = extractPreInts(srcMem);
+    serVer = extractSerVer(srcMem);
+
+    familyID = extractFamilyID(srcMem);
+    if (familyID != Family.KLL.getID()) { memoryValidateThrow(0, familyID); }
+    famName = idToFamily(familyID).toString();
+    flags = extractFlags(srcMem);
+    empty = extractEmptyFlag(srcMem);
+    level0Sorted  = extractLevelZeroSortedFlag(srcMem);
+    singleItem    = extractSingleItemFlag(srcMem);
+    doublesSketch = extractDoubleSketchFlag(srcMem);
+    updatable    = extractUpdatableFlag(srcMem);
+    k = extractK(srcMem);
+    m = extractM(srcMem);
+    KllHelper.checkM(m);
+    KllHelper.checkK(k, m);
+    if ((serVer == SERIAL_VERSION_UPDATABLE) ^ updatable) { memoryValidateThrow(10, 0); }
+
+    if (updatable) { updatableMemoryValidate((WritableMemory) srcMem); }
+    else { compactMemoryValidate(srcMem); }
+  }
+
+  void compactMemoryValidate(final Memory srcMem) {
+    if (empty && singleItem) { memoryValidateThrow(20, 0); }
+    final int sw = (empty ? 1 : 0) | (singleItem ? 4 : 0) | (doublesSketch ? 8 : 0);
+    switch (sw) {
+      case 0: { //FLOAT_FULL_COMPACT
+        if (preInts != PREAMBLE_INTS_FLOAT) { memoryValidateThrow(6, preInts); }
+        if (serVer != SERIAL_VERSION_EMPTY_FULL) { memoryValidateThrow(2, serVer); }
+        layout = Layout.FLOAT_FULL_COMPACT;
+        n = extractN(srcMem);
+        dyMinK = extractDyMinK(srcMem);
+        numLevels = extractNumLevels(srcMem);
+        int offset = DATA_START_ADR_FLOAT;
+        // LEVELS MEM
+        final int[] myLevelsArr = new int[numLevels + 1];
+        srcMem.getIntArray(offset, myLevelsArr, 0, numLevels); //copies all except the last one
+        myLevelsArr[numLevels] = KllHelper.computeTotalItemCapacity(k, m, numLevels); //load the last one
+        levelsArrCompact = Memory.wrap(myLevelsArr); //separate from srcMem,
+        offset += (int)levelsArrCompact.getCapacity() - Integer.BYTES; // but one larger than srcMem
+        // MIN/MAX MEM
+        minMaxArrCompact = srcMem.region(offset, 2L * Float.BYTES);
+        offset += (int)minMaxArrCompact.getCapacity();
+        // ITEMS MEM
+        itemsArrStart = offset;
+        capacityItems = myLevelsArr[numLevels];
+        itemsRetained = capacityItems - myLevelsArr[0];
+        final float[] myItemsArr = new float[capacityItems];
+        srcMem.getFloatArray(offset, myItemsArr, myLevelsArr[0], itemsRetained);
+        itemsArrCompact = Memory.wrap(myItemsArr);
+        sketchBytes = offset + itemsRetained * Float.BYTES;
+        break;
+      }
+      case 1: { //FLOAT_EMPTY_COMPACT
+        if (preInts != PREAMBLE_INTS_EMPTY_SINGLE) { memoryValidateThrow(1, preInts); }
+        if (serVer != SERIAL_VERSION_EMPTY_FULL) { memoryValidateThrow(2, serVer); }
+        layout = Layout.FLOAT_EMPTY_COMPACT;
+        n = 0;           //assumed
+        dyMinK = k;      //assumed
+        numLevels = 1;   //assumed
+        // LEVELS MEM
+        levelsArrCompact = Memory.wrap(new int[] {k, k});
+        // MIN/MAX MEM
+        minMaxArrCompact = Memory.wrap(new float[] {Float.NaN, Float.NaN});
+        // ITEMS MEM
+        capacityItems = k;
+        itemsRetained = 0;
+        itemsArrCompact = Memory.wrap(new float[k]);
+        sketchBytes = DATA_START_ADR_SINGLE_ITEM; //also used for empty
+        itemsArrStart = DATA_START_ADR_SINGLE_ITEM;
+        break;
+      }
+      case 4: { //FLOAT_SINGLE_COMPACT
+        if (preInts != PREAMBLE_INTS_EMPTY_SINGLE) { memoryValidateThrow(1, preInts); }
+        if (serVer != SERIAL_VERSION_SINGLE) { memoryValidateThrow(4, serVer); }
+        layout = Layout.FLOAT_SINGLE_COMPACT;
+        n = 1;
+        dyMinK = k;
+        numLevels = 1;
+        // LEVELS MEM
+        levelsArrCompact = Memory.wrap(new int[] {k - 1, k});
+        final float minMax = srcMem.getFloat(DATA_START_ADR_SINGLE_ITEM);
+        // MIN/MAX MEM
+        minMaxArrCompact = Memory.wrap(new float[] {minMax, minMax});
+        // ITEMS MEM
+        capacityItems = k;
+        itemsRetained = 1;
+        final float[] myFloatItems = new float[k];
+        myFloatItems[k - 1] = minMax;
+        itemsArrCompact = Memory.wrap(myFloatItems);
+        sketchBytes = DATA_START_ADR_SINGLE_ITEM + Float.BYTES;
+        itemsArrStart = DATA_START_ADR_SINGLE_ITEM;
+        break;
+      }
+      case 8: { //DOUBLE_FULL_COMPACT
+        if (preInts != PREAMBLE_INTS_DOUBLE) { memoryValidateThrow(5, preInts); }
+        if (serVer != SERIAL_VERSION_EMPTY_FULL) { memoryValidateThrow(2, serVer); }
+        layout = Layout.DOUBLE_FULL_COMPACT;
+        n = extractN(srcMem);
+        dyMinK = extractDyMinK(srcMem);
+        numLevels = extractNumLevels(srcMem);
+        int offset = DATA_START_ADR_DOUBLE;
+        // LEVELS MEM
+        final int[] myLevelsArr = new int[numLevels + 1];
+        srcMem.getIntArray(offset, myLevelsArr, 0, numLevels); //all except the last one
+        myLevelsArr[numLevels] = KllHelper.computeTotalItemCapacity(k, m, numLevels); //load the last one
+        levelsArrCompact = Memory.wrap(myLevelsArr); //separate from srcMem
+        offset += (int)levelsArrCompact.getCapacity() - Integer.BYTES;
+        // MIN/MAX MEM
+        minMaxArrCompact = srcMem.region(offset, 2L * Double.BYTES);
+        offset += (int)minMaxArrCompact.getCapacity();
+        // ITEMS MEM
+        itemsArrStart = offset;
+        capacityItems = myLevelsArr[numLevels];
+        itemsRetained = capacityItems - myLevelsArr[0];
+        final double[] myItemsArr = new double[capacityItems];
+        srcMem.getDoubleArray(offset, myItemsArr, myLevelsArr[0], itemsRetained);
+        itemsArrCompact = Memory.wrap(myItemsArr);
+        sketchBytes = offset + itemsRetained * Double.BYTES;
+        break;
+      }
+      case 9: { //DOUBLE_EMPTY_COMPACT
+        if (preInts != PREAMBLE_INTS_EMPTY_SINGLE) { memoryValidateThrow(1, preInts); }
+        if (serVer != SERIAL_VERSION_EMPTY_FULL) { memoryValidateThrow(2, serVer); }
+        layout = Layout.DOUBLE_EMPTY_COMPACT;
+        n = 0;
+        dyMinK = k;
+        numLevels = 1;
+
+        // LEVELS MEM
+        levelsArrCompact = Memory.wrap(new int[] {k, k});
+        // MIN/MAX MEM
+        minMaxArrCompact = Memory.wrap(new double[] {Double.NaN, Double.NaN});
+        // ITEMS MEM
+        capacityItems = k;
+        itemsRetained = 0;
+        itemsArrCompact = Memory.wrap(new double[k]);
+        sketchBytes = DATA_START_ADR_SINGLE_ITEM; //also used for empty
+        itemsArrStart = DATA_START_ADR_SINGLE_ITEM;
+        break;
+      }
+      case 12: { //DOUBLE_SINGLE_COMPACT
+        if (preInts != PREAMBLE_INTS_EMPTY_SINGLE) { memoryValidateThrow(1, preInts); }
+        if (serVer != SERIAL_VERSION_SINGLE) { memoryValidateThrow(4, serVer); }
+        layout = Layout.DOUBLE_SINGLE_COMPACT;
+        n = 1;
+        dyMinK = k;
+        numLevels = 1;
+
+        // LEVELS MEM
+        levelsArrCompact = Memory.wrap(new int[] {k - 1, k});
+        final double minMax = srcMem.getDouble(DATA_START_ADR_SINGLE_ITEM);
+        // MIN/MAX MEM
+        minMaxArrCompact = Memory.wrap(new double[] {minMax, minMax});
+        // ITEMS MEM
+        capacityItems = k;
+        itemsRetained = 1;
+        final double[] myDoubleItems = new double[k];
+        myDoubleItems[k - 1] = minMax;
+        itemsArrCompact = Memory.wrap(myDoubleItems);
+        sketchBytes = DATA_START_ADR_SINGLE_ITEM + Double.BYTES;
+        itemsArrStart = DATA_START_ADR_SINGLE_ITEM;
+        break;
+      }
+      default: break; //can't happen
+    }
+  }
+
+  void updatableMemoryValidate(final WritableMemory wSrcMem) {
+    if (doublesSketch) { //DOUBLE_UPDATABLE
+      if (preInts != PREAMBLE_INTS_DOUBLE) { memoryValidateThrow(5, preInts); }
+      layout = Layout.DOUBLE_UPDATABLE;
+      n = extractN(wSrcMem);
+      empty = n == 0;       //empty & singleItem are set for convenience
+      singleItem = n == 1;  // there is no error checking on these bits
+      dyMinK = extractDyMinK(wSrcMem);
+      numLevels = extractNumLevels(wSrcMem);
+
+      int offset = DATA_START_ADR_DOUBLE;
+      //LEVELS
+      levelsArrUpdatable = wSrcMem.writableRegion(offset, (numLevels + 1L) * Integer.BYTES);
+      offset += (int)levelsArrUpdatable.getCapacity();
+      //MIN/MAX
+      minMaxArrUpdatable = wSrcMem.writableRegion(offset, 2L * Double.BYTES);
+      offset += (int)minMaxArrUpdatable.getCapacity();
+      //ITEMS
+      capacityItems = levelsArrUpdatable.getInt((long)numLevels * Integer.BYTES);
+      final int itemsArrBytes = capacityItems * Double.BYTES;
+      itemsArrStart = offset;
+      itemsArrUpdatable = wSrcMem.writableRegion(offset, itemsArrBytes);
+      sketchBytes = offset + itemsArrBytes;
+    }
+    else { //FLOAT_UPDATABLE
+      if (preInts != PREAMBLE_INTS_FLOAT) { memoryValidateThrow(6, preInts); }
+      layout = Layout.FLOAT_UPDATABLE;
+      n = extractN(wSrcMem);
+      empty = n == 0;       //empty & singleItem are set for convenience
+      singleItem = n == 1;  // there is no error checking on these bits
+      dyMinK = extractDyMinK(wSrcMem);
+      numLevels = extractNumLevels(wSrcMem);
+      int offset = DATA_START_ADR_FLOAT;
+      //LEVELS
+      levelsArrUpdatable = wSrcMem.writableRegion(offset, (numLevels + 1L) * Integer.BYTES);
+      offset += (int)levelsArrUpdatable.getCapacity();
+      //MIN/MAX
+      minMaxArrUpdatable = wSrcMem.writableRegion(offset, 2L * Float.BYTES);
+      offset += (int)minMaxArrUpdatable.getCapacity();
+      //ITEMS
+      capacityItems = levelsArrUpdatable.getInt((long)numLevels * Integer.BYTES);
+      final int itemsArrBytes = capacityItems * Float.BYTES;
+      itemsArrStart = offset;
+      itemsArrUpdatable = wSrcMem.writableRegion(offset, itemsArrBytes);
+      sketchBytes = itemsArrStart + itemsArrBytes;
+    }
+  }
+
+  private static void memoryValidateThrow(final int errNo, final int value) {
+    String msg = "";
+    switch (errNo) {
+      case 0: msg = "FamilyID Field must be: " + Family.KLL.getID() + ", NOT: " + value; break;
+      case 1: msg = "Empty Bit: 1 -> PreInts: " + PREAMBLE_INTS_EMPTY_SINGLE + ", NOT: " + value; break;
+      case 2: msg = "Empty Bit: 1 -> SerVer: " + SERIAL_VERSION_EMPTY_FULL + ", NOT: " + value; break;
+      //case 3: msg = "Single Item Bit: 1 -> PreInts: " + PREAMBLE_INTS_EMPTY_SINGLE + ", NOT: " + value; break;
+      case 4: msg = "Single Item Bit: 1 -> SerVer: " + SERIAL_VERSION_SINGLE + ", NOT: " + value; break;
+      case 5: msg = "Double Sketch Bit: 1 -> PreInts: " + PREAMBLE_INTS_DOUBLE + ", NOT: " + value; break;
+      case 6: msg = "Double Sketch Bit: 0 -> PreInts: " + PREAMBLE_INTS_FLOAT + ", NOT: " + value; break;
+      //case 7: msg = "The M field must be set to " + DEFAULT_M + ", NOT: " + value; break;
+      //case 8: msg = "The dynamic MinK must be equal to K, NOT: " + value; break;
+      //case 9: msg = "numLevels must be one, NOT: " + value; break;
+      case 10: msg = "((SerVer == 3) ^ (Updatable Bit)) must = 0."; break;
+      case 20: msg = "Empty flag bit and SingleItem flag bit cannot both be set. Flags: " + value; break;
+      //case 21: msg = "N != 0 and empty bit is set. N: " + value; break;

Review comment:
       unused code?

##########
File path: src/test/java/org/apache/datasketches/kll/KllDirectFloatsSketchTest.java
##########
@@ -0,0 +1,594 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+//import static org.apache.datasketches.Util.getResourceBytes; //don't have matching numbers from C++
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.memory.DefaultMemoryRequestServer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class KllDirectFloatsSketchTest {
+
+  private static final double PMF_EPS_FOR_K_8 = 0.35; // PMF rank error (epsilon) for k=8
+  private static final double PMF_EPS_FOR_K_128 = 0.025; // PMF rank error (epsilon) for k=128
+  private static final double PMF_EPS_FOR_K_256 = 0.013; // PMF rank error (epsilon) for k=256
+  private static final double NUMERIC_NOISE_TOLERANCE = 1E-6;
+  private static final DefaultMemoryRequestServer memReqSvr = new DefaultMemoryRequestServer();
+
+  @Test
+  public void empty() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(Float.NaN); // this must not change anything
+    assertTrue(sketch.isEmpty());
+    assertEquals(sketch.getN(), 0);
+    assertEquals(sketch.getNumRetained(), 0);
+    assertTrue(Double.isNaN(sketch.getRank(0)));
+    assertTrue(Float.isNaN(sketch.getMinValue()));
+    assertTrue(Float.isNaN(sketch.getMaxValue()));
+    assertTrue(Float.isNaN(sketch.getQuantile(0.5)));
+    assertNull(sketch.getQuantiles(new double[] {0}));
+    assertNull(sketch.getPMF(new float[] {0}));
+    assertNotNull(sketch.toString(true, true));
+    assertNotNull(sketch.toString());
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantileInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantile(-1.0);
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantilesInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantiles(new double[] {2.0});
+  }
+
+  @Test
+  public void oneItem() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    assertFalse(sketch.isEmpty());
+    assertEquals(sketch.getN(), 1);
+    assertEquals(sketch.getNumRetained(), 1);
+    assertEquals(sketch.getRank(1), 0.0);
+    assertEquals(sketch.getRank(2), 1.0);
+    assertEquals(sketch.getMinValue(), 1f);
+    assertEquals(sketch.getMaxValue(), 1f);
+    assertEquals(sketch.getQuantile(0.5), 1f);
+  }
+
+  @Test
+  public void manyItemsEstimationMode() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1_000_000;
+
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getN(), n);
+
+    // test getRank
+    for (int i = 0; i < n; i++) {
+      final double trueRank = (double) i / n;
+      assertEquals(sketch.getRank(i), trueRank, PMF_EPS_FOR_K_256, "for value " + i);
+    }
+
+    // test getPMF
+    final double[] pmf = sketch.getPMF(new float[] {n / 2}); // split at median
+    assertEquals(pmf.length, 2);
+    assertEquals(pmf[0], 0.5, PMF_EPS_FOR_K_256);
+    assertEquals(pmf[1], 0.5, PMF_EPS_FOR_K_256);
+
+    assertEquals(sketch.getMinValue(), 0f); // min value is exact
+    assertEquals(sketch.getQuantile(0), 0f); // min value is exact
+    assertEquals(sketch.getMaxValue(), n - 1f); // max value is exact
+    assertEquals(sketch.getQuantile(1), n - 1f); // max value is exact
+
+    // check at every 0.1 percentage point
+    final double[] fractions = new double[1001];
+    final double[] reverseFractions = new double[1001]; // check that ordering doesn't matter
+    for (int i = 0; i <= 1000; i++) {
+      fractions[i] = (double) i / 1000;
+      reverseFractions[1000 - i] = fractions[i];
+    }
+    final float[] quantiles = sketch.getQuantiles(fractions);
+    final float[] reverseQuantiles = sketch.getQuantiles(reverseFractions);
+    double previousQuantile = 0;
+    for (int i = 0; i <= 1000; i++) {
+      final double quantile = sketch.getQuantile(fractions[i]);
+      assertEquals(quantile, quantiles[i]);
+      assertEquals(quantile, reverseQuantiles[1000 - i]);
+      assertTrue(previousQuantile <= quantile);
+      previousQuantile = quantile;
+    }
+  }
+
+  @Test
+  public void getRankGetCdfGetPmfConsistency() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1000;
+    final float[] values = new float[n];
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+      values[i] = i;
+    }
+    final double[] ranks = sketch.getCDF(values);
+    final double[] pmf = sketch.getPMF(values);
+    double sumPmf = 0;
+    for (int i = 0; i < n; i++) {
+      assertEquals(ranks[i], sketch.getRank(values[i]), NUMERIC_NOISE_TOLERANCE,
+          "rank vs CDF for value " + i);
+      sumPmf += pmf[i];
+      assertEquals(ranks[i], sumPmf, NUMERIC_NOISE_TOLERANCE, "CDF vs PMF for value " + i);
+    }
+    sumPmf += pmf[n];
+    assertEquals(sumPmf, 1.0, NUMERIC_NOISE_TOLERANCE);
+    assertEquals(ranks[n], 1.0, NUMERIC_NOISE_TOLERANCE);
+  }
+
+  @Test
+  public void merge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i * 1.0F);
+      sketch2.update((2 * n - i - 1) * 1.0F);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (n - 1) * 1.0);
+
+    assertEquals(sketch2.getMinValue(), n * 1.0);
+    assertEquals(sketch2.getMaxValue(), (2 * n - 1) * 1.0);
+
+    sketch1.merge(sketch2);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2L * n);
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (2 * n - 1) * 1.0F);
+    assertEquals(sketch1.getQuantile(0.5), n * 1.0F, n * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+      sketch2.update(2 * n - i - 1);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+
+    assertEquals(sketch2.getMinValue(), n);
+    assertEquals(sketch2.getMaxValue(), 2f * n - 1f);
+
+    assertTrue(sketch1.getNormalizedRankError(false) < sketch2.getNormalizedRankError(false));
+    assertTrue(sketch1.getNormalizedRankError(true) < sketch2.getNormalizedRankError(true));
+    sketch1.merge(sketch2);
+
+    // sketch1 must get "contaminated" by the lower K in sketch2
+    assertEquals(sketch1.getNormalizedRankError(false), sketch2.getNormalizedRankError(false));
+    assertEquals(sketch1.getNormalizedRankError(true), sketch2.getNormalizedRankError(true));
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2 * n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), 2f * n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n, n * PMF_EPS_FOR_K_128);
+  }
+
+  @Test
+  public void mergeEmptyLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+
+    // rank error should not be affected by a merge with an empty sketch with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+
+    //merge the other way
+    sketch2.merge(sketch1);
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeExactModeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+    sketch2.update(1);
+
+    // rank error should not be affected by a merge with a sketch in exact mode with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+  }
+
+  @Test
+  public void mergeMinMinValueFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    sketch1.update(1);
+    sketch2.update(2);
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1.0F);
+  }
+
+  @Test
+  public void mergeMinAndMaxFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(8, 0); //was 200
+    final KllDirectFloatsSketch sketch2 = getDFSketch(8, 0); //was 200
+    for (int i = 1; i <= 9; i++) { //was 1_000_000
+      sketch1.update(i);
+    }
+    //System.out.println(sketch1.toString(true, true));
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1F);
+    assertEquals(sketch2.getMaxValue(), 9F); //was 1_000_000
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooSmall() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(DEFAULT_M - 1, 0);
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooLarge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(MAX_K + 1, 0);
+  }
+
+  @Test
+  public void minK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(DEFAULT_M, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), DEFAULT_M);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_8);
+  }
+
+  @Test
+  public void maxK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(MAX_K, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), MAX_K);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void serializeDeserializeEmpty() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeEmpty2() { //updatable serialize then new (loaded) KllDirectDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toUpdatableByteArray();
+    final KllDirectFloatsSketch sketch2 =
+        KllDirectFloatsSketch.writableWrap(WritableMemory.writableWrap(bytes),memReqSvr);
+    assertEquals(bytes.length, sketch1.getCurrentUpdatableSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeOneItem() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    sketch1.update(1);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertFalse(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), 1);
+    assertEquals(sketch2.getN(), 1);
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertFalse(Double.isNaN(sketch2.getMinValue()));
+    assertFalse(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), 8 + Float.BYTES);
+  }
+
+  @Test
+  public void serializeDeserializeOneItem2() { //updatable serialize then new (loaded) KllDirectDoublesSketch

Review comment:
       and here

##########
File path: src/test/java/org/apache/datasketches/kll/KllDirectFloatsSketchTest.java
##########
@@ -0,0 +1,594 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+//import static org.apache.datasketches.Util.getResourceBytes; //don't have matching numbers from C++
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.memory.DefaultMemoryRequestServer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class KllDirectFloatsSketchTest {
+
+  private static final double PMF_EPS_FOR_K_8 = 0.35; // PMF rank error (epsilon) for k=8
+  private static final double PMF_EPS_FOR_K_128 = 0.025; // PMF rank error (epsilon) for k=128
+  private static final double PMF_EPS_FOR_K_256 = 0.013; // PMF rank error (epsilon) for k=256
+  private static final double NUMERIC_NOISE_TOLERANCE = 1E-6;
+  private static final DefaultMemoryRequestServer memReqSvr = new DefaultMemoryRequestServer();
+
+  @Test
+  public void empty() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(Float.NaN); // this must not change anything
+    assertTrue(sketch.isEmpty());
+    assertEquals(sketch.getN(), 0);
+    assertEquals(sketch.getNumRetained(), 0);
+    assertTrue(Double.isNaN(sketch.getRank(0)));
+    assertTrue(Float.isNaN(sketch.getMinValue()));
+    assertTrue(Float.isNaN(sketch.getMaxValue()));
+    assertTrue(Float.isNaN(sketch.getQuantile(0.5)));
+    assertNull(sketch.getQuantiles(new double[] {0}));
+    assertNull(sketch.getPMF(new float[] {0}));
+    assertNotNull(sketch.toString(true, true));
+    assertNotNull(sketch.toString());
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantileInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantile(-1.0);
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantilesInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantiles(new double[] {2.0});
+  }
+
+  @Test
+  public void oneItem() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    assertFalse(sketch.isEmpty());
+    assertEquals(sketch.getN(), 1);
+    assertEquals(sketch.getNumRetained(), 1);
+    assertEquals(sketch.getRank(1), 0.0);
+    assertEquals(sketch.getRank(2), 1.0);
+    assertEquals(sketch.getMinValue(), 1f);
+    assertEquals(sketch.getMaxValue(), 1f);
+    assertEquals(sketch.getQuantile(0.5), 1f);
+  }
+
+  @Test
+  public void manyItemsEstimationMode() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1_000_000;
+
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getN(), n);
+
+    // test getRank
+    for (int i = 0; i < n; i++) {
+      final double trueRank = (double) i / n;
+      assertEquals(sketch.getRank(i), trueRank, PMF_EPS_FOR_K_256, "for value " + i);
+    }
+
+    // test getPMF
+    final double[] pmf = sketch.getPMF(new float[] {n / 2}); // split at median
+    assertEquals(pmf.length, 2);
+    assertEquals(pmf[0], 0.5, PMF_EPS_FOR_K_256);
+    assertEquals(pmf[1], 0.5, PMF_EPS_FOR_K_256);
+
+    assertEquals(sketch.getMinValue(), 0f); // min value is exact
+    assertEquals(sketch.getQuantile(0), 0f); // min value is exact
+    assertEquals(sketch.getMaxValue(), n - 1f); // max value is exact
+    assertEquals(sketch.getQuantile(1), n - 1f); // max value is exact
+
+    // check at every 0.1 percentage point
+    final double[] fractions = new double[1001];
+    final double[] reverseFractions = new double[1001]; // check that ordering doesn't matter
+    for (int i = 0; i <= 1000; i++) {
+      fractions[i] = (double) i / 1000;
+      reverseFractions[1000 - i] = fractions[i];
+    }
+    final float[] quantiles = sketch.getQuantiles(fractions);
+    final float[] reverseQuantiles = sketch.getQuantiles(reverseFractions);
+    double previousQuantile = 0;
+    for (int i = 0; i <= 1000; i++) {
+      final double quantile = sketch.getQuantile(fractions[i]);
+      assertEquals(quantile, quantiles[i]);
+      assertEquals(quantile, reverseQuantiles[1000 - i]);
+      assertTrue(previousQuantile <= quantile);
+      previousQuantile = quantile;
+    }
+  }
+
+  @Test
+  public void getRankGetCdfGetPmfConsistency() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1000;
+    final float[] values = new float[n];
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+      values[i] = i;
+    }
+    final double[] ranks = sketch.getCDF(values);
+    final double[] pmf = sketch.getPMF(values);
+    double sumPmf = 0;
+    for (int i = 0; i < n; i++) {
+      assertEquals(ranks[i], sketch.getRank(values[i]), NUMERIC_NOISE_TOLERANCE,
+          "rank vs CDF for value " + i);
+      sumPmf += pmf[i];
+      assertEquals(ranks[i], sumPmf, NUMERIC_NOISE_TOLERANCE, "CDF vs PMF for value " + i);
+    }
+    sumPmf += pmf[n];
+    assertEquals(sumPmf, 1.0, NUMERIC_NOISE_TOLERANCE);
+    assertEquals(ranks[n], 1.0, NUMERIC_NOISE_TOLERANCE);
+  }
+
+  @Test
+  public void merge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i * 1.0F);
+      sketch2.update((2 * n - i - 1) * 1.0F);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (n - 1) * 1.0);
+
+    assertEquals(sketch2.getMinValue(), n * 1.0);
+    assertEquals(sketch2.getMaxValue(), (2 * n - 1) * 1.0);
+
+    sketch1.merge(sketch2);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2L * n);
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (2 * n - 1) * 1.0F);
+    assertEquals(sketch1.getQuantile(0.5), n * 1.0F, n * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+      sketch2.update(2 * n - i - 1);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+
+    assertEquals(sketch2.getMinValue(), n);
+    assertEquals(sketch2.getMaxValue(), 2f * n - 1f);
+
+    assertTrue(sketch1.getNormalizedRankError(false) < sketch2.getNormalizedRankError(false));
+    assertTrue(sketch1.getNormalizedRankError(true) < sketch2.getNormalizedRankError(true));
+    sketch1.merge(sketch2);
+
+    // sketch1 must get "contaminated" by the lower K in sketch2
+    assertEquals(sketch1.getNormalizedRankError(false), sketch2.getNormalizedRankError(false));
+    assertEquals(sketch1.getNormalizedRankError(true), sketch2.getNormalizedRankError(true));
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2 * n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), 2f * n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n, n * PMF_EPS_FOR_K_128);
+  }
+
+  @Test
+  public void mergeEmptyLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+
+    // rank error should not be affected by a merge with an empty sketch with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+
+    //merge the other way
+    sketch2.merge(sketch1);
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeExactModeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+    sketch2.update(1);
+
+    // rank error should not be affected by a merge with a sketch in exact mode with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+  }
+
+  @Test
+  public void mergeMinMinValueFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    sketch1.update(1);
+    sketch2.update(2);
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1.0F);
+  }
+
+  @Test
+  public void mergeMinAndMaxFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(8, 0); //was 200
+    final KllDirectFloatsSketch sketch2 = getDFSketch(8, 0); //was 200
+    for (int i = 1; i <= 9; i++) { //was 1_000_000
+      sketch1.update(i);
+    }
+    //System.out.println(sketch1.toString(true, true));
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1F);
+    assertEquals(sketch2.getMaxValue(), 9F); //was 1_000_000
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooSmall() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(DEFAULT_M - 1, 0);
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooLarge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(MAX_K + 1, 0);
+  }
+
+  @Test
+  public void minK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(DEFAULT_M, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), DEFAULT_M);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_8);
+  }
+
+  @Test
+  public void maxK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(MAX_K, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), MAX_K);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void serializeDeserializeEmpty() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeEmpty2() { //updatable serialize then new (loaded) KllDirectDoublesSketch

Review comment:
       same here

##########
File path: src/test/java/org/apache/datasketches/kll/KllDoublesSketchTest.java
##########
@@ -19,6 +19,9 @@
 
 package org.apache.datasketches.kll;
 
+//import static org.apache.datasketches.Util.getResourceBytes; //don't have matching numbers from C++

Review comment:
       do we need this commented line?

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectSketch.java
##########
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.kll.KllPreambleUtil.extractDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractM;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractN;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
+import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class implements all the methods for the Direct (off-heap) sketches that are independent
+ * of the sketch type (float or double).
+ */
+abstract class KllDirectSketch extends KllSketch {
+  //All these members are constant for the life of this object. If the WritableMemory changes,
+  // it may require rebuilding this class
+  final boolean updatable = true;
+  WritableMemory levelsArrUpdatable;
+  WritableMemory minMaxArrUpdatable;
+  WritableMemory itemsArrUpdatable;
+
+  /**
+   * For the direct sketches it is important that the methods implemented here are designed to work dynamically
+   * as the sketch grows off-heap.
+   * @param sketchType either DOUBLE_SKETCH or FLOAT_SKETCH
+   * @param wmem the current WritableMemory
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   */
+  KllDirectSketch(final SketchType sketchType, final WritableMemory wmem, final MemoryRequestServer memReqSvr,
+      final MemoryValidate memVal) {
+    super(sketchType, wmem, memReqSvr);
+    levelsArrUpdatable = memVal.levelsArrUpdatable;
+    minMaxArrUpdatable = memVal.minMaxArrUpdatable;
+    itemsArrUpdatable = memVal.itemsArrUpdatable;
+  }
+
+  @Override
+  public int getK() {
+    return extractK(wmem);
+  }
+
+  @Override
+  public int getM() {
+    return extractM(wmem);
+  }
+
+  @Override
+  public long getN() {
+    return extractN(wmem);
+  }
+
+  @Override
+  public byte[] toUpdatableByteArray() {
+    final int bytes = (int) wmem.getCapacity();
+    final byte[] byteArr = new byte[bytes];
+    wmem.getByteArray(0, byteArr, 0, bytes);
+    return byteArr;
+  }
+
+  @Override
+  double[] getDoubleItemsArray() {
+    if (sketchType == FLOATS_SKETCH) { return null; }
+    final int items = getItemsArrLengthItems();
+    final double[] itemsArr = new double[items];
+    itemsArrUpdatable.getDoubleArray(0, itemsArr, 0, items);
+    return itemsArr;
+  }
+
+  @Override
+  double getDoubleItemsArrayAt(final int index) {
+    if (sketchType == FLOATS_SKETCH) { return Double.NaN; }
+    return itemsArrUpdatable.getDouble((long)index * Double.BYTES);
+  }
+
+  @Override
+  int getDyMinK() {

Review comment:
       the name does not convey the meaning (to me)

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectSketch.java
##########
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.kll.KllPreambleUtil.extractDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractM;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractN;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
+import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class implements all the methods for the Direct (off-heap) sketches that are independent
+ * of the sketch type (float or double).
+ */
+abstract class KllDirectSketch extends KllSketch {
+  //All these members are constant for the life of this object. If the WritableMemory changes,
+  // it may require rebuilding this class
+  final boolean updatable = true;
+  WritableMemory levelsArrUpdatable;
+  WritableMemory minMaxArrUpdatable;
+  WritableMemory itemsArrUpdatable;
+
+  /**
+   * For the direct sketches it is important that the methods implemented here are designed to work dynamically
+   * as the sketch grows off-heap.
+   * @param sketchType either DOUBLE_SKETCH or FLOAT_SKETCH
+   * @param wmem the current WritableMemory
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   */
+  KllDirectSketch(final SketchType sketchType, final WritableMemory wmem, final MemoryRequestServer memReqSvr,
+      final MemoryValidate memVal) {
+    super(sketchType, wmem, memReqSvr);
+    levelsArrUpdatable = memVal.levelsArrUpdatable;
+    minMaxArrUpdatable = memVal.minMaxArrUpdatable;
+    itemsArrUpdatable = memVal.itemsArrUpdatable;
+  }
+
+  @Override
+  public int getK() {
+    return extractK(wmem);
+  }
+
+  @Override
+  public int getM() {
+    return extractM(wmem);
+  }
+
+  @Override
+  public long getN() {
+    return extractN(wmem);
+  }
+
+  @Override
+  public byte[] toUpdatableByteArray() {
+    final int bytes = (int) wmem.getCapacity();
+    final byte[] byteArr = new byte[bytes];
+    wmem.getByteArray(0, byteArr, 0, bytes);
+    return byteArr;
+  }
+
+  @Override
+  double[] getDoubleItemsArray() {
+    if (sketchType == FLOATS_SKETCH) { return null; }
+    final int items = getItemsArrLengthItems();
+    final double[] itemsArr = new double[items];
+    itemsArrUpdatable.getDoubleArray(0, itemsArr, 0, items);
+    return itemsArr;
+  }
+
+  @Override
+  double getDoubleItemsArrayAt(final int index) {
+    if (sketchType == FLOATS_SKETCH) { return Double.NaN; }

Review comment:
       logic error? and a few more like this below

##########
File path: src/test/java/org/apache/datasketches/kll/KllDirectFloatsSketchTest.java
##########
@@ -0,0 +1,594 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+//import static org.apache.datasketches.Util.getResourceBytes; //don't have matching numbers from C++
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.memory.DefaultMemoryRequestServer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class KllDirectFloatsSketchTest {
+
+  private static final double PMF_EPS_FOR_K_8 = 0.35; // PMF rank error (epsilon) for k=8
+  private static final double PMF_EPS_FOR_K_128 = 0.025; // PMF rank error (epsilon) for k=128
+  private static final double PMF_EPS_FOR_K_256 = 0.013; // PMF rank error (epsilon) for k=256
+  private static final double NUMERIC_NOISE_TOLERANCE = 1E-6;
+  private static final DefaultMemoryRequestServer memReqSvr = new DefaultMemoryRequestServer();
+
+  @Test
+  public void empty() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(Float.NaN); // this must not change anything
+    assertTrue(sketch.isEmpty());
+    assertEquals(sketch.getN(), 0);
+    assertEquals(sketch.getNumRetained(), 0);
+    assertTrue(Double.isNaN(sketch.getRank(0)));
+    assertTrue(Float.isNaN(sketch.getMinValue()));
+    assertTrue(Float.isNaN(sketch.getMaxValue()));
+    assertTrue(Float.isNaN(sketch.getQuantile(0.5)));
+    assertNull(sketch.getQuantiles(new double[] {0}));
+    assertNull(sketch.getPMF(new float[] {0}));
+    assertNotNull(sketch.toString(true, true));
+    assertNotNull(sketch.toString());
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantileInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantile(-1.0);
+  }
+
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void getQuantilesInvalidArg() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    sketch.getQuantiles(new double[] {2.0});
+  }
+
+  @Test
+  public void oneItem() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    sketch.update(1);
+    assertFalse(sketch.isEmpty());
+    assertEquals(sketch.getN(), 1);
+    assertEquals(sketch.getNumRetained(), 1);
+    assertEquals(sketch.getRank(1), 0.0);
+    assertEquals(sketch.getRank(2), 1.0);
+    assertEquals(sketch.getMinValue(), 1f);
+    assertEquals(sketch.getMaxValue(), 1f);
+    assertEquals(sketch.getQuantile(0.5), 1f);
+  }
+
+  @Test
+  public void manyItemsEstimationMode() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1_000_000;
+
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getN(), n);
+
+    // test getRank
+    for (int i = 0; i < n; i++) {
+      final double trueRank = (double) i / n;
+      assertEquals(sketch.getRank(i), trueRank, PMF_EPS_FOR_K_256, "for value " + i);
+    }
+
+    // test getPMF
+    final double[] pmf = sketch.getPMF(new float[] {n / 2}); // split at median
+    assertEquals(pmf.length, 2);
+    assertEquals(pmf[0], 0.5, PMF_EPS_FOR_K_256);
+    assertEquals(pmf[1], 0.5, PMF_EPS_FOR_K_256);
+
+    assertEquals(sketch.getMinValue(), 0f); // min value is exact
+    assertEquals(sketch.getQuantile(0), 0f); // min value is exact
+    assertEquals(sketch.getMaxValue(), n - 1f); // max value is exact
+    assertEquals(sketch.getQuantile(1), n - 1f); // max value is exact
+
+    // check at every 0.1 percentage point
+    final double[] fractions = new double[1001];
+    final double[] reverseFractions = new double[1001]; // check that ordering doesn't matter
+    for (int i = 0; i <= 1000; i++) {
+      fractions[i] = (double) i / 1000;
+      reverseFractions[1000 - i] = fractions[i];
+    }
+    final float[] quantiles = sketch.getQuantiles(fractions);
+    final float[] reverseQuantiles = sketch.getQuantiles(reverseFractions);
+    double previousQuantile = 0;
+    for (int i = 0; i <= 1000; i++) {
+      final double quantile = sketch.getQuantile(fractions[i]);
+      assertEquals(quantile, quantiles[i]);
+      assertEquals(quantile, reverseQuantiles[1000 - i]);
+      assertTrue(previousQuantile <= quantile);
+      previousQuantile = quantile;
+    }
+  }
+
+  @Test
+  public void getRankGetCdfGetPmfConsistency() {
+    final KllDirectFloatsSketch sketch = getDFSketch(200, 0);
+    final int n = 1000;
+    final float[] values = new float[n];
+    for (int i = 0; i < n; i++) {
+      sketch.update(i);
+      values[i] = i;
+    }
+    final double[] ranks = sketch.getCDF(values);
+    final double[] pmf = sketch.getPMF(values);
+    double sumPmf = 0;
+    for (int i = 0; i < n; i++) {
+      assertEquals(ranks[i], sketch.getRank(values[i]), NUMERIC_NOISE_TOLERANCE,
+          "rank vs CDF for value " + i);
+      sumPmf += pmf[i];
+      assertEquals(ranks[i], sumPmf, NUMERIC_NOISE_TOLERANCE, "CDF vs PMF for value " + i);
+    }
+    sumPmf += pmf[n];
+    assertEquals(sumPmf, 1.0, NUMERIC_NOISE_TOLERANCE);
+    assertEquals(ranks[n], 1.0, NUMERIC_NOISE_TOLERANCE);
+  }
+
+  @Test
+  public void merge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i * 1.0F);
+      sketch2.update((2 * n - i - 1) * 1.0F);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (n - 1) * 1.0);
+
+    assertEquals(sketch2.getMinValue(), n * 1.0);
+    assertEquals(sketch2.getMaxValue(), (2 * n - 1) * 1.0);
+
+    sketch1.merge(sketch2);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2L * n);
+    assertEquals(sketch1.getMinValue(), 0.0);
+    assertEquals(sketch1.getMaxValue(), (2 * n - 1) * 1.0F);
+    assertEquals(sketch1.getQuantile(0.5), n * 1.0F, n * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10_000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+      sketch2.update(2 * n - i - 1);
+    }
+
+    assertEquals(sketch1.getMinValue(), 0.0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+
+    assertEquals(sketch2.getMinValue(), n);
+    assertEquals(sketch2.getMaxValue(), 2f * n - 1f);
+
+    assertTrue(sketch1.getNormalizedRankError(false) < sketch2.getNormalizedRankError(false));
+    assertTrue(sketch1.getNormalizedRankError(true) < sketch2.getNormalizedRankError(true));
+    sketch1.merge(sketch2);
+
+    // sketch1 must get "contaminated" by the lower K in sketch2
+    assertEquals(sketch1.getNormalizedRankError(false), sketch2.getNormalizedRankError(false));
+    assertEquals(sketch1.getNormalizedRankError(true), sketch2.getNormalizedRankError(true));
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), 2 * n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), 2f * n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n, n * PMF_EPS_FOR_K_128);
+  }
+
+  @Test
+  public void mergeEmptyLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+
+    // rank error should not be affected by a merge with an empty sketch with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+
+    //merge the other way
+    sketch2.merge(sketch1);
+    assertFalse(sketch1.isEmpty());
+    assertEquals(sketch1.getN(), n);
+    assertEquals(sketch1.getMinValue(), 0f);
+    assertEquals(sketch1.getMaxValue(), n - 1f);
+    assertEquals(sketch1.getQuantile(0.5), n / 2f, n / 2 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void mergeExactModeLowerK() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(256, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(128, 0);
+    final int n = 10000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+    sketch2.update(1);
+
+    // rank error should not be affected by a merge with a sketch in exact mode with lower K
+    final double rankErrorBeforeMerge = sketch1.getNormalizedRankError(true);
+    sketch1.merge(sketch2);
+    assertEquals(sketch1.getNormalizedRankError(true), rankErrorBeforeMerge);
+  }
+
+  @Test
+  public void mergeMinMinValueFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final KllDirectFloatsSketch sketch2 = getDFSketch(200, 0);
+    sketch1.update(1);
+    sketch2.update(2);
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1.0F);
+  }
+
+  @Test
+  public void mergeMinAndMaxFromOther() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(8, 0); //was 200
+    final KllDirectFloatsSketch sketch2 = getDFSketch(8, 0); //was 200
+    for (int i = 1; i <= 9; i++) { //was 1_000_000
+      sketch1.update(i);
+    }
+    //System.out.println(sketch1.toString(true, true));
+    sketch2.merge(sketch1);
+    assertEquals(sketch2.getMinValue(), 1F);
+    assertEquals(sketch2.getMaxValue(), 9F); //was 1_000_000
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooSmall() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(DEFAULT_M - 1, 0);
+  }
+
+  @SuppressWarnings("unused")
+  @Test(expectedExceptions = SketchesArgumentException.class)
+  public void kTooLarge() {
+    final KllDirectFloatsSketch sketch1 = getDFSketch(MAX_K + 1, 0);
+  }
+
+  @Test
+  public void minK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(DEFAULT_M, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), DEFAULT_M);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_8);
+  }
+
+  @Test
+  public void maxK() {
+    final KllDirectFloatsSketch sketch = getDFSketch(MAX_K, 0);
+    for (int i = 0; i < 1000; i++) {
+      sketch.update(i);
+    }
+    assertEquals(sketch.getK(), MAX_K);
+    assertEquals(sketch.getQuantile(0.5), 500, 500 * PMF_EPS_FOR_K_256);
+  }
+
+  @Test
+  public void serializeDeserializeEmpty() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeEmpty2() { //updatable serialize then new (loaded) KllDirectDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final byte[] bytes = sketch1.toUpdatableByteArray();
+    final KllDirectFloatsSketch sketch2 =
+        KllDirectFloatsSketch.writableWrap(WritableMemory.writableWrap(bytes),memReqSvr);
+    assertEquals(bytes.length, sketch1.getCurrentUpdatableSerializedSizeBytes());
+    assertTrue(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertTrue(Double.isNaN(sketch2.getMinValue()));
+    assertTrue(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserializeOneItem() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    sketch1.update(1);
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 = KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertFalse(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), 1);
+    assertEquals(sketch2.getN(), 1);
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertFalse(Double.isNaN(sketch2.getMinValue()));
+    assertFalse(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), 8 + Float.BYTES);
+  }
+
+  @Test
+  public void serializeDeserializeOneItem2() { //updatable serialize then new (loaded) KllDirectDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    sketch1.update(1);
+    final byte[] bytes = sketch1.toUpdatableByteArray();
+    final KllDirectFloatsSketch sketch2 =
+        KllDirectFloatsSketch.writableWrap(WritableMemory.writableWrap(bytes),memReqSvr);
+    assertEquals(bytes.length, sketch1.getCurrentUpdatableSerializedSizeBytes());
+    assertFalse(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), 1);
+    assertEquals(sketch2.getN(), 1);
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertFalse(Double.isNaN(sketch2.getMinValue()));
+    assertFalse(Double.isNaN(sketch2.getMaxValue()));
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), 8 + Float.BYTES);
+  }
+
+  @Test
+  public void serializeDeserialize() { //compact serialize then heapify using KllDoublesSketch
+    final KllDirectFloatsSketch sketch1 = getDFSketch(200, 0);
+    final int n = 1000;
+    for (int i = 0; i < n; i++) {
+      sketch1.update(i);
+    }
+    final byte[] bytes = sketch1.toByteArray();
+    final KllFloatsSketch sketch2 =  KllFloatsSketch.heapify(Memory.wrap(bytes));
+    assertEquals(bytes.length, sketch1.getCurrentCompactSerializedSizeBytes());
+    assertFalse(sketch2.isEmpty());
+    assertEquals(sketch2.getNumRetained(), sketch1.getNumRetained());
+    assertEquals(sketch2.getN(), sketch1.getN());
+    assertEquals(sketch2.getNormalizedRankError(false), sketch1.getNormalizedRankError(false));
+    assertEquals(sketch2.getMinValue(), sketch1.getMinValue());
+    assertEquals(sketch2.getMaxValue(), sketch1.getMaxValue());
+    assertEquals(sketch2.getCurrentCompactSerializedSizeBytes(), sketch1.getCurrentCompactSerializedSizeBytes());
+  }
+
+  @Test
+  public void serializeDeserialize2() { //updatable serialize then new (loaded) KllDirectDoublesSketch

Review comment:
       and here

##########
File path: src/main/java/org/apache/datasketches/kll/KllDoublesSketch.java
##########
@@ -389,441 +271,81 @@ public double getQuantileLowerBound(final double fraction) {
    * @return an approximate rank of the given value
    */
   public double getRank(final double value) {
-    if (isEmpty()) { return Double.NaN; }
-    int level = 0;
-    int weight = 1;
-    long total = 0;
-    while (level < numLevels_) {
-      final int fromIndex = levels_[level];
-      final int toIndex = levels_[level + 1]; // exclusive
-      for (int i = fromIndex; i < toIndex; i++) {
-        if (items_[i] < value) {
-          total += weight;
-        } else if (level > 0 || isLevelZeroSorted_) {
-          break; // levels above 0 are sorted, no point comparing further
-        }
-      }
-      level++;
-      weight *= 2;
-    }
-    return (double) total / n_;
-  }
-
-  /**
-   * Returns the number of bytes this sketch would require to store.
-   * @return the number of bytes this sketch would require to store.
-   */
-  public int getSerializedSizeBytes() {
-    if (isEmpty()) { return N_LONG; }
-    return getSerializedSizeBytes(numLevels_, getNumRetained());
+    return getDoubleRank(value);
   }
 
   /**
    * @return the iterator for this class
    */
   public KllDoublesSketchIterator iterator() {
-    return new KllDoublesSketchIterator(items_, levels_, numLevels_);
+    return new KllDoublesSketchIterator(getDoubleItemsArray(), getLevelsArray(), getNumLevels());
   }
 
   /**
    * Merges another sketch into this one.
    * @param other sketch to merge into this one
    */
-  public void merge(final KllDoublesSketch other) {
-    if (other == null || other.isEmpty()) { return; }
-    if (m_ != other.m_) {
-      throw new SketchesArgumentException("incompatible M: " + m_ + " and " + other.m_);
-    }
-    final long finalN = n_ + other.n_;
-    //update this sketch with level0 items from the other sketch
-    for (int i = other.levels_[0]; i < other.levels_[1]; i++) {
-      update(other.items_[i]);
-    }
-    if (other.numLevels_ >= 2) { //now merge other levels if they exist
-      mergeHigherLevels(other, finalN);
-    }
-    //update min, max values, n
-    if (Double.isNaN(minValue_) || other.minValue_ < minValue_) { minValue_ = other.minValue_; }
-    if (Double.isNaN(maxValue_) || other.maxValue_ > maxValue_) { maxValue_ = other.maxValue_; }
-    n_ = finalN;
-
-    assert KllHelper.sumTheSampleWeights(numLevels_, levels_) == n_;
-    if (other.isEstimationMode()) {
-      minK_ = min(minK_, other.minK_);
-    }
-  }
-
-  @Override
-  public byte[] toByteArray() {
-    final byte[] bytes = new byte[getSerializedSizeBytes()];
-    final WritableMemory wmem = WritableMemory.writableWrap(bytes);
-    final boolean singleItem = n_ == 1;
-    final boolean empty = isEmpty();
-    //load the preamble
-    wmem.putByte(PREAMBLE_INTS_BYTE, (byte) (empty || singleItem ? PREAMBLE_INTS_EMPTY_SINGLE : PREAMBLE_INTS_DOUBLE));
-    wmem.putByte(SER_VER_BYTE, singleItem ? SERIAL_VERSION_SINGLE : SERIAL_VERSION);
-    wmem.putByte(FAMILY_BYTE, (byte) Family.KLL.getID());
-    final byte flags = (byte) (
-        (empty ? 1 << Flags.IS_EMPTY.ordinal() : 0)
-      | (isLevelZeroSorted_ ? 1 << Flags.IS_LEVEL_ZERO_SORTED.ordinal() : 0)
-      | (singleItem ? 1 << Flags.IS_SINGLE_ITEM.ordinal() : 0));
-    wmem.putByte(FLAGS_BYTE, flags);
-    wmem.putShort(K_SHORT, (short) k_);
-    wmem.putByte(M_BYTE, (byte) m_);
-    if (empty) { return bytes; }
-    //load data
-    int offset = DATA_START_SINGLE_ITEM;
-    if (!singleItem) {
-      wmem.putLong(N_LONG, n_);
-      wmem.putShort(MIN_K_SHORT, (short) minK_);
-      wmem.putByte(NUM_LEVELS_BYTE, (byte) numLevels_);
-      offset = DATA_START_DOUBLE;
-      // the last integer in levels_ is not serialized because it can be derived
-      final int len = levels_.length - 1;
-      wmem.putIntArray(offset, levels_, 0, len);
-      offset += len * Integer.BYTES;
-      wmem.putDouble(offset, minValue_);
-      offset += Double.BYTES;
-      wmem.putDouble(offset, maxValue_);
-      offset += Double.BYTES;
-    }
-    wmem.putDoubleArray(offset, items_, levels_[0], getNumRetained());
-    return bytes;
-  }
-
-  @Override
-  public String toString(final boolean withLevels, final boolean withData) {
-    final String epsPct = String.format("%.3f%%", getNormalizedRankError(false) * 100);
-    final String epsPMFPct = String.format("%.3f%%", getNormalizedRankError(true) * 100);
-    final StringBuilder sb = new StringBuilder();
-    sb.append(Util.LS).append("### KLL sketch summary:").append(Util.LS);
-    sb.append("   K                    : ").append(k_).append(Util.LS);
-    sb.append("   min K                : ").append(minK_).append(Util.LS);
-    sb.append("   M                    : ").append(m_).append(Util.LS);
-    sb.append("   N                    : ").append(n_).append(Util.LS);
-    sb.append("   Epsilon              : ").append(epsPct).append(Util.LS);
-    sb.append("   Epsison PMF          : ").append(epsPMFPct).append(Util.LS);
-    sb.append("   Empty                : ").append(isEmpty()).append(Util.LS);
-    sb.append("   Estimation Mode      : ").append(isEstimationMode()).append(Util.LS);
-    sb.append("   Levels               : ").append(numLevels_).append(Util.LS);
-    sb.append("   Level 0 Sorted       : ").append(isLevelZeroSorted_).append(Util.LS);
-    sb.append("   Capacity Items       : ").append(items_.length).append(Util.LS);
-    sb.append("   Retained Items       : ").append(getNumRetained()).append(Util.LS);
-    sb.append("   Storage Bytes        : ").append(getSerializedSizeBytes()).append(Util.LS);
-    sb.append("   Min Value            : ").append(minValue_).append(Util.LS);
-    sb.append("   Max Value            : ").append(maxValue_).append(Util.LS);
-    sb.append("### End sketch summary").append(Util.LS);
-
-    if (withLevels) {
-      sb.append("### KLL sketch levels:").append(Util.LS)
-      .append(" level, offset: nominal capacity, actual size").append(Util.LS);
-      for (int i = 0; i < numLevels_; i++) {
-        sb.append("   ").append(i).append(", ").append(levels_[i]).append(": ")
-        .append(KllHelper.levelCapacity(k_, numLevels_, i, m_))
-        .append(", ").append(currentLevelSize(i)).append(Util.LS);
-      }
-      sb.append("### End sketch levels").append(Util.LS);
-    }
-
-    if (withData) {
-      sb.append("### KLL sketch data {index, item}:").append(Util.LS);
-      if (levels_[0] > 0) {
-        sb.append(" Garbage:" + Util.LS);
-        for (int i = 0; i < levels_[0]; i++) {
-          if (items_[i] == 0.0f) { continue; }
-          sb.append("   ").append(i + ", ").append(items_[i]).append(Util.LS);
-        }
-      }
-      int level = 0;
-      while (level < numLevels_) {
-        final int fromIndex = levels_[level];
-        final int toIndex = levels_[level + 1]; // exclusive
-        if (fromIndex < toIndex) {
-          sb.append(" level[").append(level).append("]: offset: " + levels_[level] + " wt: " + (1 << level));
-          sb.append(Util.LS);
-        }
-        for (int i = fromIndex; i < toIndex; i++) {
-          sb.append("   ").append(i + ", ").append(items_[i]).append(Util.LS);
-        }
-        level++;
-      }
-      sb.append(" level[" + level + "]: offset: " + levels_[level] + " (Exclusive)");
-      sb.append(Util.LS);
-      sb.append("### End sketch data").append(Util.LS);
-    }
-
-    return sb.toString();
+  public void merge(final KllSketch other) {
+    if (other.isDirect()) { kllSketchThrow(35); }
+    if (!other.isDoublesSketch()) { kllSketchThrow(33); }
+    mergeDoubleImpl(other);
   }
 
   /**
    * Updates this sketch with the given data item.
    *
    * @param value an item from a stream of items. NaNs are ignored.
    */
-  public void update(final double value) {
-    if (Double.isNaN(value)) { return; }
-    if (isEmpty()) {
-      minValue_ = value;
-      maxValue_ = value;
-    } else {
-      if (value < minValue_) { minValue_ = value; }
-      if (value > maxValue_) { maxValue_ = value; }
-    }
-    if (levels_[0] == 0) {
-      compressWhileUpdating();
-    }
-    n_++;
-    isLevelZeroSorted_ = false;
-    final int nextPos = levels_[0] - 1;
-    assert levels_[0] >= 0;
-    levels_[0] = nextPos;
-    items_[nextPos] = value;
-  }
-
-  // Restricted Methods
-
-  private KllDoublesQuantileCalculator getQuantileCalculator() {
-    sortLevelZero(); // sort in the sketch to reuse if possible
-    return new KllDoublesQuantileCalculator(items_, levels_, numLevels_, n_);
-  }
-
-  private double[] getPmfOrCdf(final double[] splitPoints, final boolean isCdf) {
-    if (isEmpty()) { return null; }
-    KllDoublesHelper.validateDoubleValues(splitPoints);
-    final double[] buckets = new double[splitPoints.length + 1];
-    int level = 0;
-    int weight = 1;
-    while (level < numLevels_) {
-      final int fromIndex = levels_[level];
-      final int toIndex = levels_[level + 1]; // exclusive
-      if (level == 0 && !isLevelZeroSorted_) {
-        incrementBucketsUnsortedLevel(fromIndex, toIndex, weight, splitPoints, buckets);
-      } else {
-        incrementBucketsSortedLevel(fromIndex, toIndex, weight, splitPoints, buckets);
-      }
-      level++;
-      weight *= 2;
-    }
-    // normalize and, if CDF, convert to cumulative
-    if (isCdf) {
-      double subtotal = 0;
-      for (int i = 0; i < buckets.length; i++) {
-        subtotal += buckets[i];
-        buckets[i] = subtotal / n_;
-      }
-    } else {
-      for (int i = 0; i < buckets.length; i++) {
-        buckets[i] /= n_;
-      }
-    }
-    return buckets;
-  }
-
-  private void incrementBucketsUnsortedLevel(final int fromIndex, final int toIndex,
-      final int weight, final double[] splitPoints, final double[] buckets) {
-    for (int i = fromIndex; i < toIndex; i++) {
-      int j;
-      for (j = 0; j < splitPoints.length; j++) {
-        if (items_[i] < splitPoints[j]) {
-          break;
-        }
-      }
-      buckets[j] += weight;
-    }
-  }
-
-  private void incrementBucketsSortedLevel(final int fromIndex, final int toIndex,
-      final int weight, final double[] splitPoints, final double[] buckets) {
-    int i = fromIndex;
-    int j = 0;
-    while (i <  toIndex && j < splitPoints.length) {
-      if (items_[i] < splitPoints[j]) {
-        buckets[j] += weight; // this sample goes into this bucket
-        i++; // move on to next sample and see whether it also goes into this bucket
-      } else {
-        j++; // no more samples for this bucket
-      }
-    }
-    // now either i == toIndex (we are out of samples), or
-    // j == numSplitPoints (we are out of buckets, but there are more samples remaining)
-    // we only need to do something in the latter case
-    if (j == splitPoints.length) {
-      buckets[j] += weight * (toIndex - i);
-    }
-  }
-
-  // The following code is only valid in the special case of exactly reaching capacity while updating.
-  // It cannot be used while merging, while reducing k, or anything else.
-  private void compressWhileUpdating() {
-    final int level = findLevelToCompact();
-
-    // It is important to do add the new top level right here. Be aware that this operation
-    // grows the buffer and shifts the data and also the boundaries of the data and grows the
-    // levels array and increments numLevels_
-    if (level == numLevels_ - 1) {
-      addEmptyTopLevelToCompletelyFullSketch();
-    }
-
-    final int rawBeg = levels_[level];
-    final int rawLim = levels_[level + 1];
-    // +2 is OK because we already added a new top level if necessary
-    final int popAbove = levels_[level + 2] - rawLim;
-    final int rawPop = rawLim - rawBeg;
-    final boolean oddPop = isOdd(rawPop);
-    final int adjBeg = oddPop ? rawBeg + 1 : rawBeg;
-    final int adjPop = oddPop ? rawPop - 1 : rawPop;
-    final int halfAdjPop = adjPop / 2;
-
-    // level zero might not be sorted, so we must sort it if we wish to compact it
-    if (level == 0) {
-      Arrays.sort(items_, adjBeg, adjBeg + adjPop);
-    }
-    if (popAbove == 0) {
-      KllDoublesHelper.randomlyHalveUpDoubles(items_, adjBeg, adjPop, random);
-    } else {
-      KllDoublesHelper.randomlyHalveDownDoubles(items_, adjBeg, adjPop, random);
-      KllDoublesHelper.mergeSortedDoubleArrays(
-          items_, adjBeg, halfAdjPop,
-          items_, rawLim, popAbove,
-          items_, adjBeg + halfAdjPop);
-    }
-    levels_[level + 1] -= halfAdjPop;          // adjust boundaries of the level above
-    if (oddPop) {
-      levels_[level] = levels_[level + 1] - 1; // the current level now contains one item
-      items_[levels_[level]] = items_[rawBeg]; // namely this leftover guy
-    } else {
-      levels_[level] = levels_[level + 1];     // the current level is now empty
-    }
-
-    // verify that we freed up halfAdjPop array slots just below the current level
-    assert levels_[level] == rawBeg + halfAdjPop;
-
-    // finally, we need to shift up the data in the levels below
-    // so that the freed-up space can be used by level zero
-    if (level > 0) {
-      final int amount = rawBeg - levels_[0];
-      System.arraycopy(items_, levels_[0], items_, levels_[0] + halfAdjPop, amount);
-      for (int lvl = 0; lvl < level; lvl++) {
-        levels_[lvl] += halfAdjPop;
-      }
-    }
+  public void update(final double value) {  //possibly move proxy

Review comment:
       what proxy?

##########
File path: src/main/java/org/apache/datasketches/kll/KllSketch.java
##########
@@ -0,0 +1,1634 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static java.lang.Math.abs;
+import static java.lang.Math.ceil;
+import static java.lang.Math.exp;
+import static java.lang.Math.log;
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static java.lang.Math.round;
+import static org.apache.datasketches.Util.isOdd;
+import static org.apache.datasketches.kll.KllPreambleUtil.DEFAULT_M;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_SINGLE_ITEM;
+import static org.apache.datasketches.kll.KllPreambleUtil.MAX_K;
+import static org.apache.datasketches.kll.KllPreambleUtil.N_LONG_ADR;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_DOUBLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_EMPTY_SINGLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_FLOAT;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_EMPTY_FULL;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_SINGLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDoubleSketchFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertEmptyFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertFamilyID;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertM;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertPreInts;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSerVer;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertSingleItemFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertUpdatableFlag;
+import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
+import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
+
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.datasketches.Family;
+import org.apache.datasketches.SketchesArgumentException;
+import org.apache.datasketches.Util;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/*
+ * Sampled stream data (floats or doubles) is stored as an array or as part of a Memory object.
+ * This array is partitioned into sections called levels and the indices into the array of items
+ * are tracked by a small integer array called levels or levels array.
+ * The data for level i lies in positions levelsArray[i] through levelsArray[i + 1] - 1 inclusive.
+ * Hence, the levelsArray must contain (numLevels + 1) indices.
+ * The valid portion of items array is completely packed and sorted, except for level 0,
+ * which is filled from the top down. Any items below the index levelsArray[0] is garbage and will be
+ * overwritten by subsequent updates.
+ *
+ * Invariants:
+ * 1) After a compaction, or an update, or a merge, all levels are sorted except for level zero.
+ * 2) After a compaction, (sum of capacities) - (sum of items) >= 1,
+ *  so there is room for least 1 more item in level zero.
+ * 3) There are no gaps except at the bottom, so if levels_[0] = 0,
+ *  the sketch is exactly filled to capacity and must be compacted or the itemsArray and levelsArray
+ *  must be expanded to include more levels.
+ * 4) Sum of weights of all retained items == N.
+ * 5) Current total item capacity = itemsArray.length = levelsArray[numLevels].
+ */
+
+/**
+ * This class is the root of the KLL sketch class hierarchy. It includes the public API that is independent
+ * of either sketch type (float or double) and independent of whether the sketch is targeted for use on the
+ * heap or Direct (off-heap.
+ *
+ * <p>Please refer to the documentation in the package-info:<br>
+ * {@link org.apache.datasketches.kll}</p>
+ *
+ * @author Lee Rhodes, Kevin Lang
+ */
+public abstract class KllSketch {
+  static final Random random = new Random();
+  static final boolean compatible = true; //rank 0.0 and 1.0. compatible with classic Quantiles Sketch
+  //final int M = DEFAULT_M; // configured minimum buffer "width", default is 8.
+  SketchType sketchType;
+  WritableMemory wmem;
+  MemoryRequestServer memReqSvr;
+  boolean direct;
+
+  /**
+   *
+   * @param sketchType either DOUBLE_SKETCH or FLOAT_SKETCH
+   * @param wmem  the current WritableMemory or null
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   */
+  KllSketch(final SketchType sketchType, final WritableMemory wmem, final MemoryRequestServer memReqSvr) {
+   this.sketchType = sketchType;
+   this.wmem = wmem;
+   if (wmem != null) {
+     this.direct = true;
+     this.memReqSvr = memReqSvr;
+   } else {
+     this.direct = false;
+     this.memReqSvr = null;
+   }
+  }
+
+public enum SketchType { FLOATS_SKETCH, DOUBLES_SKETCH }
+
+  //Static methods
+
+  /**
+   * Gets the approximate value of <em>k</em> to use given epsilon, the normalized rank error.
+   * @param epsilon the normalized rank error between zero and one.
+   * @param pmf if true, this function returns the value of <em>k</em> assuming the input epsilon
+   * is the desired "double-sided" epsilon for the getPMF() function. Otherwise, this function
+   * returns the value of <em>k</em> assuming the input epsilon is the desired "single-sided"
+   * epsilon for all the other queries.
+   *
+   * <p>Please refer to the documentation in the package-info:<br>
+   * {@link org.apache.datasketches.kll}</p>
+   * @return the value of <i>k</i> given a value of epsilon.
+   */
+  // constants were derived as the best fit to 99 percentile empirically measured max error in
+  // thousands of trials
+  public static int getKFromEpsilon(final double epsilon, final boolean pmf) {
+    //Ensure that eps is >= than the lowest possible eps given MAX_K and pmf=false.
+    final double eps = max(epsilon, 4.7634E-5);
+    final double kdbl = pmf
+        ? exp(log(2.446 / eps) / 0.9433)

Review comment:
       I have seen the same constants in KllHelper. better keep them in one place?

##########
File path: src/main/java/org/apache/datasketches/kll/KllDirectSketch.java
##########
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.kll.KllPreambleUtil.extractDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractK;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractM;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractN;
+import static org.apache.datasketches.kll.KllPreambleUtil.extractNumLevels;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertDyMinK;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertLevelZeroSortedFlag;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertN;
+import static org.apache.datasketches.kll.KllPreambleUtil.insertNumLevels;
+import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
+import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+/**
+ * This class implements all the methods for the Direct (off-heap) sketches that are independent
+ * of the sketch type (float or double).
+ */
+abstract class KllDirectSketch extends KllSketch {
+  //All these members are constant for the life of this object. If the WritableMemory changes,
+  // it may require rebuilding this class
+  final boolean updatable = true;
+  WritableMemory levelsArrUpdatable;
+  WritableMemory minMaxArrUpdatable;
+  WritableMemory itemsArrUpdatable;
+
+  /**
+   * For the direct sketches it is important that the methods implemented here are designed to work dynamically
+   * as the sketch grows off-heap.
+   * @param sketchType either DOUBLE_SKETCH or FLOAT_SKETCH
+   * @param wmem the current WritableMemory
+   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
+   */
+  KllDirectSketch(final SketchType sketchType, final WritableMemory wmem, final MemoryRequestServer memReqSvr,
+      final MemoryValidate memVal) {
+    super(sketchType, wmem, memReqSvr);
+    levelsArrUpdatable = memVal.levelsArrUpdatable;
+    minMaxArrUpdatable = memVal.minMaxArrUpdatable;
+    itemsArrUpdatable = memVal.itemsArrUpdatable;
+  }
+
+  @Override
+  public int getK() {
+    return extractK(wmem);
+  }
+
+  @Override
+  public int getM() {
+    return extractM(wmem);
+  }
+
+  @Override
+  public long getN() {
+    return extractN(wmem);
+  }
+
+  @Override
+  public byte[] toUpdatableByteArray() {
+    final int bytes = (int) wmem.getCapacity();
+    final byte[] byteArr = new byte[bytes];
+    wmem.getByteArray(0, byteArr, 0, bytes);
+    return byteArr;
+  }
+
+  @Override
+  double[] getDoubleItemsArray() {
+    if (sketchType == FLOATS_SKETCH) { return null; }
+    final int items = getItemsArrLengthItems();
+    final double[] itemsArr = new double[items];
+    itemsArrUpdatable.getDoubleArray(0, itemsArr, 0, items);
+    return itemsArr;
+  }
+
+  @Override
+  double getDoubleItemsArrayAt(final int index) {
+    if (sketchType == FLOATS_SKETCH) { return Double.NaN; }
+    return itemsArrUpdatable.getDouble((long)index * Double.BYTES);
+  }
+
+  @Override
+  int getDyMinK() {
+    return extractDyMinK(wmem);
+  }
+
+  @Override
+  float[] getFloatItemsArray() {
+    if (sketchType == DOUBLES_SKETCH) { return null; }
+    final int items = getItemsArrLengthItems();
+    final float[] itemsArr = new float[items];
+    itemsArrUpdatable.getFloatArray(0, itemsArr, 0, items);
+    return itemsArr;
+  }
+
+  @Override
+  float getFloatItemsArrayAt(final int index) {
+    if (sketchType == DOUBLES_SKETCH) { return Float.NaN; }
+    return itemsArrUpdatable.getFloat((long)index * Float.BYTES);
+  }
+
+  int getItemsArrLengthItems() {
+    return getLevelsArray()[getNumLevels()];
+  }
+
+  @Override
+  int[] getLevelsArray() {
+    final int numInts = getNumLevels() + 1;
+    final int[] myLevelsArr = new int[numInts];
+    levelsArrUpdatable.getIntArray(0, myLevelsArr, 0, numInts);
+    return myLevelsArr;
+  }
+
+  @Override
+  int getLevelsArrayAt(final int index) {
+    return levelsArrUpdatable.getInt((long)index * Integer.BYTES);
+  }
+
+  @Override
+  double getMaxDoubleValue() {
+    return minMaxArrUpdatable.getDouble(Double.BYTES);
+  }
+
+  @Override
+  float getMaxFloatValue() {
+    return minMaxArrUpdatable.getFloat(Float.BYTES);
+  }
+
+  @Override
+  double getMinDoubleValue() {
+    return minMaxArrUpdatable.getDouble(0);
+  }
+
+  @Override
+  float getMinFloatValue() {
+    return minMaxArrUpdatable.getFloat(0);
+  }
+
+  @Override
+  int getNumLevels() {
+    return extractNumLevels(wmem);
+  }
+
+  @Override
+  void incN() {
+    if (!updatable) { kllSketchThrow(30); }

Review comment:
       magic number, and a few more below




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org