Class ByteArraySelector
Special version of ArraySelector
class, optimized for selecting from byte[] arrays.
We recommend to use methods of this class, instead of ArraySelector.select(int[], byte[], int)
and
ArraySelector.select(double[], byte[], int)
, for relatively large byte arrays.
Typically, short byte[] arrays (≤50–100 elements) are sorted faster by ArraySelector
methods, and arrays larger than 200–300 bytes are sorted faster by this class.
But if you need to find a lot of percentiles for the same array, for example ≥5 levels,
this class may provide better speed even for shorter arrays (50–100 bytes).
For very short arrays this class does not provide optimization.
Note that this class allocates some memory (~256 bytes) while instantiation and reuse it while every call of its methods.
This class is not immutable and not thread-safe, but is thread-compatible (allows manual synchronization for multithreading access).
- Author:
- Daniel Alievsky
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
select
(byte[] results, double[] percentileLevels, byte[] array, int length) Finds the percentiles with the specified levels (from 0.0 to 1.0) among first length elements of the passed array of bytes and returns them in results argument.void
select
(byte[] results, int[] percentileIndexes, byte[] array, int length) Finds the percentiles with the specified indexes among first length elements of the passed array of bytes and returns them in results argument.
-
Constructor Details
-
ByteArraySelector
public ByteArraySelector()
-
-
Method Details
-
select
public void select(byte[] results, int[] percentileIndexes, byte[] array, int length) Finds the percentiles with the specified indexes among first length elements of the passed array of bytes and returns them in results argument. Percentile with index=percentileIndexes[k] is the value of the element of the passed array, which will be placed at position array[index] after sorting this array in increasing order; but this method does not actually modify this array.For example, percentileIndexes={0, length/2, length-1} requests to find the minimum, median and maximum and return them in results[0], results[1], results[2].
Note that the elements of this array are supposed to be unsigned: we always compare array[i] & 0xFF and array[j] & 0xFF instead of simple array[i] and array[j].
Note: list of indexes percentileIndexes must be sorted in increasing order. You can provide this, for example, but simple call of standard Java sorting method Arrays.sort(percentileIndexes) before calling this method. If these indexes are not sorted, or if they are out of range 0..length, the results will be unpredictable. You can check these indexes yourself by
ArraySelector.checkPercentileIndexes(int[], int)
method.This method does not modify the passed array.
- Parameters:
results
- results of this method: values of some elements of the passed array.percentileIndexes
- list of indexes inside the array, the values of which, in increasing order, must be returned.array
- array of bytes.length
- number of elements: only elements array[0..length-1 are analysed.- Throws:
NullPointerException
- if one of the arguments is null.IllegalArgumentException
- if length≤0, or length>array.length.
-
select
public void select(byte[] results, double[] percentileLevels, byte[] array, int length) Finds the percentiles with the specified levels (from 0.0 to 1.0) among first length elements of the passed array of bytes and returns them in results argument.For example, percentileLevels={0.0, 0.5, 1.0} requests to find the minimum, median and maximum and return them in results[0], results[1], results[2].
This method is equivalent to
select
(results, percentileIndexes, array, length)ArraySelector.percentileIndex
(percentileLevels[k], length) for every k=0,...,percentileLevels.length-1.Note that the elements of this array are supposed to be unsigned: we always compare array[i] & 0xFF and array[j] & 0xFF instead of simple array[i] and array[j].
Note: list of levels percentileLevels must be sorted in increasing order. You can provide this, for example, but simple call of standard Java sorting method Arrays.sort(percentileLevels) before calling this method. If these levels are not sorted, or if they are out of range 0.0..1.0, the results will be unpredictable. You can check these levels yourself by
ArraySelector.checkPercentileLevels(double[])
method.This method does not modify the passed array.
- Parameters:
results
- results of this method: values of some elements of the passed array.percentileLevels
- list of percentile levels: required indexes, divided by array length.array
- array of bytes.length
- number of elements: only elements array[0..length-1 are analysed.- Throws:
NullPointerException
- if one of the arguments is null.IllegalArgumentException
- if length≤0, or length>array.length.
-