Class Util
- Since:
- 0.1
- Author:
- Sebastiano Vigna
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final RuntimeA static reference toRuntime.getRuntime(). -
Method Summary
Modifier and TypeMethodDescriptionstatic longReturns the amount of available memory (free memory plus never allocated memory).static voidTries to compact memory as much as possible by forcing garbage collection.static int[]composePermutations(int[] p, int[] q) Computes the composition of two permutations expressed as arrays of n distinct integers in [0 .. n).static int[]composePermutations(int[] p, int[] q, int[] r) Computes the composition of two permutations expressed as arrays of n distinct integers in [0 .. n).static long[][]composePermutations(long[][] p, long[][] q) Computes the composition of two permutations expressed as big arrays of n distinct long integers in [0 .. n).static long[][]composePermutations(long[][] p, long[][] q, long[][] r) Computes the composition of two permutations expressed as big arrays of n distinct long integers in [0 .. n).static int[]composePermutationsInPlace(int[] p, int[] q) Computes in place the composition of two permutations expressed as arrays of n distinct integers in [0 .. n).static long[][]composePermutationsInPlace(long[][] p, long[][] q) Computes in place the composition of two permutations expressed as big arrays of n distinct long integers in [0 .. n).static Stringformat(double d) Formats a number.static Stringformat(double d, NumberFormat format) Formats a number using a specifiedNumberFormat.static Stringformat(long l) Formats a number.static Stringformat(long l, NumberFormat format) Formats a number using a specifiedNumberFormat.static StringformatBinarySize(long l) Formats a binary size.static StringformatBinarySize(long l, NumberFormat format) Formats a size using a specifiedNumberFormat.static StringformatSize(long l) Formats a size.static StringformatSize(long l, NumberFormat format) Formats a size using a specifiedNumberFormat.static StringformatSize2(long l) Formats a size.static StringformatSize2(long l, NumberFormat format) Formats a size using a specifiedNumberFormatand binary unit multipliers.static int[]identity(int n) Stores the identity permutation in a new array of given length.static int[]identity(int[] perm) Stores the identity permutation in an array.static long[][]identity(long n) Stores the identity permutation in a new big array of given length.static long[][]identity(long[][] perm) Stores the identity permutation in a big array.static int[]invertPermutation(int[] perm) Computes the inverse of a permutation expressed as an array of n distinct integers in [0 .. n) and stores the result in a new array.static int[]invertPermutation(int[] perm, int[] inv) Computes the inverse of a permutation expressed as an array of n distinct integers in [0 .. n).static long[][]invertPermutation(long[][] perm) Computes the inverse of a permutation expressed as a big array of n distinct long integers in [0 .. n) and stores the result in a new big array.static long[][]invertPermutation(long[][] perm, long[][] inv) Computes the inverse of a permutation expressed as a big array of n distinct long integers in [0 .. n).static int[]invertPermutationInPlace(int[] perm) Computes in place the inverse of a permutation expressed as an array of n distinct integers in [0 .. n).static long[][]invertPermutationInPlace(long[][] perm) Computes in place the inverse of a permutation expressed as a big array of n distinct long integers in [0 .. n).static booleanReturns true if less then 5% of the available memory is free.static intReturns the percentage of available memory (free memory plus never allocated memory).static longReturns a random seed generated by taking the output of aXoRoShiRo128PlusRandomGenerator(seeded at startup withSystem.nanoTime()) and xoring it withSystem.nanoTime().static byte[]Returns a random seed generated byrandomSeed()under the form of an array of eight bytes.
-
Field Details
-
RUNTIME
A static reference toRuntime.getRuntime().
-
-
Method Details
-
format
Formats a number.This method formats a double separating thousands and printing just two fractional digits.
Note that the method is synchronized, as it uses a static
NumberFormat.- Parameters:
d- a number.- Returns:
- a string containing a pretty print of the number.
-
format
Formats a number.This method formats a long separating thousands.
Note that the method is synchronized, as it uses a static
NumberFormat.- Parameters:
l- a number.- Returns:
- a string containing a pretty print of the number.
-
format
Formats a number using a specifiedNumberFormat.- Parameters:
d- a number.format- a format.- Returns:
- a string containing a pretty print of the number.
-
format
Formats a number using a specifiedNumberFormat.- Parameters:
l- a number.format- a format.- Returns:
- a string containing a pretty print of the number.
-
formatSize
Formats a size.This method formats a long using suitable unit multipliers (e.g.,
K,M,G, andT) and printing just two fractional digits.Note that the method is synchronized, as it uses a static
NumberFormat.- Parameters:
l- a number, representing a size (e.g., memory).- Returns:
- a string containing a pretty print of the number using unit multipliers.
-
formatBinarySize
Formats a binary size.This method formats a long using suitable unit binary multipliers (e.g.,
Ki,Mi,Gi, andTi) and printing no fractional digits. The argument must be a power of 2.Note that the method is synchronized, as it uses a static
NumberFormat.- Parameters:
l- a number, representing a binary size (e.g., memory); must be a power of 2.- Returns:
- a string containing a pretty print of the number using binary unit multipliers.
-
formatSize2
Formats a size.This method formats a long using suitable binary unit multipliers (e.g.,
Ki,Mi,Gi, andTi) and printing just two fractional digits.Note that the method is synchronized, as it uses a static
NumberFormat.- Parameters:
l- a number, representing a size (e.g., memory).- Returns:
- a string containing a pretty print of the number using binary unit multipliers.
-
formatSize
Formats a size using a specifiedNumberFormat.This method formats a long using suitable unit multipliers (e.g.,
K,M,G, andT) and the givenNumberFormatfor the digits.- Parameters:
l- a number, representing a size (e.g., memory).format- a format.- Returns:
- a string containing a pretty print of the number using unit multipliers.
-
formatBinarySize
Formats a size using a specifiedNumberFormat.This method formats a long using suitable unit binary multipliers (e.g.,
Ki,Mi,Gi, andTi) and the givenNumberFormatfor the digits. The argument must be a power of 2.- Parameters:
l- a number, representing a binary size (e.g., memory); must be a power of 2.format- a format.- Returns:
- a string containing a pretty print of the number using binary unit multipliers.
-
formatSize2
Formats a size using a specifiedNumberFormatand binary unit multipliers.This method formats a long using suitable binary unit multipliers (e.g.,
Ki,Mi,Gi, andTi) and the givenNumberFormatfor the digits.- Parameters:
l- a number, representing a size (e.g., memory).format- a format.- Returns:
- a string containing a pretty print of the number using binary unit multipliers.
-
memoryIsLow
public static boolean memoryIsLow()Returns true if less then 5% of the available memory is free.- Returns:
- true if less then 5% of the available memory is free.
-
availableMemory
public static long availableMemory()Returns the amount of available memory (free memory plus never allocated memory).- Returns:
- the amount of available memory, in bytes.
-
percAvailableMemory
public static int percAvailableMemory()Returns the percentage of available memory (free memory plus never allocated memory).- Returns:
- the percentage of available memory.
-
compactMemory
public static void compactMemory()Tries to compact memory as much as possible by forcing garbage collection. -
randomSeed
public static long randomSeed()Returns a random seed generated by taking the output of aXoRoShiRo128PlusRandomGenerator(seeded at startup withSystem.nanoTime()) and xoring it withSystem.nanoTime().- Returns:
- a reasonably good random seed.
-
randomSeedBytes
public static byte[] randomSeedBytes()Returns a random seed generated byrandomSeed()under the form of an array of eight bytes.- Returns:
- a reasonably good random seed.
-
invertPermutationInPlace
public static int[] invertPermutationInPlace(int[] perm) Computes in place the inverse of a permutation expressed as an array of n distinct integers in [0 .. n).Warning: if
permis not a permutation, essentially anything can happen.- Parameters:
perm- the permutation to be inverted.- Returns:
perm.
-
invertPermutation
public static int[] invertPermutation(int[] perm, int[] inv) Computes the inverse of a permutation expressed as an array of n distinct integers in [0 .. n).Warning: if
permis not a permutation, essentially anything can happen.- Parameters:
perm- the permutation to be inverted.inv- the array storing the inverse.- Returns:
inv.
-
invertPermutation
public static int[] invertPermutation(int[] perm) Computes the inverse of a permutation expressed as an array of n distinct integers in [0 .. n) and stores the result in a new array.Warning: if
permis not a permutation, essentially anything can happen.- Parameters:
perm- the permutation to be inverted.- Returns:
- a new array containing the inverse permutation.
-
identity
public static int[] identity(int[] perm) Stores the identity permutation in an array.- Parameters:
perm- an array of integers.- Returns:
perm, filled with the identity permutation.
-
identity
public static int[] identity(int n) Stores the identity permutation in a new array of given length.- Parameters:
n- the size of the array.- Returns:
- a new array of length
n, filled with the identity permutation.
-
composePermutations
public static int[] composePermutations(int[] p, int[] q, int[] r) Computes the composition of two permutations expressed as arrays of n distinct integers in [0 .. n).Warning: if the arguments are not permutations, essentially anything can happen.
- Parameters:
p- the first permutation.q- the second permutation.r- an array that will store the resulting permutation:r[i] = q[p[i]].- Returns:
r.
-
composePermutations
public static int[] composePermutations(int[] p, int[] q) Computes the composition of two permutations expressed as arrays of n distinct integers in [0 .. n).Warning: if the arguments are not permutations, essentially anything can happen.
- Parameters:
p- the first permutation.q- the second permutation.- Returns:
- an array
rcontaining the resulting permutation:r[i] = q[p[i]]. - See Also:
-
composePermutationsInPlace
public static int[] composePermutationsInPlace(int[] p, int[] q) Computes in place the composition of two permutations expressed as arrays of n distinct integers in [0 .. n).Warning: if the arguments are not permutations, essentially anything can happen.
- Parameters:
p- the first permutation.q- the second permutation, which will contain the result at the end.- Returns:
q.- See Also:
-
invertPermutationInPlace
public static long[][] invertPermutationInPlace(long[][] perm) Computes in place the inverse of a permutation expressed as a big array of n distinct long integers in [0 .. n).Warning: if
permis not a permutation, essentially anything can happen.- Parameters:
perm- the permutation to be inverted.- Returns:
perm.
-
invertPermutation
public static long[][] invertPermutation(long[][] perm, long[][] inv) Computes the inverse of a permutation expressed as a big array of n distinct long integers in [0 .. n).Warning: if
permis not a permutation, essentially anything can happen.- Parameters:
perm- the permutation to be inverted.inv- the big array storing the inverse.- Returns:
inv.
-
invertPermutation
public static long[][] invertPermutation(long[][] perm) Computes the inverse of a permutation expressed as a big array of n distinct long integers in [0 .. n) and stores the result in a new big array.Warning: if
permis not a permutation, essentially anything can happen.- Parameters:
perm- the permutation to be inverted.- Returns:
- a new big array containing the inverse permutation.
-
identity
public static long[][] identity(long[][] perm) Stores the identity permutation in a big array.- Parameters:
perm- a big array.- Returns:
perm, filled with the identity permutation.
-
identity
public static long[][] identity(long n) Stores the identity permutation in a new big array of given length.- Parameters:
n- the size of the array.- Returns:
- a new array of length
n, filled with the identity permutation.
-
composePermutations
public static long[][] composePermutations(long[][] p, long[][] q, long[][] r) Computes the composition of two permutations expressed as big arrays of n distinct long integers in [0 .. n).Warning: if the arguments are not permutations, essentially anything can happen.
- Parameters:
p- the first permutation.q- the second permutation.r- an array that will store the resulting permutation:r[i] = q[p[i]].- Returns:
r.
-
composePermutations
public static long[][] composePermutations(long[][] p, long[][] q) Computes the composition of two permutations expressed as big arrays of n distinct long integers in [0 .. n).Warning: if the arguments are not permutations, essentially anything can happen.
- Parameters:
p- the first permutation.q- the second permutation.- Returns:
- an array
rcontaining the resulting permutation:r[i] = q[p[i]]. - See Also:
-
composePermutationsInPlace
public static long[][] composePermutationsInPlace(long[][] p, long[][] q) Computes in place the composition of two permutations expressed as big arrays of n distinct long integers in [0 .. n).Warning: if the arguments are not permutations, essentially anything can happen.
- Parameters:
p- the first permutation.q- the second permutation, which will contain the result at the end.- Returns:
q.- See Also:
-