rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
35 lines • 1.57 kB
JavaScript
import { _Debug } from "../../debug/_debug.js";
import { mathBound } from "../../math/impl/math-bound.js";
/**
* @internal
*/
export function binaryFindInsertionIndex(indexable, comparisonValueToSearchFor, getComparisonValueAtIndex, isLow, adjustValue, length, startIndex = 0) {
_BUILD.DEBUG && _Debug.runBlock(() => {
_Debug.assert(startIndex < length, "start index must be within bounds");
let prev = -Infinity;
_Debug.assert(comparisonValueToSearchFor === comparisonValueToSearchFor, "NaN is not a permissible comparison value");
if (!_Debug.isFlagSet("DISABLE_EXPENSIVE_CHECKS")) {
for (let i = startIndex; i < length; ++i) {
const current = getComparisonValueAtIndex(indexable, i);
_Debug.assert(current === current, "NaN is not a permissible comparison value");
_Debug.assert(current >= prev, "expected data to be sorted");
prev = current;
}
}
});
let low = startIndex;
let high = length;
while (low < high) {
const midIndex = (low + high) >> 1;
const currentComparisonValue = getComparisonValueAtIndex(indexable, midIndex);
// i.e. the comparison value is less than the value we're searching for
if (isLow(comparisonValueToSearchFor, currentComparisonValue)) {
low = midIndex + 1;
}
else {
high = midIndex;
}
}
return mathBound(adjustValue(high), startIndex, length - 1);
}
//# sourceMappingURL=binary-find-insertion-index.js.map