Skip to content

Commit

Permalink
Minor method renaming that propogated to a lot of places.
Browse files Browse the repository at this point in the history
  • Loading branch information
leerho committed May 21, 2022
1 parent f02347a commit 4db2da1
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 125 deletions.
69 changes: 0 additions & 69 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

29 changes: 13 additions & 16 deletions src/main/java/org/apache/datasketches/Util.java
Expand Up @@ -421,41 +421,38 @@ public static boolean isPowerOf2(final int v) {
* Checks the given parameter to make sure it is positive, an integer-power of 2 and greater than
* zero.
*
* @param v The input argument.
* @param powerOf2 The input argument must be a power of 2 and greater than zero.
* @param argName Used in the thrown exception.
*/
public static void checkIfPowerOf2(final int v, final String argName) {
if (v > 0 && (v & v - 1) == 0) {
return;
}
public static void checkIfPowerOf2(final int powerOf2, final String argName) {
if (powerOf2 > 0 && (powerOf2 & powerOf2 - 1) == 0) { return; }
throw new SketchesArgumentException("The value of the parameter \"" + argName
+ "\" must be a positive integer-power of 2" + " and greater than 0: " + v);
+ "\" must be a positive integer-power of 2" + " and greater than 0: " + powerOf2);
}

/**
* Checks the given value if it is a power of 2. If not, it throws an exception.
* Otherwise, returns the log-base2 of the given value.
* @param value must be a power of 2 and greater than zero.
* @param powerOf2 must be a power of 2 and greater than zero.
* @param argName the argument name used in the exception if thrown.
* @return the log-base2 of the given value
*/
public static int toLog2(final int value, final String argName) {
checkIfPowerOf2(value, argName);
return Integer.numberOfTrailingZeros(value);
public static int exactLog2(final int powerOf2, final String argName) {
checkIfPowerOf2(powerOf2, argName);
return Integer.numberOfTrailingZeros(powerOf2);
}

/**
* Gives the log2 of a long that is known to be a power of 2.
*
* @param x number that is greater than zero
* @param powerOf2 must be a power of 2 and greater than zero.
* @return the log2 of a long that is known to be a power of 2.
*/
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.");
public static int exactLog2OfLong(final long powerOf2) {
if (powerOf2 <= 0 || (powerOf2 & (powerOf2 - 1L)) != 0) {
throw new SketchesArgumentException("Argument 'powerOf2' must be a positive power of 2.");
}
return exp;
return Long.numberOfTrailingZeros(powerOf2);
}

/**
Expand Down
Expand Up @@ -21,7 +21,7 @@

import static org.apache.datasketches.Util.LS;
import static org.apache.datasketches.Util.isPowerOf2;
import static org.apache.datasketches.Util.toLog2;
import static org.apache.datasketches.Util.exactLog2;
import static org.apache.datasketches.frequencies.PreambleUtil.EMPTY_FLAG_MASK;
import static org.apache.datasketches.frequencies.PreambleUtil.SER_VER;
import static org.apache.datasketches.frequencies.PreambleUtil.extractActiveItems;
Expand Down Expand Up @@ -188,7 +188,7 @@ public class ItemsSketch<T> {
* functions of maxMapSize.
*/
public ItemsSketch(final int maxMapSize) {
this(toLog2(maxMapSize, "maxMapSize"), LG_MIN_MAP_SIZE);
this(exactLog2(maxMapSize, "maxMapSize"), LG_MIN_MAP_SIZE);
}

/**
Expand Down
Expand Up @@ -21,7 +21,7 @@

import static org.apache.datasketches.Util.LS;
import static org.apache.datasketches.Util.isPowerOf2;
import static org.apache.datasketches.Util.toLog2;
import static org.apache.datasketches.Util.exactLog2;
import static org.apache.datasketches.frequencies.PreambleUtil.EMPTY_FLAG_MASK;
import static org.apache.datasketches.frequencies.PreambleUtil.SER_VER;
import static org.apache.datasketches.frequencies.PreambleUtil.extractActiveItems;
Expand Down Expand Up @@ -185,7 +185,7 @@ public class LongsSketch {
* function of maxMapSize.
*/
public LongsSketch(final int maxMapSize) {
this(toLog2(maxMapSize, "maxMapSize"), LG_MIN_MAP_SIZE);
this(exactLog2(maxMapSize, "maxMapSize"), LG_MIN_MAP_SIZE);
}

/**
Expand Down
Expand Up @@ -20,7 +20,7 @@
package org.apache.datasketches.frequencies;

import static org.apache.datasketches.Util.LS;
import static org.apache.datasketches.Util.toLog2;
import static org.apache.datasketches.Util.exactLog2;
import static org.apache.datasketches.frequencies.Util.hash;

import java.lang.reflect.Array;
Expand Down Expand Up @@ -59,7 +59,7 @@ class ReversePurgeItemHashMap<T> {
* The hash table will be expected to store LOAD_FACTOR * mapSize (key, value) pairs.
*/
ReversePurgeItemHashMap(final int mapSize) {
lgLength = toLog2(mapSize, "mapSize");
lgLength = exactLog2(mapSize, "mapSize");
this.loadThreshold = (int) (mapSize * LOAD_FACTOR);
this.keys = new Object[mapSize];
this.values = new long[mapSize];
Expand Down
Expand Up @@ -20,7 +20,7 @@
package org.apache.datasketches.frequencies;

import static org.apache.datasketches.Util.LS;
import static org.apache.datasketches.Util.toLog2;
import static org.apache.datasketches.Util.exactLog2;
import static org.apache.datasketches.frequencies.Util.hash;

import org.apache.datasketches.QuickSelect;
Expand Down Expand Up @@ -56,7 +56,7 @@ class ReversePurgeLongHashMap {
* The hash table will be expected to store LOAD_FACTOR * mapSize (key, value) pairs.
*/
ReversePurgeLongHashMap(final int mapSize) {
lgLength = toLog2(mapSize, "mapSize");
lgLength = exactLog2(mapSize, "mapSize");
loadThreshold = (int) (mapSize * LOAD_FACTOR);
keys = new long[mapSize];
values = new long[mapSize];
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/datasketches/hll/PreambleUtil.java
Expand Up @@ -20,7 +20,7 @@
package org.apache.datasketches.hll;

import static org.apache.datasketches.Util.ceilingPowerOf2;
import static org.apache.datasketches.Util.simpleLog2OfLong;
import static org.apache.datasketches.Util.exactLog2OfLong;
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;
Expand Down Expand Up @@ -463,10 +463,10 @@ static int computeLgArr(final Memory mem, final int count, final int lgConfigK)
int ceilPwr2 = ceilingPowerOf2(count);
if ((RESIZE_DENOM * count) > (RESIZE_NUMER * ceilPwr2)) { ceilPwr2 <<= 1; }
if (curMode == CurMode.SET) {
return Math.max(LG_INIT_SET_SIZE, simpleLog2OfLong(ceilPwr2));
return Math.max(LG_INIT_SET_SIZE, exactLog2OfLong(ceilPwr2));
}
//only used for HLL4
return Math.max(LG_AUX_ARR_INTS[lgConfigK], simpleLog2OfLong(ceilPwr2));
return Math.max(LG_AUX_ARR_INTS[lgConfigK], exactLog2OfLong(ceilPwr2));
}

}
Expand Up @@ -91,7 +91,7 @@ private ReservoirItemsSketch(final int k, final ResizeFactor rf) {

itemsSeen_ = 0;

final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(reservoirSize_), "ReservoirItemsSketch");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(reservoirSize_), "ReservoirItemsSketch");
final int initialLgSize =
SamplingUtil.startingSubMultiple(ceilingLgK, rf_.lg(), MIN_LG_ARR_ITEMS);

Expand Down Expand Up @@ -249,8 +249,8 @@ public static <T> ReservoirItemsSketch<T> heapify(final Memory srcMem,
if (itemsSeen < k) {
// under-full so determine size to allocate, using ceilingLog2(totalSeen) as minimum
// casts to int are safe since under-full
final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(k), "heapify");
final int minLgSize = Util.toLog2(Util.ceilingPowerOf2((int) itemsSeen), "heapify");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(k), "heapify");
final int minLgSize = Util.exactLog2(Util.ceilingPowerOf2((int) itemsSeen), "heapify");
final int initialLgSize = SamplingUtil.startingSubMultiple(ceilingLgK, rf.lg(),
Math.max(minLgSize, MIN_LG_ARR_ITEMS));

Expand Down Expand Up @@ -335,7 +335,7 @@ public void update(final T item) {
* Resets this sketch to the empty state, but retains the original value of k.
*/
public void reset() {
final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(reservoirSize_), "ReservoirItemsSketch");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(reservoirSize_), "ReservoirItemsSketch");
final int initialLgSize =
SamplingUtil.startingSubMultiple(ceilingLgK, rf_.lg(), MIN_LG_ARR_ITEMS);

Expand Down
Expand Up @@ -92,7 +92,7 @@ private ReservoirLongsSketch(final int k, final ResizeFactor rf) {

itemsSeen_ = 0;

final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(reservoirSize_), "ReservoirLongsSketch");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(reservoirSize_), "ReservoirLongsSketch");
final int initialLgSize =
SamplingUtil.startingSubMultiple(ceilingLgK, rf_.lg(), MIN_LG_ARR_LONGS);

Expand Down Expand Up @@ -229,8 +229,8 @@ public static ReservoirLongsSketch heapify(final Memory srcMem) {
if (itemsSeen < k) {
// under-full so determine size to allocate, using ceilingLog2(totalSeen) as minimum
// casts to int are safe since under-full
final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(k), "heapify");
final int minLgSize = Util.toLog2(Util.ceilingPowerOf2((int) itemsSeen), "heapify");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(k), "heapify");
final int minLgSize = Util.exactLog2(Util.ceilingPowerOf2((int) itemsSeen), "heapify");
final int initialLgSize = SamplingUtil.startingSubMultiple(ceilingLgK, rf.lg(),
Math.max(minLgSize, MIN_LG_ARR_LONGS));

Expand Down Expand Up @@ -334,7 +334,7 @@ public void update(final long item) {
* Resets this sketch to the empty state, but retains the original value of k.
*/
public void reset() {
final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(reservoirSize_),
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(reservoirSize_),
"ReservoirLongsSketch");
final int initialLgSize =
SamplingUtil.startingSubMultiple(ceilingLgK, rf_.lg(), MIN_LG_ARR_LONGS);
Expand Down
Expand Up @@ -76,7 +76,7 @@ public static short computeSize(final int k) {
+ "less than " + MAX_ABS_VALUE + ", found: " + k);
}

final int p = Util.toLog2(Util.floorPowerOf2(k), "computeSize: p");
final int p = Util.exactLog2(Util.floorPowerOf2(k), "computeSize: p");

// because of floor() + 1 below, need to check power of 2 here
if (Util.isPowerOf2(k)) {
Expand Down
Expand Up @@ -136,7 +136,7 @@ private VarOptItemsSketch(final int k, final ResizeFactor rf) {
totalWtR_ = 0;
numMarksInH_ = 0;

final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(k_), "VarOptItemsSketch");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(k_), "VarOptItemsSketch");
final int initialLgSize =
SamplingUtil.startingSubMultiple(ceilingLgK, rf_.lg(), MIN_LG_ARR_ITEMS);

Expand Down Expand Up @@ -347,8 +347,8 @@ public static <T> VarOptItemsSketch<T> heapify(final Memory srcMem,

if (rCount == 0) {
// Not in sampling mode, so determine size to allocate, using ceilingLog2(hCount) as minimum
final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(k), "heapify");
final int minLgSize = Util.toLog2(Util.ceilingPowerOf2(hCount), "heapify");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(k), "heapify");
final int minLgSize = Util.exactLog2(Util.ceilingPowerOf2(hCount), "heapify");
final int initialLgSize = SamplingUtil.startingSubMultiple(ceilingLgK, rf.lg(),
Math.max(minLgSize, MIN_LG_ARR_ITEMS));

Expand Down Expand Up @@ -474,7 +474,7 @@ public void update(final T item, final double weight) {
* Resets this sketch to the empty state, but retains the original value of k.
*/
public void reset() {
final int ceilingLgK = Util.toLog2(Util.ceilingPowerOf2(k_), "VarOptItemsSketch");
final int ceilingLgK = Util.exactLog2(Util.ceilingPowerOf2(k_), "VarOptItemsSketch");
final int initialLgSize =
SamplingUtil.startingSubMultiple(ceilingLgK, rf_.lg(), MIN_LG_ARR_ITEMS);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/datasketches/theta/AnotBimpl.java
Expand Up @@ -24,7 +24,7 @@
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.simpleLog2OfLong;
import static org.apache.datasketches.Util.exactLog2OfLong;

import java.util.Arrays;

Expand Down Expand Up @@ -184,7 +184,7 @@ private static long[] getResultHashArr( //returns a new array
final long[] tmpHashArrA = new long[countA];

//search for non matches and build temp arrays
final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
final int lgHTBLen = exactLog2OfLong(hashTableB.length);
int nonMatches = 0;
for (int i = 0; i < countA; i++) {
final long hash = hashArrA[i];
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/datasketches/tuple/AnotB.java
Expand Up @@ -23,7 +23,7 @@
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.simpleLog2OfLong;
import static org.apache.datasketches.Util.exactLog2OfLong;

import java.lang.reflect.Method;
import java.util.Arrays;
Expand Down Expand Up @@ -522,7 +522,7 @@ private static <S extends Summary> DataArrays<S> getCopyOfResultArraysTuple(
final S[] tmpSummaryArrA = Util.newSummaryArray(summaryArrA, countA);

//search for non matches and build temp arrays
final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
final int lgHTBLen = exactLog2OfLong(hashTableB.length);
int nonMatches = 0;
for (int i = 0; i < countA; i++) {
final long hash = hashArrA[i];
Expand Down Expand Up @@ -568,7 +568,7 @@ private static <S extends Summary> DataArrays<S> getCopyOfResultArraysTheta(
final S[] tmpSummaryArrA = Util.newSummaryArray(summaryArrA, countA);

//search for non matches and build temp arrays
final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
final int lgHTBLen = exactLog2OfLong(hashTableB.length);
int nonMatches = 0;
for (int i = 0; i < countA; i++) {
final long hash = hashArrA[i];
Expand Down
Expand Up @@ -23,7 +23,7 @@
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.simpleLog2OfLong;
import static org.apache.datasketches.Util.exactLog2OfLong;

import java.lang.reflect.Array;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -261,7 +261,7 @@ public int getNominalEntries() {
* @return log_base2 of Nominal Entries
*/
public int getLgK() {
return simpleLog2OfLong(nomEntries_);
return exactLog2OfLong(nomEntries_);
}

/**
Expand Down
Expand Up @@ -25,7 +25,7 @@
import static org.apache.datasketches.HashOperations.count;
import static org.apache.datasketches.HashOperations.hashSearch;
import static org.apache.datasketches.Util.REBUILD_THRESHOLD;
import static org.apache.datasketches.Util.simpleLog2OfLong;
import static org.apache.datasketches.Util.exactLog2OfLong;

import java.util.Arrays;

Expand Down Expand Up @@ -174,7 +174,7 @@ private static DataArrays getResultArrays(
double[] tmpValuesArrA = new double[countA * numValues];

//search for non matches and build temp arrays
final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
final int lgHTBLen = exactLog2OfLong(hashTableB.length);
int nonMatches = 0;
for (int i = 0; i < countA; i++) {
final long hash = hashArrA[i];
Expand Down

0 comments on commit 4db2da1

Please sign in to comment.