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 int
The upper threshold to use a modified insertion sort to find unique indices. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static int
ceilLog2
(int x) Computeceil(log2(x))
.(package private) 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).(package private) static void
checkIndex
(int fromIndex, int toIndex, int index) Checks if theindex
is within the half-open interval[fromIndex, toIndex)
.(package private) static void
checkIndices
(int fromIndex, int toIndex, int[] k) Checks if theindex
is within the half-open interval[fromIndex, toIndex)
.private static int
compressDuplicates
(int[] data, int n) Compress duplicates in the ascending data.(package private) static int
countIndices
(UpdatingInterval keys, int n) Count the number of indices.(package private) static UpdatingInterval
createUpdatingInterval
(int left, int right, int[] k, int n) Returns an interval that covers the specified indicesk
.private static boolean
isAscending
(int[] data, int n) Test the data is in ascending order:data[i] <= data[i+1]
for alli
.private static String
msgIndexOutOfBounds
(int fromIndex, int toIndex, int index) Format a message when index is not within range [from, to).private static String
msgRangeOutOfBounds
(int fromIndex, int toIndex, int length) Format a message when range [from, to) is not entirely within the length.private static UpdatingInterval
newUpdatingInterval
(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 indexk
is 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 = 0
in 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 indexk
is 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.checkFromToIndex
introduced 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 < 0
fromIndex > toIndex
toIndex > length
length < 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 theindex
is 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 theindex
is 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
-