NumericUtils

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, .floatToSortableInt. 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: java.util.Date.getTime).

Functions

Link copied to clipboard
fun add(bytesPerDim: Int, dim: Int, a: ByteArray, b: ByteArray, result: ByteArray)

Result = a + b, where a and b are unsigned. If there is an overflow, IllegalArgumentException is thrown.

Link copied to clipboard
fun bigIntToSortableBytes(bigInt: BigInteger, bigIntSize: Int, result: ByteArray, offset: Int)

Encodes a BigInteger value such that unsigned byte order comparison is consistent with BigInteger.compareTo. This also sign-extends the value to bigIntSize bytes if necessary: useful to create a fixed-width size.

Link copied to clipboard

Converts a double value to a sortable signed long. 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 (including ) is defined by Double.compareTo; NaN is greater than positive infinity.

Link copied to clipboard

Converts a float value to a sortable signed int. 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 be used as an int. The sort order (including ) is defined by Float.compareTo; NaN is greater than positive infinity.

Link copied to clipboard
fun intToSortableBytes(value: Int, result: ByteArray, offset: Int)

Encodes an integer value such that unsigned byte order comparison is consistent with Integer.compare

Link copied to clipboard
fun longToSortableBytes(value: Long, result: ByteArray, offset: Int)

Encodes an long value such that unsigned byte order comparison is consistent with Long.compare

Link copied to clipboard
fun sortableBytesToBigInt(encoded: ByteArray, offset: Int, length: Int): BigInteger

Decodes a BigInteger value previously written with .bigIntToSortableBytes

Link copied to clipboard
fun sortableBytesToInt(encoded: ByteArray, offset: Int): Int

Decodes an integer value previously written with .intToSortableBytes

Link copied to clipboard
fun sortableBytesToLong(encoded: ByteArray, offset: Int): Long

Decodes a long value previously written with .longToSortableBytes

Link copied to clipboard

Converts IEEE 754 representation of a double to sortable order (or back to the original)

Link copied to clipboard

Converts IEEE 754 representation of a float to sortable order (or back to the original)

Link copied to clipboard

Converts a sortable int back to a float.

Link copied to clipboard

Converts a sortable long back to a double.

Link copied to clipboard
fun subtract(bytesPerDim: Int, dim: Int, a: ByteArray, b: ByteArray, result: ByteArray)

Result = a - b, where a >= b, else IllegalArgumentException is thrown.