bisectLeft method
- E element,
- {int compare(
- E,
- E
- int low,
- int high}
Returns the index where an element should be inserted in a list
,
assuming the list
is sorted by a compare
function.
If this element
is already in this list
,returns the leftmost index
where it can be inserted.
The compare
function must act as a Comparator
.
The default implementation uses Comparable.compare
if compare
is omitted.
Two optional parameters low
(0
by default) and high
(list.length
by default)
can be provided to bisect only an slice of this list where the element will be inserted.
Implementation
int bisectLeft(E element, {int Function(E, E) compare, int low, int high}) {
compare ??= Comparable.compare as Function(E, E);
low ??= 0;
high ??= length;
if (low < 0) {
throw ArgumentError('low must be non-negative');
}
// This algorithm is identical to bisectRight with only
// a minor tweak, so when the element is found on the list
// it is inserted to the leftmost position.
while (low < high) {
var mid = (low + high) ~/ 2;
if (compare(this[mid], element) < 0) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}