Class Conversions

java.lang.Object
org.apache.commons.rng.simple.internal.Conversions

final class Conversions extends Object
Performs seed conversions.

Note: Legacy converters from version 1.0 use instances of the SeedConverter interface. Instances are no longer required as no state is used during conversion and converters can use static methods.

Since:
1.5
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final long
    The fractional part of the golden ratio, phi, scaled to 64-bits and rounded to odd.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    No instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static int
    byteArray2Int(byte[] input)
    Creates an int value from a sequence of bytes.
    (package private) static int[]
    byteArray2IntArray(byte[] input, int length)
    Creates an int[] value from a sequence of bytes.
    (package private) static long
    byteArray2Long(byte[] input)
    Creates a long value from a sequence of bytes.
    (package private) static long[]
    byteArray2LongArray(byte[] input, int length)
    Creates a long[] value from a sequence of bytes.
    (package private) static int[]
    int2IntArray(int input, int length)
    Creates an int[] value from an int.
    (package private) static long
    int2Long(int input)
    Creates a long value from an int.
    (package private) static long[]
    int2LongArray(int input, int length)
    Creates a long[] value from an int.
    (package private) static int
    intArray2Int(int[] input)
    Creates an int value from a sequence of ints.
    (package private) static long
    intArray2Long(int[] input)
    Creates a long value from a sequence of ints.
    (package private) static long[]
    intArray2LongArray(int[] input, int length)
    Creates a long[] value from a sequence of ints.
    (package private) static int
    Compute the size of an int array required to hold the specified number of bytes.
    (package private) static int
    Compute the size of an int array required to hold the specified number of longs.
    (package private) static int
    long2Int(long input)
    Creates an int value from a long.
    (package private) static int[]
    long2IntArray(long input, int length)
    Creates an int[] value from a long.
    (package private) static long[]
    long2LongArray(long input, int length)
    Creates a long[] value from a long.
    (package private) static int
    longArray2Int(long[] input)
    Creates an int value from a sequence of longs.
    (package private) static int[]
    longArray2IntArray(long[] input, int length)
    Creates a int[] value from a sequence of longs.
    (package private) static long
    longArray2Long(long[] input)
    Creates a long value from a sequence of longs.
    (package private) static int
    Compute the size of an long array required to hold the specified number of bytes.
    (package private) static int
    Compute the size of an long array required to hold the specified number of ints.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • GOLDEN_RATIO

      private static final long GOLDEN_RATIO
      The fractional part of the golden ratio, phi, scaled to 64-bits and rounded to odd.
       phi = (sqrt(5) - 1) / 2) * 2^64
       
      See Also:
  • Constructor Details

    • Conversions

      private Conversions()
      No instances.
  • Method Details

    • intSizeFromByteSize

      static int intSizeFromByteSize(int size)
      Compute the size of an int array required to hold the specified number of bytes. Allows space for any remaining bytes that do not fit exactly in a 4 byte integer.
       n = ceil(size / 4)
       
      Parameters:
      size - the size in bytes (assumed to be positive)
      Returns:
      the size in ints
    • longSizeFromByteSize

      static int longSizeFromByteSize(int size)
      Compute the size of an long array required to hold the specified number of bytes. Allows space for any remaining bytes that do not fit exactly in an 8 byte long.
       n = ceil(size / 8)
       
      Parameters:
      size - the size in bytes (assumed to be positive)
      Returns:
      the size in longs
    • intSizeFromLongSize

      static int intSizeFromLongSize(int size)
      Compute the size of an int array required to hold the specified number of longs. Prevents overflow to a negative number when doubling the size.
       n = min(size * 2, 2^31 - 1)
       
      Parameters:
      size - the size in longs (assumed to be positive)
      Returns:
      the size in ints
    • longSizeFromIntSize

      static int longSizeFromIntSize(int size)
      Compute the size of an long array required to hold the specified number of ints. Allows space for an odd int.
       n = ceil(size / 2)
       
      Parameters:
      size - the size in ints (assumed to be positive)
      Returns:
      the size in longs
    • int2Long

      static long int2Long(int input)
      Creates a long value from an int. The conversion is made as if seeding a SplitMix64 RNG and calling nextLong().
      Parameters:
      input - Input
      Returns:
      a long.
    • int2IntArray

      static int[] int2IntArray(int input, int length)
      Creates an int[] value from an int. The conversion is made as if seeding a SplitMix64 RNG and calling nextLong() to generate the sequence and filling the ints in little-endian order (least significant byte first).
      Parameters:
      input - Input
      length - Array length
      Returns:
      an int[].
    • int2LongArray

      static long[] int2LongArray(int input, int length)
      Creates a long[] value from an int. The conversion is made as if seeding a SplitMix64 RNG and calling nextLong() to generate the sequence.
      Parameters:
      input - Input
      length - Array length
      Returns:
      a long[].
    • long2Int

      static int long2Int(long input)
      Creates an int value from a long. The conversion is made by xoring the upper and lower bits.
      Parameters:
      input - Input
      Returns:
      an int.
    • long2IntArray

      static int[] long2IntArray(long input, int length)
      Creates an int[] value from a long. The conversion is made as if seeding a SplitMix64 RNG and calling nextLong() to generate the sequence and filling the ints in little-endian order (least significant byte first).

      A special case is made to avoid an array filled with zeros for the initial 2 positions. It is still possible to create a zero in position 0. However any RNG with an int[] native type is expected to require at least 2 int values.

      Parameters:
      input - Input
      length - Array length
      Returns:
      an int[].
    • long2LongArray

      static long[] long2LongArray(long input, int length)
      Creates a long[] value from a long. The conversion is made as if seeding a SplitMix64 RNG and calling nextLong() to generate the sequence.
      Parameters:
      input - Input
      length - Array length
      Returns:
      a long.
    • intArray2Int

      static int intArray2Int(int[] input)
      Creates an int value from a sequence of ints. The conversion is made by combining all the longs with a xor operation.
      Parameters:
      input - Input bytes
      Returns:
      an int.
    • intArray2Long

      static long intArray2Long(int[] input)
      Creates a long value from a sequence of ints. The conversion is made as if converting to a long[] array by filling the longs in little-endian order (least significant byte first), then combining all the longs with a xor operation.
      Parameters:
      input - Input bytes
      Returns:
      a long.
    • intArray2LongArray

      static long[] intArray2LongArray(int[] input, int length)
      Creates a long[] value from a sequence of ints. The longs are filled in little-endian order (least significant byte first).
      Parameters:
      input - Input ints
      length - Output array length
      Returns:
      a long[].
    • longArray2Int

      static int longArray2Int(long[] input)
      Creates an int value from a sequence of longs. The conversion is made as if combining all the longs with a xor operation, then folding the long high and low parts using a xor operation.
      Parameters:
      input - Input longs
      Returns:
      an int.
    • longArray2Long

      static long longArray2Long(long[] input)
      Creates a long value from a sequence of longs. The conversion is made by combining all the longs with a xor operation.
      Parameters:
      input - Input longs
      Returns:
      a long.
    • longArray2IntArray

      static int[] longArray2IntArray(long[] input, int length)
      Creates a int[] value from a sequence of longs. The ints are filled in little-endian order (least significant byte first).
      Parameters:
      input - Input longs
      length - Output array length
      Returns:
      an int[].
    • byteArray2Int

      static int byteArray2Int(byte[] input)
      Creates an int value from a sequence of bytes. The conversion is made as if converting to a int[] array by filling the ints in little-endian order (least significant byte first), then combining all the ints with a xor operation.
      Parameters:
      input - Input bytes
      Returns:
      an int.
    • byteArray2IntArray

      static int[] byteArray2IntArray(byte[] input, int length)
      Creates an int[] value from a sequence of bytes. The ints are filled in little-endian order (least significant byte first).
      Parameters:
      input - Input bytes
      length - Output array length
      Returns:
      a int[].
    • byteArray2Long

      static long byteArray2Long(byte[] input)
      Creates a long value from a sequence of bytes. The conversion is made as if converting to a long[] array by filling the longs in little-endian order (least significant byte first), then combining all the longs with a xor operation.
      Parameters:
      input - Input bytes
      Returns:
      a long.
    • byteArray2LongArray

      static long[] byteArray2LongArray(byte[] input, int length)
      Creates a long[] value from a sequence of bytes. The longs are filled in little-endian order (least significant byte first).
      Parameters:
      input - Input bytes
      length - Output array length
      Returns:
      a long[].