public class LongUtils
extends Object
Provides extra functionality for long primitives.
| Modifier and Type | Method and Description |
|---|---|
static long[] |
arrayOf(long... values)
Generates a
long[] from comma-separated values. |
static Long[] |
asObjectArray(long[] array)
Converts from an
long array to an Long array. |
static long[] |
asPrimitiveArray(Long[] array)
Converts from an
Long array to an long array. |
static double |
average(long[] array)
Returns the average value of an array.
|
static double |
averageNonZeros(long[] array)
Returns the average value only among non-zero values
|
static long[] |
concatenate(long[]... arrays)
Concatenates a series of arrays.
|
static long[] |
constantArray(int N,
long value)
Returns an array filled with a given value.
|
static <A> Map<A,Long> |
constantMap(Set<A> identifiers,
long value)
Returns a map filled with a given value.
|
static boolean |
contains(long[] array,
long value)
Checks if an input array contains a given value
|
static boolean |
containsAll(long[] container,
long[] array)
Checks if an array contains all numbers from another one.
|
static boolean |
containsAny(long[] container,
long[] array)
Checks whether any element of an array is present in another one.
|
static <A extends Number> |
convertArray2Map(Set<A> identifiers,
long[] array)
Returns a map containing the association of each identifier with its corresponding
value within the array (in linear order).
|
static <A extends Number,B extends Number> |
convertTable2Map(Set<A> rowIdentifiers,
Set<B> columnIdentifiers,
long[][] table)
Returns a map containing the association of each identifier pair with its corresponding
value within the table (in linear order).
|
static long[] |
copy(long[] array)
Returns a deep copy of the input
array. |
static long[][] |
copy(long[][] array)
Returns a deep copy of the input
array. |
static double[] |
divide(long[] array,
double value)
Divides all elements in an array by a scalar.
|
static double[] |
divide(long[] array1,
long[] array2)
Divides two arrays element-to-element.
|
static double[] |
divideNonSingular(long[] array1,
long[] array2)
Divides two arrays element-to-element, but when numerator and denominator = 0, returns 0 instead of a singularity (NaN)
|
static int[] |
find(long[] array,
long value,
Constants.SearchType searchType)
Returns the position(s) where a given value can be found into an array.
|
static long |
gcd(long[] array)
Computes the greatest absolute common divisor of an integer array.
|
static long[] |
intersect(long[]... arrays)
Returns the intersection vector of a series of input arrays.
|
static String |
join(long[] array,
String separator)
Joins the elements in an input array using a given separator.
|
static List<Long> |
listOf(long... values)
Returns a list of the comma-separated input values.
|
static int[] |
maxIndexes(long[] array,
Constants.SearchType searchType)
Returns the position(s) in which the maximum value is found.
|
static int[][] |
maxMinIndexes(long[] array,
Constants.SearchType searchType)
Returns the position(s) in which the maximum/minimum values are found.
|
static long[] |
maxMinValues(long[] array)
Returns the maximum/minimum values of an input array.
|
static long |
maxValue(Collection<Long> collection)
Returns the maximum value in the input collection.
|
static long |
maxValue(long[] array)
Returns the maximum value in the input array.
|
static long |
maxValue(long[][] array)
Returns the maximum value in the input array.
|
static int[] |
minIndexes(long[] array,
Constants.SearchType searchType)
Returns the position(s) in which the minimum value is found.
|
static long |
minValue(Collection<Long> collection)
Returns the minimum value in the input collection.
|
static long |
minValue(long[] array)
Returns the minimum value in the input array.
|
static double[][] |
mult(long[][] matrix,
double value)
Multiplies all elements in a matrix by a scalar.
|
static double[] |
mult(long[] array,
double value)
Multiplies all elements in an array by a scalar.
|
static double[] |
mult(long[] array1,
long[] array2)
Multiplies two arrays element-to-element.
|
static long[] |
ones(int N)
Returns an array filled with ones.
|
static void |
reverse(long[] array)
Reverses the order of the elements of the input array (it will be overriden).
|
static double |
scalarProduct(long[] array1,
long[] array2)
Scalar product of two vectors.
|
static <A> double |
scalarProduct(Map<A,Long> map1,
Map<A,Long> map2)
Scalar product of two maps.
|
static long[] |
select(long[] array,
int[] indexes)
Returns a set of selected elements from an input array.
|
static long[] |
selectColumn(long[][] array,
int column)
Returns a column of a bidimensional input array.
|
static long[] |
selectRow(long[][] array,
int row)
Returns a row of a bidimensional input array.
|
static long[] |
setdiff(long[]... arrays)
Returns the elements contained in the first array, but not any of the others.
|
static Set<Long> |
setOf(long... values)
Returns a list of the comma-separated input values.
|
static void |
sort(long[] array,
Constants.OrderingType orderingType)
Sorts the input array (it will be overriden).
|
static int[] |
sortIndexes(long[] array,
Constants.OrderingType orderingType)
Sorts indexes of the
array into ascending/descending order in a stable way. |
static double |
std(long[] array)
Returns the standard deviation of an array using the Welford's method.
|
static long[] |
substract(long[] array1,
long[] array2)
Returns the element-wise substraction of two arrays.
|
static double |
sum(long[] array)
Returns the sum of all elements in the array.
|
static double[] |
sum(long[] array1,
long[] array2)
Returns the element-wise sum of two arrays.
|
static long[] |
toArray(Collection<Long> list)
Converts a collection (
List, Set...) of Long objects to a long array. |
static double[] |
toDoubleArray(long[] array)
Converts a
long array to a double array |
static int[] |
toIntArray(long[] array)
Converts a
long array to an int array (truncation may happen). |
static List<Long> |
toList(long[] array)
Converts from a
long array to a list. |
static long[] |
union(long[]... arrays)
Returns an array with all elements in input arrays (no repetitions).
|
static Set<Long> |
unique(Collection<Long> collection)
Returns the same values of the input
array but with no repetitions. |
static long[] |
unique(long[] array)
Returns the same values of the input
array but with no repetitions. |
static long[] |
zeros(int N)
Returns an array filled with zeros.
|
public static long[] arrayOf(long... values)
long[] from comma-separated values.values - Comma-separated long valueslong[]public static Long[] asObjectArray(long[] array)
long array to an Long array.array - long arrayLong arraypublic static long[] asPrimitiveArray(Long[] array)
Long array to an long array.array - Long arraylong arraypublic static double average(long[] array)
array - Input arraypublic static double averageNonZeros(long[] array)
array - Input arraypublic static long[] concatenate(long[]... arrays)
arrays - List of arrayspublic static long[] constantArray(int N,
long value)
N - Number of elementsvalue - Value for all elementsN with the given valuepublic static <A> Map<A,Long> constantMap(Set<A> identifiers,
long value)
A - Key typeidentifiers - Set of map keysvalue - Value for all elementspublic static boolean contains(long[] array,
long value)
array - Input arrayvalue - Value to searchtrue if value is present in array, and false otherwise. If array is empty, it will return falsepublic static boolean containsAll(long[] container,
long[] array)
container - Container arrayarray - Array with elements to be checkedtrue if all elements in array are present in container, and false otherwise. If container is empty, it will return falsepublic static boolean containsAny(long[] container,
long[] array)
intersection(array1, array2).length == 0.container - Container arrayarray - Array with elements to be checkedtrue if any element in array is present in container, and false otherwise. If container is empty, it will return falsepublic static <A extends Number> Map<A,Long> convertArray2Map(Set<A> identifiers,
long[] array)
A - Key typeidentifiers - Set of identifier in ascending iterator order (duplicated and null values are not allowed)array - Input arraypublic static <A extends Number,B extends Number> Map<Pair<A,B>,Long> convertTable2Map(Set<A> rowIdentifiers, Set<B> columnIdentifiers, long[][] table)
A - Row key typeB - Column key typerowIdentifiers - Set of row identifiers in ascending iterator order (null values are not allowed)columnIdentifiers - Set of column identifiers in ascending iterator order (duplicated and null values are not allowed)table - Input tablepublic static long[] copy(long[] array)
array.array - Input arrayarraypublic static long[][] copy(long[][] array)
array.array - Input arrayarraypublic static double[] divide(long[] array,
double value)
array - Input arrayvalue - Scalarpublic static double[] divide(long[] array1,
long[] array2)
array1 - Numeratorarray2 - Denominatorpublic static double[] divideNonSingular(long[] array1,
long[] array2)
array1 - Numeratorarray2 - Denominatorpublic static int[] find(long[] array,
long value,
Constants.SearchType searchType)
array - Input arrayvalue - Value to be searched forsearchType - Indicates whether the first, the last, or all occurrences are returnedpublic static long gcd(long[] array)
array - Input arraygcd(0, 0) is equal to zero and gcd([]) is equal to onepublic static long[] intersect(long[]... arrays)
arrays - Vector of input arrayspublic static String join(long[] array,
String separator)
Arrays.toString().array - Input arrayseparator - SeparatorString representation of the input arraypublic static List<Long> listOf(long... values)
values - Comma-separated input valuespublic static int[] maxIndexes(long[] array,
Constants.SearchType searchType)
array - Input arraysearchType - Indicates whether the first, the last, or all maximum positions are returnedpublic static int[][] maxMinIndexes(long[] array,
Constants.SearchType searchType)
Returns the position(s) in which the maximum/minimum values are found.
out[0] are the maximum positions, while out[1] are the minimum positions
array - Input arraysearchType - Indicates whether the first, the last, or all maximum positions are returnedpublic static long[] maxMinValues(long[] array)
array - Input arraypublic static long maxValue(Collection<Long> collection)
collection - Input collectionpublic static long maxValue(long[] array)
array - Input arraypublic static long maxValue(long[][] array)
array - Input arraypublic static int[] minIndexes(long[] array,
Constants.SearchType searchType)
array - Input arraysearchType - Indicates whether the first, the last, or all minimum positions are returnedpublic static long minValue(Collection<Long> collection)
collection - Input collectionpublic static long minValue(long[] array)
array - Input arraypublic static double[][] mult(long[][] matrix,
double value)
matrix - Input arrayvalue - Scalarpublic static double[] mult(long[] array,
double value)
array - Input arrayvalue - Scalarpublic static double[] mult(long[] array1,
long[] array2)
array1 - Input array 1array2 - Input array 2public static long[] ones(int N)
N - Number of elementsNpublic static void reverse(long[] array)
array - Input arraypublic static double scalarProduct(long[] array1,
long[] array2)
array1 - Input array 1array2 - Input array 2public static <A> double scalarProduct(Map<A,Long> map1,
Map<A,Long> map2)
A - Key typemap1 - Input map 1map2 - Input map 2public static long[] select(long[] array,
int[] indexes)
array - Input arrayindexes - Position of elements to be selected.indexespublic static long[] selectColumn(long[][] array,
int column)
array - Input arraycolumn - Columnpublic static long[] selectRow(long[][] array,
int row)
array - Input arrayrow - Rowpublic static long[] setdiff(long[]... arrays)
arrays - Input arrayspublic static Set<Long> setOf(long... values)
values - Comma-separated input valuespublic static void sort(long[] array,
Constants.OrderingType orderingType)
array - Input arrayorderingType - Ascending or descendingpublic static int[] sortIndexes(long[] array,
Constants.OrderingType orderingType)
array into ascending/descending order in a stable way. Stable means that index order doesn't change if values are the same.array - Array to be sortedorderingType - Ascending or descendingpublic static double std(long[] array)
array - Input arraypublic static long[] substract(long[] array1,
long[] array2)
array1 - Input array 1array2 - Input array 2public static double sum(long[] array)
array - Input arraypublic static double[] sum(long[] array1,
long[] array2)
array1 - Input array 1array2 - Input array 2public static long[] toArray(Collection<Long> list)
List, Set...) of Long objects to a long array.list - Input listlong arraypublic static double[] toDoubleArray(long[] array)
long array to a double arrayarray - Input arraydouble arraypublic static int[] toIntArray(long[] array)
long array to an int array (truncation may happen).array - Input arrayint arraypublic static List<Long> toList(long[] array)
long array to a list.array - Input arrayLong objectspublic static long[] union(long[]... arrays)
arrays - Input arrayspublic static Set<Long> unique(Collection<Long> collection)
array but with no repetitions. There is no order guarantee.collection - Input arrayarray but with no repetitionspublic static long[] unique(long[] array)
array but with no repetitions. There is no order guarantee.array - Input arrayarray but with no repetitionspublic static long[] zeros(int N)
N - Number of elementsN