UNPKG

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.

39 lines 1.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.binaryFindInsertionIndex = void 0; const _debug_js_1 = require("../../debug/_debug.js"); const math_bound_js_1 = require("../../math/impl/math-bound.js"); /** * @internal */ function binaryFindInsertionIndex(indexable, comparisonValueToSearchFor, getComparisonValueAtIndex, isLow, adjustValue, length, startIndex = 0) { _BUILD.DEBUG && _debug_js_1._Debug.runBlock(() => { _debug_js_1._Debug.assert(startIndex < length, "start index must be within bounds"); let prev = -Infinity; _debug_js_1._Debug.assert(comparisonValueToSearchFor === comparisonValueToSearchFor, "NaN is not a permissible comparison value"); if (!_debug_js_1._Debug.isFlagSet("DISABLE_EXPENSIVE_CHECKS")) { for (let i = startIndex; i < length; ++i) { const current = getComparisonValueAtIndex(indexable, i); _debug_js_1._Debug.assert(current === current, "NaN is not a permissible comparison value"); _debug_js_1._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 (0, math_bound_js_1.mathBound)(adjustValue(high), startIndex, length - 1); } exports.binaryFindInsertionIndex = binaryFindInsertionIndex; //# sourceMappingURL=binary-find-insertion-index.js.map