-
Notifications
You must be signed in to change notification settings - Fork 184
/
sorted-array-set.js
54 lines (42 loc) · 1.65 KB
/
sorted-array-set.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
"use strict";
module.exports = SortedArraySet;
var Shim = require("./shim");
var SortedArray = require("./sorted-array");
var GenericSet = require("./generic-set");
var PropertyChanges = require("./listen/property-changes");
function SortedArraySet(values, equals, compare, getDefault) {
if (!(this instanceof SortedArraySet)) {
return new SortedArraySet(values, equals, compare, getDefault);
}
SortedArray.call(this, values, equals, compare, getDefault);
}
// hack so require("sorted-array-set".SortedArraySet works in MontageJS
SortedArraySet.SortedArraySet = SortedArraySet;
SortedArraySet.prototype = Object.create(SortedArray.prototype);
SortedArraySet.prototype.constructor = SortedArraySet;
Object.addEach(SortedArraySet.prototype, GenericSet.prototype);
Object.addEach(SortedArraySet.prototype, PropertyChanges.prototype);
SortedArraySet.from = SortedArray.from;
SortedArraySet.prototype.isSorted = true;
SortedArraySet.prototype.add = function (value) {
if (!this.has(value)) {
SortedArray.prototype.add.call(this, value);
return true;
} else {
return false;
}
};
SortedArraySet.prototype.reduce = function (callback, basis /*, thisp*/) {
var self = this;
var thisp = arguments[2];
return this.array.reduce(function (basis, value, index) {
return callback.call(thisp, basis, value, index, self);
}, basis);
};
SortedArraySet.prototype.reduceRight = function (callback, basis /*, thisp*/) {
var self = this;
var thisp = arguments[2];
return this.array.reduceRight(function (basis, value, index) {
return callback.call(thisp, basis, value, index, self);
}, basis);
};