Bug 1060742 - Reorder BinarySearch.jsm arguments for easier curried search functions. r=adw

This commit is contained in:
Alexander J. Vincent 2014-09-08 22:28:41 -07:00
parent e7dda92793
commit 33b645579d
3 changed files with 18 additions and 15 deletions

View File

@ -18,8 +18,8 @@ this.BinarySearch = Object.freeze({
*
* @return The index of `target` in `array` or -1 if `target` is not found.
*/
indexOf: function (array, target, comparator) {
let [found, idx] = this.search(array, target, comparator);
indexOf: function (comparator, array, target) {
let [found, idx] = this.search(comparator, array, target);
return found ? idx : -1;
},
@ -32,17 +32,13 @@ this.BinarySearch = Object.freeze({
* @return The index in `array` where `target` may be inserted to keep `array`
* ordered.
*/
insertionIndexOf: function (array, target, comparator) {
return this.search(array, target, comparator)[1];
insertionIndexOf: function (comparator, array, target) {
return this.search(comparator, array, target)[1];
},
/**
* Searches for the given target in the given array.
*
* @param array
* An array whose elements are ordered by `comparator`.
* @param target
* The value to search for.
* @param comparator
* A function that takes two arguments and compares them, returning a
* negative number if the first should be ordered before the second,
@ -50,13 +46,17 @@ this.BinarySearch = Object.freeze({
* number if the second should be ordered before the first. The first
* argument is always `target`, and the second argument is a value
* from the array.
* @param array
* An array whose elements are ordered by `comparator`.
* @param target
* The value to search for.
* @return An array with two elements. If `target` is found, the first
* element is true, and the second element is its index in the array.
* If `target` is not found, the first element is false, and the
* second element is the index where it may be inserted to keep the
* array ordered.
*/
search: function (array, target, comparator) {
search: function (comparator, array, target) {
let low = 0;
let high = array.length - 1;
while (low <= high) {

View File

@ -632,8 +632,7 @@ let PlacesProvider = {
i++;
}
for (let link of outOfOrder) {
i = BinarySearch.insertionIndexOf(links, link,
Links.compareLinks.bind(Links));
i = BinarySearch.insertionIndexOf(Links.compareLinks, links, link);
links.splice(i, 0, link);
}
@ -871,6 +870,8 @@ let Links = {
* @return A negative number if aLink1 is ordered before aLink2, zero if
* aLink1 and aLink2 have the same ordering, or a positive number if
* aLink1 is ordered after aLink2.
*
* @note compareLinks's this object is bound to Links below.
*/
compareLinks: function Links_compareLinks(aLink1, aLink2) {
for (let prop of this._sortProperties) {
@ -1037,7 +1038,7 @@ let Links = {
},
_binsearch: function Links__binsearch(aArray, aLink, aMethod) {
return BinarySearch[aMethod](aArray, aLink, this.compareLinks.bind(this));
return BinarySearch[aMethod](this.compareLinks, aArray, aLink);
},
/**
@ -1066,6 +1067,8 @@ let Links = {
Ci.nsISupportsWeakReference])
};
Links.compareLinks = Links.compareLinks.bind(Links);
/**
* Singleton used to collect telemetry data.
*

View File

@ -67,13 +67,13 @@ function run_test() {
}
function ok(array, target, expectedFound, expectedIdx) {
let [found, idx] = BinarySearch.search(array, target, cmp);
let [found, idx] = BinarySearch.search(cmp, array, target);
do_check_eq(found, expectedFound);
do_check_eq(idx, expectedIdx);
idx = expectedFound ? expectedIdx : -1;
do_check_eq(BinarySearch.indexOf(array, target, cmp), idx);
do_check_eq(BinarySearch.insertionIndexOf(array, target, cmp), expectedIdx);
do_check_eq(BinarySearch.indexOf(cmp, array, target), idx);
do_check_eq(BinarySearch.insertionIndexOf(cmp, array, target), expectedIdx);
}
function cmp(num1, num2) {