mirror of
https://github.com/androidx/media.git
synced 2025-05-10 00:59:51 +08:00
Group binarySearchFloor overloads
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=182770109
This commit is contained in:
parent
a06a670d63
commit
d8c61532b6
@ -461,39 +461,6 @@ public final class Util {
|
|||||||
return stayInBounds ? Math.max(0, index) : index;
|
return stayInBounds ? Math.max(0, index) : index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the index of the smallest element in {@code array} that is greater than (or optionally
|
|
||||||
* equal to) a specified {@code value}.
|
|
||||||
* <p>
|
|
||||||
* The search is performed using a binary search algorithm, so the array must be sorted. If
|
|
||||||
* the array contains multiple elements equal to {@code value} and {@code inclusive} is true, the
|
|
||||||
* index of the last one will be returned.
|
|
||||||
*
|
|
||||||
* @param array The array to search.
|
|
||||||
* @param value The value being searched for.
|
|
||||||
* @param inclusive If the value is present in the array, whether to return the corresponding
|
|
||||||
* index. If false then the returned index corresponds to the smallest element strictly
|
|
||||||
* greater than the value.
|
|
||||||
* @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the
|
|
||||||
* value is greater than the largest element in the array. If false then {@code a.length} will
|
|
||||||
* be returned.
|
|
||||||
* @return The index of the smallest element in {@code array} that is greater than (or optionally
|
|
||||||
* equal to) {@code value}.
|
|
||||||
*/
|
|
||||||
public static int binarySearchCeil(long[] array, long value, boolean inclusive,
|
|
||||||
boolean stayInBounds) {
|
|
||||||
int index = Arrays.binarySearch(array, value);
|
|
||||||
if (index < 0) {
|
|
||||||
index = ~index;
|
|
||||||
} else {
|
|
||||||
while ((++index) < array.length && array[index] == value) {}
|
|
||||||
if (inclusive) {
|
|
||||||
index--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return stayInBounds ? Math.min(array.length - 1, index) : index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the index of the largest element in {@code list} that is less than (or optionally equal
|
* Returns the index of the largest element in {@code list} that is less than (or optionally equal
|
||||||
* to) a specified {@code value}.
|
* to) a specified {@code value}.
|
||||||
@ -527,6 +494,39 @@ public final class Util {
|
|||||||
return stayInBounds ? Math.max(0, index) : index;
|
return stayInBounds ? Math.max(0, index) : index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the index of the smallest element in {@code array} that is greater than (or optionally
|
||||||
|
* equal to) a specified {@code value}.
|
||||||
|
*
|
||||||
|
* <p>The search is performed using a binary search algorithm, so the array must be sorted. If the
|
||||||
|
* array contains multiple elements equal to {@code value} and {@code inclusive} is true, the
|
||||||
|
* index of the last one will be returned.
|
||||||
|
*
|
||||||
|
* @param array The array to search.
|
||||||
|
* @param value The value being searched for.
|
||||||
|
* @param inclusive If the value is present in the array, whether to return the corresponding
|
||||||
|
* index. If false then the returned index corresponds to the smallest element strictly
|
||||||
|
* greater than the value.
|
||||||
|
* @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the
|
||||||
|
* value is greater than the largest element in the array. If false then {@code a.length} will
|
||||||
|
* be returned.
|
||||||
|
* @return The index of the smallest element in {@code array} that is greater than (or optionally
|
||||||
|
* equal to) {@code value}.
|
||||||
|
*/
|
||||||
|
public static int binarySearchCeil(
|
||||||
|
long[] array, long value, boolean inclusive, boolean stayInBounds) {
|
||||||
|
int index = Arrays.binarySearch(array, value);
|
||||||
|
if (index < 0) {
|
||||||
|
index = ~index;
|
||||||
|
} else {
|
||||||
|
while ((++index) < array.length && array[index] == value) {}
|
||||||
|
if (inclusive) {
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stayInBounds ? Math.min(array.length - 1, index) : index;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the index of the smallest element in {@code list} that is greater than (or optionally
|
* Returns the index of the smallest element in {@code list} that is greater than (or optionally
|
||||||
* equal to) a specified value.
|
* equal to) a specified value.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user