Skip to content

Commit 10f1048

Browse files
authored
Merge pull request #500 from apache/common_util_cleanup
Consolidate / remove some duplicate code in datasketches/common/Util.
2 parents ff07407 + 2043519 commit 10f1048

34 files changed

+135
-185
lines changed

src/main/java/org/apache/datasketches/common/Util.java

+28-50
Original file line numberDiff line numberDiff line change
@@ -290,88 +290,66 @@ public static boolean isMultipleOf8AndGT0(final long v) {
290290
//Powers of 2 or powers of base related
291291

292292
/**
293-
* Returns true if given int argument is exactly a positive power of 2 and greater than zero.
293+
* Returns true if given long argument is exactly a positive power of 2.
294294
*
295-
* @param powerOf2 The input argument.
296-
* @return true if argument is exactly a positive power of 2 and greater than zero.
297-
*/
298-
public static boolean isIntPowerOf2(final int powerOf2) {
299-
return powerOf2 > 0 && (powerOf2 & powerOf2 - 1) == 0; //or (v > 0) && ((v & -v) == v)
300-
}
301-
302-
/**
303-
* Returns true if given long argument is exactly a positive power of 2 and greater than zero.
304-
*
305-
* @param powerOf2 The input argument.
306-
* @return true if argument is exactly a positive power of 2 and greater than zero.
307-
*/
308-
public static boolean isLongPowerOf2(final long powerOf2) {
309-
return powerOf2 > 0 && (powerOf2 & powerOf2 - 1L) == 0; //or (v > 0) && ((v & -v) == v)
310-
}
311-
312-
/**
313-
* Checks the given int argument to make sure it is a positive power of 2 and greater than zero.
314-
* If not it throws an exception with the user supplied local argument name.
315-
* @param powerOf2 The input int argument must be a power of 2 and greater than zero.
316-
* @param argName Used in the thrown exception.
317-
* @throws SketchesArgumentException if not a power of 2 nor greater than zero.
295+
* @param n The input argument.
296+
* @return true if argument is exactly a positive power of 2.
318297
*/
319-
public static void checkIfIntPowerOf2(final int powerOf2, final String argName) {
320-
if (isIntPowerOf2(powerOf2)) { return; }
321-
throw new SketchesArgumentException("The value of the int argument \"" + argName + "\""
322-
+ " must be a positive integer-power of 2" + " and greater than 0: " + powerOf2);
298+
public static boolean isPowerOf2(final long n) {
299+
return (n > 0) && ((n & (n - 1L)) == 0); //or (n > 0) && ((n & -n) == n)
323300
}
324301

325302
/**
326-
* Checks the given long argument to make sure it is a positive power of 2 and greater than zero.
327-
* If not, it throws an exception with the user supplied local argument name.
328-
* @param powerOf2 The input long argument must be a power of 2 and greater than zero.
329-
* @param argName Used in the thrown exception.
330-
* @throws SketchesArgumentException if not a power of 2 nor greater than zero.
303+
* Checks the given long argument if it is a positive integer power of 2.
304+
* If not, it throws an exception with the user supplied local argument name, if not null.
305+
* @param n The input long argument must be a positive integer power of 2.
306+
* @param argName Used in the thrown exception. It may be null.
307+
* @throws SketchesArgumentException if not a positive integer power of 2.
331308
*/
332-
public static void checkIfLongPowerOf2(final long powerOf2, final String argName) {
333-
if (isLongPowerOf2(powerOf2)) { return; }
334-
throw new SketchesArgumentException("The value of the int argument \"" + argName + "\""
335-
+ " must be a positive integer-power of 2" + " and greater than 0: " + powerOf2);
309+
public static void checkIfPowerOf2(final long n, String argName) {
310+
if (isPowerOf2(n)) { return; }
311+
argName = (argName == null) ? "" : argName;
312+
throw new SketchesArgumentException("The value of the argument \"" + argName + "\""
313+
+ " must be a positive integer power of 2: " + n);
336314
}
337315

338316
/**
339317
* Computes the int ceiling power of 2 within the range [1, 2^30]. This is the smallest positive power
340-
* of 2 that is equal to or greater than the given n and a mathematical integer.
318+
* of 2 that is equal to or greater than the given n and a positive integer.
341319
*
342320
* <p>For:
343321
* <ul>
344322
* <li>n &le; 1: returns 1</li>
345323
* <li>2^30 &le; n &le; 2^31 -1 : returns 2^30</li>
346324
* <li>n == an exact power of 2 : returns n</li>
347-
* <li>otherwise returns the smallest power of 2 &ge; n and equal to a mathematical integer</li>
325+
* <li>otherwise returns the smallest power of 2 &ge; n and equal to a positive integer</li>
348326
* </ul>
349327
*
350328
* @param n The input int argument.
351329
* @return the ceiling power of 2.
352330
*/
353-
public static int ceilingIntPowerOf2(final int n) {
331+
public static int ceilingPowerOf2(final int n) {
354332
if (n <= 1) { return 1; }
355333
final int topIntPwrOf2 = 1 << 30;
356334
return n >= topIntPwrOf2 ? topIntPwrOf2 : Integer.highestOneBit(n - 1 << 1);
357335
}
358336

359337
/**
360338
* Computes the long ceiling power of 2 within the range [1, 2^62]. This is the smallest positive power
361-
* of 2 that is equal to or greater than the given n and a mathematical long.
339+
* of 2 that is equal to or greater than the given n and a positive long.
362340
*
363341
* <p>For:
364342
* <ul>
365343
* <li>n &le; 1: returns 1</li>
366344
* <li>2^62 &le; n &le; 2^63 -1 : returns 2^62</li>
367345
* <li>n == an exact power of 2 : returns n</li>
368-
* <li>otherwise returns the smallest power of 2 &ge; n and equal to a mathematical integer</li>
346+
* <li>otherwise returns the smallest power of 2 &ge; n and equal to a positive long</li>
369347
* </ul>
370348
*
371349
* @param n The input long argument.
372350
* @return the ceiling power of 2.
373351
*/
374-
public static long ceilingLongPowerOf2(final long n) {
352+
public static long ceilingPowerOf2(final long n) {
375353
if (n <= 1L) { return 1L; }
376354
final long topIntPwrOf2 = 1L << 62;
377355
return n >= topIntPwrOf2 ? topIntPwrOf2 : Long.highestOneBit(n - 1L << 1);
@@ -380,7 +358,7 @@ public static long ceilingLongPowerOf2(final long n) {
380358
/**
381359
* Computes the floor power of 2 given <i>n</i> is in the range [1, 2^31-1].
382360
* This is the largest positive power of 2 that equal to or less than the given n and equal
383-
* to a mathematical integer.
361+
* to a positive integer.
384362
*
385363
* <p>For:
386364
* <ul>
@@ -402,7 +380,7 @@ public static int floorPowerOf2(final int n) {
402380
/**
403381
* Computes the floor power of 2 given <i>n</i> is in the range [1, 2^63-1].
404382
* This is the largest positive power of 2 that is equal to or less than the given <i>n</i> and
405-
* equal to a mathematical integer.
383+
* equal to a positive integer.
406384
*
407385
* <p>For:
408386
* <ul>
@@ -534,7 +512,7 @@ public static double powerSeriesNextDouble(final int ppb, final double curPoint,
534512
* Returns the ceiling of a given <i>n</i> given a <i>base</i>, where the ceiling is an integral power of the base.
535513
* This is the smallest positive power of <i>base</i> that is equal to or greater than the given <i>n</i>
536514
* and equal to a mathematical integer.
537-
* The result of this function is consistent with {@link #ceilingIntPowerOf2(int)} for values
515+
* The result of this function is consistent with {@link #ceilingPowerOf2(int)} for values
538516
* less than one. I.e., if <i>n &lt; 1,</i> the result is 1.
539517
*
540518
* <p>The formula is: <i>base<sup>ceiling(log<sub>base</sub>(x))</sup></i></p>
@@ -622,7 +600,7 @@ public static int numberOfLeadingOnes(final long v) {
622600
* @throws SketchesArgumentException if not a power of 2 nor greater than zero.
623601
*/
624602
public static int exactLog2OfInt(final int powerOf2, final String argName) {
625-
checkIfIntPowerOf2(powerOf2, argName);
603+
checkIfPowerOf2(powerOf2, argName);
626604
return Integer.numberOfTrailingZeros(powerOf2);
627605
}
628606

@@ -635,7 +613,7 @@ public static int exactLog2OfInt(final int powerOf2, final String argName) {
635613
* @throws SketchesArgumentException if not a power of 2 nor greater than zero.
636614
*/
637615
public static int exactLog2OfLong(final long powerOf2, final String argName) {
638-
checkIfLongPowerOf2(powerOf2, argName);
616+
checkIfPowerOf2(powerOf2, argName);
639617
return Long.numberOfTrailingZeros(powerOf2);
640618
}
641619

@@ -646,7 +624,7 @@ public static int exactLog2OfLong(final long powerOf2, final String argName) {
646624
* @return the log2 of the given int value if it is an exact power of 2 and greater than zero.
647625
*/
648626
public static int exactLog2OfInt(final int powerOf2) {
649-
if (!isIntPowerOf2(powerOf2)) {
627+
if (!isPowerOf2(powerOf2)) {
650628
throw new SketchesArgumentException("Argument 'powerOf2' must be a positive power of 2.");
651629
}
652630
return Long.numberOfTrailingZeros(powerOf2);
@@ -659,7 +637,7 @@ public static int exactLog2OfInt(final int powerOf2) {
659637
* @return the log2 of the given long value if it is an exact power of 2 and greater than zero.
660638
*/
661639
public static int exactLog2OfLong(final long powerOf2) {
662-
if (!isLongPowerOf2(powerOf2)) {
640+
if (!isPowerOf2(powerOf2)) {
663641
throw new SketchesArgumentException("Argument 'powerOf2' must be a positive power of 2.");
664642
}
665643
return Long.numberOfTrailingZeros(powerOf2);

src/main/java/org/apache/datasketches/cpc/CompressionCharacterization.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.apache.datasketches.cpc;
2121

2222
import static org.apache.datasketches.common.Util.INVERSE_GOLDEN_U64;
23-
import static org.apache.datasketches.common.Util.ceilingIntPowerOf2;
23+
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
2424
import static org.apache.datasketches.common.Util.log2;
2525
import static org.apache.datasketches.common.Util.powerSeriesNextDouble;
2626
import static org.apache.datasketches.cpc.CompressedState.importFromMemory;
@@ -109,7 +109,7 @@ private void doRangeOfNAtLgK(final int lgK) {
109109

110110
while (n <= maxN) {
111111
final double lgT = (slope * log2(n)) + lgMaxT;
112-
final int totTrials = Math.max(ceilingIntPowerOf2((int) Math.pow(2.0, lgT)), (1 << lgMinT));
112+
final int totTrials = Math.max(ceilingPowerOf2((int) Math.pow(2.0, lgT)), (1 << lgMinT));
113113
doTrialsAtLgKAtN(lgK, n, totTrials);
114114
n = Math.round(powerSeriesNextDouble(uPPO, n, true, 2.0));
115115
}

src/main/java/org/apache/datasketches/fdt/PostProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.apache.datasketches.fdt;
2121

22-
import static org.apache.datasketches.common.Util.ceilingIntPowerOf2;
22+
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
2323
import static org.apache.datasketches.thetacommon.HashOperations.hashSearchOrInsert;
2424
import static org.apache.datasketches.tuple.Util.stringHash;
2525

@@ -64,7 +64,7 @@ public PostProcessor(final FdtSketch sketch, final Group group, final char sep)
6464
this.sketch = sketch.copy();
6565
this.sep = sep;
6666
final int numEntries = sketch.getRetainedEntries();
67-
mapArrSize = ceilingIntPowerOf2((int)(numEntries / 0.75));
67+
mapArrSize = ceilingPowerOf2((int)(numEntries / 0.75));
6868
hashArr = new long[mapArrSize];
6969
priKeyArr = new String[mapArrSize];
7070
counterArr = new int[mapArrSize];

src/main/java/org/apache/datasketches/frequencies/ItemsSketch.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.apache.datasketches.common.Util.LS;
2323
import static org.apache.datasketches.common.Util.checkBounds;
2424
import static org.apache.datasketches.common.Util.exactLog2OfInt;
25-
import static org.apache.datasketches.common.Util.isIntPowerOf2;
25+
import static org.apache.datasketches.common.Util.isPowerOf2;
2626
import static org.apache.datasketches.frequencies.PreambleUtil.EMPTY_FLAG_MASK;
2727
import static org.apache.datasketches.frequencies.PreambleUtil.SER_VER;
2828
import static org.apache.datasketches.frequencies.PreambleUtil.extractActiveItems;
@@ -321,7 +321,7 @@ public int getCurrentMapCapacity() {
321321
* @return epsilon used to compute <i>a priori</i> error.
322322
*/
323323
public static double getEpsilon(final int maxMapSize) {
324-
if (!isIntPowerOf2(maxMapSize)) {
324+
if (!isPowerOf2(maxMapSize)) {
325325
throw new SketchesArgumentException("maxMapSize is not a power of 2.");
326326
}
327327
return 3.5 / maxMapSize;

src/main/java/org/apache/datasketches/frequencies/LongsSketch.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.apache.datasketches.common.Util.LS;
2323
import static org.apache.datasketches.common.Util.checkBounds;
2424
import static org.apache.datasketches.common.Util.exactLog2OfInt;
25-
import static org.apache.datasketches.common.Util.isIntPowerOf2;
25+
import static org.apache.datasketches.common.Util.isPowerOf2;
2626
import static org.apache.datasketches.frequencies.PreambleUtil.EMPTY_FLAG_MASK;
2727
import static org.apache.datasketches.frequencies.PreambleUtil.SER_VER;
2828
import static org.apache.datasketches.frequencies.PreambleUtil.extractActiveItems;
@@ -363,7 +363,7 @@ public int getCurrentMapCapacity() {
363363
* @return epsilon used to compute <i>a priori</i> error.
364364
*/
365365
public static double getEpsilon(final int maxMapSize) {
366-
if (!isIntPowerOf2(maxMapSize)) {
366+
if (!isPowerOf2(maxMapSize)) {
367367
throw new SketchesArgumentException("maxMapSize is not a power of 2.");
368368
}
369369
return 3.5 / maxMapSize;

src/main/java/org/apache/datasketches/hash/MurmurHash3Adaptor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.apache.datasketches.hash;
2121

2222
import static java.nio.charset.StandardCharsets.UTF_8;
23-
import static org.apache.datasketches.common.Util.ceilingIntPowerOf2;
23+
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
2424
import static org.apache.datasketches.hash.MurmurHash3.hash;
2525

2626
import java.nio.ByteBuffer;
@@ -368,7 +368,7 @@ private static int asInteger(final long[] data, final int n) {
368368
throw new SketchesStateException(
369369
"Internal Error: Failed to find integer &lt; n within 10000 iterations.");
370370
}
371-
final long mask = ceilingIntPowerOf2(n) - 1;
371+
final long mask = ceilingPowerOf2(n) - 1;
372372
while (++cnt < 10000) {
373373
final long[] h = MurmurHash3.hash(data, seed);
374374
t = (int) (h[0] & mask);

src/main/java/org/apache/datasketches/hll/PreambleUtil.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.apache.datasketches.hll;
2121

22-
import static org.apache.datasketches.common.Util.ceilingIntPowerOf2;
22+
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
2323
import static org.apache.datasketches.common.Util.exactLog2OfLong;
2424
import static org.apache.datasketches.common.Util.zeroPad;
2525
import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS;
@@ -460,7 +460,7 @@ static int computeLgArr(final Memory mem, final int count, final int lgConfigK)
460460
//value is missing, recompute
461461
final CurMode curMode = extractCurMode(mem);
462462
if (curMode == CurMode.LIST) { return HllUtil.LG_INIT_LIST_SIZE; }
463-
int ceilPwr2 = ceilingIntPowerOf2(count);
463+
int ceilPwr2 = ceilingPowerOf2(count);
464464
if ((RESIZE_DENOM * count) > (RESIZE_NUMER * ceilPwr2)) { ceilPwr2 <<= 1; }
465465
if (curMode == CurMode.SET) {
466466
return Math.max(LG_INIT_SET_SIZE, exactLog2OfLong(ceilPwr2));

src/main/java/org/apache/datasketches/hllmap/CouponHashMap.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.apache.datasketches.hllmap;
2121

22-
import static org.apache.datasketches.common.Util.checkIfIntPowerOf2;
22+
import static org.apache.datasketches.common.Util.checkIfPowerOf2;
2323
import static org.apache.datasketches.common.Util.invPow2;
2424

2525
import java.util.Arrays;
@@ -266,7 +266,7 @@ int getCapacityCouponsPerEntry() {
266266
}
267267

268268
private static final void checkMaxCouponsPerKey(final int maxCouponsPerKey) {
269-
checkIfIntPowerOf2(maxCouponsPerKey, "maxCouponsPerKey");
269+
checkIfPowerOf2(maxCouponsPerKey, "maxCouponsPerKey");
270270
final int cpk = maxCouponsPerKey;
271271
if ((cpk < 16) || (cpk > 256)) {
272272
throw new SketchesArgumentException(

src/main/java/org/apache/datasketches/quantiles/ClassicUtil.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import static java.lang.Math.min;
2828
import static java.lang.Math.pow;
2929
import static java.lang.Math.round;
30-
import static org.apache.datasketches.common.Util.ceilingIntPowerOf2;
31-
import static org.apache.datasketches.common.Util.isIntPowerOf2;
30+
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
31+
import static org.apache.datasketches.common.Util.isPowerOf2;
3232
import static org.apache.datasketches.quantiles.PreambleUtil.COMPACT_FLAG_MASK;
3333
import static org.apache.datasketches.quantiles.PreambleUtil.EMPTY_FLAG_MASK;
3434
import static org.apache.datasketches.quantiles.PreambleUtil.ORDERED_FLAG_MASK;
@@ -104,7 +104,7 @@ public static int getKFromEpsilon(final double epsilon, final boolean pmf) {
104104
* @param k must be greater than 1 and less than 65536 and a power of 2.
105105
*/
106106
static void checkK(final int k) {
107-
if ((k < MIN_K) || (k > MAX_K) || !isIntPowerOf2(k)) {
107+
if ((k < MIN_K) || (k > MAX_K) || !isPowerOf2(k)) {
108108
throw new SketchesArgumentException(
109109
"K must be >= " + MIN_K + " and <= " + MAX_K + " and a power of 2: " + k);
110110
}
@@ -210,7 +210,7 @@ static int computeCombinedBufferItemCapacity(final int k, final long n) {
210210
final int totLevels = computeNumLevelsNeeded(k, n);
211211
if (totLevels == 0) {
212212
final int bbItems = computeBaseBufferItems(k, n);
213-
return Math.max(2 * MIN_K, ceilingIntPowerOf2(bbItems));
213+
return Math.max(2 * MIN_K, ceilingPowerOf2(bbItems));
214214
}
215215
return (2 + totLevels) * k;
216216
}

src/main/java/org/apache/datasketches/quantiles/DoublesMergeImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.apache.datasketches.quantiles;
2121

22-
import static org.apache.datasketches.common.Util.checkIfIntPowerOf2;
22+
import static org.apache.datasketches.common.Util.checkIfPowerOf2;
2323
import static org.apache.datasketches.quantiles.PreambleUtil.EMPTY_FLAG_MASK;
2424
import static org.apache.datasketches.quantiles.PreambleUtil.FLAGS_BYTE;
2525

@@ -151,7 +151,7 @@ static void downSamplingMergeInto(final DoublesSketch src, final UpdateDoublesSk
151151
}
152152

153153
final int downFactor = sourceK / targetK;
154-
checkIfIntPowerOf2(downFactor, "source.getK()/target.getK() ratio");
154+
checkIfPowerOf2(downFactor, "source.getK()/target.getK() ratio");
155155
final int lgDownFactor = Integer.numberOfTrailingZeros(downFactor);
156156

157157
if (src.isEmpty()) { return; }

src/main/java/org/apache/datasketches/quantiles/DoublesSketch.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import static java.lang.Math.max;
2323
import static java.lang.Math.min;
24-
import static org.apache.datasketches.common.Util.ceilingIntPowerOf2;
24+
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
2525
import static org.apache.datasketches.quantiles.ClassicUtil.MAX_PRELONGS;
2626
import static org.apache.datasketches.quantiles.ClassicUtil.MIN_K;
2727
import static org.apache.datasketches.quantiles.ClassicUtil.checkIsCompactMemory;
@@ -456,7 +456,7 @@ public static int getUpdatableStorageBytes(final int k, final long n) {
456456
final int metaPre = MAX_PRELONGS + 2; //plus min, max
457457
final int totLevels = computeNumLevelsNeeded(k, n);
458458
if (n <= k) {
459-
final int ceil = Math.max(ceilingIntPowerOf2((int)n), MIN_K * 2);
459+
final int ceil = Math.max(ceilingPowerOf2((int)n), MIN_K * 2);
460460
return metaPre + ceil << 3;
461461
}
462462
return metaPre + (2 + totLevels) * k << 3;

src/main/java/org/apache/datasketches/quantiles/ItemsMergeImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.apache.datasketches.quantiles;
2121

2222
import static java.lang.System.arraycopy;
23-
import static org.apache.datasketches.common.Util.checkIfIntPowerOf2;
23+
import static org.apache.datasketches.common.Util.checkIfPowerOf2;
2424

2525
import java.util.Arrays;
2626
import java.util.Comparator;
@@ -144,7 +144,7 @@ static <T> void downSamplingMergeInto(final ItemsSketch<T> src, final ItemsSketc
144144
}
145145

146146
final int downFactor = sourceK / targetK;
147-
checkIfIntPowerOf2(downFactor, "source.getK()/target.getK() ratio");
147+
checkIfPowerOf2(downFactor, "source.getK()/target.getK() ratio");
148148
final int lgDownFactor = Integer.numberOfTrailingZeros(downFactor);
149149

150150
final Object[] sourceLevels = src.getCombinedBuffer(); // aliasing is a bit dangerous

0 commit comments

Comments
 (0)