This repository was archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgraph.js
63 lines (54 loc) · 1.85 KB
/
graph.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
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/* Graphs represented using adjacency sets. */
if(typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require, exports) {
var numset = require('./numset');
function Graph() {
this.succ = [];
};
var id2node = Graph.prototype.id2node = [];
var nextNodeId = 1;
var nodeId = Graph.prototype.nodeId = function nodeId(nd) {
var id = nd.attr.hasOwnProperty('node_id') ? nd.attr.node_id
: (nd.attr.node_id = nextNodeId++);
id2node[+id] = nd;
return id;
};
Graph.prototype.addEdge = function(from, to) {
var fromId = nodeId(from), toId = nodeId(to);
if(fromId === toId)
return;
this.succ[fromId] = numset.add(this.succ[fromId], toId);
};
Graph.prototype.addEdges = function(from, tos) {
for(var i=0;i<tos.length;++i)
this.addEdge(from, tos[i]);
};
Graph.prototype.iter = function(cb) {
for(var i=0;i<this.succ.length;++i) {
if(!this.succ[i])
continue;
var from = id2node[i];
numset.iter(this.succ[i], function(succ) {
cb(from, id2node[succ]);
});
}
};
Graph.prototype.hasEdge = function(from, to) {
var fromId = nodeId(from), toId = nodeId(to);
return numset.contains(this.succ[fromId], toId);
}
exports.Graph = Graph;
return exports;
});