bisect_right<E> function
int
bisect_right<E>(
- List<
E> a, - E x, {
- int lo = 0,
- int? hi,
- ToKey<
E, Object> ? key, - Comparator<
E> ? compare,
Similar to bisect_left, but returns an insertion point which comes after (to the right of)
any existing entries of x
in a
.
Implementation
int bisect_right<E>(List<E> a, E x,
{int lo = 0, int? hi, ToKey<E, Object>? key, Comparator<E>? compare}) {
compare = argToComparator<E>(compare, key);
if (lo < 0) {
throw ArgumentError.value(
lo, 'lo must be non-negative'); // in Python this disallowed too
}
if (hi != null && hi < 0) {
// Python allows negative `hi` values, but returns strange results.
// I failed to make a Dart code that returns the same, in particular in the case of `hi=-1`.
// But negative `hi` does not make sense. So we'll just disallow it
throw ArgumentError.value(lo, 'hi must be non-negative');
}
int h = hi ?? a.length;
while (lo < h) {
var mid = (lo + h) >> 1; // in Python it's `//2`
if (compare(x, a[mid]) < 0) {
h = mid;
} else {
lo = mid + 1;
}
}
return lo;
}