Class ObjectBigArrays
Note that BinIO
and TextIO
contain several methods make it possible to load and save big arrays of primitive types as
sequences of elements in DataInput
format (i.e., not as objects) or as sequences
of lines of text.
Parallel operations
Some algorithms provide a parallel version that will by default use the common pool, but this can be overridden by calling the function in a task already in theForkJoinPool
that the operation should run in. For
example, something along the lines of
"poolToParallelSortIn.invoke(() -> parallelQuickSort(arrayToSort))
" will run the parallel
sort in poolToParallelSortIn
instead of the default pool.
Warning: creating arrays using
reflection, as it happens in
ensureCapacity(Object[][],long,long)
and grow(Object[][],long,long)
, is
significantly slower than using new
. This phenomenon is particularly evident in
the first growth phases of an array reallocated with doubling (or similar) logic.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Object[][]
A static, final, empty big array to be used as default big array in allocations.static final Object[][]
A static, final, empty big array.static final Hash.Strategy
A type-specific content-based hash strategy for big arrays. -
Method Summary
Modifier and TypeMethodDescriptionstatic <K> long
binarySearch
(K[][] a, long from, long to, K key) Searches a range of the specified big array for the specified value using the binary search algorithm.static <K> long
binarySearch
(K[][] a, long from, long to, K key, Comparator<K> c) Searches a range of the specified big array for the specified value using the binary search algorithm and a specified comparator.static <K> long
binarySearch
(K[][] a, Object key) Searches a big array for the specified value using the binary search algorithm.static <K> long
binarySearch
(K[][] a, K key, Comparator<K> c) Searches a big array for the specified value using the binary search algorithm and a specified comparator.static <K> K[][]
copy
(K[][] array) Deprecated.static <K> K[][]
copy
(K[][] array, long offset, long length) Deprecated.Please use the version inBigArrays
.static <K> void
copy
(K[][] srcArray, long srcPos, K[][] destArray, long destPos, long length) Deprecated.Please use the version inBigArrays
.static <K> void
copyFromBig
(K[][] srcArray, long srcPos, K[] destArray, int destPos, int length) Deprecated.Please use the version inBigArrays
.static <K> void
copyToBig
(K[] srcArray, int srcPos, K[][] destArray, long destPos, long length) Deprecated.Please use the version inBigArrays
.static <K> K[][]
ensureCapacity
(K[][] array, long length) Deprecated.Please use the version inBigArrays
.static <K> K[][]
ensureCapacity
(K[][] array, long length, long preserve) Deprecated.Please use the version inBigArrays
.static <K> void
ensureFromTo
(K[][] a, long from, long to) Deprecated.Please use the version inBigArrays
.static <K> void
ensureOffsetLength
(K[][] a, long offset, long length) Deprecated.Please use the version inBigArrays
.static <K> void
ensureSameLength
(K[][] a, K[][] b) Deprecated.Please use the version inBigArrays
.static <K> boolean
equals
(K[][] a1, K[][] a2) Deprecated.Please use the version inBigArrays
.static <K> void
fill
(K[][] array, long from, long to, K value) Deprecated.Please use the version inBigArrays
.static <K> void
fill
(K[][] array, K value) Deprecated.Please use the version inBigArrays
.static <K> K[][]
forceCapacity
(K[][] array, long length, long preserve) Deprecated.Please use the version inBigArrays
.static <K> K
get
(K[][] array, long index) Deprecated.Please use the version inBigArrays
.static <K> K[][]
grow
(K[][] array, long length) Deprecated.Please use the version inBigArrays
.static <K> K[][]
grow
(K[][] array, long length, long preserve) Deprecated.Please use the version inBigArrays
.static <K> long
length
(K[][] array) Deprecated.Please use the version inBigArrays
.static Object[][]
newBigArray
(long length) Creates a new big array.static Object[][]
newBigArray
(Class<?> componentType, long length) Creates a new big array using a given component type.static <K> K[][]
newBigArray
(K[][] prototype, long length) Creates a new big array using the given one as prototype.static <K> void
parallelQuickSort
(K[][] x) Sorts a big array according to the natural ascending order using a parallel quicksort.static <K> void
parallelQuickSort
(K[][] x, long from, long to) Sorts the specified range of elements according to the natural ascending order using a parallel quicksort.static <K> void
parallelQuickSort
(K[][] x, long from, long to, Comparator<K> comp) Sorts the specified range of elements according to the order induced by the specified comparator using a parallel quicksort.static <K> void
parallelQuickSort
(K[][] x, Comparator<K> comp) Sorts a big array according to the order induced by the specified comparator using a parallel quicksort.static <K> void
quickSort
(K[][] x) Sorts the specified big array according to the natural ascending order using quicksort.static <K> void
quickSort
(K[][] x, long from, long to) Sorts the specified range of elements according to the natural ascending order using quicksort.static <K> void
quickSort
(K[][] x, long from, long to, Comparator<K> comp) Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.static <K> void
quickSort
(K[][] x, Comparator<K> comp) Sorts the specified big array according to the order induced by the specified comparator using quicksort.static <K> void
set
(K[][] array, long index, K value) Deprecated.Please use the version inBigArrays
.static <K> K[][]
setLength
(K[][] array, long length) Deprecated.Please use the version inBigArrays
.static <K> K[][]
Shuffles the specified big array fragment using the specified pseudorandom number generator.static <K> K[][]
Shuffles the specified big array using the specified pseudorandom number generator.static <K> void
swap
(K[][] array, long first, long second) Deprecated.Please use the version inBigArrays
.static <K> String
toString
(K[][] a) Deprecated.static <K> K[][]
trim
(K[][] array, long length) Deprecated.Please use the version inBigArrays
.static <K> K[][]
wrap
(K[] array) Deprecated.Please use the version inBigArrays
.
-
Field Details
-
EMPTY_BIG_ARRAY
A static, final, empty big array. -
DEFAULT_EMPTY_BIG_ARRAY
A static, final, empty big array to be used as default big array in allocations. An object distinct fromEMPTY_BIG_ARRAY
makes it possible to have different behaviors depending on whether the user required an empty allocation, or we are just lazily delaying allocation.- See Also:
-
HASH_STRATEGY
A type-specific content-based hash strategy for big arrays.This hash strategy may be used in custom hash collections whenever keys are big arrays, and they must be considered equal by content. This strategy will handle
null
correctly, and it is serializable.
-
-
Method Details
-
get
Deprecated.Please use the version inBigArrays
.Returns the element of the given big array of specified index.- Parameters:
array
- a big array.index
- a position in the big array.- Returns:
- the element of the big array at the specified position.
-
set
Deprecated.Please use the version inBigArrays
.Sets the element of the given big array of specified index.- Parameters:
array
- a big array.index
- a position in the big array.value
- the new value for the array element at the specified position.
-
swap
Deprecated.Please use the version inBigArrays
.Swaps the element of the given big array of specified indices.- Parameters:
array
- a big array.first
- a position in the big array.second
- a position in the big array.
-
length
Deprecated.Please use the version inBigArrays
.Returns the length of the given big array.- Parameters:
array
- a big array.- Returns:
- the length of the given big array.
-
copy
@Deprecated public static <K> void copy(K[][] srcArray, long srcPos, K[][] destArray, long destPos, long length) Deprecated.Please use the version inBigArrays
.Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination big array. Handles correctly overlapping regions of the same big array.- Parameters:
srcArray
- the source big array.srcPos
- the starting position in the source big array.destArray
- the destination big array.destPos
- the starting position in the destination data.length
- the number of elements to be copied.
-
copyFromBig
@Deprecated public static <K> void copyFromBig(K[][] srcArray, long srcPos, K[] destArray, int destPos, int length) Deprecated.Please use the version inBigArrays
.Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination array.- Parameters:
srcArray
- the source big array.srcPos
- the starting position in the source big array.destArray
- the destination array.destPos
- the starting position in the destination data.length
- the number of elements to be copied.
-
copyToBig
@Deprecated public static <K> void copyToBig(K[] srcArray, int srcPos, K[][] destArray, long destPos, long length) Deprecated.Please use the version inBigArrays
.Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination big array.- Parameters:
srcArray
- the source array.srcPos
- the starting position in the source array.destArray
- the destination big array.destPos
- the starting position in the destination data.length
- the number of elements to be copied.
-
newBigArray
public static <K> K[][] newBigArray(K[][] prototype, long length) Creates a new big array using the given one as prototype.This method returns a new big array of the given length whose element are of the same class as of those of
prototype
. In case of an empty big array, it tries to returnEMPTY_BIG_ARRAY
, if possible.- Parameters:
prototype
- a big array that will be used to type the new one.length
- the length of the new big array.- Returns:
- a new big array of given type and length.
-
newBigArray
Creates a new big array using a given component type.This method returns a new big array whose segments are of class
componentType
. In case of an empty big array, it tries to returnEMPTY_BIG_ARRAY
, if possible.- Parameters:
componentType
- a class representing the type of segments of the array to be created.length
- the length of the new big array.- Returns:
- a new big array of given type and length.
-
newBigArray
Creates a new big array.- Parameters:
length
- the length of the new big array.- Returns:
- a new big array of given length.
-
wrap
Deprecated.Please use the version inBigArrays
.Turns a standard array into a big array.Note that the returned big array might contain as a segment the original array.
- Parameters:
array
- an array.- Returns:
- a new big array with the same length and content of
array
.
-
ensureCapacity
Deprecated.Please use the version inBigArrays
.Ensures that a big array can contain the given number of entries.If you cannot foresee whether this big array will need again to be enlarged, you should probably use
grow()
instead.Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
- Parameters:
array
- a big array.length
- the new minimum length for this big array.- Returns:
array
, if it containslength
entries or more; otherwise, a big array withlength
entries whose firstlength(array)
entries are the same as those ofarray
.
-
forceCapacity
Deprecated.Please use the version inBigArrays
.Forces a big array to contain the given number of entries, preserving just a part of the big array.This method returns a new big array of the given length whose element are of the same class as of those of
array
.Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
- Parameters:
array
- a big array.length
- the new minimum length for this big array.preserve
- the number of elements of the big array that must be preserved in case a new allocation is necessary.- Returns:
- a big array with
length
entries whose firstpreserve
entries are the same as those ofarray
.
-
ensureCapacity
Deprecated.Please use the version inBigArrays
.Ensures that a big array can contain the given number of entries, preserving just a part of the big array.This method returns a new big array of the given length whose element are of the same class as of those of
array
.Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
- Parameters:
array
- a big array.length
- the new minimum length for this big array.preserve
- the number of elements of the big array that must be preserved in case a new allocation is necessary.- Returns:
array
, if it can containlength
entries or more; otherwise, a big array withlength
entries whose firstpreserve
entries are the same as those ofarray
.
-
grow
Deprecated.Please use the version inBigArrays
.Grows the given big array to the maximum between the given length and the current length increased by 50%, provided that the given length is larger than the current length.If you want complete control on the big array growth, you should probably use
ensureCapacity()
instead.Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
- Parameters:
array
- a big array.length
- the new minimum length for this big array.- Returns:
array
, if it can containlength
entries; otherwise, a big array with max(length
,length(array)
/φ) entries whose firstlength(array)
entries are the same as those ofarray
.
-
grow
Deprecated.Please use the version inBigArrays
.Grows the given big array to the maximum between the given length and the current length increased by 50%, provided that the given length is larger than the current length, preserving just a part of the big array.If you want complete control on the big array growth, you should probably use
ensureCapacity()
instead.Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
- Parameters:
array
- a big array.length
- the new minimum length for this big array.preserve
- the number of elements of the big array that must be preserved in case a new allocation is necessary.- Returns:
array
, if it can containlength
entries; otherwise, a big array with max(length
,length(array)
/φ) entries whose firstpreserve
entries are the same as those ofarray
.
-
trim
Deprecated.Please use the version inBigArrays
.Trims the given big array to the given length.Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
- Parameters:
array
- a big array.length
- the new maximum length for the big array.- Returns:
array
, if it containslength
entries or less; otherwise, a big array withlength
entries whose entries are the same as the firstlength
entries ofarray
.
-
setLength
Deprecated.Please use the version inBigArrays
.Sets the length of the given big array.Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
- Parameters:
array
- a big array.length
- the new length for the big array.- Returns:
array
, if it contains exactlylength
entries; otherwise, if it contains more thanlength
entries, a big array withlength
entries whose entries are the same as the firstlength
entries ofarray
; otherwise, a big array withlength
entries whose firstlength(array)
entries are the same as those ofarray
.
-
copy
Deprecated.Please use the version inBigArrays
.Returns a copy of a portion of a big array.- Parameters:
array
- a big array.offset
- the first element to copy.length
- the number of elements to copy.- Returns:
- a new big array containing
length
elements ofarray
starting atoffset
.
-
copy
Deprecated.Please use the version inBigArrays
.Returns a copy of a big array.- Parameters:
array
- a big array.- Returns:
- a copy of
array
.
-
fill
Deprecated.Please use the version inBigArrays
.Fills the given big array with the given value.This method uses a backward loop. It is significantly faster than the corresponding method in
Arrays
.- Parameters:
array
- a big array.value
- the new value for all elements of the big array.
-
fill
Deprecated.Please use the version inBigArrays
.Fills a portion of the given big array with the given value.If possible (i.e.,
from
is 0) this method uses a backward loop. In this case, it is significantly faster than the corresponding method inArrays
.- Parameters:
array
- a big array.from
- the starting index of the portion to fill.to
- the end index of the portion to fill.value
- the new value for all elements of the specified portion of the big array.
-
equals
Deprecated.Please use the version inBigArrays
.Returns true if the two big arrays are elementwise equal.This method uses a backward loop. It is significantly faster than the corresponding method in
Arrays
.- Parameters:
a1
- a big array.a2
- another big array.- Returns:
- true if the two big arrays are of the same length, and their elements are equal.
-
toString
Deprecated. -
ensureFromTo
Deprecated.Please use the version inBigArrays
.Ensures that a range given by its first (inclusive) and last (exclusive) elements fits a big array.This method may be used whenever a big array range check is needed.
- Parameters:
a
- a big array.from
- a start index (inclusive).to
- an end index (inclusive).- Throws:
IllegalArgumentException
- iffrom
is greater thanto
.ArrayIndexOutOfBoundsException
- iffrom
orto
are greater than the big array length or negative.
-
ensureOffsetLength
Deprecated.Please use the version inBigArrays
.Ensures that a range given by an offset and a length fits a big array.This method may be used whenever a big array range check is needed.
- Parameters:
a
- a big array.offset
- a start index.length
- a length (the number of elements in the range).- Throws:
IllegalArgumentException
- iflength
is negative.ArrayIndexOutOfBoundsException
- ifoffset
is negative oroffset
+length
is greater than the big array length.
-
ensureSameLength
Deprecated.Please use the version inBigArrays
.Ensures that two big arrays are of the same length.- Parameters:
a
- a big array.b
- another big array.- Throws:
IllegalArgumentException
- if the two argument arrays are not of the same length.
-
quickSort
Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.comp
- the comparator to determine the sorting order.
-
quickSort
Sorts the specified big array according to the order induced by the specified comparator using quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.comp
- the comparator to determine the sorting order.
-
quickSort
public static <K> void quickSort(K[][] x, long from, long to) Sorts the specified range of elements according to the natural ascending order using quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.
-
quickSort
public static <K> void quickSort(K[][] x) Sorts the specified big array according to the natural ascending order using quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.
-
parallelQuickSort
public static <K> void parallelQuickSort(K[][] x, long from, long to) Sorts the specified range of elements according to the natural ascending order using a parallel quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.
-
parallelQuickSort
public static <K> void parallelQuickSort(K[][] x) Sorts a big array according to the natural ascending order using a parallel quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.
-
parallelQuickSort
Sorts the specified range of elements according to the order induced by the specified comparator using a parallel quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.comp
- the comparator to determine the sorting order.
-
parallelQuickSort
Sorts a big array according to the order induced by the specified comparator using a parallel quicksort.The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
- Parameters:
x
- the big array to be sorted.comp
- the comparator to determine the sorting order.
-
binarySearch
public static <K> long binarySearch(K[][] a, long from, long to, K key) Searches a range of the specified big array for the specified value using the binary search algorithm. The range must be sorted prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
a
- the big array to be searched.from
- the index of the first element (inclusive) to be searched.to
- the index of the last element (exclusive) to be searched.key
- the value to be searched for.- Returns:
- index of the search key, if it is contained in the big array; otherwise,
(-(insertion point) - 1)
. The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. - See Also:
-
binarySearch
Searches a big array for the specified value using the binary search algorithm. The range must be sorted prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
a
- the big array to be searched.key
- the value to be searched for.- Returns:
- index of the search key, if it is contained in the big array; otherwise,
(-(insertion point) - 1)
. The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. - See Also:
-
binarySearch
Searches a range of the specified big array for the specified value using the binary search algorithm and a specified comparator. The range must be sorted following the comparator prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
a
- the big array to be searched.from
- the index of the first element (inclusive) to be searched.to
- the index of the last element (exclusive) to be searched.key
- the value to be searched for.c
- a comparator.- Returns:
- index of the search key, if it is contained in the big array; otherwise,
(-(insertion point) - 1)
. The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. - See Also:
-
binarySearch
Searches a big array for the specified value using the binary search algorithm and a specified comparator. The range must be sorted following the comparator prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
a
- the big array to be searched.key
- the value to be searched for.c
- a comparator.- Returns:
- index of the search key, if it is contained in the big array; otherwise,
(-(insertion point) - 1)
. The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. - See Also:
-
shuffle
Shuffles the specified big array fragment using the specified pseudorandom number generator.- Parameters:
a
- the big array to be shuffled.from
- the index of the first element (inclusive) to be shuffled.to
- the index of the last element (exclusive) to be shuffled.random
- a pseudorandom number generator.- Returns:
a
.
-
shuffle
Shuffles the specified big array using the specified pseudorandom number generator.- Parameters:
a
- the big array to be shuffled.random
- a pseudorandom number generator.- Returns:
a
.
-
BigArrays
.