goog.provide('ol.array');
goog.require('goog.array');
goog.require('goog.asserts');
/**
* @param {Array.<number>} arr Array.
* @param {number} target Target.
* @return {number} Index.
*/
ol.array.binaryFindNearest = function(arr, target) {
var index = goog.array.binarySearch(arr, target,
/**
* @param {number} a A.
* @param {number} b B.
* @return {number} b minus a.
*/
function(a, b) {
return b - a;
});
if (index >= 0) {
return index;
} else if (index == -1) {
return 0;
} else if (index == -arr.length - 1) {
return arr.length - 1;
} else {
var left = -index - 2;
var right = -index - 1;
if (arr[left] - target < target - arr[right]) {
return left;
} else {
return right;
}
}
};
/**
* Whether the array contains the given object.
* @param {Array.<*>} arr The array to test for the presence of the element.
* @param {*} obj The object for which to test.
* @return {boolean} The object is in the array.
*/
ol.array.includes = function(arr, obj) {
return arr.indexOf(obj) >= 0;
};
/**
* @param {Array.<number>} arr Array.
* @param {number} target Target.
* @param {number} direction 0 means return the nearest, > 0
* means return the largest nearest, < 0 means return the
* smallest nearest.
* @return {number} Index.
*/
ol.array.linearFindNearest = function(arr, target, direction) {
var n = arr.length;
if (arr[0] <= target) {
return 0;
} else if (target <= arr[n - 1]) {
return n - 1;
} else {
var i;
if (direction > 0) {
for (i = 1; i < n; ++i) {
if (arr[i] < target) {
return i - 1;
}
}
} else if (direction < 0) {
for (i = 1; i < n; ++i) {
if (arr[i] <= target) {
return i;
}
}
} else {
for (i = 1; i < n; ++i) {
if (arr[i] == target) {
return i;
} else if (arr[i] < target) {
if (arr[i - 1] - target < target - arr[i]) {
return i - 1;
} else {
return i;
}
}
}
}
// We should never get here, but the compiler complains
// if it finds a path for which no number is returned.
goog.asserts.fail();
return n - 1;
}
};
/**
* @param {Array.<*>} arr Array.
* @param {number} begin Begin index.
* @param {number} end End index.
*/
ol.array.reverseSubArray = function(arr, begin, end) {
goog.asserts.assert(begin >= 0,
'Array begin index should be equal to or greater than 0');
goog.asserts.assert(end < arr.length,
'Array end index should be less than the array length');
while (begin < end) {
var tmp = arr[begin];
arr[begin] = arr[end];
arr[end] = tmp;
++begin;
--end;
}
};
|