/
index.js
262 lines (207 loc) · 4.6 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Tolstoy — ascetic graph structure
*
* @module tolstoy
*/
/**
* @constructor
*
* @param {Iterable} list Initial structure, whether nodes or connections
*/
function Graph (arg) {
if (!(this instanceof Graph)) return new Graph(arg);
this.constructor(arg);
}
/**
* Separated constructor
* To let it able to be called separately from inheritants
*/
Graph.prototype.constructor = function (list) {
//set of nodes
this.nodes = new Set();
//set of connections for nodes
this.edges = new Map();
//have to keep these separately to reduce calculations on .disconnect(a) etc.
this.inputs = new Map();
this.outputs = new Map();
//passed other graph
if (list instanceof Graph) {
var self = this;
list.nodes.forEach(function (node) {
self.add(node);
});
list.edges.forEach(function (toSet, from) {
toSet.forEach(function (to) {
self.connect(from, to);
});
});
}
else if (list) {
//array-like arg
if (list.length) {
for (var i = 0, l = list.length; i < l; i++) {
var item = list[i];
//[1,2], [2,3], [3,1]
if (item instanceof Array) {
this.add(item[0]);
this.add(item[1]);
this.connect(item[0], item[1]);
}
//1, 2, 3
else {
this.add(item);
}
};
}
//single arg
else {
this.add(list);
}
}
}
/**
* Add a new node
*
* @param {*} a Anything as a node object
*
* @return {Graph} self reference, to chain
*/
Graph.prototype.add = function (a) {
if (this.nodes.has(a)) return this;
//save node
this.nodes.add(a);
//create set of connection for the node a
this.edges.set(a, new Set());
//create set of directed connections for the node a
this.inputs.set(a, new Set());
this.outputs.set(a, new Set());
return this;
};
/**
* Delete node
*
* @return {Boolean} Result of operation
*/
Graph.prototype.delete = function (a) {
if (!this.nodes.has(a)) return false;
//disconnect all pointing nodes
this.inputs.get(a).forEach(function (b) {
this.disconnect(b, a);
}, this);
//disconnect all outward nodes
this.disconnect(a);
//remove any reference
this.nodes.delete(a);
this.edges.delete(a);
this.inputs.delete(a);
this.outputs.delete(a);
return true;
};
/**
* Clear graph
*
* return undefined
*/
Graph.prototype.clear = function () {
this.nodes.forEach(this.delete, this);
};
/**
* Whether graph has node
*
* @return {Boolean} Result
*/
Graph.prototype.has = function (node) {
return this.nodes.has(node);
};
/**
* Iterator by nodes
*/
Graph.prototype.forEach = function (fn, ctx) {
return this.nodes.forEach(fn, ctx);
};
/**
* Connect node a to node b
*
* @return {Graph} Self
*/
Graph.prototype.connect = function (a, b) {
if (!this.has(a) || !this.has(b)) return this;
if (this.isConnected(a, b)) return this;
//save the connections
this.edges.get(a).add(b);
this.edges.get(b).add(a);
this.outputs.get(a).add(b);
this.inputs.get(b).add(a);
return this;
};
/**
* Disconnect node a from node b, one-direction
*
* @return {Boolean} Result of operation
*/
Graph.prototype.disconnect = function (a, b) {
if (!this.edges.has(a)) return false;
//if no b - disconnect node outputs
if (arguments.length < 2) {
this.outputs.get(a).forEach(function (b) {
this.disconnect(a, b);
}, this);
return true;
}
if (!this.edges.has(b)) return false;
if (!this.isConnected(a, b)) return false;
//delete connection
this.edges.get(a).delete(b);
this.outputs.get(a).delete(b);
this.inputs.get(b).delete(a);
return true;
};
/**
* Simple connection checker
*/
Graph.prototype.isConnected = function (a, b) {
if (!this.has(a) || !this.has(b)) return false;
return this.outputs.get(a).has(b);
};
/**
* Serialize to JSON
*/
Graph.prototype.toJSON = function () {
var result = {
//serialized objects
nodes: [],
//ids with connections, based on position in nodes
edges: []
};
var idxMap = new Map();
var i = 0;
this.nodes.forEach(function (node) {
idxMap.set(node, i++);
});
this.nodes.forEach(function (node) {
var idx = idxMap.get(node);
//nodes are like [2,3,4], by indexes
result.nodes[idx] = node;
result.edges[idx] = [];
//edges and inputs are like [[1,2], [2,3], ...], where numbers are node's idxes
this.edges.get(node).forEach(function (target) {
result.edges[idx].push(idxMap.get(target));
});
}, this);
idxMap.clear();
return result;
};
/**
* Deserialize from JSON
*/
Graph.prototype.fromJSON = function (obj) {
if (!obj.nodes) return;
for (var i = 0; i < obj.nodes.length; i++) {
this.add(obj.nodes[i]);
}
for (var i = 0; i < obj.edges.length; i++) {
this.connect(obj.nodes[i], obj.nodes[obj.edges[i]]);
}
return this;
};
module.exports = Graph;