Class Util
public final class Util extends Object
- Author:
- Lee Rhodes
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_NOMINAL_ENTRIESThe default nominal entries is provided as a convenience for those cases where the nominal sketch size in number of entries is not provided.static longDEFAULT_UPDATE_SEEDThe seed 9001 used in the sketch update methods is a prime number that was chosen very early on in experimental testing.static doubleiGoldenThe inverse golden ratio as a fraction.static longiGoldenU64The inverse golden ratio as an unsigned long.static doubleLOG2The natural logarithm of 2.0.static doubleLONG_MAX_VALUE_AS_DOUBLELong.MAX_VALUE as a double.static StringLSThe java line separator character as a String.static intMAX_LG_NOM_LONGSThe largest Log2 nom entries allowed: 26.static intMIN_LG_ARR_LONGSThe smallest Log2 cache size allowed: 5.static intMIN_LG_NOM_LONGSThe smallest Log2 nom entries allowed: 4.static doubleREBUILD_THRESHOLDThe hash table rebuild threshold = 15.0/16.0.static doubleRESIZE_THRESHOLDThe resize threshold = 0.5; tuned for speed.static charTABThe tab character -
Method Summary
Modifier and Type Method Description static intbytesToInt(byte[] arr)Returns an int extracted from a Little-Endian byte array.static longbytesToLong(byte[] arr)Returns a long extracted from a Little-Endian byte array.static StringbytesToString(byte[] arr, boolean signed, boolean littleEndian, String sep)Returns a string view of a byte arraystatic intceilingPowerOf2(int n)Computes the ceiling power of 2 within the range [1, 2^30].static doubleceilingPowerOfBdouble(double b, double n)Computes the ceiling power of B as a double.static StringcharacterPad(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 voidcheckBounds(long reqOff, long reqLen, long allocSize)Check the requested offset and length against the allocated size.static voidcheckIfMultipleOf8AndGT0(long v, String argName)Checks if parameter v is a multiple of 8 and greater than zero.static voidcheckIfPowerOf2(int v, String argName)Checks the given parameter to make sure it is positive, an integer-power of 2 and greater than zero.static intcheckNomLongs(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 voidcheckProbability(double p, String argName)Checks the given parameter to make sure it is positive and between 0.0 inclusive and 1.0 inclusive.static shortcheckSeedHashes(short seedHashA, short seedHashB)Check if the two seed hashes are equal.static shortcomputeSeedHash(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 intfloorPowerOf2(int n)Computes the floor power of 2 given n is in therange [1, 2^31-1].static longfloorPowerOf2(long n)Computes the floor power of 2 given n is in the range [1, 2^63-1].static doublefloorPowerOfBdouble(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 FilegetResourceFile(String shortFileName)Gets the file defined by the given resource file's shortFileName.static StringgetResourcePath(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 doubleinvPow2(int e)Computes the inverse integer power of 2: 1/(2^e) = 2^(-e).static booleanisLessThanUnsigned(long n1, long n2)Unsigned compare with longs.static booleanisMultipleOf8AndGT0(long v)Returns true if v is a multiple of 8 and greater than zerostatic booleanisPowerOf2(int v)Returns true if argument is exactly a positive power of 2 and greater than zero.static doublelog2(double value)The log base 2 of the valuestatic doublelogB(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 StringlongToHexBytes(long v)Returns a string of spaced hex bytes in Big-Endian order.static StringmilliSecToString(long mS)Returns the given time in milliseconds formatted as Hours:Min:Sec.mSecstatic StringnanoSecToString(long nS)Returns the given time in nanoseconds formatted as Sec.mSec uSec nSecstatic intnumberOfLeadingOnes(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 intnumberOfTrailingOnes(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 intpwr2LawNext(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 intpwr2LawPrev(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 doublepwrLawNextDouble(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 intsimpleLog2OfLong(long x)Gives the log2 of a long that is known to be a power of 2.static intstartingSubMultiple(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 inttoLog2(int value, String argName)Checks the given value if it is a power of 2.static voidvalidateValues(float[] values)Checks the sequential validity of the given array of float values.static StringzeroPad(String s, int fieldLength)Prepend the given string with zeros.
-
Field Details
-
MIN_LG_ARR_LONGS
public static final int MIN_LG_ARR_LONGSThe smallest Log2 cache size allowed: 5.- See Also:
- Constant Field Values
-
MIN_LG_NOM_LONGS
public static final int MIN_LG_NOM_LONGSThe smallest Log2 nom entries allowed: 4.- See Also:
- Constant Field Values
-
MAX_LG_NOM_LONGS
public static final int MAX_LG_NOM_LONGSThe largest Log2 nom entries allowed: 26.- See Also:
- Constant Field Values
-
REBUILD_THRESHOLD
public static final double REBUILD_THRESHOLDThe hash table rebuild threshold = 15.0/16.0.- See Also:
- Constant Field Values
-
RESIZE_THRESHOLD
public static final double RESIZE_THRESHOLDThe resize threshold = 0.5; tuned for speed.- See Also:
- Constant Field Values
-
DEFAULT_NOMINAL_ENTRIES
public static final int DEFAULT_NOMINAL_ENTRIESThe default nominal entries is provided as a convenience for those cases where the nominal sketch size in number of entries is not provided. A sketch of 4096 entries has a Relative Standard Error (RSE) of +/- 1.56% at a confidence of 68%; or equivalently, a Relative Error of +/- 3.1% at a confidence of 95.4%. See Default Nominal Entries- See Also:
- Constant Field Values
-
DEFAULT_UPDATE_SEED
public static final long DEFAULT_UPDATE_SEEDThe seed 9001 used in the sketch update methods is a prime number that was chosen very early on in experimental testing. Choosing a seed is somewhat arbitrary, and the author cannot prove that this particular seed is somehow superior to other seeds. There was some early Internet discussion that a seed of 0 did not produce as clean avalanche diagrams as non-zero seeds, but this may have been more related to the MurmurHash2 release, which did have some issues. As far as the author can determine, MurmurHash3 does not have these problems.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.
- See Also:
- Constant Field Values
-
LS
The java line separator character as a String. -
TAB
public static final char TABThe tab character- See Also:
- Constant Field Values
-
LOG2
public static final double LOG2The natural logarithm of 2.0. -
iGoldenU64
public static final long iGoldenU64The inverse golden ratio as an unsigned long.- See Also:
- Constant Field Values
-
iGolden
public static final double iGoldenThe inverse golden ratio as a fraction. This has more precision than using the formula: (Math.sqrt(5.0) - 1.0) / 2.0.- See Also:
- Constant Field Values
-
LONG_MAX_VALUE_AS_DOUBLE
public static final double LONG_MAX_VALUE_AS_DOUBLELong.MAX_VALUE as a double.- See Also:
- Constant Field Values
-
-
Method Details
-
bytesToInt
public static int bytesToInt(byte[] arr)Returns an int extracted from a Little-Endian byte array.- Parameters:
arr- the given byte array- Returns:
- an int extracted from a Little-Endian byte array.
-
bytesToLong
public static long bytesToLong(byte[] arr)Returns a long extracted from a Little-Endian byte array.- Parameters:
arr- the given byte array- Returns:
- a long extracted from a Little-Endian byte array.
-
intToBytes
public static byte[] intToBytes(int v, byte[] arr)Returns a Little-Endian byte array extracted from the given int.- Parameters:
v- the given intarr- a given array of 4 bytes that will be returned with the data- Returns:
- a Little-Endian byte array extracted from the given int.
-
longToBytes
public static byte[] longToBytes(long v, byte[] arr)Returns a Little-Endian byte array extracted from the given long.- Parameters:
v- the given longarr- a given array of 8 bytes that will be returned with the data- Returns:
- a Little-Endian byte array extracted from the given long.
-
longToHexBytes
Returns a string of spaced hex bytes in Big-Endian order.- Parameters:
v- the given long- Returns:
- string of spaced hex bytes in Big-Endian order.
-
bytesToString
Returns a string view of a byte array- Parameters:
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 bytes- Returns:
- a string view of a byte array
-
nanoSecToString
Returns the given time in nanoseconds formatted as Sec.mSec uSec nSec- Parameters:
nS- the given nanoseconds- Returns:
- the given time in nanoseconds formatted as Sec.mSec uSec nSec
-
milliSecToString
Returns the given time in milliseconds formatted as Hours:Min:Sec.mSec- Parameters:
mS- the given nanoseconds- Returns:
- the given time in milliseconds formatted as Hours:Min:Sec.mSec
-
zeroPad
Prepend the given string with zeros. If the given string is equal or greater than the given field length, it will be returned without modification.- Parameters:
s- the given stringfieldLength- desired total field length including the given string- Returns:
- the given string prepended with zeros.
-
characterPad
Prepend or postpend the given string with the given character to fill the given field length. If the given string is equal or greater than the given field length, it will be returned without modification.- Parameters:
s- the given stringfieldLength- the desired field lengthpadChar- the desired pad characterpostpend- if true append the pacCharacters to the end of the string.- Returns:
- prepended or postpended given string with the given character to fill the given field length.
-
checkSeedHashes
public static short checkSeedHashes(short seedHashA, short seedHashB)Check if the two seed hashes are equal. If not, throw an SketchesArgumentException.- Parameters:
seedHashA- the seedHash AseedHashB- the seedHash B- Returns:
- seedHashA if they are equal
-
computeSeedHash
public static short computeSeedHash(long seed)Computes and checks the 16-bit seed hash from the given long seed. The seed hash may not be zero in order to maintain compatibility with older serialized versions that did not have this concept.- Parameters:
seed- See Update Hash Seed- Returns:
- the seed hash.
-
checkIfMultipleOf8AndGT0
Checks if parameter v is a multiple of 8 and greater than zero.- Parameters:
v- The parameter to checkargName- This name will be part of the error message if the check fails.
-
isMultipleOf8AndGT0
public static boolean isMultipleOf8AndGT0(long v)Returns true if v is a multiple of 8 and greater than zero- Parameters:
v- The parameter to check- Returns:
- true if v is a multiple of 8 and greater than zero
-
numberOfTrailingOnes
public 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.- Parameters:
v- the value whose number of trailing ones is to be computed.- 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.
-
numberOfLeadingOnes
public 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.- Parameters:
v- the value whose number of leading ones is to be computed.- Returns:
- the number of one bits preceding 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.
-
isPowerOf2
public static boolean isPowerOf2(int v)Returns true if argument is exactly a positive power of 2 and greater than zero.- Parameters:
v- The input argument.- Returns:
- true if argument is exactly a positive power of 2 and greater than zero.
-
checkIfPowerOf2
Checks the given parameter to make sure it is positive, an integer-power of 2 and greater than zero.- Parameters:
v- The input argument.argName- Used in the thrown exception.
-
toLog2
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.- Parameters:
value- must be a power of 2 and greater than zero.argName- the argument name used in the exception if thrown.- Returns:
- the log-base2 of the given value
-
ceilingPowerOf2
public static int ceilingPowerOf2(int n)Computes the ceiling power of 2 within the range [1, 2^30]. This is the smallest positive power of 2 that equal to or greater than the given n and equal to a mathematical integer.For:
- n ≤ 1: returns 1
- 2^30 ≤ n ≤ 2^31 -1 : returns 2^30
- n == a power of 2 : returns n
- otherwise returns the smallest power of 2 greater than n and equal to a mathematical integer
- Parameters:
n- The input argument.- Returns:
- the ceiling power of 2.
-
evenlySpaced
public static double[] evenlySpaced(double value1, double value2, int num)Returns a double array of evenly spaced values between value1 and value2 inclusive. If value2 > value1, the resulting sequence will be increasing. If value2 < value1, the resulting sequence will be decreasing.- Parameters:
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.- Returns:
- a double array of evenly spaced values between value1 and value2 inclusive.
-
evenlySpacedFloats
public static float[] evenlySpacedFloats(float value1, float value2, int num)Returns a float array of evenly spaced values between value1 and value2 inclusive. If value2 > value1, the resulting sequence will be increasing. If value2 < value1, the resulting sequence will be decreasing.- Parameters:
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.- Returns:
- a float array of evenly spaced values between value1 and value2 inclusive.
-
evenlyLogSpaced
public 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. If value2 > value1, the resulting sequence will be increasing. If value2 < value1, the resulting sequence will be decreasing.- Parameters:
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 greater- Returns:
- a double array of exponentially spaced values between value1 and value2 inclusive.
-
floorPowerOf2
public static int floorPowerOf2(int n)Computes the floor power of 2 given n 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.For:
- n ≤ 1: returns 1
- 2^30 ≤ n ≤ 2^31 -1 : returns 2^30
- n == a power of 2 : returns n
- otherwise returns the largest power of 2 less than n and equal to a mathematical integer.
- Parameters:
n- The given int argument.- Returns:
- the floor power of 2 as an int.
-
floorPowerOf2
public static long floorPowerOf2(long n)Computes the floor power of 2 given n is in the range [1, 2^63-1]. This is the largest positive power of 2 that is equal to or less than the given n and equal to a mathematical integer.For:
- n ≤ 1: returns 1
- 2^62 ≤ n ≤ 2^63 -1 : returns 2^62
- n == a power of 2 : returns n
- otherwise returns the largest power of 2 less than n and equal to a mathematical integer.
- Parameters:
n- The given long argument.- Returns:
- the floor power of 2 as a long
-
invPow2
public static double invPow2(int e)Computes the inverse integer power of 2: 1/(2^e) = 2^(-e).- Parameters:
e- a positive value between 0 and 1023 inclusive- Returns:
- the inverse integer power of 2: 1/(2^e) = 2^(-e)
-
pwr2LawNext
public 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. For illustration, this can be used in a loop as follows: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- Parameters:
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.- Returns:
- the next point in the power series.
-
pwr2LawPrev
public 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. For illustration, this can be used in a loop as follows: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- Parameters:
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.- Returns:
- the previous, smaller point in the power series. A returned value of zero terminates the series.
-
log2
public static double log2(double value)The log base 2 of the value- Parameters:
value- the given value- Returns:
- The log base 2 of the value
-
simpleLog2OfLong
public static int simpleLog2OfLong(long x)Gives the log2 of a long that is known to be a power of 2.- Parameters:
x- number that is greater than zero- Returns:
- the log2 of a long that is known to be a power of 2.
-
startingSubMultiple
public 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.- Parameters:
lgTarget- Log2 of the target sizelgRF- Log_base2 of Resize Factor. See Resize FactorlgMin- Log2 of the minimum allowed starting size- Returns:
- The Log2 of the starting size
-
ceilingPowerOfBdouble
public static double ceilingPowerOfBdouble(double b, double n)Computes the ceiling power of B as a double. This is the smallest positive power of B that equal to or greater than the given n and equal to a mathematical integer. The result of this function is consistent withceilingPowerOf2(int)for values less than one. I.e., if n < 1, the result is 1.- Parameters:
b- The base in the expression ⌈bn⌉.n- The input argument.- Returns:
- the ceiling power of B as a double and equal to a mathematical integer.
-
floorPowerOfBdouble
public static double floorPowerOfBdouble(double b, double n)Computes the floor power of B as a double. This is the largest positive power of B that equal to or less than the given n and equal to a mathematical integer. The result of this function is consistent withfloorPowerOf2(int)for values less than one. I.e., if n < 1, the result is 1.- Parameters:
b- The base in the expression ⌊bn⌋.n- The input argument.- Returns:
- the floor power of 2 and equal to a mathematical integer.
-
logB
public static double logB(double logBase, double x)Returns the logarithm_logBase of x. Example: logB(2.0, x) = log(x) / log(2.0).- Parameters:
logBase- the base of the logarithm usedx- the given value- Returns:
- the logarithm_logBase of x: Example: logB(2.0, x) = log(x) / log(2.0).
-
pwrLawNextDouble
public 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. For illustration, this can be used in a loop as follows: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- Parameters:
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 logarithms- Returns:
- the next point in the power series.
-
checkBounds
public static void checkBounds(long reqOff, long reqLen, long allocSize)Check the requested offset and length against the allocated size. The invariants equation is:0 <= reqOff <= reqLen <= reqOff + reqLen <= allocSize. If this equation is violated anSketchesArgumentExceptionwill be thrown.- Parameters:
reqOff- the requested offsetreqLen- the requested lengthallocSize- the allocated size.
-
checkNomLongs
public 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.- Parameters:
nomLongs- the given number of nominal longs. This can be any value from 16 to 67108864, inclusive.- Returns:
- The Log2 of the ceiling power of 2 of the given nomLongs.
-
checkProbability
Checks the given parameter to make sure it is positive and between 0.0 inclusive and 1.0 inclusive.- Parameters:
p- See Sampling Probability, pargName- Used in the thrown exception.
-
isLessThanUnsigned
public static boolean isLessThanUnsigned(long n1, long n2)Unsigned compare with longs.- Parameters:
n1- A long to be treated as if unsigned.n2- A long to be treated as if unsigned.- Returns:
- true if n1 > n2.
-
getResourcePath
Gets the absolute path of the given resource file's shortName.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.
- Parameters:
shortFileName- the last name in the pathname's name sequence.- Returns:
- the absolute path of the given resource file's shortName.
-
getResourceFile
Gets the file defined by the given resource file's shortFileName.- Parameters:
shortFileName- the last name in the pathname's name sequence.- Returns:
- the file defined by the given resource file's shortFileName.
-
getResourceBytes
Returns a byte array of the contents of the file defined by the given resource file's shortFileName.- Parameters:
shortFileName- the last name in the pathname's name sequence.- Returns:
- a byte array of the contents of the file defined by the given resource file's shortFileName.
-
validateValues
public static void validateValues(float[] values)Checks the sequential validity of the given array of float values. They must be unique, monotonically increasing and not NaN.- Parameters:
values- the given array of values
-