public class DoubleArrays extends Object
In particular, the ensureCapacity()
, grow()
,
trim()
and setLength()
methods allow to handle
arrays much like array lists. This can be very useful when efficiency (or
syntactic simplicity) reasons make array lists unsuitable.
Note that BinIO
and TextIO
contain several methods make it possible to load and save arrays of primitive types as sequences
of elements in DataInput
format (i.e., not as objects) or as sequences of lines of text.
Arrays
Modifier and Type | Field and Description |
---|---|
static double[] |
EMPTY_ARRAY
A static, final, empty array.
|
static Hash.Strategy<double[]> |
HASH_STRATEGY
A type-specific content-based hash strategy for arrays.
|
Modifier and Type | Method and Description |
---|---|
static int |
binarySearch(double[] a,
double key)
Searches an array for the specified value using
the binary search algorithm.
|
static int |
binarySearch(double[] a,
double key,
DoubleComparator c)
Searches an array for the specified value using
the binary search algorithm and a specified comparator.
|
static int |
binarySearch(double[] a,
int from,
int to,
double key)
Searches a range of the specified array for the specified value using
the binary search algorithm.
|
static int |
binarySearch(double[] a,
int from,
int to,
double key,
DoubleComparator c)
Searches a range of the specified array for the specified value using
the binary search algorithm and a specified comparator.
|
static double[] |
copy(double[] array)
Returns a copy of an array.
|
static double[] |
copy(double[] array,
int offset,
int length)
Returns a copy of a portion of an array.
|
static double[] |
ensureCapacity(double[] array,
int length)
Ensures that an array can contain the given number of entries.
|
static double[] |
ensureCapacity(double[] array,
int length,
int preserve)
Ensures that an array can contain the given number of entries, preserving just a part of the array.
|
static void |
ensureFromTo(double[] a,
int from,
int to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.
|
static void |
ensureOffsetLength(double[] a,
int offset,
int length)
Ensures that a range given by an offset and a length fits an array.
|
static boolean |
equals(double[] a1,
double[] a2)
Deprecated.
Please use the corresponding
Arrays method, which is intrinsified in recent JVMs. |
static void |
fill(double[] array,
double value)
Fills the given array with the given value.
|
static void |
fill(double[] array,
int from,
int to,
double value)
Fills a portion of the given array with the given value.
|
static double[] |
grow(double[] array,
int length)
Grows the given array to the maximum between the given length and
the current length multiplied by two, provided that the given
length is larger than the current length.
|
static double[] |
grow(double[] array,
int length,
int preserve)
Grows the given array to the maximum between the given length and
the current length multiplied by two, provided that the given
length is larger than the current length, preserving just a part of the array.
|
static void |
mergeSort(double[] a)
Sorts an array according to the natural ascending order using mergesort.
|
static void |
mergeSort(double[] a,
DoubleComparator comp)
Sorts an array according to the order induced by the specified
comparator using mergesort.
|
static void |
mergeSort(double[] a,
int from,
int to)
Sorts the specified range of elements according to the natural ascending order using mergesort.
|
static void |
mergeSort(double[] a,
int from,
int to,
double[] supp)
Sorts the specified range of elements according to the natural ascending order using mergesort, using a given support array.
|
static void |
mergeSort(double[] a,
int from,
int to,
DoubleComparator comp)
Sorts the specified range of elements according to the order induced by the specified
comparator using mergesort.
|
static void |
mergeSort(double[] a,
int from,
int to,
DoubleComparator comp,
double[] supp)
Sorts the specified range of elements according to the order induced by the specified
comparator using mergesort, using a given support array.
|
static void |
quickSort(double[] x)
Deprecated.
Use the corresponding
sort() method in Arrays . |
static void |
quickSort(double[] x,
DoubleComparator comp)
Sorts an array according to the order induced by the specified
comparator using quicksort.
|
static void |
quickSort(double[] x,
int from,
int to)
Deprecated.
Use the corresponding
sort() method in Arrays . |
static void |
quickSort(double[] x,
int from,
int to,
DoubleComparator comp)
Sorts the specified range of elements according to the order induced by the specified
comparator using quicksort.
|
static void |
radixSort(double[] a)
Sorts the specified array using radix sort.
|
static void |
radixSort(double[][] a)
Sorts the specified array of arrays lexicographically using radix sort.
|
static void |
radixSort(double[][] a,
int from,
int to)
Sorts the specified array of arrays lexicographically using radix sort.
|
static void |
radixSort(double[] a,
double[] b)
Sorts the specified pair of arrays lexicographically using radix sort.
|
static void |
radixSort(double[] a,
double[] b,
int from,
int to)
Sorts the specified pair of arrays lexicographically using radix sort.
|
static void |
radixSort(double[] a,
int from,
int to)
Sorts the specified array using radix sort.
|
static void |
radixSortIndirect(int[] perm,
double[] a,
boolean stable)
Sorts the specified array using indirect radix sort.
|
static void |
radixSortIndirect(int[] perm,
double[] a,
double[] b,
boolean stable)
Sorts the specified pair of arrays lexicographically using indirect radix sort.
|
static void |
radixSortIndirect(int[] perm,
double[] a,
double[] b,
int from,
int to,
boolean stable)
Sorts the specified pair of arrays lexicographically using indirect radix sort.
|
static void |
radixSortIndirect(int[] perm,
double[] a,
int from,
int to,
boolean stable)
Sorts the specified array using indirect radix sort.
|
static double[] |
reverse(double[] a)
Reverses the order of the elements in the specified array.
|
static double[] |
setLength(double[] array,
int length)
Sets the length of the given array.
|
static double[] |
shuffle(double[] a,
int from,
int to,
Random random)
Shuffles the specified array fragment using the specified pseudorandom number generator.
|
static double[] |
shuffle(double[] a,
Random random)
Shuffles the specified array using the specified pseudorandom number generator.
|
static double[] |
trim(double[] array,
int length)
Trims the given array to the given length.
|
public static final double[] EMPTY_ARRAY
public static final Hash.Strategy<double[]> HASH_STRATEGY
This hash strategy may be used in custom hash collections whenever keys are
arrays, and they must be considered equal by content. This strategy
will handle null
correctly, and it is serializable.
public static double[] ensureCapacity(double[] array, int length)
If you cannot foresee whether this array will need again to be
enlarged, you should probably use grow()
instead.
array
- an array.length
- the new minimum length for this array.array
, if it contains length
entries or more; otherwise,
an array with length
entries whose first array.length
entries are the same as those of array
.public static double[] ensureCapacity(double[] array, int length, int preserve)
array
- an array.length
- the new minimum length for this array.preserve
- the number of elements of the array that must be preserved in case a new allocation is necessary.array
, if it can contain length
entries or more; otherwise,
an array with length
entries whose first preserve
entries are the same as those of array
.public static double[] grow(double[] array, int length)
If you want complete control on the array growth, you
should probably use ensureCapacity()
instead.
array
- an array.length
- the new minimum length for this array.array
, if it can contain length
entries; otherwise, an array with
max(length
,array.length
/φ) entries whose first
array.length
entries are the same as those of array
.public static double[] grow(double[] array, int length, int preserve)
If you want complete control on the array growth, you
should probably use ensureCapacity()
instead.
array
- an array.length
- the new minimum length for this array.preserve
- the number of elements of the array that must be preserved in case a new allocation is necessary.array
, if it can contain length
entries; otherwise, an array with
max(length
,array.length
/φ) entries whose first
preserve
entries are the same as those of array
.public static double[] trim(double[] array, int length)
array
- an array.length
- the new maximum length for the array.array
, if it contains length
entries or less; otherwise, an array with
length
entries whose entries are the same as
the first length
entries of array
.public static double[] setLength(double[] array, int length)
array
- an array.length
- the new length for the array.array
, if it contains exactly length
entries; otherwise, if it contains more than
length
entries, an array with length
entries
whose entries are the same as the first length
entries of
array
; otherwise, an array with length
entries
whose first array.length
entries are the same as those of
array
.public static double[] copy(double[] array, int offset, int length)
array
- an array.offset
- the first element to copy.length
- the number of elements to copy.length
elements of array
starting at offset
.public static double[] copy(double[] array)
array
- an array.array
.public static void fill(double[] array, double value)
This method uses a backward loop. It is significantly faster than the corresponding
method in Arrays
.
array
- an array.value
- the new value for all elements of the array.public static void fill(double[] array, int from, int to, double 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 in Arrays
.
array
- an array.from
- the starting index of the portion to fill (inclusive).to
- the end index of the portion to fill (exclusive).value
- the new value for all elements of the specified portion of the array.@Deprecated public static boolean equals(double[] a1, double[] a2)
Arrays
method, which is intrinsified in recent JVMs.a1
- an array.a2
- another array.public static void ensureFromTo(double[] a, int from, int to)
This method may be used whenever an array range check is needed.
a
- an array.from
- a start index (inclusive).to
- an end index (exclusive).IllegalArgumentException
- if from
is greater than to
.ArrayIndexOutOfBoundsException
- if from
or to
are greater than the array length or negative.public static void ensureOffsetLength(double[] a, int offset, int length)
This method may be used whenever an array range check is needed.
a
- an array.offset
- a start index.length
- a length (the number of elements in the range).IllegalArgumentException
- if length
is negative.ArrayIndexOutOfBoundsException
- if offset
is negative or offset
+length
is greater than the array length.public static void quickSort(double[] x, int from, int to, DoubleComparator comp)
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.
x
- the 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.public static void quickSort(double[] x, DoubleComparator comp)
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.
x
- the array to be sorted.comp
- the comparator to determine the sorting order.@Deprecated public static void quickSort(double[] x, int from, int to)
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.
x
- the 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.@Deprecated public static void quickSort(double[] x)
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.
x
- the array to be sorted.public static void mergeSort(double[] a, int from, int to, double[] supp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. Moreover, no support arrays will be allocated.
a
- the 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.supp
- a support array containing at least to
elements.public static void mergeSort(double[] a, int from, int to)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the 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.public static void mergeSort(double[] a)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the array to be sorted.public static void mergeSort(double[] a, int from, int to, DoubleComparator comp, double[] supp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. Moreover, no support arrays will be allocated.
a
- the 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.supp
- a support array containing at least to
elements.public static void mergeSort(double[] a, int from, int to, DoubleComparator comp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the 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.public static void mergeSort(double[] a, DoubleComparator comp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the array to be sorted.comp
- the comparator to determine the sorting order.public static int binarySearch(double[] a, int from, int to, double key)
a
- the 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.Arrays
public static int binarySearch(double[] a, double key)
a
- the array to be searched.key
- the value to be searched for.Arrays
public static int binarySearch(double[] a, int from, int to, double key, DoubleComparator c)
a
- the 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.Arrays
public static int binarySearch(double[] a, double key, DoubleComparator c)
a
- the array to be searched.key
- the value to be searched for.c
- a comparator.Arrays
public static void radixSort(double[] a)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted.
a
- the array to be sorted.public static void radixSort(double[] a, int from, int to)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted.
a
- the 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.public static void radixSortIndirect(int[] perm, double[] a, boolean stable)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implement an indirect sort. The elements of perm
(which must
be exactly the numbers in the interval [0..perm.length)
) will be permuted so that
a[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.
This implementation is significantly faster than quicksort (unstable) or mergesort (stable)
already at small sizes (say, more than 10000 elements), but it can only
sort in ascending order.
It will allocate a support array of bytes with the same number of elements as the array to be sorted,
and, in the stable case, a further support array as large as perm
(note that the stable
version is slightly faster).
perm
- a permutation array indexing a
.a
- the array to be sorted.stable
- whether the sorting algorithm should be stable.public static void radixSortIndirect(int[] perm, double[] a, int from, int to, boolean stable)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implement an indirect sort. The elements of perm
(which must
be exactly the numbers in the interval [0..perm.length)
) will be permuted so that
a[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.
This implementation is significantly faster than quicksort (unstable) or mergesort (stable)
already at small sizes (say, more than 10000 elements), but it can only
sort in ascending order.
It will allocate a support array of bytes with the same number of elements as the array to be sorted,
and, in the stable case, a further support array as large as perm
(note that the stable
version is slightly faster).
perm
- a permutation array indexing a
.a
- the array to be sorted.from
- the index of the first element of perm
(inclusive) to be permuted.to
- the index of the last element of perm
(exclusive) to be permuted.stable
- whether the sorting algorithm should be stable.public static void radixSort(double[] a, double[] b)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implements a lexicographical sorting of the arguments. Pairs of elements
in the same position in the two provided arrays will be considered a single key, and permuted
accordingly. In the end, either a[ i ] < a[ i + 1 ]
or a[ i ] == a[ i + 1 ]
and b[ i ] <= b[ i + 1 ]
.
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
a
- the first array to be sorted.b
- the second array to be sorted.public static void radixSort(double[] a, double[] b, int from, int to)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implements a lexicographical sorting of the arguments. Pairs of elements
in the same position in the two provided arrays will be considered a single key, and permuted
accordingly. In the end, either a[ i ] < a[ i + 1 ]
or a[ i ] == a[ i + 1 ]
and b[ i ] <= b[ i + 1 ]
.
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
a
- the first array to be sorted.b
- the second 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.public static void radixSortIndirect(int[] perm, double[] a, double[] b, boolean stable)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implement an indirect sort. The elements of perm
(which must
be exactly the numbers in the interval [0..perm.length)
) will be permuted so that
a[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.
This implementation is significantly faster than quicksort (unstable) or mergesort (stable)
already at small sizes (say, more than 10000 elements), but it can only
sort in ascending order.
It will allocate a support array of bytes with the same number of elements as the array to be sorted,
and, in the stable case, a further support array as large as perm
(note that the stable
version is slightly faster).
perm
- a permutation array indexing a
.a
- the array to be sorted.b
- the second array to be sorted.stable
- whether the sorting algorithm should be stable.public static void radixSortIndirect(int[] perm, double[] a, double[] b, int from, int to, boolean stable)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implement an indirect sort. The elements of perm
(which must
be exactly the numbers in the interval [0..perm.length)
) will be permuted so that
a[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.
This implementation is significantly faster than quicksort (unstable) or mergesort (stable)
already at small sizes (say, more than 10000 elements), but it can only
sort in ascending order.
It will allocate a support array of bytes with the same number of elements as the array to be sorted,
and, in the stable case, a further support array as large as perm
(note that the stable
version is slightly faster).
perm
- a permutation array indexing a
.a
- the array to be sorted.b
- the second array to be sorted.from
- the index of the first element of perm
(inclusive) to be permuted.to
- the index of the last element of perm
(exclusive) to be permuted.stable
- whether the sorting algorithm should be stable.public static void radixSort(double[][] a)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implements a lexicographical sorting of the provided arrays. Tuples of elements in the same position will be considered a single key, and permuted accordingly.
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
a
- an array containing arrays of equal length to be sorted lexicographically in parallel.public static void radixSort(double[][] a, int from, int to)
The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implements a lexicographical sorting of the provided arrays. Tuples of elements in the same position will be considered a single key, and permuted accordingly.
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
a
- an array containing arrays of equal length to be sorted lexicographically in parallel.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.public static double[] shuffle(double[] a, int from, int to, Random random)
a
- the 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 (please use a XorShift* generator).a
.public static double[] shuffle(double[] a, Random random)
a
- the array to be shuffled.random
- a pseudorandom number generator (please use a XorShift* generator).a
.public static double[] reverse(double[] a)
a
- the array to be reversed.a
.