public final class Util extends Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_NOMINAL_ENTRIES
The default nominal entries is provided as a convenience for those cases where the
nominal sketch size in number of entries is not provided.
|
static long |
DEFAULT_UPDATE_SEED
The seed 9001 used in the sketch update methods is a prime number that
was chosen very early on in experimental testing.
|
static double |
iGolden
The inverse golden ratio as a fraction.
|
static long |
iGoldenU64
The inverse golden ratio as an unsigned long.
|
static double |
LOG2
The natural logarithm of 2.0.
|
static double |
LONG_MAX_VALUE_AS_DOUBLE
Long.MAX_VALUE as a double.
|
static String |
LS
The java line separator character as a String.
|
static int |
MAX_LG_NOM_LONGS
The largest Log2 nom entries allowed: 26.
|
static int |
MIN_LG_ARR_LONGS
The smallest Log2 cache size allowed: 5.
|
static int |
MIN_LG_NOM_LONGS
The smallest Log2 nom entries allowed: 4.
|
static double |
REBUILD_THRESHOLD
The hash table rebuild threshold = 15.0/16.0.
|
static double |
RESIZE_THRESHOLD
The resize threshold = 0.5; tuned for speed.
|
static char |
TAB
The tab character
|
Modifier and Type | Method and Description |
---|---|
static int |
bytesToInt(byte[] arr)
Returns an int extracted from a Little-Endian byte array.
|
static long |
bytesToLong(byte[] arr)
Returns a long extracted from a Little-Endian byte array.
|
static String |
bytesToString(byte[] arr,
boolean signed,
boolean littleEndian,
String sep)
Returns a string view of a byte array
|
static int |
ceilingPowerOf2(int n)
Computes the ceiling power of 2 within the range [1, 2^30].
|
static double |
ceilingPowerOfBdouble(double b,
double n)
Computes the ceiling power of B as a double.
|
static String |
characterPad(String s,
int fieldLength,
char padChar,
boolean postpend)
Prepend or postpend the given string with the given character to fill the given field length.
|
static void |
checkIfMultipleOf8AndGT0(long v,
String argName)
Checks if parameter v is a multiple of 8 and greater than zero.
|
static void |
checkIfPowerOf2(int v,
String argName)
Checks the given parameter to make sure it is positive, an integer-power of 2 and greater than
zero.
|
static int |
checkNomLongs(int nomLongs)
Checks that the given nomLongs is within bounds and returns the Log2 of the ceiling power of 2
of the given nomLongs.
|
static void |
checkProbability(double p,
String argName)
Checks the given parameter to make sure it is positive and between 0.0 inclusive and 1.0
inclusive.
|
static short |
checkSeedHashes(short seedHashA,
short seedHashB)
Check if the two seed hashes are equal.
|
static short |
computeSeedHash(long seed)
Computes and checks the 16-bit seed hash from the given long seed.
|
static double[] |
evenlyLogSpaced(double value1,
double value2,
int num)
Returns a double array of values between min and max inclusive where the log of the
returned values are evenly spaced.
|
static double[] |
evenlySpaced(double value1,
double value2,
int num)
Returns a double array of evenly spaced values between value1 and value2 inclusive.
|
static float[] |
evenlySpacedFloats(float value1,
float value2,
int num)
Returns a float array of evenly spaced values between value1 and value2 inclusive.
|
static int |
floorPowerOf2(int n)
Computes the floor power of 2 given n is in therange [1, 2^31-1].
|
static long |
floorPowerOf2(long n)
Computes the floor power of 2 given n is in the range [1, 2^63-1].
|
static double |
floorPowerOfBdouble(double b,
double n)
Computes the floor power of B as a double.
|
static byte[] |
getResourceBytes(String shortFileName)
Returns a byte array of the contents of the file defined by the given resource file's
shortFileName.
|
static File |
getResourceFile(String shortFileName)
Gets the file defined by the given resource file's shortFileName.
|
static String |
getResourcePath(String shortFileName)
Gets the absolute path of the given resource file's shortName.
|
static byte[] |
intToBytes(int v,
byte[] arr)
Returns a Little-Endian byte array extracted from the given int.
|
static double |
invPow2(int e)
Computes the inverse integer power of 2: 1/(2^e) = 2^(-e).
|
static boolean |
isLessThanUnsigned(long n1,
long n2)
Unsigned compare with longs.
|
static boolean |
isMultipleOf8AndGT0(long v)
Returns true if v is a multiple of 8 and greater than zero
|
static boolean |
isPowerOf2(int v)
Returns true if argument is exactly a positive power of 2 and greater than zero.
|
static double |
log2(double value)
The log base 2 of the value
|
static double |
logB(double logBase,
double x)
Returns the logarithm_logBase of x.
|
static byte[] |
longToBytes(long v,
byte[] arr)
Returns a Little-Endian byte array extracted from the given long.
|
static String |
longToHexBytes(long v)
Returns a string of spaced hex bytes in Big-Endian order.
|
static String |
milliSecToString(long mS)
Returns the given time in milliseconds formatted as Hours:Min:Sec.mSec
|
static String |
nanoSecToString(long nS)
Returns the given time in nanoseconds formatted as Sec.mSec uSec nSec
|
static int |
numberOfLeadingOnes(long v)
Returns the number of one bits preceding the highest-order ("leftmost") zero-bit in the
two's complement binary representation of the specified long value, or 64 if the value is equal
to minus one.
|
static int |
numberOfTrailingOnes(long v)
Returns the number of one bits following the lowest-order ("rightmost") zero-bit in the
two's complement binary representation of the specified long value, or 64 if the value is equal
to minus one.
|
static int |
pwr2LawNext(int ppo,
int curPoint)
Computes the next larger integer point in the power series
point = 2( i / ppo ) given the current point in the series.
|
static int |
pwr2LawPrev(int ppo,
int curPoint)
Computes the previous, smaller integer point in the power series
point = 2( i / ppo ) given the current point in the series.
|
static double |
pwrLawNextDouble(int ppo,
double curPoint,
boolean roundToInt,
double logBase)
Computes the next larger double in the power series
point = logBase( i / ppo ) given the current point in the series.
|
static int |
simpleLog2OfLong(long x)
Gives the log2 of a long that is known to be a power of 2.
|
static int |
startingSubMultiple(int lgTarget,
int lgRF,
int lgMin)
Gets the smallest allowed exponent of 2 that it is a sub-multiple of the target by zero,
one or more resize factors.
|
static int |
toLog2(int value,
String argName)
Checks the given value if it is a power of 2.
|
static void |
validateValues(float[] values)
Checks the sequential validity of the given array of float values.
|
static String |
zeroPad(String s,
int fieldLength)
Prepend the given string with zeros.
|
public static final int MIN_LG_ARR_LONGS
public static final int MIN_LG_NOM_LONGS
public static final int MAX_LG_NOM_LONGS
public static final double REBUILD_THRESHOLD
public static final double RESIZE_THRESHOLD
public static final int DEFAULT_NOMINAL_ENTRIES
public static final long DEFAULT_UPDATE_SEED
In order to perform set operations on two sketches it is critical that the same hash function and seed are identical for both sketches, otherwise the assumed 1:1 relationship between the original source key value and the hashed bit string would be violated. Once you have developed a history of stored sketches you are stuck with it.
WARNING: This seed is used internally by library sketches in different packages and thus must be declared public. However, this seed value must not be used by library users with the MurmurHash3 function. It should be viewed as existing for exclusive, private use by the library.
public static final String LS
public static final char TAB
public static final double LOG2
public static final long iGoldenU64
public static final double iGolden
public static final double LONG_MAX_VALUE_AS_DOUBLE
public static int bytesToInt(byte[] arr)
arr
- the given byte arraypublic static long bytesToLong(byte[] arr)
arr
- the given byte arraypublic static byte[] intToBytes(int v, byte[] arr)
v
- the given intarr
- a given array of 4 bytes that will be returned with the datapublic static byte[] longToBytes(long v, byte[] arr)
v
- the given longarr
- a given array of 8 bytes that will be returned with the datapublic static String longToHexBytes(long v)
v
- the given longpublic static String bytesToString(byte[] arr, boolean signed, boolean littleEndian, String sep)
arr
- the given byte arraysigned
- set true if you want the byte values signed.littleEndian
- set true if you want Little-Endian ordersep
- the separator string between bytespublic static String nanoSecToString(long nS)
nS
- the given nanosecondspublic static String milliSecToString(long mS)
mS
- the given nanosecondspublic static String zeroPad(String s, int fieldLength)
s
- the given stringfieldLength
- desired total field length including the given stringpublic static String characterPad(String s, int fieldLength, char padChar, boolean postpend)
s
- the given stringfieldLength
- the desired field lengthpadChar
- the desired pad characterpostpend
- if true append the pacCharacters to the end of the string.public static short checkSeedHashes(short seedHashA, short seedHashB)
seedHashA
- the seedHash AseedHashB
- the seedHash Bpublic static short computeSeedHash(long seed)
seed
- See Update Hash Seedpublic static void checkIfMultipleOf8AndGT0(long v, String argName)
v
- The parameter to checkargName
- This name will be part of the error message if the check fails.public static boolean isMultipleOf8AndGT0(long v)
v
- The parameter to checkpublic static int numberOfTrailingOnes(long v)
v
- the value whose number of trailing ones is to be computed.public static int numberOfLeadingOnes(long v)
v
- the value whose number of leading ones is to be computed.public static boolean isPowerOf2(int v)
v
- The input argument.public static void checkIfPowerOf2(int v, String argName)
v
- The input argument.argName
- Used in the thrown exception.public static int toLog2(int value, String argName)
value
- must be a power of 2 and greater than zero.argName
- the argument name used in the exception if thrown.public static int ceilingPowerOf2(int n)
For:
n
- The input argument.public static double[] evenlySpaced(double value1, double value2, int num)
value1
- will be in index 0 of the returned arrayvalue2
- will be in the highest index of the returned arraynum
- the total number of values including value1 and value2. Must be 2 or greater.public static float[] evenlySpacedFloats(float value1, float value2, int num)
value1
- will be in index 0 of the returned arrayvalue2
- will be in the highest index of the returned arraynum
- the total number of values including value1 and value2. Must be 2 or greater.public static double[] evenlyLogSpaced(double value1, double value2, int num)
value1
- will be in index 0 of the returned array, and must be greater than zero.value2
- will be in the highest index of the returned array, and must be greater than zero.num
- the total number of values including value1 and value2. Must be 2 or greaterpublic static int floorPowerOf2(int n)
For:
n
- The given int argument.public static long floorPowerOf2(long n)
For:
n
- The given long argument.public static double invPow2(int e)
e
- a positive value between 0 and 1023 inclusivepublic static int pwr2LawNext(int ppo, int curPoint)
int maxP = 1024;
int minP = 1;
int ppo = 2;
for (int p = minP; p <= maxP; p = pwr2LawNext(ppo, p)) {
System.out.print(p + " ");
}
//generates the following series:
//1 2 3 4 6 8 11 16 23 32 45 64 91 128 181 256 362 512 724 1024
ppo
- Points-Per-Octave, or the number of points per integer powers of 2 in the series.curPoint
- the current point of the series. Must be ≥ 1.public static int pwr2LawPrev(int ppo, int curPoint)
int maxP = 1024;
int minP = 1;
int ppo = 2;
for (int p = maxP; p >= minP; p = pwr2LawPrev(ppo, p)) {
System.out.print(p + " ");
}
//generates the following series:
//1024 724 512 362 256 181 128 91 64 45 32 23 16 11 8 6 4 3 2 1
ppo
- Points-Per-Octave, or the number of points per integer powers of 2 in the series.curPoint
- the current point of the series. Must be ≥ 1.public static double log2(double value)
value
- the given valuepublic static int simpleLog2OfLong(long x)
x
- number that is greater than zeropublic static int startingSubMultiple(int lgTarget, int lgRF, int lgMin)
lgTarget
- Log2 of the target sizelgRF
- Log_base2 of Resize Factor.
See Resize FactorlgMin
- Log2 of the minimum allowed starting sizepublic static double ceilingPowerOfBdouble(double b, double n)
ceilingPowerOf2(int)
for values
less than one. I.e., if n < 1, the result is 1.b
- The base in the expression ⌈bn⌉.n
- The input argument.public static double floorPowerOfBdouble(double b, double n)
floorPowerOf2(int)
for values
less than one. I.e., if n < 1, the result is 1.b
- The base in the expression ⌊bn⌋.n
- The input argument.public static double logB(double logBase, double x)
logBase
- the base of the logarithm usedx
- the given valuepublic static double pwrLawNextDouble(int ppo, double curPoint, boolean roundToInt, double logBase)
double maxP = 1024.0;
double minP = 1.0;
int ppo = 2;
double logBase = 2.0;
for (double p = minP; p <= maxP; p = pwr2LawNextDouble(ppo, p, true, logBase)) {
System.out.print(Math.round(p) + " ");
}
//generates the following series:
//1 2 3 4 6 8 11 16 23 32 45 64 91 128 181 256 362 512 724 1024
ppo
- Points-Per-Octave, or the number of points per integer powers of 2 in the series.curPoint
- the current point of the series. Must be ≥ 1.0.roundToInt
- if true the output will be rounded to the nearest integer.logBase
- the desired base of the logarithmspublic static int checkNomLongs(int nomLongs)
nomLongs
- the given number of nominal longs. This can be any value from 16 to
67108864, inclusive.public static void checkProbability(double p, String argName)
p
- See Sampling Probability, pargName
- Used in the thrown exception.public static boolean isLessThanUnsigned(long n1, long n2)
n1
- A long to be treated as if unsigned.n2
- A long to be treated as if unsigned.public static String getResourcePath(String shortFileName)
Note that the ClassLoader.getResource(shortName) returns a URL, which can have special characters, e.g., "%20" for spaces. This method obtains the URL, converts it to a URI, then does a uri.getPath(), which decodes any special characters in the URI path. This is required to make obtaining resources operating-system independent.
shortFileName
- the last name in the pathname's name sequence.public static File getResourceFile(String shortFileName)
shortFileName
- the last name in the pathname's name sequence.public static byte[] getResourceBytes(String shortFileName)
shortFileName
- the last name in the pathname's name sequence.public static void validateValues(float[] values)
values
- the given array of valuesCopyright © 2015–2021 The Apache Software Foundation. All rights reserved.