Remove low and high from binarysearch, use numberSafeCompareFunction
This commit is contained in:
+6
-25
@@ -2,9 +2,6 @@ goog.provide('ol.array');
|
|||||||
|
|
||||||
goog.require('goog.asserts');
|
goog.require('goog.asserts');
|
||||||
|
|
||||||
function defaultCompare(a, b) {
|
|
||||||
return a > b ? 1 : a < b ? -1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a binary search on the provided sorted list and returns the index of the item if found. If it can't be found it'll return -1.
|
* Performs a binary search on the provided sorted list and returns the index of the item if found. If it can't be found it'll return -1.
|
||||||
@@ -12,31 +9,14 @@ function defaultCompare(a, b) {
|
|||||||
*
|
*
|
||||||
* @param {Array<*>} haystack Items to search through.
|
* @param {Array<*>} haystack Items to search through.
|
||||||
* @param {*} needle The item to look for.
|
* @param {*} needle The item to look for.
|
||||||
* @param {Function=} opt_comparator Comparator function
|
* @param {Function=} opt_comparator Comparator function.
|
||||||
* @param {number=} low Lower bounds.
|
|
||||||
* @param {number=} high Higher bounds.
|
|
||||||
* @return {number} The index of the item if found, -1 if not.
|
* @return {number} The index of the item if found, -1 if not.
|
||||||
*/
|
*/
|
||||||
ol.array.binarySearch = function(haystack, needle, opt_comparator, low, high) {
|
ol.array.binarySearch = function(haystack, needle, opt_comparator) {
|
||||||
var mid, cmp;
|
var mid, cmp;
|
||||||
var comparator = opt_comparator || defaultCompare;
|
var comparator = opt_comparator || ol.array.numberSafeCompareFunction;
|
||||||
if (low === undefined)
|
var low = 0;
|
||||||
low = 0;
|
var high = haystack.length - 1;
|
||||||
|
|
||||||
else {
|
|
||||||
low = low | 0;
|
|
||||||
if (low < 0 || low >= haystack.length)
|
|
||||||
throw new RangeError('invalid lower bound');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (high === undefined)
|
|
||||||
high = haystack.length - 1;
|
|
||||||
|
|
||||||
else {
|
|
||||||
high = high | 0;
|
|
||||||
if (high < low || high >= haystack.length)
|
|
||||||
throw new RangeError('invalid upper bound');
|
|
||||||
}
|
|
||||||
|
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
/* Note that "(low + high) >>> 1" may overflow, and results in a typecast
|
/* Note that "(low + high) >>> 1" may overflow, and results in a typecast
|
||||||
@@ -185,6 +165,7 @@ ol.array.reverseSubArray = function(arr, begin, end) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Array.<*>} arr Array.
|
* @param {Array.<*>} arr Array.
|
||||||
* @return {!Array<?>} Flattened Array.
|
* @return {!Array<?>} Flattened Array.
|
||||||
|
|||||||
Reference in New Issue
Block a user