- java.lang.Object
-
- java.lang.Enum<ArrayUtils>
-
- org.chocosolver.util.tools.ArrayUtils
-
- All Implemented Interfaces:
Serializable
,Comparable<ArrayUtils>
public enum ArrayUtils extends Enum<ArrayUtils>
This class contains various methods for manipulating arrays.- Since:
- 17 sept. 2010
- Author:
- Charles Prud'homme, Jean-Guillaume Fages, Rene Helmke
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int[]
append(int[]... toAppend)
Append int arraysstatic BoolVar[]
append(BoolVar[]... toAppend)
Append two Arraysstatic IntVar[]
append(IntVar[]... toAppend)
Append two Arraysstatic <T> T[]
append(T[]... toAppend)
Append two Arraysstatic int[]
array(int lb, int ub)
Creates an array of ints of consecutive values from lb (inclusive) to ub (inclusive as well), i.e. the range [lb, ub], For instance: {3,4,...,99,100}static int
binarySearchInc(int[] a, int fromIndex, int toIndex, int key, boolean gq)
Adapted from java.util.Arrays#binarySearch0(int[], int, int, int) , it returns the value greater or equal to key in an increasing order value array If the key exists in a, it returns the index of key in a, otherwise it returns the index of the closest value greater than key when gq is set to true, or the index of the closest value smaller than key when gq is set to false.static int[]
concat(int[] array, int... elements)
Append elements at the end of another arraystatic BoolVar[]
concat(BoolVar[] array, BoolVar... elements)
Append elements at the end of another arraystatic IntVar[]
concat(IntVar[] array, IntVar... elements)
Append elements at the end of another arraystatic <T> T[]
concat(T[] array, T... elements)
Append elements at the end of another arraystatic <T> boolean
contains(T[] array, T obj)
static int[]
createNonRedundantSortedValues(gnu.trove.list.array.TIntArrayList values)
static int[]
flatten(int[][] matrix)
Flattens a matrix M[n][m] in an array F[n*m] such that F[i*n+j] = M[i][j].static <T> T[]
flatten(T[][] matrix)
Flattens a matrix M[n][m] in an array F[n*m] such that F[i*n+j] = M[i][j].static <T> T[]
flatten(T[][][] matrix)
Flattens a matrix M[n][m][p] in an array F[n*m*p] such that F[(i*n*m) + (j*m) + k] = M[i][j][k].static <T> T[]
flattenSubMatrix(int iMin, int iLength, int jMin, int jLength, T[][] matrix)
static <T> T
get(int index, List<T>... arrays)
Returns the element in position index when considering all arrays appended altogether.static <T> T
get(int index, T[]... arrays)
Returns the element in position index when considering all arrays appended altogether.static double[]
getColumn(double[][] array, int c)
Returns the column c extracted from matrix array.static int[]
getColumn(int[][] array, int c)
Returns the column c extracted from matrix array.static <T> T[]
getColumn(T[][] array, int c)
Returns the column c extracted from matrix array.static <T> T[]
getColumn(T[][] array, int c, Class<? extends T[]> newType)
Returns the column c extracted from matrix array.static void
inverseSign(int[] tab)
Reverse all signs of the a given int table.static <T> int
length(T[]... arrays)
Sum the lengths of arraysstatic int[]
linspace(int begin, int end)
Create an array of size (end - begin) and assigns to the element i the value (i + begin).static int[]
mergeAndSortIfNot(int[] values)
Sorts the input array if it is not already sorted, and removes multiple occurrences of the same valuestatic <T> void
permutation(int[] permutation, T[] tab)
Permutes elements from tab wrt to permutuation: tab[i] = tab[permutation[i]].static void
randomPermutations(int[] tab, long seed)
Permutes randomly elements from tabstatic void
randomPermutations(int[] tab, Random r)
Permutes randomly elements from tabstatic <E> void
randomPermutations(E[] tab, long seed)
Permutes randomly elements from tabstatic <E> void
randomPermutations(E[] tab, Random r)
Permutes randomly elements from tabstatic void
reverse(int[] tab)
Turns back to from the elements of tab from the middle position.static <T> void
reverse(T[] tab)
Turns back to from the elements of tab from the middle position.static <T extends Comparable<T>>
T[]sort(Set<T> set)
Create an array of elements in set and sort them usingArrays.sort(Object[])
static int[][][]
swallowCopy(int[][][] arr)
Duplicates arr and returns the new copystatic int[][][][]
swallowCopy(int[][][][] arr)
Duplicates arr and returns the new copystatic <T> T[]
toArray(Class c, List<T> list)
Returns an array composed of elements from list.static <T> T[]
toArray(List<T> list)
Creates an array from elements in list.static <T> T[]
toArray(T... elt)
Creates an array from vargs elt.static <T> List<T>
toList(T[] array)
Returns a list composed of elements from array.static int[][]
transpose(int[][] matrix)
Transposes a matrix M[n][m] in a matrix MT[m][n] such that MT[i][j] = M[j][i]static <T> T[][]
transpose(T[][] matrix)
Transposes a matrix M[n][m] in a matrix MT[m][n] such that MT[i][j] = M[j][i]static ArrayUtils
valueOf(String name)
Returns the enum constant of this type with the specified name.static ArrayUtils[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.static int[]
zeroToNShuffle(int nb)
Creates and returns an array of ints composed of unique values from 0 (inclusive) to nb (exclusive), in random order.static int[]
zeroToNShuffle(int nb, long seed)
Creates and returns an array of ints composed of unique values from 0 (inclusive) to nb (exclusive), in random order.
-
-
-
Method Detail
-
values
public static ArrayUtils[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (ArrayUtils c : ArrayUtils.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static ArrayUtils valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
array
public static int[] array(int lb, int ub)
Creates an array of ints of consecutive values from lb (inclusive) to ub (inclusive as well), i.e. the range [lb, ub], For instance: {3,4,...,99,100}- Parameters:
lb
- first element in the arrayub
- last element in the array- Returns:
- an array of ints
- Throws:
NegativeArraySizeException
- if ub
-
linspace
public static int[] linspace(int begin, int end)
Create an array of size (end - begin) and assigns to the element i the value (i + begin). begin must be greater or equal to end.- Parameters:
begin
- first valueend
- last value- Returns:
- null if begin > end, an array of ints otherwise.
- Throws:
NegativeArraySizeException
- if begin > end is negative
-
getColumn
public static int[] getColumn(int[][] array, int c)
Returns the column c extracted from matrix array.- Parameters:
array
- double entry matrixc
- index of the column to get- Returns:
- the column c from array, or null if array is null or array.length is null, or if c is negative or if array.length < c
-
getColumn
public static double[] getColumn(double[][] array, int c)
Returns the column c extracted from matrix array.- Parameters:
array
- double entry matrixc
- index of the column to get- Returns:
- the column c from array, or null if array is null or array.length is null, or if c is negative or if array.length < c
-
getColumn
public static <T> T[] getColumn(T[][] array, int c)
Returns the column c extracted from matrix array.- Type Parameters:
T
- the class of the objects in the input matrix- Parameters:
array
- double entry matrixc
- index of the column to get- Returns:
- the column c from array, or null if array is null or array.length is null, or if c is negative or if array.length < c
-
getColumn
public static <T> T[] getColumn(T[][] array, int c, Class<? extends T[]> newType)
Returns the column c extracted from matrix array.- Type Parameters:
T
- the class of the objects in the input matrix- Parameters:
array
- double entry matrixc
- index of the column to getnewType
- the class of the copy to be returned- Returns:
- the column c from array, or null if array is null or array.length is null, or if c is negative or if array.length < c
-
length
@SafeVarargs public static <T> int length(T[]... arrays)
Sum the lengths of arrays- Type Parameters:
T
- the class of the objects in the input array- Parameters:
arrays
- list of arrays- Returns:
- the sum of lengths of arrays
-
contains
public static <T> boolean contains(T[] array, T obj)
- Type Parameters:
T
- the class of the objects in the input array- Parameters:
array
- array of elementsobj
- element to find- Returns:
- true if array contains obj
-
get
@SafeVarargs public static <T> T get(int index, T[]... arrays)
Returns the element in position index when considering all arrays appended altogether.- Type Parameters:
T
- the class of the objects in the input arrays- Parameters:
index
- position of the element to returnarrays
- list of arrays- Returns:
- the element in position index when considering all arrays appended altogether.
-
get
@SafeVarargs public static <T> T get(int index, List<T>... arrays)
Returns the element in position index when considering all arrays appended altogether.- Type Parameters:
T
- the class of the objects in the input lists- Parameters:
index
- position of the element to returnarrays
- list of arrays- Returns:
- the element in position index when considering all arrays appended altogether.
-
append
public static <T> T[] append(T[]... toAppend)
Append two Arrays- Parameters:
toAppend
- array of arrays to append- Returns:
- a new Array composed of both given in parameters.
-
append
public static IntVar[] append(IntVar[]... toAppend)
Append two Arrays- Parameters:
toAppend
- array of arrays to append- Returns:
- a new Array composed of both given in parameters.
-
append
public static BoolVar[] append(BoolVar[]... toAppend)
Append two Arrays- Parameters:
toAppend
- array of arrays to append- Returns:
- a new Array composed of both given in parameters.
-
concat
public static <T> T[] concat(T[] array, T... elements)
Append elements at the end of another array- Parameters:
array
- array of arrays to appendelements
- elements to append- Returns:
- a new Array composed of both given in parameters.
-
concat
public static IntVar[] concat(IntVar[] array, IntVar... elements)
Append elements at the end of another array- Parameters:
array
- array of arrays to appendelements
- elements to append- Returns:
- a new Array composed of both given in parameters.
-
concat
public static BoolVar[] concat(BoolVar[] array, BoolVar... elements)
Append elements at the end of another array- Parameters:
array
- array of arrays to appendelements
- elements to append- Returns:
- a new Array composed of both given in parameters.
-
append
public static int[] append(int[]... toAppend)
Append int arrays- Parameters:
toAppend
- array of arrays to append- Returns:
- a new Array composed of those given in parameters.
-
concat
public static int[] concat(int[] array, int... elements)
Append elements at the end of another array- Parameters:
array
- array of arrays to appendelements
- elements to append- Returns:
- a new Array composed of both given in parameters.
-
inverseSign
public static void inverseSign(int[] tab)
Reverse all signs of the a given int table.- Parameters:
tab
- array to inverse
-
reverse
public static void reverse(int[] tab)
Turns back to from the elements of tab from the middle position.- Parameters:
tab
- array to reverse
-
reverse
public static <T> void reverse(T[] tab)
Turns back to from the elements of tab from the middle position.- Type Parameters:
T
- the class of the objects in the input array- Parameters:
tab
- array to reverse
-
permutation
public static <T> void permutation(int[] permutation, T[] tab)
Permutes elements from tab wrt to permutuation: tab[i] = tab[permutation[i]].- Type Parameters:
T
- the class of the objects in the input array- Parameters:
permutation
- permutationtab
- array of ints
-
toList
public static <T> List<T> toList(T[] array)
Returns a list composed of elements from array.- Type Parameters:
T
- the class of the objects in the input array- Parameters:
array
- array of elements- Returns:
- a list composed of elements from array.
-
toArray
public static <T> T[] toArray(Class c, List<T> list)
Returns an array composed of elements from list.- Type Parameters:
T
- the class of the objects in the input array- Parameters:
c
- the class of the copy to be returnedlist
- list of elements- Returns:
- an array composed of elements from list.
-
toArray
@SafeVarargs public static <T> T[] toArray(T... elt)
Creates an array from vargs elt.- Type Parameters:
T
- the class of the objects in the returned array- Parameters:
elt
- elements to put in an array- Returns:
- an array from vargs elt
-
toArray
public static <T> T[] toArray(List<T> list)
Creates an array from elements in list.- Type Parameters:
T
- the class of the objects in the returned array- Parameters:
list
- elements to put in an array- Returns:
- an array from element in list
-
transpose
public static <T> T[][] transpose(T[][] matrix)
Transposes a matrix M[n][m] in a matrix MT[m][n] such that MT[i][j] = M[j][i]- Type Parameters:
T
- the class of the objects in the input matrix- Parameters:
matrix
- matrix to transpose- Returns:
- a matrix
-
transpose
public static int[][] transpose(int[][] matrix)
Transposes a matrix M[n][m] in a matrix MT[m][n] such that MT[i][j] = M[j][i]- Parameters:
matrix
- matrix to transpose- Returns:
- a matrix
-
flatten
public static <T> T[] flatten(T[][] matrix)
Flattens a matrix M[n][m] in an array F[n*m] such that F[i*n+j] = M[i][j].- Type Parameters:
T
- the class of the objects in the input matrix- Parameters:
matrix
- matrix to flatten- Returns:
- a matrix
-
flatten
public static <T> T[] flatten(T[][][] matrix)
Flattens a matrix M[n][m][p] in an array F[n*m*p] such that F[(i*n*m) + (j*m) + k] = M[i][j][k].- Type Parameters:
T
- the class of the objects in the input matrix- Parameters:
matrix
- matrix to flatten- Returns:
- a matrix
-
flattenSubMatrix
public static <T> T[] flattenSubMatrix(int iMin, int iLength, int jMin, int jLength, T[][] matrix)
-
flatten
public static int[] flatten(int[][] matrix)
Flattens a matrix M[n][m] in an array F[n*m] such that F[i*n+j] = M[i][j].- Parameters:
matrix
- matrix to flatten- Returns:
- a matrix
-
createNonRedundantSortedValues
public static int[] createNonRedundantSortedValues(gnu.trove.list.array.TIntArrayList values)
-
sort
public static <T extends Comparable<T>> T[] sort(Set<T> set)
Create an array of elements in set and sort them usingArrays.sort(Object[])
- Type Parameters:
T
- the class of the objects in the input set.- Parameters:
set
- set of elements- Returns:
- an array of sorted elements from set
-
swallowCopy
public static int[][][][] swallowCopy(int[][][][] arr)
Duplicates arr and returns the new copy- Parameters:
arr
- matrix to duplicate- Returns:
- a copy of arr
-
swallowCopy
public static int[][][] swallowCopy(int[][][] arr)
Duplicates arr and returns the new copy- Parameters:
arr
- matrix to duplicate- Returns:
- a copy of arr
-
zeroToNShuffle
public static int[] zeroToNShuffle(int nb)
Creates and returns an array of ints composed of unique values from 0 (inclusive) to nb (exclusive), in random order.- Parameters:
nb
- upper value (exclusive)- Returns:
- an array of ints composed of unique values from 0 (inclusive) to nb (exclusive), in random order.
-
zeroToNShuffle
public static int[] zeroToNShuffle(int nb, long seed)
Creates and returns an array of ints composed of unique values from 0 (inclusive) to nb (exclusive), in random order.- Parameters:
nb
- upper value (exclusive)seed
- seed for randomness- Returns:
- an array of ints composed of unique values from 0 (inclusive) to nb (exclusive), in random order.
-
randomPermutations
public static void randomPermutations(int[] tab, Random r)
Permutes randomly elements from tab- Parameters:
tab
- array of intsr
- randomness generator
-
randomPermutations
public static void randomPermutations(int[] tab, long seed)
Permutes randomly elements from tab- Parameters:
tab
- array of intsseed
- seed for randomness
-
randomPermutations
public static <E> void randomPermutations(E[] tab, Random r)
Permutes randomly elements from tab- Type Parameters:
E
- the class of the objects in the input array- Parameters:
tab
- array of intsr
- randomness generator
-
randomPermutations
public static <E> void randomPermutations(E[] tab, long seed)
Permutes randomly elements from tab- Type Parameters:
E
- the class of the objects in the input array- Parameters:
tab
- array of intsseed
- seed for randomness
-
binarySearchInc
public static int binarySearchInc(int[] a, int fromIndex, int toIndex, int key, boolean gq)
Adapted from java.util.Arrays#binarySearch0(int[], int, int, int) , it returns the value greater or equal to key in an increasing order value array If the key exists in a, it returns the index of key in a, otherwise it returns the index of the closest value greater than key when gq is set to true, or the index of the closest value smaller than key when gq is set to false.- Parameters:
a
- the values, increasingly orderedfromIndex
- starting index (inclusive)toIndex
- ending index (exclusive)key
- value to look forgq
- set to true to look for the value greater or equal to key, false to look for the value smaller or equal to the key
-
mergeAndSortIfNot
public static int[] mergeAndSortIfNot(int[] values)
Sorts the input array if it is not already sorted, and removes multiple occurrences of the same value- Parameters:
values
- array of values- Returns:
- a sorted array containing each value of values exactly once
-
-