|
|
@ -1140,6 +1140,8 @@ exports.mergeOptions = function (mergeTarget, options, option) { |
|
|
|
exports.binarySearch = function(orderedItems, range, field, field2) { |
|
|
|
var array = orderedItems; |
|
|
|
|
|
|
|
var maxIterations = 10000; |
|
|
|
var iteration = 0; |
|
|
|
var found = false; |
|
|
|
var low = 0; |
|
|
|
var high = array.length; |
|
|
@ -1162,7 +1164,7 @@ exports.binarySearch = function(orderedItems, range, field, field2) { |
|
|
|
else { |
|
|
|
high -= 1; |
|
|
|
|
|
|
|
while (found == false) { |
|
|
|
while (found == false && iteration < maxIterations) { |
|
|
|
value = field2 === undefined ? array[guess][field] : array[guess][field][field2]; |
|
|
|
|
|
|
|
if (array[guess].isVisible(range)) { |
|
|
@ -1185,6 +1187,10 @@ exports.binarySearch = function(orderedItems, range, field, field2) { |
|
|
|
guess = Math.floor(0.5*(high+low)); |
|
|
|
} |
|
|
|
} |
|
|
|
iteration++; |
|
|
|
} |
|
|
|
if (iteration >= maxIterations) { |
|
|
|
console.log("BinarySearch too many iterations. Aborting."); |
|
|
|
} |
|
|
|
} |
|
|
|
return guess; |
|
|
@ -1208,6 +1214,8 @@ exports.binarySearch = function(orderedItems, range, field, field2) { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
exports.binarySearchGeneric = function(orderedItems, target, field, sidePreference) { |
|
|
|
var maxIterations = 10000; |
|
|
|
var iteration = 0; |
|
|
|
var array = orderedItems; |
|
|
|
var found = false; |
|
|
|
var low = 0; |
|
|
@ -1230,7 +1238,7 @@ exports.binarySearchGeneric = function(orderedItems, target, field, sidePreferen |
|
|
|
} |
|
|
|
else { |
|
|
|
high -= 1; |
|
|
|
while (found == false) { |
|
|
|
while (found == false && iteration < maxIterations) { |
|
|
|
prevValue = array[Math.max(0,guess - 1)][field]; |
|
|
|
value = array[guess][field]; |
|
|
|
nextValue = array[Math.min(array.length-1,guess + 1)][field]; |
|
|
@ -1252,10 +1260,10 @@ exports.binarySearchGeneric = function(orderedItems, target, field, sidePreferen |
|
|
|
} |
|
|
|
else { |
|
|
|
if (value < target) { // it is too small --> increase low
|
|
|
|
low = Math.floor(0.5*(high+low)); |
|
|
|
newLow = Math.floor(0.5*(high+low)); |
|
|
|
} |
|
|
|
else { // it is too big --> decrease high
|
|
|
|
high = Math.floor(0.5*(high+low)); |
|
|
|
newHigh = Math.floor(0.5*(high+low)); |
|
|
|
} |
|
|
|
newGuess = Math.floor(0.5*(high+low)); |
|
|
|
// not in list;
|
|
|
@ -1268,6 +1276,10 @@ exports.binarySearchGeneric = function(orderedItems, target, field, sidePreferen |
|
|
|
guess = Math.floor(0.5*(high+low)); |
|
|
|
} |
|
|
|
} |
|
|
|
iteration++; |
|
|
|
} |
|
|
|
if (iteration >= maxIterations) { |
|
|
|
console.log("BinarySearch too many iterations. Aborting."); |
|
|
|
} |
|
|
|
} |
|
|
|
return guess; |