Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / binaryIndex.js
1 var binaryIndexBy = require('./binaryIndexBy'),
2     identity = require('../utility/identity');
3
4 /** Used as references for the maximum length and index of an array. */
5 var MAX_ARRAY_LENGTH = 4294967295,
6     HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
7
8 /**
9  * Performs a binary search of `array` to determine the index at which `value`
10  * should be inserted into `array` in order to maintain its sort order.
11  *
12  * @private
13  * @param {Array} array The sorted array to inspect.
14  * @param {*} value The value to evaluate.
15  * @param {boolean} [retHighest] Specify returning the highest qualified index.
16  * @returns {number} Returns the index at which `value` should be inserted
17  *  into `array`.
18  */
19 function binaryIndex(array, value, retHighest) {
20   var low = 0,
21       high = array ? array.length : low;
22
23   if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
24     while (low < high) {
25       var mid = (low + high) >>> 1,
26           computed = array[mid];
27
28       if ((retHighest ? (computed <= value) : (computed < value)) && computed !== null) {
29         low = mid + 1;
30       } else {
31         high = mid;
32       }
33     }
34     return high;
35   }
36   return binaryIndexBy(array, value, identity, retHighest);
37 }
38
39 module.exports = binaryIndex;