mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 00:57:48 +00:00
Bump to v3.0.0.
This commit is contained in:
40
internal/binaryIndex.js
Normal file
40
internal/binaryIndex.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import binaryIndexBy from './binaryIndexBy';
|
||||
import identity from '../utility/identity';
|
||||
|
||||
/** Used as references for the maximum length and index of an array. */
|
||||
var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1,
|
||||
HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
|
||||
|
||||
/**
|
||||
* Performs a binary search of `array` to determine the index at which `value`
|
||||
* should be inserted into `array` in order to maintain its sort order.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The sorted array to inspect.
|
||||
* @param {*} value The value to evaluate.
|
||||
* @param {boolean} [retHighest] Specify returning the highest, instead
|
||||
* of the lowest, index at which a value should be inserted into `array`.
|
||||
* @returns {number} Returns the index at which `value` should be inserted
|
||||
* into `array`.
|
||||
*/
|
||||
function binaryIndex(array, value, retHighest) {
|
||||
var low = 0,
|
||||
high = array ? array.length : low;
|
||||
|
||||
if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
|
||||
while (low < high) {
|
||||
var mid = (low + high) >>> 1,
|
||||
computed = array[mid];
|
||||
|
||||
if (retHighest ? (computed <= value) : (computed < value)) {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
return high;
|
||||
}
|
||||
return binaryIndexBy(array, value, identity, retHighest);
|
||||
}
|
||||
|
||||
export default binaryIndex;
|
||||
Reference in New Issue
Block a user