UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

39 lines (31 loc) 784 B
/** * @template T,V * @param {T[]|ArrayLike<T>|Float32Array} array * @param {V} el * @param {function(V,T):number} compareFunction * @param {number} [minIndex] * @param {number} [maxIndex] * @return {number} Index */ export function binarySearchHighIndex( array, el, compareFunction, minIndex = 0, maxIndex = array.length - 1 ) { let min = minIndex; let max = maxIndex; while (min <= max) { const pivotIndex = (min + max) >> 1; const cmp = compareFunction(el, array[pivotIndex]); if (cmp < 0) { max = pivotIndex - 1; } else if (cmp > 0) { min = pivotIndex + 1; } else { return pivotIndex; } } return min; }