You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2020/02/03 23:05:17 UTC

[incubator-datasketches-java] 03/04: update javadoc in Conversions.curMinAndNum() to make more clear.

This is an automated email from the ASF dual-hosted git repository.

leerho pushed a commit to branch CheckCurMin
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-java.git

commit 0a1de039c77b1f8204c38a8327fee94f944bc553
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Mon Feb 3 15:02:31 2020 -0800

    update javadoc in Conversions.curMinAndNum() to make more clear.
    
    Added IsomorphicTest.checkCurMinConversions() to improve confidence that
    curMin is computed after a type conversion from 8 or 6 to 4.
    
    Added additional check for isRebuildCurMinNumKxQFlag() in
    union.getResult() and union toString(...).
---
 .../org/apache/datasketches/hll/Conversions.java   |  9 +++++--
 .../java/org/apache/datasketches/hll/Union.java    |  8 +++----
 .../apache/datasketches/hll/IsomorphicTest.java    | 28 +++++++++++++++++++++-
 3 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/datasketches/hll/Conversions.java b/src/main/java/org/apache/datasketches/hll/Conversions.java
index f92628c..0f06ffe 100644
--- a/src/main/java/org/apache/datasketches/hll/Conversions.java
+++ b/src/main/java/org/apache/datasketches/hll/Conversions.java
@@ -69,8 +69,13 @@ class Conversions {
   }
 
   /**
-   * If the given absHllArr is type HLL_4, this returns the correct curMin and numAtCurMin.
-   * For HLL_6 and HLL_8, curMin is always 0, and numAtCurMin is the number of zero slots.
+   * This returns curMin and numAtCurMin as a pair and will be correct independent of the TgtHllType
+   * of the input AbstractHllArray.
+   *
+   * <p>In general, it is always true that for HLL_6 and HLL_8, curMin is always 0, and numAtCurMin
+   * is the number of zero slots. For these two types there is no need to track curMin nor to track
+   * numAtCurMin once all the slots are filled.
+   *
    * @param absHllArr an instance of AbstractHllArray
    * @return pair values representing numAtCurMin and curMin
    */
diff --git a/src/main/java/org/apache/datasketches/hll/Union.java b/src/main/java/org/apache/datasketches/hll/Union.java
index cc00d41..e9cf76f 100644
--- a/src/main/java/org/apache/datasketches/hll/Union.java
+++ b/src/main/java/org/apache/datasketches/hll/Union.java
@@ -200,10 +200,7 @@ public class Union extends BaseHllSketch {
    * @return the result of this union operator as an HLL_4 sketch.
    */
   public HllSketch getResult() {
-    if (gadget.hllSketchImpl.isRebuildCurMinNumKxQFlag()) {
-      rebuildCurMinNumKxQ((AbstractHllArray)(gadget.hllSketchImpl));
-    }
-    return gadget.copyAs(HllSketch.DEFAULT_HLL_TYPE);
+    return getResult(HllSketch.DEFAULT_HLL_TYPE);
   }
 
   /**
@@ -309,6 +306,9 @@ public class Union extends BaseHllSketch {
   @Override
   public String toString(final boolean summary, final boolean hllDetail,
       final boolean auxDetail, final boolean all) {
+    if (gadget.hllSketchImpl.isRebuildCurMinNumKxQFlag()) {
+      rebuildCurMinNumKxQ((AbstractHllArray)(gadget.hllSketchImpl));
+    }
     return gadget.toString(summary, hllDetail, auxDetail, all);
   }
 
diff --git a/src/test/java/org/apache/datasketches/hll/IsomorphicTest.java b/src/test/java/org/apache/datasketches/hll/IsomorphicTest.java
index d896b7d..7b2c680 100644
--- a/src/test/java/org/apache/datasketches/hll/IsomorphicTest.java
+++ b/src/test/java/org/apache/datasketches/hll/IsomorphicTest.java
@@ -25,6 +25,7 @@ import static org.apache.datasketches.hll.CurMode.SET;
 import static org.apache.datasketches.hll.TgtHllType.HLL_4;
 import static org.apache.datasketches.hll.TgtHllType.HLL_6;
 import static org.apache.datasketches.hll.TgtHllType.HLL_8;
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
 
 import org.apache.datasketches.memory.WritableMemory;
@@ -328,6 +329,31 @@ public class IsomorphicTest {
     return ((lgK < 8) && (curMode == HLL)) ? (1 << lgK) : 1 << (lgK - 3);
   }
 
+  @Test
+  public void checkCurMinConversion() {
+    TgtHllType hll8 = HLL_8;
+    TgtHllType hll4 = HLL_4;
+    for (int lgK = 4; lgK <= 21; lgK++) {
+      HllSketch sk8 = new HllSketch(lgK, hll8);
+      //The Coupon Collector Problem predicts that all slots will be filled by k Log(k).
+      int n = (1 << lgK) * lgK;
+      for (int i = 0; i < n; i++) { sk8.update(i); }
+      double est8 = sk8.getEstimate();
+      AbstractHllArray aharr8 = (AbstractHllArray)sk8.hllSketchImpl;
+      int curMin8 = aharr8.getCurMin();
+      int numAtCurMin8 = aharr8.getNumAtCurMin();
+      HllSketch sk4 = sk8.copyAs(hll4);
+      AbstractHllArray aharr4 = (AbstractHllArray)sk4.hllSketchImpl;
+      int curMin4 = ((AbstractHllArray)sk4.hllSketchImpl).getCurMin();
+      int numAtCurMin4 =aharr4.getNumAtCurMin();
+      double est4 = sk4.getEstimate();
+      assertEquals(est4, est8, 0.0);
+      assertEquals(curMin4, 1);
+      //println("Est 8 = " + est8 + ", CurMin = " + curMin8 + ", #CurMin + " + numAtCurMin8);
+      //println("Est 4 = " + est4 + ", CurMin = " + curMin4 + ", #CurMin + " + numAtCurMin4);
+    }
+  }
+
   private static double bytesToDouble(byte[] arr, int offset) {
     long v = 0;
     for (int i = offset; i < (offset + 8); i++) {
@@ -352,7 +378,7 @@ public class IsomorphicTest {
    * @param o value to print
    */
   static void print(Object o) {
-    //System.out.print(o.toString()); //disable here
+    System.out.print(o.toString()); //disable here
   }
 
   /**


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