public class IndexNumberEncoder extends Object

Encodes numbers (longs and doubles) as bytes whose order matches the numeric order.

Implementation conforms to the UTF Style Encoding section of "Number Index Entry Encoding".

See Also
  • https://docs.google.com/document/d/1QX32BCTFWFS_4BneQHFRDnPb2ts04fYrm4Vgy0HLSBg/edit#

Constant Summary

int MAX_ENCODED_BYTES The encoder assumes the input is either a long or a double and assumes the maximum length based on the largest values for each type.

Public Method Summary

static int
encodeDouble(boolean descending, double value, byte[] buffer, int offset)
Writes bytes encoding the double to the buffer at the offset, in the order specified by descending, and returns the number of bytes written.
static int
encodeLong(boolean descending, long value, byte[] buffer, int offset)
Writes bytes encoding the long to the buffer at the offset, in the order specified by descending, and returns the number of bytes written.

Inherited Method Summary

Constants

public static final int MAX_ENCODED_BYTES

The encoder assumes the input is either a long or a double and assumes the maximum length based on the largest values for each type.

11 bytes are required to render the largest longs. Long.MAX_VALUE encodes as follows:

  • exponent == 62 requires the st110eee form.
  • st110eee requires 2 leading bytes (into which exponent and 4 significand bits are packed).
  • The remaining 58 significand bits are packed into bytes with continuation bits.
  • ceil(58 / 7) == 9 bytes.

10 bytes are required to render the largest doubles. Double.MAX_VALUE encodes as follows:

  • exponent == 1023 requires the st1110ee form.
  • st1110ee requires 2 bytes (into which only the exponent is packed).
  • The remaining 52 significand bits are packed into bytes with continuation bits.
  • ceil(52 / 7) == 8 bytes.

Note that while subnormal values require larger exponents to encode, their reduced precision makes them no larger to encode than Double.MAX_VALUE.

Constant Value: 11

Public Methods

public static int encodeDouble (boolean descending, double value, byte[] buffer, int offset)

Writes bytes encoding the double to the buffer at the offset, in the order specified by descending, and returns the number of bytes written.

Warning! When descending is true, NaN is encoded as (still) smaller than all other numbers. See TODO in this file.

Parameters
descending when true, produce an encoding that orders numbers in descending order
value the double to encode
buffer buffer for bytes to write the encoded bytes
offset index into buffer of first byte to write
Returns
  • number of bytes written to buffer

public static int encodeLong (boolean descending, long value, byte[] buffer, int offset)

Writes bytes encoding the long to the buffer at the offset, in the order specified by descending, and returns the number of bytes written.

Parameters
descending when true, produce an encoding that orders numbers in descending order
value the long to encode
buffer buffer for bytes to write the encoded bytes
offset index into buffer of first byte to write
Returns
  • number of bytes written to buffer