-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathArray.js
70 lines (67 loc) · 1.94 KB
/
Array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Binary Search Stubs for JS Arrays
* @license MIT
* @author Jim Chen
*/
var BinArray = (function () {
var BinArray = {};
/**
* Performs binary search on the array
* Note: The array MUST ALREADY BE SORTED. Some cases will fail but we don't
* guarantee that we can catch all cases.
*
* @param arr - array to search on
* @param what - element to search for (may not be present)
* @param how - function comparator (a, b). Returns positive value if a > b
* @return index of the element (or index of the element if it were in the array)
**/
BinArray.bsearch = function (arr, what, how) {
if (!Array.isArray(arr)) {
throw new Error('Bsearch can only be run on arrays');
}
if (arr.length === 0) {
return 0;
}
if (how(what,arr[0]) < 0) {
return 0;
}
if (how(what,arr[arr.length - 1]) >= 0) {
return arr.length;
}
var low = 0;
var i = 0;
var count = 0;
var high = arr.length - 1;
while (low <= high) {
i = Math.floor((high + low + 1)/2);
count++;
if (how(what,arr[i-1]) >= 0 && how(what,arr[i]) < 0) {
return i;
} else if (how(what,arr[i-1]) < 0) {
high = i-1;
} else if (how(what,arr[i]) >= 0) {
low = i;
} else {
throw new Error('Program Error. Inconsistent comparator or unsorted array!');
}
if (count > 1500) {
throw new Error('Iteration depth exceeded. Inconsistent comparator or astronomical dataset!');
}
}
return -1;
};
/**
* Insert an element into its position in the array signified by bsearch
*
* @param arr - array to insert into
* @param what - element to insert
* @param how - comparator (see bsearch)
* @return index that the element was inserted to.
**/
BinArray.binsert = function (arr, what, how) {
var index = BinArray.bsearch(arr,what,how);
arr.splice(index,0,what);
return index;
};
return BinArray;
})();