|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectit.unimi.dsi.fastutil.objects.ObjectBigArrays
public class ObjectBigArrays
A class providing static methods and objects that do useful things with big arrays.
In particular, the ensureCapacity()
, grow()
,
trim()
and setLength()
methods allow to handle
arrays much like array lists.
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.
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.
BigArrays
Field Summary | |
---|---|
static Object[][] |
EMPTY_BIG_ARRAY
A static, final, empty big array. |
static Hash.Strategy |
HASH_STRATEGY
A type-specific content-based hash strategy for big arrays. |
static long |
ONEOVERPHI
The inverse of the golden ratio times 216. |
Method Summary | ||
---|---|---|
static
|
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
|
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
|
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
|
binarySearch(K[][] a,
Object key)
Searches a big array for the specified value using the binary search algorithm. |
|
static
|
copy(K[][] array)
Returns a copy of a big array. |
|
static
|
copy(K[][] srcArray,
long srcPos,
K[][] 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
|
copy(K[][] array,
long offset,
long length)
Returns a copy of a portion of a big array. |
|
static
|
copyFromBig(K[][] srcArray,
long srcPos,
K[] 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
|
copyToBig(K[] srcArray,
int srcPos,
K[][] 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
|
ensureCapacity(K[][] array,
long length)
Ensures that a big array can contain the given number of entries. |
|
static
|
ensureCapacity(K[][] 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
|
ensureFromTo(K[][] a,
long from,
long to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits a big array. |
|
static
|
ensureOffsetLength(K[][] a,
long offset,
long length)
Ensures that a range given by an offset and a length fits a big array. |
|
static
|
equals(K[][] a1,
K[][] a2)
Returns true if the two big arrays are elementwise equal. |
|
static
|
fill(K[][] array,
K value)
Fills the given big array with the given value. |
|
static
|
fill(K[][] array,
long from,
long to,
K value)
Fills a portion of the given big array with the given value. |
|
static
|
get(K[][] array,
long index)
Returns the element of the given big array of specified index. |
|
static
|
grow(K[][] array,
long length)
Grows the given big array to the maximum between the given length and the current length divided by the golden ratio, provided that the given length is larger than the current length. |
|
static
|
grow(K[][] array,
long length,
long preserve)
Grows the given big array to the maximum between the given length and the current length divided by the golden ratio, provided that the given length is larger than the current length, preserving just a part of the big array. |
|
static
|
length(K[][] array)
Returns the length of the given big array. |
|
static
|
newBigArray(K[][] prototype,
long length)
Creates a new big array using the given one as prototype. |
|
static Object[][] |
newBigArray(long length)
Creates a new big array. |
|
static
|
quickSort(K[][] x)
Sorts the specified big array according to the natural ascending order using quicksort. |
|
static
|
quickSort(K[][] x,
Comparator<K> comp)
Sorts the specified big array according to the order induced by the specified comparator using quicksort. |
|
static
|
quickSort(K[][] x,
long from,
long to)
Sorts the specified range of elements according to the natural ascending order using quicksort. |
|
static
|
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
|
set(K[][] array,
long index,
K value)
Sets the element of the given big array of specified index. |
|
static
|
setLength(K[][] array,
long length)
Sets the length of the given big array. |
|
static
|
shuffle(K[][] a,
long from,
long to,
Random random)
Shuffles the specified big array fragment using the specified pseudorandom number generator. |
|
static
|
shuffle(K[][] a,
Random random)
Shuffles the specified big array using the specified pseudorandom number generator. |
|
static
|
swap(K[][] array,
long first,
long second)
Swaps the element of the given big array of specified indices. |
|
static
|
toString(K[][] a)
|
|
static
|
trim(K[][] array,
long length)
Trims the given big array to the given length. |
|
static
|
wrap(K[] array)
Turns a standard array into a big array. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final long ONEOVERPHI
public static final Object[][] 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.
Method Detail |
---|
public static <K> K get(K[][] array, long index)
array
- a big array.index
- a position in the big array.
public static <K> void set(K[][] array, long index, K value)
array
- a big array.index
- a position in the big array.public static <K> void swap(K[][] 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 <K> long length(K[][] array)
array
- a big array.
public static <K> void copy(K[][] srcArray, long srcPos, K[][] 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 <K> void copyFromBig(K[][] srcArray, long srcPos, K[] 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 <K> void copyToBig(K[] srcArray, int srcPos, K[][] 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 <K> K[][] newBigArray(K[][] prototype, long length)
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 return EMPTY_BIG_ARRAY
, if possible.
prototype
- a big array that will be used to type the new one.length
- the length of the new big array.
public static Object[][] newBigArray(long length)
length
- the length of the new big array.
public static <K> K[][] wrap(K[] array)
Note that the returned big array might contain as a segment the original array.
array
- an array.
array
.public static <K> K[][] ensureCapacity(K[][] 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 <K> K[][] ensureCapacity(K[][] array, long length, long preserve)
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.
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 <K> K[][] grow(K[][] array, long length)
Dividing by the golden ratio (φ) approximately increases the big array
length by 1.618. 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 <K> K[][] grow(K[][] array, long length, long preserve)
Dividing by the golden ratio (φ) approximately increases the big array
length by 1.618. 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 <K> K[][] trim(K[][] 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 <K> K[][] setLength(K[][] 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 <K> K[][] copy(K[][] 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 <K> K[][] copy(K[][] array)
array
- a big array.
array
.public static <K> void fill(K[][] array, K 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 <K> void fill(K[][] array, long from, long to, K 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 <K> boolean equals(K[][] a1, K[][] 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 <K> String toString(K[][] a)
public static <K> void ensureFromTo(K[][] 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 <K> void ensureOffsetLength(K[][] 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 <K> void quickSort(K[][] x, long from, long to, Comparator<K> 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 <K> void quickSort(K[][] x, Comparator<K> 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 <K> void quickSort(K[][] 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 <K> void quickSort(K[][] 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 <K> long binarySearch(K[][] a, long from, long to, K 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 <K> long binarySearch(K[][] a, Object key)
a
- the big array to be searched.key
- the value to be searched for.
Arrays
public static <K> long binarySearch(K[][] a, long from, long to, K key, Comparator<K> 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 <K> long binarySearch(K[][] a, K key, Comparator<K> c)
a
- the big array to be searched.key
- the value to be searched for.c
- a comparator.
Arrays
public static <K> K[][] shuffle(K[][] 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
.public static <K> K[][] shuffle(K[][] a, Random random)
a
- the big array to be shuffled.random
- a pseudorandom number generator (please use a XorShift* generator).
a
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |