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/08/04 22:50:09 UTC

[incubator-datasketches-java] 01/01: Rename "simpleIntLog2" to "simpleLog2OfLong" plus javadoc changes.

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

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

commit 6a935bafdb7189dbeb764328edf11199d8458265
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Tue Aug 4 15:47:50 2020 -0700

    Rename "simpleIntLog2" to "simpleLog2OfLong" plus javadoc changes.
---
 src/main/java/org/apache/datasketches/Util.java    | 43 +++++++++++++++++-----
 .../org/apache/datasketches/hll/PreambleUtil.java  |  6 +--
 .../org/apache/datasketches/theta/AnotBimpl.java   |  4 +-
 .../java/org/apache/datasketches/tuple/AnotB.java  |  6 +--
 .../datasketches/tuple/QuickSelectSketch.java      |  4 +-
 .../java/org/apache/datasketches/UtilTest.java     | 27 ++++++++++----
 6 files changed, 63 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/apache/datasketches/Util.java b/src/main/java/org/apache/datasketches/Util.java
index 464513d..0fd8c0e 100644
--- a/src/main/java/org/apache/datasketches/Util.java
+++ b/src/main/java/org/apache/datasketches/Util.java
@@ -440,8 +440,9 @@ public final class Util {
   }
 
   /**
-   * Computes the floor power of 2 within the range [1, 2^30]. This is the largest positive power of
-   * 2 that equal to or less than the given n and equal to a mathematical integer.
+   * Computes the floor power of 2 given <i>n</i> is in therange [1, 2^31-1].
+   * This is the largest positive power of 2 that equal to or less than the given n and equal
+   * to a mathematical integer.
    *
    * <p>For:
    * <ul>
@@ -452,8 +453,8 @@ public final class Util {
    * integer.</li>
    * </ul>
    *
-   * @param n The given argument.
-   * @return the floor power of 2.
+   * @param n The given int argument.
+   * @return the floor power of 2 as an int.
    */
   public static int floorPowerOf2(final int n) {
     if (n <= 1) { return 1; }
@@ -461,6 +462,28 @@ public final class Util {
   }
 
   /**
+   * Computes the floor power of 2 given <i>n</i> is in therange [1, 2^63-1].
+   * This is the largest positive power of 2 that equal to or less than the given n and equal
+   * to a mathematical integer.
+   *
+   * <p>For:
+   * <ul>
+   * <li>n &le; 1: returns 1</li>
+   * <li>2^62 &le; n &le; 2^63 -1 : returns 2^62</li>
+   * <li>n == a power of 2 : returns n</li>
+   * <li>otherwise returns the largest power of 2 less than n and equal to a mathematical
+   * integer.</li>
+   * </ul>
+   *
+   * @param n The given long argument.
+   * @return the floor power of 2 as a long
+   */
+  public static long floorPowerOf2(final long n) {
+    if (n <= 1) { return 1; }
+    return Long.highestOneBit(n);
+  }
+
+  /**
    * Computes the inverse integer power of 2: 1/(2^e) = 2^(-e).
    * @param e a positive value between 0 and 1023 inclusive
    * @return  the inverse integer power of 2: 1/(2^e) = 2^(-e)
@@ -570,15 +593,15 @@ public final class Util {
   }
 
   /**
-   * Gives the log2 of an integer that is known to be a power of 2.
+   * Gives the log2 of a long that is known to be a power of 2.
    *
    * @param x number that is greater than zero
-   * @return the log2 of an integer that is known to be a power of 2.
+   * @return the log2 of a long that is known to be a power of 2.
    */
-  public static int simpleIntLog2(final int x) {
-    final int exp = Integer.numberOfTrailingZeros(x);
-    if (x != (1 << exp)) {
-      throw new SketchesArgumentException("Argument x cannot be negative or zero.");
+  public static int simpleLog2OfLong(final long x) {
+    final int exp = Long.numberOfTrailingZeros(x);
+    if (x != (1L << exp)) {
+      throw new SketchesArgumentException("Argument x must be a positive power of 2.");
     }
     return exp;
   }
diff --git a/src/main/java/org/apache/datasketches/hll/PreambleUtil.java b/src/main/java/org/apache/datasketches/hll/PreambleUtil.java
index 0106a18..eb8c95c 100644
--- a/src/main/java/org/apache/datasketches/hll/PreambleUtil.java
+++ b/src/main/java/org/apache/datasketches/hll/PreambleUtil.java
@@ -20,7 +20,7 @@
 package org.apache.datasketches.hll;
 
 import static org.apache.datasketches.Util.ceilingPowerOf2;
-import static org.apache.datasketches.Util.simpleIntLog2;
+import static org.apache.datasketches.Util.simpleLog2OfLong;
 import static org.apache.datasketches.Util.zeroPad;
 import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS;
 import static org.apache.datasketches.hll.HllUtil.LG_INIT_SET_SIZE;
@@ -463,10 +463,10 @@ final class PreambleUtil {
     int ceilPwr2 = ceilingPowerOf2(count);
     if ((RESIZE_DENOM * count) > (RESIZE_NUMER * ceilPwr2)) { ceilPwr2 <<= 1; }
     if (curMode == CurMode.SET) {
-      return Math.max(LG_INIT_SET_SIZE, simpleIntLog2(ceilPwr2));
+      return Math.max(LG_INIT_SET_SIZE, simpleLog2OfLong(ceilPwr2));
     }
     //only used for HLL4
-    return Math.max(LG_AUX_ARR_INTS[lgConfigK], simpleIntLog2(ceilPwr2));
+    return Math.max(LG_AUX_ARR_INTS[lgConfigK], simpleLog2OfLong(ceilPwr2));
   }
 
 }
diff --git a/src/main/java/org/apache/datasketches/theta/AnotBimpl.java b/src/main/java/org/apache/datasketches/theta/AnotBimpl.java
index 9eecee3..ca6f03d 100644
--- a/src/main/java/org/apache/datasketches/theta/AnotBimpl.java
+++ b/src/main/java/org/apache/datasketches/theta/AnotBimpl.java
@@ -24,7 +24,7 @@ import static org.apache.datasketches.HashOperations.hashSearch;
 import static org.apache.datasketches.Util.REBUILD_THRESHOLD;
 import static org.apache.datasketches.Util.checkSeedHashes;
 import static org.apache.datasketches.Util.computeSeedHash;
-import static org.apache.datasketches.Util.simpleIntLog2;
+import static org.apache.datasketches.Util.simpleLog2OfLong;
 
 import java.util.Arrays;
 
@@ -180,7 +180,7 @@ final class AnotBimpl extends AnotB {
     final long[] tmpHashArrA = new long[countA];
 
     //search for non matches and build temp arrays
-    final int lgHTBLen = simpleIntLog2(hashTableB.length);
+    final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
     int nonMatches = 0;
     for (int i = 0; i < countA; i++) {
       final long hash = hashArrA[i];
diff --git a/src/main/java/org/apache/datasketches/tuple/AnotB.java b/src/main/java/org/apache/datasketches/tuple/AnotB.java
index 8cedd96..0292315 100644
--- a/src/main/java/org/apache/datasketches/tuple/AnotB.java
+++ b/src/main/java/org/apache/datasketches/tuple/AnotB.java
@@ -22,7 +22,7 @@ package org.apache.datasketches.tuple;
 import static org.apache.datasketches.HashOperations.convertToHashTable;
 import static org.apache.datasketches.HashOperations.hashSearch;
 import static org.apache.datasketches.Util.REBUILD_THRESHOLD;
-import static org.apache.datasketches.Util.simpleIntLog2;
+import static org.apache.datasketches.Util.simpleLog2OfLong;
 
 import java.lang.reflect.Array;
 import java.lang.reflect.Method;
@@ -366,7 +366,7 @@ public final class AnotB<S extends Summary> {
     final S[] tmpSummaryArrA = (S[]) Array.newInstance(summaryType, countA);
 
     //search for non matches and build temp arrays
-    final int lgHTBLen = simpleIntLog2(hashTableB.length);
+    final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
     int nonMatches = 0;
     for (int i = 0; i < countA; i++) {
       final long hash = hashArrA[i];
@@ -414,7 +414,7 @@ public final class AnotB<S extends Summary> {
     final S[] tmpSummaryArrA = (S[]) Array.newInstance(summaryType, countA);
 
     //search for non matches and build temp arrays
-    final int lgHTBLen = simpleIntLog2(hashTableB.length);
+    final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
     int nonMatches = 0;
     for (int i = 0; i < countA; i++) {
       final long hash = hashArrA[i];
diff --git a/src/main/java/org/apache/datasketches/tuple/QuickSelectSketch.java b/src/main/java/org/apache/datasketches/tuple/QuickSelectSketch.java
index e6fb943..99166e2 100644
--- a/src/main/java/org/apache/datasketches/tuple/QuickSelectSketch.java
+++ b/src/main/java/org/apache/datasketches/tuple/QuickSelectSketch.java
@@ -22,7 +22,7 @@ package org.apache.datasketches.tuple;
 import static org.apache.datasketches.Util.REBUILD_THRESHOLD;
 import static org.apache.datasketches.Util.RESIZE_THRESHOLD;
 import static org.apache.datasketches.Util.ceilingPowerOf2;
-import static org.apache.datasketches.Util.simpleIntLog2;
+import static org.apache.datasketches.Util.simpleLog2OfLong;
 
 import java.lang.reflect.Array;
 import java.nio.ByteOrder;
@@ -227,7 +227,7 @@ class QuickSelectSketch<S extends Summary> extends Sketch<S> {
    * @return log_base2 of Nominal Entries
    */
   public int getLgK() {
-    return simpleIntLog2(nomEntries_);
+    return simpleLog2OfLong(nomEntries_);
   }
 
   /**
diff --git a/src/test/java/org/apache/datasketches/UtilTest.java b/src/test/java/org/apache/datasketches/UtilTest.java
index 52b92dc..4ca1445 100644
--- a/src/test/java/org/apache/datasketches/UtilTest.java
+++ b/src/test/java/org/apache/datasketches/UtilTest.java
@@ -43,7 +43,7 @@ import static org.apache.datasketches.Util.nanoSecToString;
 import static org.apache.datasketches.Util.pwr2LawNext;
 import static org.apache.datasketches.Util.pwr2LawPrev;
 import static org.apache.datasketches.Util.pwrLawNextDouble;
-import static org.apache.datasketches.Util.simpleIntLog2;
+import static org.apache.datasketches.Util.simpleLog2OfLong;
 import static org.apache.datasketches.Util.zeroPad;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
@@ -111,7 +111,7 @@ public class UtilTest {
   }
 
   @Test
-  public void checkFloorPowerOf2() {
+  public void checkFloorPowerOf2Int() {
     Assert.assertEquals(floorPowerOf2( -1), 1);
     Assert.assertEquals(floorPowerOf2(0), 1);
     Assert.assertEquals(floorPowerOf2(1), 1);
@@ -123,7 +123,19 @@ public class UtilTest {
     Assert.assertEquals(floorPowerOf2((1 << 30)), (1 << 30));
     Assert.assertEquals(floorPowerOf2((1 << 30) + 1), (1 << 30));
   }
-
+  @Test
+  public void checkFloorPowerOf2Long() {
+    Assert.assertEquals(floorPowerOf2( -1L), 1L);
+    Assert.assertEquals(floorPowerOf2(0L), 1L);
+    Assert.assertEquals(floorPowerOf2(1L), 1L);
+    Assert.assertEquals(floorPowerOf2(2L), 2L);
+    Assert.assertEquals(floorPowerOf2(3L), 2L);
+    Assert.assertEquals(floorPowerOf2(4L), 4L);
+
+    Assert.assertEquals(floorPowerOf2((1L << 63) - 1L), (1L << 62));
+    Assert.assertEquals(floorPowerOf2((1L << 62)), (1L << 62));
+    Assert.assertEquals(floorPowerOf2((1L << 62) + 1L), (1L << 62));
+  }
   @Test
   public void checkFloorPowerOf2double() {
     Assert.assertEquals(floorPowerOfBdouble(2.0, -1.0), 1.0);
@@ -318,11 +330,12 @@ public class UtilTest {
   }
 
   @Test
-  public void checkSimpleIntLog2() {
-    Assert.assertEquals(simpleIntLog2(2), 1);
-    Assert.assertEquals(simpleIntLog2(1), 0);
+  public void checkSimpleLog2OfLong() {
+    Assert.assertEquals(simpleLog2OfLong(2), 1);
+    Assert.assertEquals(simpleLog2OfLong(1), 0);
+    Assert.assertEquals(simpleLog2OfLong(1L << 62), 62);
     try {
-      simpleIntLog2(0);
+      simpleLog2OfLong(0);
       fail();
     } catch (final SketchesArgumentException e) { }
   }


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