forked from jensnicolay/jipda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
181 lines (161 loc) · 3.66 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
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
"use strict";
function Edge(source, g, target)
{
this.source = source;
this.g = g;
this.target = target;
var prime = 7;
var result = 1;
result = prime * result + this.source.hashCode();
result = prime * result + HashCode.hashCode(this.g);
result = prime * result + this.target.hashCode();
this._hashCode = result;
}
Edge.source =
function (edge)
{
return edge.source;
}
Edge.target =
function (edge)
{
return edge.target;
}
Edge.prototype.equals =
function (x)
{
return x instanceof Edge
&& (this.source === x.source || this.source.equals(x.source))
&& (this.g === x.g || (this.g && this.g.equals(x.g)))
&& (this.target === x.target || this.target.equals(x.target))
}
Edge.prototype.toString =
function ()
{
return "{" + this.source + "," + this.g + "," + this.target + "}";
}
Edge.prototype.hashCode =
function ()
{
return this._hashCode;
}
function Graph(fw, bw)
{
this._fw = fw;
this._bw = bw;
}
Graph._emptySet = ArraySet.empty();
Graph.empty =
function ()
{
return new Graph(new TrieMap.empty(), new TrieMap.empty());
}
Graph.prototype.equals =
function (x)
{
return x instanceof Graph
&& this._fw.equals(x._fw);
}
Graph.prototype.hashCode =
function ()
{
return this._fw.hashCode();
}
Graph.prototype.toString =
function ()
{
return this.nodes().length + "/" + this.edges().length;
}
Graph.prototype.join =
function (x)
{
return new Graph(this._fw.join(x._fw), this._bw.join(x._bw));
}
Graph.prototype.addEdge =
function (edge)
{
var fw = this._fw;
var source = edge.source;
var fwedges = fw.get(source) || Graph._emptySet;
if (fwedges.contains(edge))
{
return this;
}
fwedges = fwedges.add(edge);
fw = fw.put(source, fwedges);
var target = edge.target;
var bw = this._bw;
var bwedges = bw.get(target) || Graph._emptySet;
bwedges = bwedges.add(edge);
bw = bw.put(target, bwedges);
return new Graph(fw, bw);
}
Graph.prototype.addEdges =
function (edges)
{
return edges.reduce(function (edges, edge) {return edges.add(edge)}, this._edges);
}
//Graph.prototype.removeEdge =
// function (edge)
// {
// return new Graph(this._edges.remove(edge));
// }
//
//Graph.prototype.removeEdges =
// function (edges)
// {
// return new Graph(this._edges.removeAll(edges));
// }
Graph.prototype.containsEdge =
function (edge)
{
var source = edge.source;
var fwedges = this._fw.get(source) || Graph._emptySet;
return fwedges.contains(edge);
}
Graph.prototype.containsSource =
function (source)
{
var fwedges = this._fw.get(source);
return !!fwedges;
}
Graph.prototype.containsTarget =
function (target)
{
var bwedges = this._bw.get(target);
return !!bwedges;
}
Graph.prototype.outgoing =
function (source)
{
var fwedges = this._fw.get(source) || Graph._emptySet;
return fwedges.values();
}
Graph.prototype.incoming =
function (target)
{
var bwedges = this._bw.get(target) || Graph._emptySet;
return bwedges.values();
}
Graph.prototype.successors =
function (source)
{
var fwedges = this._fw.get(source) || Graph._emptySet;
return Arrays.deleteDuplicates(fwedges.values().map(Edge.target));
}
Graph.prototype.predecessors =
function (target)
{
var bwedges = this._bw.get(target) || Graph._emptySet;
return Arrays.deleteDuplicates(bwedges.values().map(Edge.source));
}
Graph.prototype.nodes =
function ()
{
return Arrays.union(this._fw.keys(), this._bw.keys());
}
Graph.prototype.edges =
function ()
{
return this._fw.values().flatMap(function (x) {return x.values()});
}