Class BooleanBigArrays
- java.lang.Object
-
- it.unimi.dsi.fastutil.booleans.BooleanBigArrays
-
public final class BooleanBigArrays extends java.lang.Object
A class providing static methods and objects that do useful things with big arrays.In particular, the
forceCapacity()
,ensureCapacity()
,grow()
,trim()
andsetLength()
methods allow to handle big arrays much like array lists.Note that
BinIO
andTextIO
contain several methods that make it possible to load and save big arrays of primitive types as sequences of elements inDataInput
format (i.e., not as objects) or as sequences of lines of text.- See Also:
BigArrays
-
-
Field Summary
Fields Modifier and Type Field Description static boolean[][]
DEFAULT_EMPTY_BIG_ARRAY
A static, final, empty big array to be used as default big array in allocations.static boolean[][]
EMPTY_BIG_ARRAY
A static, final, empty big array.static Hash.Strategy
HASH_STRATEGY
A type-specific content-based hash strategy for big arrays.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean[][]
copy(boolean[][] array)
Returns a copy of a big array.static void
copy(boolean[][] srcArray, long srcPos, boolean[][] 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 boolean[][]
copy(boolean[][] array, long offset, long length)
Returns a copy of a portion of a big array.static void
copyFromBig(boolean[][] srcArray, long srcPos, boolean[] 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(boolean[] srcArray, int srcPos, boolean[][] 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 boolean[][]
ensureCapacity(boolean[][] array, long length)
Ensures that a big array can contain the given number of entries.static boolean[][]
ensureCapacity(boolean[][] 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(boolean[][] 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(boolean[][] a, long offset, long length)
Ensures that a range given by an offset and a length fits a big array.static boolean
equals(boolean[][] a1, boolean[][] a2)
Returns true if the two big arrays are elementwise equal.static void
fill(boolean[][] array, boolean value)
Fills the given big array with the given value.static void
fill(boolean[][] array, long from, long to, boolean value)
Fills a portion of the given big array with the given value.static boolean[][]
forceCapacity(boolean[][] array, long length, long preserve)
Forces a big array to contain the given number of entries, preserving just a part of the big array.static boolean
get(boolean[][] array, long index)
Returns the element of the given big array of specified index.static boolean[][]
grow(boolean[][] array, long length)
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.static boolean[][]
grow(boolean[][] array, long length, long preserve)
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.static long
length(boolean[][] array)
Returns the length of the given big array.static boolean[][]
newBigArray(long length)
Creates a new big array.static void
quickSort(boolean[][] x)
Sorts the specified big array according to the natural ascending order using quicksort.static void
quickSort(boolean[][] x, long from, long to)
Sorts the specified range of elements according to the natural ascending order using quicksort.static void
quickSort(boolean[][] x, long from, long to, BooleanComparator comp)
Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.static void
quickSort(boolean[][] x, BooleanComparator comp)
Sorts the specified big array according to the order induced by the specified comparator using quicksort.static void
set(boolean[][] array, long index, boolean value)
Sets the element of the given big array of specified index.static boolean[][]
setLength(boolean[][] array, long length)
Sets the length of the given big array.static boolean[][]
shuffle(boolean[][] a, long from, long to, java.util.Random random)
Shuffles the specified big array fragment using the specified pseudorandom number generator.static boolean[][]
shuffle(boolean[][] a, java.util.Random random)
Shuffles the specified big array using the specified pseudorandom number generator.static void
swap(boolean[][] array, long first, long second)
Swaps the element of the given big array of specified indices.static java.lang.String
toString(boolean[][] a)
static boolean[][]
trim(boolean[][] array, long length)
Trims the given big array to the given length.static boolean[][]
wrap(boolean[] array)
Turns a standard array into a big array.
-
-
-
Field Detail
-
EMPTY_BIG_ARRAY
public static final boolean[][] EMPTY_BIG_ARRAY
A static, final, empty big array.
-
DEFAULT_EMPTY_BIG_ARRAY
public static final boolean[][] 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:
ArrayList
-
HASH_STRATEGY
public static final Hash.Strategy 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 Detail
-
get
public static boolean get(boolean[][] array, long index)
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
public static void set(boolean[][] array, long index, boolean value)
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
public static void swap(boolean[][] array, long first, long second)
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
public static long length(boolean[][] array)
Returns the length of the given big array.- Parameters:
array
- a big array.- Returns:
- the length of the given big array.
-
copy
public static void copy(boolean[][] srcArray, long srcPos, boolean[][] 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. 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
public static void copyFromBig(boolean[][] srcArray, long srcPos, boolean[] 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.- 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
public static void copyToBig(boolean[] srcArray, int srcPos, boolean[][] 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.- 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 boolean[][] newBigArray(long length)
Creates a new big array.- Parameters:
length
- the length of the new big array.- Returns:
- a new big array of given length.
-
wrap
public static boolean[][] wrap(boolean[] array)
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
public static boolean[][] ensureCapacity(boolean[][] array, long length)
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
public static boolean[][] forceCapacity(boolean[][] array, long length, long preserve)
Forces a big array to contain the given number of entries, preserving just a part of the 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 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
public static boolean[][] ensureCapacity(boolean[][] 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.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
public static boolean[][] grow(boolean[][] array, long length)
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
public static boolean[][] grow(boolean[][] array, long length, long preserve)
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
public static boolean[][] trim(boolean[][] array, long length)
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
public static boolean[][] setLength(boolean[][] array, long length)
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
public static boolean[][] copy(boolean[][] array, long offset, long length)
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
public static boolean[][] copy(boolean[][] array)
Returns a copy of a big array.- Parameters:
array
- a big array.- Returns:
- a copy of
array
.
-
fill
public static void fill(boolean[][] array, boolean value)
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
public static void fill(boolean[][] array, long from, long to, boolean value)
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
public static boolean equals(boolean[][] a1, boolean[][] a2)
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
public static java.lang.String toString(boolean[][] a)
-
ensureFromTo
public static void ensureFromTo(boolean[][] a, long from, long to)
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:
java.lang.IllegalArgumentException
- iffrom
is greater thanto
.java.lang.ArrayIndexOutOfBoundsException
- iffrom
orto
are greater than the big array length or negative.
-
ensureOffsetLength
public static void ensureOffsetLength(boolean[][] a, long offset, long length)
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:
java.lang.IllegalArgumentException
- iflength
is negative.java.lang.ArrayIndexOutOfBoundsException
- ifoffset
is negative oroffset
+length
is greater than the big array length.
-
quickSort
public static void quickSort(boolean[][] x, long from, long to, BooleanComparator comp)
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
public static void quickSort(boolean[][] x, BooleanComparator comp)
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 void quickSort(boolean[][] 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 void quickSort(boolean[][] 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.
-
shuffle
public static boolean[][] shuffle(boolean[][] a, long from, long to, java.util.Random random)
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
public static boolean[][] shuffle(boolean[][] a, java.util.Random random)
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
.
-
-