binarySearchWithComparison method
Searches this List or its range for an element for which the given comparison function returns zero using the binary search algorithm.
Implementation
int binarySearchWithComparison({
required int Function(E element) comparison,
int? fromIndex,
int? toIndex,
}) {
fromIndex ??= 0;
toIndex ??= length;
rangeCheck(length: length, fromIndex: fromIndex, toIndex: toIndex);
var low = fromIndex;
var high = toIndex - 1;
while (low <= high) {
// todo: Change to `>>>` operator
// Change when stable `Dart` SDK version becomes to 2.14.0 over.
final mid = (low + high) ~/ 2;
final midVal = this[mid];
final cmp = comparison(midVal);
if (cmp < 0) {
low = mid + 1;
} else if (cmp > 0) {
high = mid - 1;
} else {
return mid;
}
}
return -(low + 1);
}