binarySearchLower method

int binarySearchLower(
  1. List<T> list,
  2. T value, {
  3. int? start,
  4. int? end,
})

Performs a binary search of value on the sorted list. Returns the the first suitable insertion index such that list[index - 1] < value <= list[index] (lower bound). The result is undefined if the list is not sorted.

By default the whole list is searched, but if start and/or end are supplied, only that range is searched.

Implementation

int binarySearchLower(List<T> list, T value, {int? start, int? end}) {
  var min = start ??= 0;
  var max = end ??= list.length;
  while (min < max) {
    final mid = min + ((max - min) >> 1);
    if (this(list[mid], value) < 0) {
      min = mid + 1;
    } else {
      max = mid;
    }
  }
  assert(start <= min && min <= end,
      'Index $min not in inclusive range $start..$end');
  return min;
}