-
Notifications
You must be signed in to change notification settings - Fork 2
/
Array.js
94 lines (85 loc) · 2.3 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Extensions to Array's prototype */
(function(arrayPrototype) {
if (!arrayPrototype.forEach) {
arrayPrototype.forEach = function(lambda, thisObject) {
for (var i=0 ; i<this.length ; i++) {
lambda.call(thisObject || window, this[i], i, this);
}
};
}
if (!arrayPrototype.map) {
arrayPrototype.map = function(lambda, thisObject) {
var ret = [];
for (var i=0 ; i<this.length ; i++) {
ret.push(lambda.call(thisObject || window, this[i], i, this));
}
return ret;
};
}
if (!arrayPrototype.filter) {
arrayPrototype.filter = function(lambda, thisObject) {
var result = [];
for (var i=0 ; i<this.length ; i++) {
if (lambda.call(thisObject || window, this[i], i, this)) {
result.push(this[i]);
}
}
return result;
};
}
if (!Array.indexOf) {
arrayPrototype.indexOf = function(obj, fromIndex) {
for (var i = fromIndex || 0; i < this.length; i++) {
if (this[i] === obj) {
return i;
}
}
return -1;
};
}
/* Returns the index of 'item'. A negative index means that the item wasn't found, but should occupy index abs(index+1) if inserted. */
arrayPrototype.findInSorted = function(item, comparator) {
comparator = comparator || this.comparator;
var left = -1;
var right = this.length;
var mid;
while (right - left > 1) {
mid = (left + right) >>> 1;
var c = comparator(this[mid], item);
if(c<0) {
left = mid;
}else{
right = mid;
if (!c) {
break;
}
}
}
return (right === this.length || comparator(this[right], item)) ? -right-1 : right;
};
/* This is more efficient than relying on findInSorted: */
arrayPrototype.comparator = function (a, b) { return a<b ? -1 : (a>b ? 1 : 0); };
arrayPrototype.insertIntoSorted = function(item, comparator) {
comparator = comparator || this.comparator;
var left = -1;
var right = this.length;
var mid;
while (right - left > 1) {
mid = (left + right) >>> 1;
if(comparator(this[mid], item) < 0) {
left = mid;
}else{
right = mid;
}
}
this.splice(right, 0, item);
return right;
};
arrayPrototype.removeFromSorted = function(item, comparator) {
comparator = comparator || this.comparator;
var existingIndex = this.findInSorted(item, comparator);
if (existingIndex >= 0) {
this.splice(existingIndex, 1);
}
};
})(Array.prototype);