Class Conversions
java.lang.Object
org.apache.commons.rng.simple.internal.Conversions
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
FieldsModifier and TypeFieldDescriptionprivate static final long
The fractional part of the golden ratio, phi, scaled to 64-bits and rounded to odd. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static int
byteArray2Int
(byte[] input) Creates anint
value from a sequence of bytes.(package private) static int[]
byteArray2IntArray
(byte[] input, int length) Creates anint[]
value from a sequence of bytes.(package private) static long
byteArray2Long
(byte[] input) Creates along
value from a sequence of bytes.(package private) static long[]
byteArray2LongArray
(byte[] input, int length) Creates along[]
value from a sequence of bytes.(package private) static int[]
int2IntArray
(int input, int length) Creates anint[]
value from anint
.(package private) static long
int2Long
(int input) Creates along
value from anint
.(package private) static long[]
int2LongArray
(int input, int length) Creates along[]
value from anint
.(package private) static int
intArray2Int
(int[] input) Creates anint
value from a sequence of ints.(package private) static long
intArray2Long
(int[] input) Creates along
value from a sequence of ints.(package private) static long[]
intArray2LongArray
(int[] input, int length) Creates along[]
value from a sequence of ints.(package private) static int
intSizeFromByteSize
(int size) Compute the size of anint
array required to hold the specified number of bytes.(package private) static int
intSizeFromLongSize
(int size) Compute the size of anint
array required to hold the specified number of longs.(package private) static int
long2Int
(long input) Creates anint
value from along
.(package private) static int[]
long2IntArray
(long input, int length) Creates anint[]
value from along
.(package private) static long[]
long2LongArray
(long input, int length) Creates along[]
value from along
.(package private) static int
longArray2Int
(long[] input) Creates anint
value from a sequence of longs.(package private) static int[]
longArray2IntArray
(long[] input, int length) Creates aint[]
value from a sequence of longs.(package private) static long
longArray2Long
(long[] input) Creates along
value from a sequence of longs.(package private) static int
longSizeFromByteSize
(int size) Compute the size of anlong
array required to hold the specified number of bytes.(package private) static int
longSizeFromIntSize
(int size) Compute the size of anlong
array required to hold the specified number of ints.
-
Field Details
-
GOLDEN_RATIO
private static final long GOLDEN_RATIOThe 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 anint
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 anlong
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 anint
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 anlong
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 along
value from anint
. 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 anint[]
value from anint
. 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
- Inputlength
- Array length- Returns:
- an
int[]
.
-
int2LongArray
static long[] int2LongArray(int input, int length) Creates along[]
value from anint
. The conversion is made as if seeding a SplitMix64 RNG and calling nextLong() to generate the sequence.- Parameters:
input
- Inputlength
- Array length- Returns:
- a
long[]
.
-
long2Int
static int long2Int(long input) Creates anint
value from along
. 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 anint[]
value from along
. 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
- Inputlength
- Array length- Returns:
- an
int[]
.
-
long2LongArray
static long[] long2LongArray(long input, int length) Creates along[]
value from along
. The conversion is made as if seeding a SplitMix64 RNG and calling nextLong() to generate the sequence.- Parameters:
input
- Inputlength
- Array length- Returns:
- a
long
.
-
intArray2Int
static int intArray2Int(int[] input) Creates anint
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 along
value from a sequence of ints. The conversion is made as if converting to along[]
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 along[]
value from a sequence of ints. The longs are filled in little-endian order (least significant byte first).- Parameters:
input
- Input intslength
- Output array length- Returns:
- a
long[]
.
-
longArray2Int
static int longArray2Int(long[] input) Creates anint
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 along
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 aint[]
value from a sequence of longs. The ints are filled in little-endian order (least significant byte first).- Parameters:
input
- Input longslength
- Output array length- Returns:
- an
int[]
.
-
byteArray2Int
static int byteArray2Int(byte[] input) Creates anint
value from a sequence of bytes. The conversion is made as if converting to aint[]
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 anint[]
value from a sequence of bytes. The ints are filled in little-endian order (least significant byte first).- Parameters:
input
- Input byteslength
- Output array length- Returns:
- a
int[]
.
-
byteArray2Long
static long byteArray2Long(byte[] input) Creates along
value from a sequence of bytes. The conversion is made as if converting to along[]
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 along[]
value from a sequence of bytes. The longs are filled in little-endian order (least significant byte first).- Parameters:
input
- Input byteslength
- Output array length- Returns:
- a
long[]
.
-