Package org.apache.lucene.util
Class NumericUtils
- java.lang.Object
-
- org.apache.lucene.util.NumericUtils
-
public final class NumericUtils extends Object
Helper APIs to encode numeric values as sortable bytes and vice-versa.To also index floating point numbers, this class supplies two methods to convert them to integer values by changing their bit layout:
doubleToSortableLong(double),floatToSortableInt(float). You will have no precision loss by converting floating point numbers to integers and back (only that the integer form is not usable). Other data types like dates can easily converted to longs or ints (e.g. date to long:Date.getTime()).- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidadd(int bytesPerDim, int dim, byte[] a, byte[] b, byte[] result)Result = a + b, where a and b are unsigned.static voidbigIntToSortableBytes(BigInteger bigInt, int bigIntSize, byte[] result, int offset)Encodes a BigIntegervaluesuch that unsigned byte order comparison is consistent withBigInteger.compareTo(BigInteger).static longdoubleToSortableLong(double value)Converts adoublevalue to a sortable signedlong.static intfloatToSortableInt(float value)Converts afloatvalue to a sortable signedint.static voidintToSortableBytes(int value, byte[] result, int offset)Encodes an integervaluesuch that unsigned byte order comparison is consistent withInteger.compare(int, int)static voidlongToSortableBytes(long value, byte[] result, int offset)Encodes an longvaluesuch that unsigned byte order comparison is consistent withLong.compare(long, long)static booleannextDown(byte[] bytes)Modify in-place the given bytes to the previous value of the same length that compares less than the current value.static booleannextUp(byte[] bytes)Modify in-place the given bytes to the next value of the same length that compares greater than the current value.static BigIntegersortableBytesToBigInt(byte[] encoded, int offset, int length)Decodes a BigInteger value previously written withbigIntToSortableBytes(java.math.BigInteger, int, byte[], int)static intsortableBytesToInt(byte[] encoded, int offset)Decodes an integer value previously written withintToSortableBytes(int, byte[], int)static longsortableBytesToLong(byte[] encoded, int offset)Decodes a long value previously written withlongToSortableBytes(long, byte[], int)static longsortableDoubleBits(long bits)Converts IEEE 754 representation of a double to sortable order (or back to the original)static intsortableFloatBits(int bits)Converts IEEE 754 representation of a float to sortable order (or back to the original)static floatsortableIntToFloat(int encoded)Converts a sortableintback to afloat.static doublesortableLongToDouble(long encoded)Converts a sortablelongback to adouble.static voidsubtract(int bytesPerDim, int dim, byte[] a, byte[] b, byte[] result)Result = a - b, where a >= b, elseIllegalArgumentExceptionis thrown.
-
-
-
Method Detail
-
doubleToSortableLong
public static long doubleToSortableLong(double value)
Converts adoublevalue to a sortable signedlong. The value is converted by getting their IEEE 754 floating-point "double format" bit layout and then some bits are swapped, to be able to compare the result as long. By this the precision is not reduced, but the value can easily used as a long. The sort order (includingDouble.NaN) is defined byDouble.compareTo(java.lang.Double);NaNis greater than positive infinity.- See Also:
sortableLongToDouble(long)
-
sortableLongToDouble
public static double sortableLongToDouble(long encoded)
Converts a sortablelongback to adouble.- See Also:
doubleToSortableLong(double)
-
floatToSortableInt
public static int floatToSortableInt(float value)
Converts afloatvalue to a sortable signedint. The value is converted by getting their IEEE 754 floating-point "float format" bit layout and then some bits are swapped, to be able to compare the result as int. By this the precision is not reduced, but the value can easily used as an int. The sort order (includingFloat.NaN) is defined byFloat.compareTo(java.lang.Float);NaNis greater than positive infinity.- See Also:
sortableIntToFloat(int)
-
sortableIntToFloat
public static float sortableIntToFloat(int encoded)
Converts a sortableintback to afloat.- See Also:
floatToSortableInt(float)
-
sortableDoubleBits
public static long sortableDoubleBits(long bits)
Converts IEEE 754 representation of a double to sortable order (or back to the original)
-
sortableFloatBits
public static int sortableFloatBits(int bits)
Converts IEEE 754 representation of a float to sortable order (or back to the original)
-
subtract
public static void subtract(int bytesPerDim, int dim, byte[] a, byte[] b, byte[] result)Result = a - b, where a >= b, elseIllegalArgumentExceptionis thrown.
-
add
public static void add(int bytesPerDim, int dim, byte[] a, byte[] b, byte[] result)Result = a + b, where a and b are unsigned. If there is an overflow,IllegalArgumentExceptionis thrown.
-
nextUp
public static boolean nextUp(byte[] bytes)
Modify in-place the given bytes to the next value of the same length that compares greater than the current value. This returns false if, and only if, the value is currently equal to the maximum possible value.
-
nextDown
public static boolean nextDown(byte[] bytes)
Modify in-place the given bytes to the previous value of the same length that compares less than the current value. This returns false if, and only if, the value is currently equal to the minimum possible value.
-
intToSortableBytes
public static void intToSortableBytes(int value, byte[] result, int offset)Encodes an integervaluesuch that unsigned byte order comparison is consistent withInteger.compare(int, int)- See Also:
sortableBytesToInt(byte[], int)
-
sortableBytesToInt
public static int sortableBytesToInt(byte[] encoded, int offset)Decodes an integer value previously written withintToSortableBytes(int, byte[], int)- See Also:
intToSortableBytes(int, byte[], int)
-
longToSortableBytes
public static void longToSortableBytes(long value, byte[] result, int offset)Encodes an longvaluesuch that unsigned byte order comparison is consistent withLong.compare(long, long)- See Also:
sortableBytesToLong(byte[], int)
-
sortableBytesToLong
public static long sortableBytesToLong(byte[] encoded, int offset)Decodes a long value previously written withlongToSortableBytes(long, byte[], int)- See Also:
longToSortableBytes(long, byte[], int)
-
bigIntToSortableBytes
public static void bigIntToSortableBytes(BigInteger bigInt, int bigIntSize, byte[] result, int offset)
Encodes a BigIntegervaluesuch that unsigned byte order comparison is consistent withBigInteger.compareTo(BigInteger). This also sign-extends the value tobigIntSizebytes if necessary: useful to create a fixed-width size.- See Also:
sortableBytesToBigInt(byte[], int, int)
-
sortableBytesToBigInt
public static BigInteger sortableBytesToBigInt(byte[] encoded, int offset, int length)
Decodes a BigInteger value previously written withbigIntToSortableBytes(java.math.BigInteger, int, byte[], int)
-
-