forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.js
158 lines (139 loc) · 4.39 KB
/
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
var Shim = require("./shim");
var List = require("./list");
var FastSet = require("./fast-set");
var GenericCollection = require("./generic-collection");
var GenericSet = require("./generic-set");
var PropertyChanges = require("./listen/property-changes");
var RangeChanges = require("./listen/range-changes");
module.exports = Set;
function Set(values, equals, hash, getDefault) {
if (!(this instanceof Set)) {
return new Set(values, equals, hash, getDefault);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentHash = hash;
this.getDefault = getDefault;
// a list of values in insertion order, used for all operations that depend
// on iterating in insertion order
this.order = new this.Order(undefined, equals);
// a set of nodes from the order list, indexed by the corresponding value,
// used for all operations that need to quickly seek value in the list
this.store = new this.Store(
undefined,
function (a, b) {
return equals(a.value, b.value);
},
function (node) {
return hash(node.value);
}
);
this.length = 0;
this.addEach(values);
}
Object.addEach(Set.prototype, GenericCollection.prototype);
Object.addEach(Set.prototype, GenericSet.prototype);
Object.addEach(Set.prototype, PropertyChanges.prototype);
Object.addEach(Set.prototype, RangeChanges.prototype);
Set.prototype.Order = List;
Set.prototype.Store = FastSet;
Set.prototype.constructClone = function (values) {
return new this.constructor(values, this.contentEquals, this.contentHash, this.getDefault);
};
Set.prototype.has = function (value) {
var node = new this.order.Node(value);
return this.store.has(node);
};
Set.prototype.get = function (value) {
var node = new this.order.Node(value);
node = this.store.get(node);
if (node) {
return node.value;
} else {
return this.getDefault(value);
}
};
Set.prototype.add = function (value) {
var node = new this.order.Node(value);
if (!this.store.has(node)) {
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange([value], [], 0);
}
this.order.add(value);
node = this.order.head.prev;
this.store.add(node);
this.length++;
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange([value], [], 0);
}
return true;
}
return false;
};
Set.prototype["delete"] = function (value) {
var node = new this.order.Node(value);
if (this.store.has(node)) {
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange([], [value], 0);
}
var node = this.store.get(node);
this.store["delete"](node); // removes from the set
node["delete"](); // removes the node from the list in place
this.length--;
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange([], [value], 0);
}
return true;
}
return false;
};
Set.prototype.pop = function () {
if (this.length) {
var result = this.order.head.prev.value;
this["delete"](result);
return result;
}
};
Set.prototype.shift = function () {
if (this.length) {
var result = this.order.head.next.value;
this["delete"](result);
return result;
}
};
Set.prototype.one = function () {
if (this.length > 0) {
return this.store.one().value;
}
};
Set.prototype.clear = function () {
this.store.clear();
this.order.clear();
this.length = 0;
};
Set.prototype.reduce = function (callback, basis /*, thisp*/) {
var thisp = arguments[2];
var list = this.order;
var index = 0;
return list.reduce(function (basis, value) {
return callback.call(thisp, basis, value, index++, this);
}, basis, this);
};
Set.prototype.reduceRight = function (callback, basis /*, thisp*/) {
var thisp = arguments[2];
var list = this.order;
var index = this.length - 1;
return list.reduceRight(function (basis, value) {
return callback.call(thisp, basis, value, index--, this);
}, basis, this);
};
Set.prototype.iterate = function () {
return this.order.iterate();
};
Set.prototype.log = function () {
var set = this.store;
return set.log.apply(set, arguments);
};