Debian Codegraph @

lib/util.js

raw symbols SCIP
1 /* eslint "no-console": off */
2
3 "use strict";
4
5 var _ = require("./lodash");
6 var Graph = require("./graphlib").Graph;
7
8 module.exports = {
9 addDummyNode: addDummyNode,
10 simplify: simplify,
11 asNonCompoundGraph: asNonCompoundGraph,
12 successorWeights: successorWeights,
13 predecessorWeights: predecessorWeights,
14 intersectRect: intersectRect,
15 buildLayerMatrix: buildLayerMatrix,
16 normalizeRanks: normalizeRanks,
17 removeEmptyRanks: removeEmptyRanks,
18 addBorderNode: addBorderNode,
19 maxRank: maxRank,
20 partition: partition,
21 time: time,
22 notime: notime
23 };
24
25 /*
26 * Adds a dummy node to the graph and return v.
27 */
28 function addDummyNode(g, type, attrs, name) {
29 var v;
30 do {
31 v = _.uniqueId(name);
32 } while (g.hasNode(v));
33
34 attrs.dummy = type;
35 g.setNode(v, attrs);
36 return v;
37 }
38
39 /*
40 * Returns a new graph with only simple edges. Handles aggregation of data
41 * associated with multi-edges.
42 */
43 function simplify(g) {
44 var simplified = new Graph().setGraph(g.graph());
45 _.forEach(g.nodes(), function(v) { simplified.setNode(v, g.node(v)); });
46 _.forEach(g.edges(), function(e) {
47 var simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
48 var label = g.edge(e);
49 simplified.setEdge(e.v, e.w, {
50 weight: simpleLabel.weight + label.weight,
51 minlen: Math.max(simpleLabel.minlen, label.minlen)
52 });
53 });
54 return simplified;
55 }
56
57 function asNonCompoundGraph(g) {
58 var simplified = new Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
59 _.forEach(g.nodes(), function(v) {
60 if (!g.children(v).length) {
61 simplified.setNode(v, g.node(v));
62 }
63 });
64 _.forEach(g.edges(), function(e) {
65 simplified.setEdge(e, g.edge(e));
66 });
67 return simplified;
68 }
69
70 function successorWeights(g) {
71 var weightMap = _.map(g.nodes(), function(v) {
72 var sucs = {};
73 _.forEach(g.outEdges(v), function(e) {
74 sucs[e.w] = (sucs[e.w] || 0) + g.edge(e).weight;
75 });
76 return sucs;
77 });
78 return _.zipObject(g.nodes(), weightMap);
79 }
80
81 function predecessorWeights(g) {
82 var weightMap = _.map(g.nodes(), function(v) {
83 var preds = {};
84 _.forEach(g.inEdges(v), function(e) {
85 preds[e.v] = (preds[e.v] || 0) + g.edge(e).weight;
86 });
87 return preds;
88 });
89 return _.zipObject(g.nodes(), weightMap);
90 }
91
92 /*
93 * Finds where a line starting at point ({x, y}) would intersect a rectangle
94 * ({x, y, width, height}) if it were pointing at the rectangle's center.
95 */
96 function intersectRect(rect, point) {
97 var x = rect.x;
98 var y = rect.y;
99
100 // Rectangle intersection algorithm from:
101 // http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes
102 var dx = point.x - x;
103 var dy = point.y - y;
104 var w = rect.width / 2;
105 var h = rect.height / 2;
106
107 if (!dx && !dy) {
108 throw new Error("Not possible to find intersection inside of the rectangle");
109 }
110
111 var sx, sy;
112 if (Math.abs(dy) * w > Math.abs(dx) * h) {
113 // Intersection is top or bottom of rect.
114 if (dy < 0) {
115 h = -h;
116 }
117 sx = h * dx / dy;
118 sy = h;
119 } else {
120 // Intersection is left or right of rect.
121 if (dx < 0) {
122 w = -w;
123 }
124 sx = w;
125 sy = w * dy / dx;
126 }
127
128 return { x: x + sx, y: y + sy };
129 }
130
131 /*
132 * Given a DAG with each node assigned "rank" and "order" properties, this
133 * function will produce a matrix with the ids of each node.
134 */
135 function buildLayerMatrix(g) {
136 var layering = _.map(_.range(maxRank(g) + 1), function() { return []; });
137 _.forEach(g.nodes(), function(v) {
138 var node = g.node(v);
139 var rank = node.rank;
140 if (!_.isUndefined(rank)) {
141 layering[rank][node.order] = v;
142 }
143 });
144 return layering;
145 }
146
147 /*
148 * Adjusts the ranks for all nodes in the graph such that all nodes v have
149 * rank(v) >= 0 and at least one node w has rank(w) = 0.
150 */
151 function normalizeRanks(g) {
152 var min = _.min(_.map(g.nodes(), function(v) { return g.node(v).rank; }));
153 _.forEach(g.nodes(), function(v) {
154 var node = g.node(v);
155 if (_.has(node, "rank")) {
156 node.rank -= min;
157 }
158 });
159 }
160
161 function removeEmptyRanks(g) {
162 // Ranks may not start at 0, so we need to offset them
163 var offset = _.min(_.map(g.nodes(), function(v) { return g.node(v).rank; }));
164
165 var layers = [];
166 _.forEach(g.nodes(), function(v) {
167 var rank = g.node(v).rank - offset;
168 if (!layers[rank]) {
169 layers[rank] = [];
170 }
171 layers[rank].push(v);
172 });
173
174 var delta = 0;
175 var nodeRankFactor = g.graph().nodeRankFactor;
176 _.forEach(layers, function(vs, i) {
177 if (_.isUndefined(vs) && i % nodeRankFactor !== 0) {
178 --delta;
179 } else if (delta) {
180 _.forEach(vs, function(v) { g.node(v).rank += delta; });
181 }
182 });
183 }
184
185 function addBorderNode(g, prefix, rank, order) {
186 var node = {
187 width: 0,
188 height: 0
189 };
190 if (arguments.length >= 4) {
191 node.rank = rank;
192 node.order = order;
193 }
194 return addDummyNode(g, "border", node, prefix);
195 }
196
197 function maxRank(g) {
198 return _.max(_.map(g.nodes(), function(v) {
199 var rank = g.node(v).rank;
200 if (!_.isUndefined(rank)) {
201 return rank;
202 }
203 }));
204 }
205
206 /*
207 * Partition a collection into two groups: `lhs` and `rhs`. If the supplied
208 * function returns true for an entry it goes into `lhs`. Otherwise it goes
209 * into `rhs.
210 */
211 function partition(collection, fn) {
212 var result = { lhs: [], rhs: [] };
213 _.forEach(collection, function(value) {
214 if (fn(value)) {
215 result.lhs.push(value);
216 } else {
217 result.rhs.push(value);
218 }
219 });
220 return result;
221 }
222
223 /*
224 * Returns a new function that wraps `fn` with a timer. The wrapper logs the
225 * time it takes to execute the function.
226 */
227 function time(name, fn) {
228 var start = _.now();
229 try {
230 return fn();
231 } finally {
232 console.log(name + " time: " + (_.now() - start) + "ms");
233 }
234 }
235
236 function notime(name, fn) {
237 return fn();
238 }