public class ByteBigArrays extends Object
In particular, the ensureCapacity()
, grow()
, trim()
and setLength()
methods allow to handle big arrays much like array lists.
Note that BinIO
and TextIO
contain several methods that 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.
BigArrays
Modifier and Type | Field and Description |
---|---|
static byte[][] |
EMPTY_BIG_ARRAY
A static, final, empty big array.
|
static Hash.Strategy |
HASH_STRATEGY
A type-specific content-based hash strategy for big arrays.
|
Modifier and Type | Method and Description |
---|---|
static void |
add(byte[][] array,
long index,
byte incr)
Adds the specified increment the element of the given big array of specified index.
|
static long |
binarySearch(byte[][] a,
byte key)
Searches a big array for the specified value using the binary search algorithm.
|
static long |
binarySearch(byte[][] a,
byte key,
ByteComparator c)
Searches a big array for the specified value using the binary search algorithm and a specified comparator.
|
static long |
binarySearch(byte[][] a,
long from,
long to,
byte key)
Searches a range of the specified big array for the specified value using the binary search algorithm.
|
static long |
binarySearch(byte[][] a,
long from,
long to,
byte key,
ByteComparator c)
Searches a range of the specified big array for the specified value using the binary search algorithm and a specified comparator.
|
static byte[][] |
copy(byte[][] array)
Returns a copy of a big array.
|
static void |
copy(byte[][] srcArray,
long srcPos,
byte[][] destArray,
long destPos,
long length)
Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination big array.
|
static byte[][] |
copy(byte[][] array,
long offset,
long length)
Returns a copy of a portion of a big array.
|
static void |
copyFromBig(byte[][] srcArray,
long srcPos,
byte[] destArray,
int destPos,
int length)
Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination array.
|
static void |
copyToBig(byte[] srcArray,
int srcPos,
byte[][] destArray,
long destPos,
long length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination big array.
|
static void |
decr(byte[][] array,
long index)
Decrements the element of the given big array of specified index.
|
static byte[][] |
ensureCapacity(byte[][] array,
long length)
Ensures that a big array can contain the given number of entries.
|
static byte[][] |
ensureCapacity(byte[][] array,
long length,
long preserve)
Ensures that a big array can contain the given number of entries, preserving just a part of the big array.
|
static void |
ensureFromTo(byte[][] a,
long from,
long to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits a big array.
|
static void |
ensureOffsetLength(byte[][] a,
long offset,
long length)
Ensures that a range given by an offset and a length fits a big array.
|
static boolean |
equals(byte[][] a1,
byte[][] a2)
Returns true if the two big arrays are elementwise equal.
|
static void |
fill(byte[][] array,
byte value)
Fills the given big array with the given value.
|
static void |
fill(byte[][] array,
long from,
long to,
byte value)
Fills a portion of the given big array with the given value.
|
static byte |
get(byte[][] array,
long index)
Returns the element of the given big array of specified index.
|
static byte[][] |
grow(byte[][] array,
long length)
Grows the given big 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 byte[][] |
grow(byte[][] array,
long length,
long preserve)
Grows the given big 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 big array.
|
static void |
incr(byte[][] array,
long index)
Increments the element of the given big array of specified index.
|
static long |
length(byte[][] array)
Returns the length of the given big array.
|
static void |
mul(byte[][] array,
long index,
byte factor)
Multiplies by the specified factor the element of the given big array of specified index.
|
static byte[][] |
newBigArray(long length)
Creates a new big array.
|
static void |
quickSort(byte[][] x)
Sorts the specified big array according to the natural ascending order using quicksort.
|
static void |
quickSort(byte[][] x,
ByteComparator comp)
Sorts the specified big array according to the order induced by the specified comparator using quicksort.
|
static void |
quickSort(byte[][] x,
long from,
long to)
Sorts the specified range of elements according to the natural ascending order using quicksort.
|
static void |
quickSort(byte[][] x,
long from,
long to,
ByteComparator comp)
Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.
|
static void |
radixSort(byte[][] a)
Sorts the specified big array using radix sort.
|
static void |
radixSort(byte[][] a,
byte[][] b)
Sorts the specified pair of big arrays lexicographically using radix sort.
|
static void |
radixSort(byte[][] a,
byte[][] b,
long from,
long to)
Sorts the specified pair of big arrays lexicographically using radix sort.
|
static void |
radixSort(byte[][] a,
long from,
long to)
Sorts the specified big array using radix sort.
|
static void |
set(byte[][] array,
long index,
byte value)
Sets the element of the given big array of specified index.
|
static byte[][] |
setLength(byte[][] array,
long length)
Sets the length of the given big array.
|
static byte[][] |
shuffle(byte[][] a,
long from,
long to,
Random random)
Shuffles the specified big array fragment using the specified pseudorandom number generator.
|
static byte[][] |
shuffle(byte[][] a,
Random random)
Shuffles the specified big array using the specified pseudorandom number generator.
|
static void |
swap(byte[][] array,
long first,
long second)
Swaps the element of the given big array of specified indices.
|
static String |
toString(byte[][] a) |
static byte[][] |
trim(byte[][] array,
long length)
Trims the given big array to the given length.
|
static byte[][] |
wrap(byte[] array)
Turns a standard array into a big array.
|
public static final byte[][] EMPTY_BIG_ARRAY
public static final Hash.Strategy HASH_STRATEGY
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.
public static byte get(byte[][] array, long index)
array
- a big array.index
- a position in the big array.public static void set(byte[][] array, long index, byte value)
array
- a big array.index
- a position in the big array.value
- the new value for the array element at the specified position.public static void swap(byte[][] array, long first, long second)
array
- a big array.first
- a position in the big array.second
- a position in the big array.public static void add(byte[][] array, long index, byte incr)
array
- a big array.index
- a position in the big array.incr
- the incrementpublic static void mul(byte[][] array, long index, byte factor)
array
- a big array.index
- a position in the big array.factor
- the factorpublic static void incr(byte[][] array, long index)
array
- a big array.index
- a position in the big array.public static void decr(byte[][] array, long index)
array
- a big array.index
- a position in the big array.public static long length(byte[][] array)
array
- a big array.public static void copy(byte[][] srcArray, long srcPos, byte[][] destArray, long destPos, long length)
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.public static void copyFromBig(byte[][] srcArray, long srcPos, byte[] destArray, int destPos, int length)
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.public static void copyToBig(byte[] srcArray, int srcPos, byte[][] destArray, long destPos, long length)
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.public static byte[][] newBigArray(long length)
length
- the length of the new big array.public static byte[][] wrap(byte[] array)
Note that the returned big array might contain as a segment the original array.
array
- an array.array
.public static byte[][] ensureCapacity(byte[][] array, long length)
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.
array
- a big array.length
- the new minimum length for this big array.array
, if it contains length
entries or more; otherwise, a big array with length
entries whose first length(array)
entries are the
same as those of array
.public static byte[][] ensureCapacity(byte[][] array, long length, long preserve)
Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.
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.array
, if it can contain length
entries or more; otherwise, a big array with length
entries whose first preserve
entries are the same
as those of array
.public static byte[][] grow(byte[][] array, long 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.
array
- a big array.length
- the new minimum length for this big array.array
, if it can contain length
entries; otherwise, a big array with max(length
,length(array)
/φ) entries whose first
length(array)
entries are the same as those of array
.public static byte[][] grow(byte[][] array, long length, long preserve)
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.
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.array
, if it can contain length
entries; otherwise, a big array with max(length
,length(array)
/φ) entries whose first
preserve
entries are the same as those of array
.public static byte[][] trim(byte[][] array, long 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.
array
- a big array.length
- the new maximum length for the big array.array
, if it contains length
entries or less; otherwise, a big array with length
entries whose entries are the same as the first
length
entries of array
.public static byte[][] setLength(byte[][] array, long 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.
array
- a big array.length
- the new length for the big array.array
, if it contains exactly length
entries; otherwise, if it contains more than length
entries, a big array with length
entries whose entries are the same as the first length
entries of array
; otherwise, a big array with length
entries whose first length(array)
entries are the same as those of array
.public static byte[][] copy(byte[][] array, long offset, long length)
array
- a big array.offset
- the first element to copy.length
- the number of elements to copy.length
elements of array
starting at offset
.public static byte[][] copy(byte[][] array)
array
- a big array.array
.public static void fill(byte[][] array, byte value)
This method uses a backward loop. It is significantly faster than the corresponding method in Arrays
.
array
- a big array.value
- the new value for all elements of the big array.public static void fill(byte[][] array, long from, long to, byte 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
- 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.public static boolean equals(byte[][] a1, byte[][] a2)
This method uses a backward loop. It is significantly faster than the corresponding method in Arrays
.
a1
- a big array.a2
- another big array.public static String toString(byte[][] a)
public static void ensureFromTo(byte[][] a, long from, long to)
This method may be used whenever a big array range check is needed.
a
- a big array.from
- a start index (inclusive).to
- an end index (inclusive).IllegalArgumentException
- if from
is greater than to
.ArrayIndexOutOfBoundsException
- if from
or to
are greater than the big array length or negative.public static void ensureOffsetLength(byte[][] a, long offset, long length)
This method may be used whenever a big array range check is needed.
a
- a big 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 big array length.public static void quickSort(byte[][] x, long from, long to, ByteComparator 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 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.public static void quickSort(byte[][] x, ByteComparator 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 big array to be sorted.comp
- the comparator to determine the sorting order.public static void quickSort(byte[][] x, long from, long 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 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.public static void quickSort(byte[][] 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 big array to be sorted.public static long binarySearch(byte[][] a, long from, long to, byte key)
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.Arrays
public static long binarySearch(byte[][] a, byte key)
a
- the big array to be searched.key
- the value to be searched for.Arrays
public static long binarySearch(byte[][] a, long from, long to, byte key, ByteComparator c)
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.Arrays
public static long binarySearch(byte[][] a, byte key, ByteComparator c)
a
- the big array to be searched.key
- the value to be searched for.c
- a comparator.Arrays
public static void radixSort(byte[][] 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 big array to be sorted.public static void radixSort(byte[][] a, long from, long 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 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.public static void radixSort(byte[][] a, byte[][] 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 big array to be sorted.b
- the second big array to be sorted.public static void radixSort(byte[][] a, byte[][] b, long from, long 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 big array to be sorted.b
- the second 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.public static byte[][] shuffle(byte[][] a, long from, long to, Random random)
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 (please use a XorShift* generator).a
.