-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays.js
executable file
·81 lines (77 loc) · 2.41 KB
/
arrays.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
71
72
73
74
75
76
77
78
79
80
81
exports.naturalOrder = naturalOrder = function(a,b) { return a==b? 0 : a<b? -1 : 1; }
exports.identity = identity = function(o) { return o; }
exports.bsearch = bsearch = function(arr, o, miss, comp) {
if (!comp) comp = naturalOrder;
var lo=0, hi=arr.length;
while (lo<hi) {
var mid = (lo+hi)>>1,
c = comp(o,arr[mid]);
if (c==0) return mid;
else if (c<0) hi=mid;
else lo=mid+1;
}
return miss? lo : -1;
}
exports.orderedArray = orderedArray = function(arr, compare) {
if (!arr) arr = [];
if (!compare) compare = naturalOrder;
arr.compare = compare;
arr.search = function(o) { return bsearch(arr, o, false, compare); }
arr.add = function(o) {
if (arguments.length>1) {
for (var i=0; i<arguments.length; i++)
arr.add(arguments[i]);
return;
}
var i = bsearch(arr, o, true, compare);
if (i<arr.length && compare(o, arr[i])==0) return -1;
arr.splice(i,0,o);
return i;
}
arr.remove = function(o) {
var i = this.search(o);
return (i>=0)? arr.splice(i,1)[0] : null;
}
arr.resort = function() {
arr.sort(compare);
}
arr.resort();
return arr;
}
exports.topArray = topArray = function(arr, compare, maximum) {
arr = orderedArray(arr,compare);
arr.maximum = maximum;
var oldAdd = arr.add;
arr.add = function(o) {
if (arguments.length>1) {
for (var i=0; i<arguments.length; i++)
arr.add(arguments[i]);
return;
}
if (this.length>=this.maximum) {
if (compare(o, this[this.length-1])>0)
// not better than the last item, so drop
return;
// else make space
this.pop();
}
oldAdd(o);
}
return arr;
}
exports.indexedArray = indexedArray = function(arr, indexer, compare) {
if (!arr) arr = [];
if (!indexer) indexer = identity;
if (!compare) compare = naturalOrder;
var itemComp = function(a,b) { return compare(indexer(a), indexer(b)); };
var keyComp = arr.keyComp = function(a,b) { return compare(a, indexer(b)); };
arr = orderedArray(arr, itemComp);
arr.indexer = indexer;
arr.searchKey = function(key) { return bsearch(arr, key, false, keyComp); }
arr.get = function(key) { return this[this.searchKey(key)]; }
arr.removeKey = function(key) {
var i = this.searchKey(key);
return (i>=0)? arr.splice(i,1)[0] : null;
}
return arr;
}