Class IndexSupport
java.lang.Object
org.apache.commons.numbers.arrays.IndexSupport
Support for creating
UpdatingInterval implementations and validating indices.- Since:
- 1.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe upper threshold to use a modified insertion sort to find unique indices. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static intceilLog2(int x) Computeceil(log2(x)).(package private) static voidcheckFromToIndex(int fromIndex, int toIndex, int length) Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).(package private) static voidcheckIndex(int fromIndex, int toIndex, int index) Checks if theindexis within the half-open interval[fromIndex, toIndex).(package private) static voidcheckIndices(int fromIndex, int toIndex, int[] k) Checks if theindexis within the half-open interval[fromIndex, toIndex).private static intcompressDuplicates(int[] data, int n) Compress duplicates in the ascending data.(package private) static intcountIndices(UpdatingInterval keys, int n) Count the number of indices.(package private) static UpdatingIntervalcreateUpdatingInterval(int left, int right, int[] k, int n) Returns an interval that covers the specified indicesk.private static booleanisAscending(int[] data, int n) Test the data is in ascending order:data[i] <= data[i+1]for alli.private static StringmsgIndexOutOfBounds(int fromIndex, int toIndex, int index) Format a message when index is not within range [from, to).private static StringmsgRangeOutOfBounds(int fromIndex, int toIndex, int length) Format a message when range [from, to) is not entirely within the length.private static UpdatingIntervalnewUpdatingInterval(int[] k, int n) Returns an interval that covers the specified indicesk.
-
Field Details
-
INSERTION_SORT_SIZE
private static final int INSERTION_SORT_SIZEThe upper threshold to use a modified insertion sort to find unique indices.- See Also:
-
-
Constructor Details
-
IndexSupport
private IndexSupport()No instances.
-
-
Method Details
-
createUpdatingInterval
Returns an interval that covers the specified indicesk.- Parameters:
left- Lower bound of data (inclusive).right- Upper bound of data (inclusive).k- Indices.n- Count of indices (must be strictly positive).- Returns:
- the interval
- Throws:
IndexOutOfBoundsException- if any indexkis not within the sub-range[left, right]
-
isAscending
private static boolean isAscending(int[] data, int n) Test the data is in ascending order:data[i] <= data[i+1]for alli. Data is assumed to be at least length 1.- Parameters:
data- Data.n- Length of data.- Returns:
- true if ascending
-
compressDuplicates
private static int compressDuplicates(int[] data, int n) Compress duplicates in the ascending data.Warning: Requires
n > 0.- Parameters:
data- Indices.n- Number of indices.- Returns:
- the number of unique indices
-
ceilLog2
private static int ceilLog2(int x) Computeceil(log2(x)). This is valid for all strictly positivex.Returns -1 for
x = 0in place of -infinity.- Parameters:
x- Value.- Returns:
ceil(log2(x))
-
newUpdatingInterval
Returns an interval that covers the specified indicesk. The indices must be sorted.- Parameters:
k- Indices.n- Count of indices (must be strictly positive).- Returns:
- the interval
- Throws:
IndexOutOfBoundsException- if any indexkis not within the sub-range[left, right]
-
countIndices
Count the number of indices. Returns a negative value if the indices are sorted.- Parameters:
keys- Keys.n- Count of indices.- Returns:
- the count of (sorted) indices
-
checkFromToIndex
static void checkFromToIndex(int fromIndex, int toIndex, int length) Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).This function provides the functionality of
java.utils.Objects.checkFromToIndexintroduced in JDK 9. The Objects javadoc has been reproduced for reference. The return value has been changed to void.The sub-range is defined to be out of bounds if any of the following inequalities is true:
fromIndex < 0fromIndex > toIndextoIndex > lengthlength < 0, which is implied from the former inequalities
- Parameters:
fromIndex- Lower-bound (inclusive) of the sub-range.toIndex- Upper-bound (exclusive) of the sub-range.length- Upper-bound (exclusive) of the range.- Throws:
IndexOutOfBoundsException- if the sub-range is out of bounds
-
checkIndices
static void checkIndices(int fromIndex, int toIndex, int[] k) Checks if theindexis within the half-open interval[fromIndex, toIndex).- Parameters:
fromIndex- Lower-bound (inclusive) of the sub-range.toIndex- Upper-bound (exclusive) of the sub-range.k- Indices.- Throws:
IndexOutOfBoundsException- if any index is out of bounds
-
checkIndex
static void checkIndex(int fromIndex, int toIndex, int index) Checks if theindexis within the half-open interval[fromIndex, toIndex).- Parameters:
fromIndex- Lower-bound (inclusive) of the sub-range.toIndex- Upper-bound (exclusive) of the sub-range.index- Index.- Throws:
IndexOutOfBoundsException- if the index is out of bounds
-
msgRangeOutOfBounds
Format a message when range [from, to) is not entirely within the length.- Parameters:
fromIndex- Lower-bound (inclusive) of the sub-range.toIndex- Upper-bound (exclusive) of the sub-range.length- Upper-bound (exclusive) of the range.- Returns:
- the message
-
msgIndexOutOfBounds
Format a message when index is not within range [from, to).- Parameters:
fromIndex- Lower-bound (inclusive) of the sub-range.toIndex- Upper-bound (exclusive) of the sub-range.index- Index.- Returns:
- the message
-