Package org.apache.lucene.util
Class ArrayUtil
- java.lang.Object
-
- org.apache.lucene.util.ArrayUtil
-
public final class ArrayUtil extends Object
Methods for manipulating arrays.- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ArrayUtil.ByteArrayComparator
Comparator for a fixed number of bytes.
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_ARRAY_LENGTH
Maximum length for an array (Integer.MAX_VALUE - RamUsageEstimator.NUM_BYTES_ARRAY_HEADER).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
compareUnsigned4(byte[] a, int aOffset, byte[] b, int bOffset)
Compare exactly 4 unsigned bytes from the provided arrays.static int
compareUnsigned8(byte[] a, int aOffset, byte[] b, int bOffset)
Compare exactly 8 unsigned bytes from the provided arrays.static byte[]
copyArray(byte[] array)
Copies an array into a new array.static char[]
copyArray(char[] array)
Copies an array into a new array.static double[]
copyArray(double[] array)
Copies an array into a new array.static float[]
copyArray(float[] array)
Copies an array into a new array.static int[]
copyArray(int[] array)
Copies an array into a new array.static long[]
copyArray(long[] array)
Copies an array into a new array.static short[]
copyArray(short[] array)
Copies an array into a new array.static <T> T[]
copyArray(T[] array)
Copies an array into a new array.static byte[]
copyOfSubArray(byte[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static char[]
copyOfSubArray(char[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static double[]
copyOfSubArray(double[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static float[]
copyOfSubArray(float[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static int[]
copyOfSubArray(int[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static long[]
copyOfSubArray(long[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static short[]
copyOfSubArray(short[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static <T> T[]
copyOfSubArray(T[] array, int from, int to)
Copies the specified range of the given array into a new sub array.static ArrayUtil.ByteArrayComparator
getUnsignedComparator(int numBytes)
Return a comparator for exactly the specified number of bytes.static byte[]
grow(byte[] array)
Returns a larger array, generally over-allocating exponentiallystatic byte[]
grow(byte[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic char[]
grow(char[] array)
Returns a larger array, generally over-allocating exponentiallystatic char[]
grow(char[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic double[]
grow(double[] array)
Returns a larger array, generally over-allocating exponentiallystatic double[]
grow(double[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic float[]
grow(float[] array)
Returns a larger array, generally over-allocating exponentiallystatic float[]
grow(float[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic int[]
grow(int[] array)
Returns a larger array, generally over-allocating exponentiallystatic int[]
grow(int[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic long[]
grow(long[] array)
Returns a larger array, generally over-allocating exponentiallystatic long[]
grow(long[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic short[]
grow(short[] array)
Returns a larger array, generally over-allocating exponentiallystatic short[]
grow(short[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic <T> T[]
grow(T[] array)
Returns a larger array, generally over-allocating exponentiallystatic <T> T[]
grow(T[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentiallystatic byte[]
growExact(byte[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic char[]
growExact(char[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic double[]
growExact(double[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic float[]
growExact(float[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic int[]
growExact(int[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic long[]
growExact(long[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic short[]
growExact(short[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic <T> T[]
growExact(T[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocatingstatic int[]
growInRange(int[] array, int minLength, int maxLength)
Returns an array whose size is at leastminLength
, generally over-allocating exponentially, but never allocating more thanmaxLength
elements.static byte[]
growNoCopy(byte[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially, and it will not copy the origin data to the new arraystatic int[]
growNoCopy(int[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially, and it will not copy the origin data to the new arraystatic long[]
growNoCopy(long[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially, and it will not copy the origin data to the new arraystatic int
hashCode(char[] array, int start, int end)
Returns hash of chars in range start (inclusive) to end (inclusive)static <T extends Comparable<? super T>>
voidintroSort(T[] a)
Sorts the given array in natural order.static <T extends Comparable<? super T>>
voidintroSort(T[] a, int fromIndex, int toIndex)
Sorts the given array slice in natural order.static <T> void
introSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
Sorts the given array slice using theComparator
.static <T> void
introSort(T[] a, Comparator<? super T> comp)
Sorts the given array using theComparator
.static int
oversize(int minTargetSize, int bytesPerElement)
Returns an array size >= minTargetSize, generally over-allocating exponentially to achieve amortized linear-time cost as the array grows.static int
parseInt(char[] chars, int offset, int len)
Parses a char array into an int.static int
parseInt(char[] chars, int offset, int len, int radix)
Parses the string argument as if it was an int value and returns the result.static <T> void
select(T[] arr, int from, int to, int k, Comparator<? super T> comparator)
Reorganizearr[from:to[
so that the element at offset k is at the same position as ifarr[from:to]
was sorted, and all elements on its left are less than or equal to it, and all elements on its right are greater than or equal to it.static <T> void
swap(T[] arr, int i, int j)
Swap values stored in slotsi
andj
static <T extends Comparable<? super T>>
voidtimSort(T[] a)
Sorts the given array in natural order.static <T extends Comparable<? super T>>
voidtimSort(T[] a, int fromIndex, int toIndex)
Sorts the given array slice in natural order.static <T> void
timSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
Sorts the given array slice using theComparator
.static <T> void
timSort(T[] a, Comparator<? super T> comp)
Sorts the given array using theComparator
.
-
-
-
Method Detail
-
parseInt
public static int parseInt(char[] chars, int offset, int len) throws NumberFormatException
Parses a char array into an int.- Parameters:
chars
- the character arrayoffset
- The offset into the arraylen
- The length- Returns:
- the int
- Throws:
NumberFormatException
- if it can't parse
-
parseInt
public static int parseInt(char[] chars, int offset, int len, int radix) throws NumberFormatException
Parses the string argument as if it was an int value and returns the result. Throws NumberFormatException if the string does not represent an int quantity. The second argument specifies the radix to use when parsing the value.- Parameters:
chars
- a string representation of an int quantity.radix
- the base to use for conversion.- Returns:
- int the value represented by the argument
- Throws:
NumberFormatException
- if the argument could not be parsed as an int quantity.
-
oversize
public static int oversize(int minTargetSize, int bytesPerElement)
Returns an array size >= minTargetSize, generally over-allocating exponentially to achieve amortized linear-time cost as the array grows.NOTE: this was originally borrowed from Python 2.4.2 listobject.c sources (attribution in LICENSE.txt), but has now been substantially changed based on discussions from java-dev thread with subject "Dynamic array reallocation algorithms", started on Jan 12 2010.
- Parameters:
minTargetSize
- Minimum required value to be returned.bytesPerElement
- Bytes used by each element of the array. See constants inRamUsageEstimator
.- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
growExact
public static <T> T[] growExact(T[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
grow
public static <T> T[] grow(T[] array)
Returns a larger array, generally over-allocating exponentially
-
grow
public static <T> T[] grow(T[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
growExact
public static short[] growExact(short[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
grow
public static short[] grow(short[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
grow
public static short[] grow(short[] array)
Returns a larger array, generally over-allocating exponentially
-
growExact
public static float[] growExact(float[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
grow
public static float[] grow(float[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
grow
public static float[] grow(float[] array)
Returns a larger array, generally over-allocating exponentially
-
growExact
public static double[] growExact(double[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
grow
public static double[] grow(double[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
grow
public static double[] grow(double[] array)
Returns a larger array, generally over-allocating exponentially
-
growExact
public static int[] growExact(int[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
growInRange
public static int[] growInRange(int[] array, int minLength, int maxLength)
Returns an array whose size is at leastminLength
, generally over-allocating exponentially, but never allocating more thanmaxLength
elements.
-
grow
public static int[] grow(int[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
growNoCopy
public static int[] growNoCopy(int[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially, and it will not copy the origin data to the new array
-
grow
public static int[] grow(int[] array)
Returns a larger array, generally over-allocating exponentially
-
growExact
public static long[] growExact(long[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
grow
public static long[] grow(long[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
growNoCopy
public static long[] growNoCopy(long[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially, and it will not copy the origin data to the new array
-
grow
public static long[] grow(long[] array)
Returns a larger array, generally over-allocating exponentially
-
growExact
public static byte[] growExact(byte[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
grow
public static byte[] grow(byte[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
growNoCopy
public static byte[] growNoCopy(byte[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially, and it will not copy the origin data to the new array
-
grow
public static byte[] grow(byte[] array)
Returns a larger array, generally over-allocating exponentially
-
growExact
public static char[] growExact(char[] array, int newLength)
Returns a new array whose size is exact the specifiednewLength
without over-allocating
-
grow
public static char[] grow(char[] array, int minSize)
Returns an array whose size is at leastminSize
, generally over-allocating exponentially
-
grow
public static char[] grow(char[] array)
Returns a larger array, generally over-allocating exponentially
-
hashCode
public static int hashCode(char[] array, int start, int end)
Returns hash of chars in range start (inclusive) to end (inclusive)
-
swap
public static <T> void swap(T[] arr, int i, int j)
Swap values stored in slotsi
andj
-
introSort
public static <T> void introSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
Sorts the given array slice using theComparator
. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.- Parameters:
fromIndex
- start index (inclusive)toIndex
- end index (exclusive)- See Also:
IntroSorter
-
introSort
public static <T> void introSort(T[] a, Comparator<? super T> comp)
Sorts the given array using theComparator
. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.- See Also:
IntroSorter
-
introSort
public static <T extends Comparable<? super T>> void introSort(T[] a, int fromIndex, int toIndex)
Sorts the given array slice in natural order. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.- Parameters:
fromIndex
- start index (inclusive)toIndex
- end index (exclusive)- See Also:
IntroSorter
-
introSort
public static <T extends Comparable<? super T>> void introSort(T[] a)
Sorts the given array in natural order. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.- See Also:
IntroSorter
-
timSort
public static <T> void timSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
Sorts the given array slice using theComparator
. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.- Parameters:
fromIndex
- start index (inclusive)toIndex
- end index (exclusive)- See Also:
TimSorter
-
timSort
public static <T> void timSort(T[] a, Comparator<? super T> comp)
Sorts the given array using theComparator
. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.- See Also:
TimSorter
-
timSort
public static <T extends Comparable<? super T>> void timSort(T[] a, int fromIndex, int toIndex)
Sorts the given array slice in natural order. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.- Parameters:
fromIndex
- start index (inclusive)toIndex
- end index (exclusive)- See Also:
TimSorter
-
timSort
public static <T extends Comparable<? super T>> void timSort(T[] a)
Sorts the given array in natural order. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.- See Also:
TimSorter
-
select
public static <T> void select(T[] arr, int from, int to, int k, Comparator<? super T> comparator)
Reorganizearr[from:to[
so that the element at offset k is at the same position as ifarr[from:to]
was sorted, and all elements on its left are less than or equal to it, and all elements on its right are greater than or equal to it.This runs in linear time on average and in
n log(n)
time in the worst case.- Parameters:
arr
- Array to be re-organized.from
- Starting index for re-organization. Elements before this index will be left as is.to
- Ending index. Elements after this index will be left as is.k
- Index of element to sort from. Value must be less than 'to' and greater than or equal to 'from'.comparator
- Comparator to use for sorting
-
copyArray
public static byte[] copyArray(byte[] array)
Copies an array into a new array.
-
copyOfSubArray
public static byte[] copyOfSubArray(byte[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
copyArray
public static char[] copyArray(char[] array)
Copies an array into a new array.
-
copyOfSubArray
public static char[] copyOfSubArray(char[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
copyArray
public static short[] copyArray(short[] array)
Copies an array into a new array.
-
copyOfSubArray
public static short[] copyOfSubArray(short[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
copyArray
public static int[] copyArray(int[] array)
Copies an array into a new array.
-
copyOfSubArray
public static int[] copyOfSubArray(int[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
copyArray
public static long[] copyArray(long[] array)
Copies an array into a new array.
-
copyOfSubArray
public static long[] copyOfSubArray(long[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
copyArray
public static float[] copyArray(float[] array)
Copies an array into a new array.
-
copyOfSubArray
public static float[] copyOfSubArray(float[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
copyArray
public static double[] copyArray(double[] array)
Copies an array into a new array.
-
copyOfSubArray
public static double[] copyOfSubArray(double[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
copyArray
public static <T> T[] copyArray(T[] array)
Copies an array into a new array.
-
copyOfSubArray
public static <T> T[] copyOfSubArray(T[] array, int from, int to)
Copies the specified range of the given array into a new sub array.- Parameters:
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)
-
getUnsignedComparator
public static ArrayUtil.ByteArrayComparator getUnsignedComparator(int numBytes)
Return a comparator for exactly the specified number of bytes.
-
compareUnsigned8
public static int compareUnsigned8(byte[] a, int aOffset, byte[] b, int bOffset)
Compare exactly 8 unsigned bytes from the provided arrays.
-
compareUnsigned4
public static int compareUnsigned4(byte[] a, int aOffset, byte[] b, int bOffset)
Compare exactly 4 unsigned bytes from the provided arrays.
-
-