UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

35 lines (27 loc) 897 B
/** * @template T,V * @param {T[]} array * @param {V} el * @param {function(V,T):number} compareFunction * @param {number} [minIndex] * @param {number} [maxIndex] * @return {number} Index */ export function binarySearchLowIndex(array, el, compareFunction, minIndex = 0, maxIndex = array.length - 1) { let result = 0; while (minIndex <= maxIndex) { const pivotIndex = (minIndex + maxIndex) >> 1; const cmp = compareFunction(el, array[pivotIndex]); if (cmp > 0) { minIndex = pivotIndex + 1; } else if (cmp < 0) { maxIndex = pivotIndex - 1; result = maxIndex; } else { //set low boundary for next step based on assumption that upper bound is higher than lower bound result = pivotIndex; break; } } return result; }