1 | import { a as isSymbol, b as baseFlatten, c as baseClone, d as baseIteratee, k as keys, e as baseFindIndex, g as baseEach, j as arrayMap, l as castFunction, m as baseForOwn, n as castPath, t as toKey, o as baseGet, p as hasIn, q as toString, f as forEach, G as Graph, h as has, i as isUndefined, r as filter, v as values, s as reduce } from "./graph-fe24fab6.js";
|
2 | import { a8 as isObject, a9 as setToString, aa as overRest, ab as root, ac as baseRest, ad as isIterateeCall, ae as keysIn, af as eq, ag as isArrayLike, ah as isArray, ai as baseFor, aj as baseAssignValue, ak as identity, al as isIndex, am as assignValue, an as baseUnary, ao as constant, ap as merge } from "./mermaid-dcacb631.js";
|
3 | var reWhitespace = /\s/;
|
4 | function trimmedEndIndex(string) {
|
5 | var index = string.length;
|
6 | while (index-- && reWhitespace.test(string.charAt(index))) {
|
7 | }
|
8 | return index;
|
9 | }
|
10 | var reTrimStart = /^\s+/;
|
11 | function baseTrim(string) {
|
12 | return string ? string.slice(0, trimmedEndIndex(string) + 1).replace(reTrimStart, "") : string;
|
13 | }
|
14 | var NAN = 0 / 0;
|
15 | var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
|
16 | var reIsBinary = /^0b[01]+$/i;
|
17 | var reIsOctal = /^0o[0-7]+$/i;
|
18 | var freeParseInt = parseInt;
|
19 | function toNumber(value) {
|
20 | if (typeof value == "number") {
|
21 | return value;
|
22 | }
|
23 | if (isSymbol(value)) {
|
24 | return NAN;
|
25 | }
|
26 | if (isObject(value)) {
|
27 | var other = typeof value.valueOf == "function" ? value.valueOf() : value;
|
28 | value = isObject(other) ? other + "" : other;
|
29 | }
|
30 | if (typeof value != "string") {
|
31 | return value === 0 ? value : +value;
|
32 | }
|
33 | value = baseTrim(value);
|
34 | var isBinary = reIsBinary.test(value);
|
35 | return isBinary || reIsOctal.test(value) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : reIsBadHex.test(value) ? NAN : +value;
|
36 | }
|
37 | var INFINITY = 1 / 0, MAX_INTEGER = 17976931348623157e292;
|
38 | function toFinite(value) {
|
39 | if (!value) {
|
40 | return value === 0 ? value : 0;
|
41 | }
|
42 | value = toNumber(value);
|
43 | if (value === INFINITY || value === -INFINITY) {
|
44 | var sign = value < 0 ? -1 : 1;
|
45 | return sign * MAX_INTEGER;
|
46 | }
|
47 | return value === value ? value : 0;
|
48 | }
|
49 | function toInteger(value) {
|
50 | var result = toFinite(value), remainder = result % 1;
|
51 | return result === result ? remainder ? result - remainder : result : 0;
|
52 | }
|
53 | function flatten(array) {
|
54 | var length = array == null ? 0 : array.length;
|
55 | return length ? baseFlatten(array, 1) : [];
|
56 | }
|
57 | function flatRest(func) {
|
58 | return setToString(overRest(func, void 0, flatten), func + "");
|
59 | }
|
60 | var CLONE_DEEP_FLAG = 1, CLONE_SYMBOLS_FLAG = 4;
|
61 | function cloneDeep(value) {
|
62 | return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG);
|
63 | }
|
64 | var now = function() {
|
65 | return root.Date.now();
|
66 | };
|
67 | const now$1 = now;
|
68 | var objectProto = Object.prototype;
|
69 | var hasOwnProperty = objectProto.hasOwnProperty;
|
70 | var defaults = baseRest(function(object, sources) {
|
71 | object = Object(object);
|
72 | var index = -1;
|
73 | var length = sources.length;
|
74 | var guard = length > 2 ? sources[2] : void 0;
|
75 | if (guard && isIterateeCall(sources[0], sources[1], guard)) {
|
76 | length = 1;
|
77 | }
|
78 | while (++index < length) {
|
79 | var source = sources[index];
|
80 | var props = keysIn(source);
|
81 | var propsIndex = -1;
|
82 | var propsLength = props.length;
|
83 | while (++propsIndex < propsLength) {
|
84 | var key = props[propsIndex];
|
85 | var value = object[key];
|
86 | if (value === void 0 || eq(value, objectProto[key]) && !hasOwnProperty.call(object, key)) {
|
87 | object[key] = source[key];
|
88 | }
|
89 | }
|
90 | }
|
91 | return object;
|
92 | });
|
93 | const defaults$1 = defaults;
|
94 | function last(array) {
|
95 | var length = array == null ? 0 : array.length;
|
96 | return length ? array[length - 1] : void 0;
|
97 | }
|
98 | function createFind(findIndexFunc) {
|
99 | return function(collection, predicate, fromIndex) {
|
100 | var iterable = Object(collection);
|
101 | if (!isArrayLike(collection)) {
|
102 | var iteratee = baseIteratee(predicate);
|
103 | collection = keys(collection);
|
104 | predicate = function(key) {
|
105 | return iteratee(iterable[key], key, iterable);
|
106 | };
|
107 | }
|
108 | var index = findIndexFunc(collection, predicate, fromIndex);
|
109 | return index > -1 ? iterable[iteratee ? collection[index] : index] : void 0;
|
110 | };
|
111 | }
|
112 | var nativeMax$1 = Math.max;
|
113 | function findIndex(array, predicate, fromIndex) {
|
114 | var length = array == null ? 0 : array.length;
|
115 | if (!length) {
|
116 | return -1;
|
117 | }
|
118 | var index = fromIndex == null ? 0 : toInteger(fromIndex);
|
119 | if (index < 0) {
|
120 | index = nativeMax$1(length + index, 0);
|
121 | }
|
122 | return baseFindIndex(array, baseIteratee(predicate), index);
|
123 | }
|
124 | var find = createFind(findIndex);
|
125 | const find$1 = find;
|
126 | function baseMap(collection, iteratee) {
|
127 | var index = -1, result = isArrayLike(collection) ? Array(collection.length) : [];
|
128 | baseEach(collection, function(value, key, collection2) {
|
129 | result[++index] = iteratee(value, key, collection2);
|
130 | });
|
131 | return result;
|
132 | }
|
133 | function map(collection, iteratee) {
|
134 | var func = isArray(collection) ? arrayMap : baseMap;
|
135 | return func(collection, baseIteratee(iteratee));
|
136 | }
|
137 | function forIn(object, iteratee) {
|
138 | return object == null ? object : baseFor(object, castFunction(iteratee), keysIn);
|
139 | }
|
140 | function forOwn(object, iteratee) {
|
141 | return object && baseForOwn(object, castFunction(iteratee));
|
142 | }
|
143 | function baseGt(value, other) {
|
144 | return value > other;
|
145 | }
|
146 | function baseLt(value, other) {
|
147 | return value < other;
|
148 | }
|
149 | function mapValues(object, iteratee) {
|
150 | var result = {};
|
151 | iteratee = baseIteratee(iteratee);
|
152 | baseForOwn(object, function(value, key, object2) {
|
153 | baseAssignValue(result, key, iteratee(value, key, object2));
|
154 | });
|
155 | return result;
|
156 | }
|
157 | function baseExtremum(array, iteratee, comparator) {
|
158 | var index = -1, length = array.length;
|
159 | while (++index < length) {
|
160 | var value = array[index], current = iteratee(value);
|
161 | if (current != null && (computed === void 0 ? current === current && !isSymbol(current) : comparator(current, computed))) {
|
162 | var computed = current, result = value;
|
163 | }
|
164 | }
|
165 | return result;
|
166 | }
|
167 | function max(array) {
|
168 | return array && array.length ? baseExtremum(array, identity, baseGt) : void 0;
|
169 | }
|
170 | function min(array) {
|
171 | return array && array.length ? baseExtremum(array, identity, baseLt) : void 0;
|
172 | }
|
173 | function minBy(array, iteratee) {
|
174 | return array && array.length ? baseExtremum(array, baseIteratee(iteratee), baseLt) : void 0;
|
175 | }
|
176 | function baseSet(object, path, value, customizer) {
|
177 | if (!isObject(object)) {
|
178 | return object;
|
179 | }
|
180 | path = castPath(path, object);
|
181 | var index = -1, length = path.length, lastIndex = length - 1, nested = object;
|
182 | while (nested != null && ++index < length) {
|
183 | var key = toKey(path[index]), newValue = value;
|
184 | if (key === "__proto__" || key === "constructor" || key === "prototype") {
|
185 | return object;
|
186 | }
|
187 | if (index != lastIndex) {
|
188 | var objValue = nested[key];
|
189 | newValue = customizer ? customizer(objValue, key, nested) : void 0;
|
190 | if (newValue === void 0) {
|
191 | newValue = isObject(objValue) ? objValue : isIndex(path[index + 1]) ? [] : {};
|
192 | }
|
193 | }
|
194 | assignValue(nested, key, newValue);
|
195 | nested = nested[key];
|
196 | }
|
197 | return object;
|
198 | }
|
199 | function basePickBy(object, paths, predicate) {
|
200 | var index = -1, length = paths.length, result = {};
|
201 | while (++index < length) {
|
202 | var path = paths[index], value = baseGet(object, path);
|
203 | if (predicate(value, path)) {
|
204 | baseSet(result, castPath(path, object), value);
|
205 | }
|
206 | }
|
207 | return result;
|
208 | }
|
209 | function baseSortBy(array, comparer) {
|
210 | var length = array.length;
|
211 | array.sort(comparer);
|
212 | while (length--) {
|
213 | array[length] = array[length].value;
|
214 | }
|
215 | return array;
|
216 | }
|
217 | function compareAscending(value, other) {
|
218 | if (value !== other) {
|
219 | var valIsDefined = value !== void 0, valIsNull = value === null, valIsReflexive = value === value, valIsSymbol = isSymbol(value);
|
220 | var othIsDefined = other !== void 0, othIsNull = other === null, othIsReflexive = other === other, othIsSymbol = isSymbol(other);
|
221 | if (!othIsNull && !othIsSymbol && !valIsSymbol && value > other || valIsSymbol && othIsDefined && othIsReflexive && !othIsNull && !othIsSymbol || valIsNull && othIsDefined && othIsReflexive || !valIsDefined && othIsReflexive || !valIsReflexive) {
|
222 | return 1;
|
223 | }
|
224 | if (!valIsNull && !valIsSymbol && !othIsSymbol && value < other || othIsSymbol && valIsDefined && valIsReflexive && !valIsNull && !valIsSymbol || othIsNull && valIsDefined && valIsReflexive || !othIsDefined && valIsReflexive || !othIsReflexive) {
|
225 | return -1;
|
226 | }
|
227 | }
|
228 | return 0;
|
229 | }
|
230 | function compareMultiple(object, other, orders) {
|
231 | var index = -1, objCriteria = object.criteria, othCriteria = other.criteria, length = objCriteria.length, ordersLength = orders.length;
|
232 | while (++index < length) {
|
233 | var result = compareAscending(objCriteria[index], othCriteria[index]);
|
234 | if (result) {
|
235 | if (index >= ordersLength) {
|
236 | return result;
|
237 | }
|
238 | var order2 = orders[index];
|
239 | return result * (order2 == "desc" ? -1 : 1);
|
240 | }
|
241 | }
|
242 | return object.index - other.index;
|
243 | }
|
244 | function baseOrderBy(collection, iteratees, orders) {
|
245 | if (iteratees.length) {
|
246 | iteratees = arrayMap(iteratees, function(iteratee) {
|
247 | if (isArray(iteratee)) {
|
248 | return function(value) {
|
249 | return baseGet(value, iteratee.length === 1 ? iteratee[0] : iteratee);
|
250 | };
|
251 | }
|
252 | return iteratee;
|
253 | });
|
254 | } else {
|
255 | iteratees = [identity];
|
256 | }
|
257 | var index = -1;
|
258 | iteratees = arrayMap(iteratees, baseUnary(baseIteratee));
|
259 | var result = baseMap(collection, function(value, key, collection2) {
|
260 | var criteria = arrayMap(iteratees, function(iteratee) {
|
261 | return iteratee(value);
|
262 | });
|
263 | return { "criteria": criteria, "index": ++index, "value": value };
|
264 | });
|
265 | return baseSortBy(result, function(object, other) {
|
266 | return compareMultiple(object, other, orders);
|
267 | });
|
268 | }
|
269 | function basePick(object, paths) {
|
270 | return basePickBy(object, paths, function(value, path) {
|
271 | return hasIn(object, path);
|
272 | });
|
273 | }
|
274 | var pick = flatRest(function(object, paths) {
|
275 | return object == null ? {} : basePick(object, paths);
|
276 | });
|
277 | const pick$1 = pick;
|
278 | var nativeCeil = Math.ceil, nativeMax = Math.max;
|
279 | function baseRange(start, end, step, fromRight) {
|
280 | var index = -1, length = nativeMax(nativeCeil((end - start) / (step || 1)), 0), result = Array(length);
|
281 | while (length--) {
|
282 | result[fromRight ? length : ++index] = start;
|
283 | start += step;
|
284 | }
|
285 | return result;
|
286 | }
|
287 | function createRange(fromRight) {
|
288 | return function(start, end, step) {
|
289 | if (step && typeof step != "number" && isIterateeCall(start, end, step)) {
|
290 | end = step = void 0;
|
291 | }
|
292 | start = toFinite(start);
|
293 | if (end === void 0) {
|
294 | end = start;
|
295 | start = 0;
|
296 | } else {
|
297 | end = toFinite(end);
|
298 | }
|
299 | step = step === void 0 ? start < end ? 1 : -1 : toFinite(step);
|
300 | return baseRange(start, end, step, fromRight);
|
301 | };
|
302 | }
|
303 | var range = createRange();
|
304 | const range$1 = range;
|
305 | var sortBy = baseRest(function(collection, iteratees) {
|
306 | if (collection == null) {
|
307 | return [];
|
308 | }
|
309 | var length = iteratees.length;
|
310 | if (length > 1 && isIterateeCall(collection, iteratees[0], iteratees[1])) {
|
311 | iteratees = [];
|
312 | } else if (length > 2 && isIterateeCall(iteratees[0], iteratees[1], iteratees[2])) {
|
313 | iteratees = [iteratees[0]];
|
314 | }
|
315 | return baseOrderBy(collection, baseFlatten(iteratees, 1), []);
|
316 | });
|
317 | const sortBy$1 = sortBy;
|
318 | var idCounter = 0;
|
319 | function uniqueId(prefix) {
|
320 | var id = ++idCounter;
|
321 | return toString(prefix) + id;
|
322 | }
|
323 | function baseZipObject(props, values2, assignFunc) {
|
324 | var index = -1, length = props.length, valsLength = values2.length, result = {};
|
325 | while (++index < length) {
|
326 | var value = index < valsLength ? values2[index] : void 0;
|
327 | assignFunc(result, props[index], value);
|
328 | }
|
329 | return result;
|
330 | }
|
331 | function zipObject(props, values2) {
|
332 | return baseZipObject(props || [], values2 || [], assignValue);
|
333 | }
|
334 | class List {
|
335 | constructor() {
|
336 | var sentinel = {};
|
337 | sentinel._next = sentinel._prev = sentinel;
|
338 | this._sentinel = sentinel;
|
339 | }
|
340 | dequeue() {
|
341 | var sentinel = this._sentinel;
|
342 | var entry = sentinel._prev;
|
343 | if (entry !== sentinel) {
|
344 | unlink(entry);
|
345 | return entry;
|
346 | }
|
347 | }
|
348 | enqueue(entry) {
|
349 | var sentinel = this._sentinel;
|
350 | if (entry._prev && entry._next) {
|
351 | unlink(entry);
|
352 | }
|
353 | entry._next = sentinel._next;
|
354 | sentinel._next._prev = entry;
|
355 | sentinel._next = entry;
|
356 | entry._prev = sentinel;
|
357 | }
|
358 | toString() {
|
359 | var strs = [];
|
360 | var sentinel = this._sentinel;
|
361 | var curr = sentinel._prev;
|
362 | while (curr !== sentinel) {
|
363 | strs.push(JSON.stringify(curr, filterOutLinks));
|
364 | curr = curr._prev;
|
365 | }
|
366 | return "[" + strs.join(", ") + "]";
|
367 | }
|
368 | }
|
369 | function unlink(entry) {
|
370 | entry._prev._next = entry._next;
|
371 | entry._next._prev = entry._prev;
|
372 | delete entry._next;
|
373 | delete entry._prev;
|
374 | }
|
375 | function filterOutLinks(k, v) {
|
376 | if (k !== "_next" && k !== "_prev") {
|
377 | return v;
|
378 | }
|
379 | }
|
380 | var DEFAULT_WEIGHT_FN = constant(1);
|
381 | function greedyFAS(g, weightFn) {
|
382 | if (g.nodeCount() <= 1) {
|
383 | return [];
|
384 | }
|
385 | var state = buildState(g, weightFn || DEFAULT_WEIGHT_FN);
|
386 | var results = doGreedyFAS(state.graph, state.buckets, state.zeroIdx);
|
387 | return flatten(
|
388 | map(results, function(e) {
|
389 | return g.outEdges(e.v, e.w);
|
390 | })
|
391 | );
|
392 | }
|
393 | function doGreedyFAS(g, buckets, zeroIdx) {
|
394 | var results = [];
|
395 | var sources = buckets[buckets.length - 1];
|
396 | var sinks = buckets[0];
|
397 | var entry;
|
398 | while (g.nodeCount()) {
|
399 | while (entry = sinks.dequeue()) {
|
400 | removeNode(g, buckets, zeroIdx, entry);
|
401 | }
|
402 | while (entry = sources.dequeue()) {
|
403 | removeNode(g, buckets, zeroIdx, entry);
|
404 | }
|
405 | if (g.nodeCount()) {
|
406 | for (var i = buckets.length - 2; i > 0; --i) {
|
407 | entry = buckets[i].dequeue();
|
408 | if (entry) {
|
409 | results = results.concat(removeNode(g, buckets, zeroIdx, entry, true));
|
410 | break;
|
411 | }
|
412 | }
|
413 | }
|
414 | }
|
415 | return results;
|
416 | }
|
417 | function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) {
|
418 | var results = collectPredecessors ? [] : void 0;
|
419 | forEach(g.inEdges(entry.v), function(edge) {
|
420 | var weight = g.edge(edge);
|
421 | var uEntry = g.node(edge.v);
|
422 | if (collectPredecessors) {
|
423 | results.push({ v: edge.v, w: edge.w });
|
424 | }
|
425 | uEntry.out -= weight;
|
426 | assignBucket(buckets, zeroIdx, uEntry);
|
427 | });
|
428 | forEach(g.outEdges(entry.v), function(edge) {
|
429 | var weight = g.edge(edge);
|
430 | var w = edge.w;
|
431 | var wEntry = g.node(w);
|
432 | wEntry["in"] -= weight;
|
433 | assignBucket(buckets, zeroIdx, wEntry);
|
434 | });
|
435 | g.removeNode(entry.v);
|
436 | return results;
|
437 | }
|
438 | function buildState(g, weightFn) {
|
439 | var fasGraph = new Graph();
|
440 | var maxIn = 0;
|
441 | var maxOut = 0;
|
442 | forEach(g.nodes(), function(v) {
|
443 | fasGraph.setNode(v, { v, in: 0, out: 0 });
|
444 | });
|
445 | forEach(g.edges(), function(e) {
|
446 | var prevWeight = fasGraph.edge(e.v, e.w) || 0;
|
447 | var weight = weightFn(e);
|
448 | var edgeWeight = prevWeight + weight;
|
449 | fasGraph.setEdge(e.v, e.w, edgeWeight);
|
450 | maxOut = Math.max(maxOut, fasGraph.node(e.v).out += weight);
|
451 | maxIn = Math.max(maxIn, fasGraph.node(e.w)["in"] += weight);
|
452 | });
|
453 | var buckets = range$1(maxOut + maxIn + 3).map(function() {
|
454 | return new List();
|
455 | });
|
456 | var zeroIdx = maxIn + 1;
|
457 | forEach(fasGraph.nodes(), function(v) {
|
458 | assignBucket(buckets, zeroIdx, fasGraph.node(v));
|
459 | });
|
460 | return { graph: fasGraph, buckets, zeroIdx };
|
461 | }
|
462 | function assignBucket(buckets, zeroIdx, entry) {
|
463 | if (!entry.out) {
|
464 | buckets[0].enqueue(entry);
|
465 | } else if (!entry["in"]) {
|
466 | buckets[buckets.length - 1].enqueue(entry);
|
467 | } else {
|
468 | buckets[entry.out - entry["in"] + zeroIdx].enqueue(entry);
|
469 | }
|
470 | }
|
471 | function run$2(g) {
|
472 | var fas = g.graph().acyclicer === "greedy" ? greedyFAS(g, weightFn(g)) : dfsFAS(g);
|
473 | forEach(fas, function(e) {
|
474 | var label = g.edge(e);
|
475 | g.removeEdge(e);
|
476 | label.forwardName = e.name;
|
477 | label.reversed = true;
|
478 | g.setEdge(e.w, e.v, label, uniqueId("rev"));
|
479 | });
|
480 | function weightFn(g2) {
|
481 | return function(e) {
|
482 | return g2.edge(e).weight;
|
483 | };
|
484 | }
|
485 | }
|
486 | function dfsFAS(g) {
|
487 | var fas = [];
|
488 | var stack = {};
|
489 | var visited = {};
|
490 | function dfs2(v) {
|
491 | if (has(visited, v)) {
|
492 | return;
|
493 | }
|
494 | visited[v] = true;
|
495 | stack[v] = true;
|
496 | forEach(g.outEdges(v), function(e) {
|
497 | if (has(stack, e.w)) {
|
498 | fas.push(e);
|
499 | } else {
|
500 | dfs2(e.w);
|
501 | }
|
502 | });
|
503 | delete stack[v];
|
504 | }
|
505 | forEach(g.nodes(), dfs2);
|
506 | return fas;
|
507 | }
|
508 | function undo$2(g) {
|
509 | forEach(g.edges(), function(e) {
|
510 | var label = g.edge(e);
|
511 | if (label.reversed) {
|
512 | g.removeEdge(e);
|
513 | var forwardName = label.forwardName;
|
514 | delete label.reversed;
|
515 | delete label.forwardName;
|
516 | g.setEdge(e.w, e.v, label, forwardName);
|
517 | }
|
518 | });
|
519 | }
|
520 | function addDummyNode(g, type, attrs, name) {
|
521 | var v;
|
522 | do {
|
523 | v = uniqueId(name);
|
524 | } while (g.hasNode(v));
|
525 | attrs.dummy = type;
|
526 | g.setNode(v, attrs);
|
527 | return v;
|
528 | }
|
529 | function simplify(g) {
|
530 | var simplified = new Graph().setGraph(g.graph());
|
531 | forEach(g.nodes(), function(v) {
|
532 | simplified.setNode(v, g.node(v));
|
533 | });
|
534 | forEach(g.edges(), function(e) {
|
535 | var simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
|
536 | var label = g.edge(e);
|
537 | simplified.setEdge(e.v, e.w, {
|
538 | weight: simpleLabel.weight + label.weight,
|
539 | minlen: Math.max(simpleLabel.minlen, label.minlen)
|
540 | });
|
541 | });
|
542 | return simplified;
|
543 | }
|
544 | function asNonCompoundGraph(g) {
|
545 | var simplified = new Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
|
546 | forEach(g.nodes(), function(v) {
|
547 | if (!g.children(v).length) {
|
548 | simplified.setNode(v, g.node(v));
|
549 | }
|
550 | });
|
551 | forEach(g.edges(), function(e) {
|
552 | simplified.setEdge(e, g.edge(e));
|
553 | });
|
554 | return simplified;
|
555 | }
|
556 | function intersectRect(rect, point) {
|
557 | var x = rect.x;
|
558 | var y = rect.y;
|
559 | var dx = point.x - x;
|
560 | var dy = point.y - y;
|
561 | var w = rect.width / 2;
|
562 | var h = rect.height / 2;
|
563 | if (!dx && !dy) {
|
564 | throw new Error("Not possible to find intersection inside of the rectangle");
|
565 | }
|
566 | var sx, sy;
|
567 | if (Math.abs(dy) * w > Math.abs(dx) * h) {
|
568 | if (dy < 0) {
|
569 | h = -h;
|
570 | }
|
571 | sx = h * dx / dy;
|
572 | sy = h;
|
573 | } else {
|
574 | if (dx < 0) {
|
575 | w = -w;
|
576 | }
|
577 | sx = w;
|
578 | sy = w * dy / dx;
|
579 | }
|
580 | return { x: x + sx, y: y + sy };
|
581 | }
|
582 | function buildLayerMatrix(g) {
|
583 | var layering = map(range$1(maxRank(g) + 1), function() {
|
584 | return [];
|
585 | });
|
586 | forEach(g.nodes(), function(v) {
|
587 | var node = g.node(v);
|
588 | var rank2 = node.rank;
|
589 | if (!isUndefined(rank2)) {
|
590 | layering[rank2][node.order] = v;
|
591 | }
|
592 | });
|
593 | return layering;
|
594 | }
|
595 | function normalizeRanks(g) {
|
596 | var min$1 = min(
|
597 | map(g.nodes(), function(v) {
|
598 | return g.node(v).rank;
|
599 | })
|
600 | );
|
601 | forEach(g.nodes(), function(v) {
|
602 | var node = g.node(v);
|
603 | if (has(node, "rank")) {
|
604 | node.rank -= min$1;
|
605 | }
|
606 | });
|
607 | }
|
608 | function removeEmptyRanks(g) {
|
609 | var offset = min(
|
610 | map(g.nodes(), function(v) {
|
611 | return g.node(v).rank;
|
612 | })
|
613 | );
|
614 | var layers = [];
|
615 | forEach(g.nodes(), function(v) {
|
616 | var rank2 = g.node(v).rank - offset;
|
617 | if (!layers[rank2]) {
|
618 | layers[rank2] = [];
|
619 | }
|
620 | layers[rank2].push(v);
|
621 | });
|
622 | var delta = 0;
|
623 | var nodeRankFactor = g.graph().nodeRankFactor;
|
624 | forEach(layers, function(vs, i) {
|
625 | if (isUndefined(vs) && i % nodeRankFactor !== 0) {
|
626 | --delta;
|
627 | } else if (delta) {
|
628 | forEach(vs, function(v) {
|
629 | g.node(v).rank += delta;
|
630 | });
|
631 | }
|
632 | });
|
633 | }
|
634 | function addBorderNode$1(g, prefix, rank2, order2) {
|
635 | var node = {
|
636 | width: 0,
|
637 | height: 0
|
638 | };
|
639 | if (arguments.length >= 4) {
|
640 | node.rank = rank2;
|
641 | node.order = order2;
|
642 | }
|
643 | return addDummyNode(g, "border", node, prefix);
|
644 | }
|
645 | function maxRank(g) {
|
646 | return max(
|
647 | map(g.nodes(), function(v) {
|
648 | var rank2 = g.node(v).rank;
|
649 | if (!isUndefined(rank2)) {
|
650 | return rank2;
|
651 | }
|
652 | })
|
653 | );
|
654 | }
|
655 | function partition(collection, fn) {
|
656 | var result = { lhs: [], rhs: [] };
|
657 | forEach(collection, function(value) {
|
658 | if (fn(value)) {
|
659 | result.lhs.push(value);
|
660 | } else {
|
661 | result.rhs.push(value);
|
662 | }
|
663 | });
|
664 | return result;
|
665 | }
|
666 | function time(name, fn) {
|
667 | var start = now$1();
|
668 | try {
|
669 | return fn();
|
670 | } finally {
|
671 | console.log(name + " time: " + (now$1() - start) + "ms");
|
672 | }
|
673 | }
|
674 | function notime(name, fn) {
|
675 | return fn();
|
676 | }
|
677 | function addBorderSegments(g) {
|
678 | function dfs2(v) {
|
679 | var children = g.children(v);
|
680 | var node = g.node(v);
|
681 | if (children.length) {
|
682 | forEach(children, dfs2);
|
683 | }
|
684 | if (has(node, "minRank")) {
|
685 | node.borderLeft = [];
|
686 | node.borderRight = [];
|
687 | for (var rank2 = node.minRank, maxRank2 = node.maxRank + 1; rank2 < maxRank2; ++rank2) {
|
688 | addBorderNode(g, "borderLeft", "_bl", v, node, rank2);
|
689 | addBorderNode(g, "borderRight", "_br", v, node, rank2);
|
690 | }
|
691 | }
|
692 | }
|
693 | forEach(g.children(), dfs2);
|
694 | }
|
695 | function addBorderNode(g, prop, prefix, sg, sgNode, rank2) {
|
696 | var label = { width: 0, height: 0, rank: rank2, borderType: prop };
|
697 | var prev = sgNode[prop][rank2 - 1];
|
698 | var curr = addDummyNode(g, "border", label, prefix);
|
699 | sgNode[prop][rank2] = curr;
|
700 | g.setParent(curr, sg);
|
701 | if (prev) {
|
702 | g.setEdge(prev, curr, { weight: 1 });
|
703 | }
|
704 | }
|
705 | function adjust(g) {
|
706 | var rankDir = g.graph().rankdir.toLowerCase();
|
707 | if (rankDir === "lr" || rankDir === "rl") {
|
708 | swapWidthHeight(g);
|
709 | }
|
710 | }
|
711 | function undo$1(g) {
|
712 | var rankDir = g.graph().rankdir.toLowerCase();
|
713 | if (rankDir === "bt" || rankDir === "rl") {
|
714 | reverseY(g);
|
715 | }
|
716 | if (rankDir === "lr" || rankDir === "rl") {
|
717 | swapXY(g);
|
718 | swapWidthHeight(g);
|
719 | }
|
720 | }
|
721 | function swapWidthHeight(g) {
|
722 | forEach(g.nodes(), function(v) {
|
723 | swapWidthHeightOne(g.node(v));
|
724 | });
|
725 | forEach(g.edges(), function(e) {
|
726 | swapWidthHeightOne(g.edge(e));
|
727 | });
|
728 | }
|
729 | function swapWidthHeightOne(attrs) {
|
730 | var w = attrs.width;
|
731 | attrs.width = attrs.height;
|
732 | attrs.height = w;
|
733 | }
|
734 | function reverseY(g) {
|
735 | forEach(g.nodes(), function(v) {
|
736 | reverseYOne(g.node(v));
|
737 | });
|
738 | forEach(g.edges(), function(e) {
|
739 | var edge = g.edge(e);
|
740 | forEach(edge.points, reverseYOne);
|
741 | if (has(edge, "y")) {
|
742 | reverseYOne(edge);
|
743 | }
|
744 | });
|
745 | }
|
746 | function reverseYOne(attrs) {
|
747 | attrs.y = -attrs.y;
|
748 | }
|
749 | function swapXY(g) {
|
750 | forEach(g.nodes(), function(v) {
|
751 | swapXYOne(g.node(v));
|
752 | });
|
753 | forEach(g.edges(), function(e) {
|
754 | var edge = g.edge(e);
|
755 | forEach(edge.points, swapXYOne);
|
756 | if (has(edge, "x")) {
|
757 | swapXYOne(edge);
|
758 | }
|
759 | });
|
760 | }
|
761 | function swapXYOne(attrs) {
|
762 | var x = attrs.x;
|
763 | attrs.x = attrs.y;
|
764 | attrs.y = x;
|
765 | }
|
766 | function run$1(g) {
|
767 | g.graph().dummyChains = [];
|
768 | forEach(g.edges(), function(edge) {
|
769 | normalizeEdge(g, edge);
|
770 | });
|
771 | }
|
772 | function normalizeEdge(g, e) {
|
773 | var v = e.v;
|
774 | var vRank = g.node(v).rank;
|
775 | var w = e.w;
|
776 | var wRank = g.node(w).rank;
|
777 | var name = e.name;
|
778 | var edgeLabel = g.edge(e);
|
779 | var labelRank = edgeLabel.labelRank;
|
780 | if (wRank === vRank + 1)
|
781 | return;
|
782 | g.removeEdge(e);
|
783 | var dummy, attrs, i;
|
784 | for (i = 0, ++vRank; vRank < wRank; ++i, ++vRank) {
|
785 | edgeLabel.points = [];
|
786 | attrs = {
|
787 | width: 0,
|
788 | height: 0,
|
789 | edgeLabel,
|
790 | edgeObj: e,
|
791 | rank: vRank
|
792 | };
|
793 | dummy = addDummyNode(g, "edge", attrs, "_d");
|
794 | if (vRank === labelRank) {
|
795 | attrs.width = edgeLabel.width;
|
796 | attrs.height = edgeLabel.height;
|
797 | attrs.dummy = "edge-label";
|
798 | attrs.labelpos = edgeLabel.labelpos;
|
799 | }
|
800 | g.setEdge(v, dummy, { weight: edgeLabel.weight }, name);
|
801 | if (i === 0) {
|
802 | g.graph().dummyChains.push(dummy);
|
803 | }
|
804 | v = dummy;
|
805 | }
|
806 | g.setEdge(v, w, { weight: edgeLabel.weight }, name);
|
807 | }
|
808 | function undo(g) {
|
809 | forEach(g.graph().dummyChains, function(v) {
|
810 | var node = g.node(v);
|
811 | var origLabel = node.edgeLabel;
|
812 | var w;
|
813 | g.setEdge(node.edgeObj, origLabel);
|
814 | while (node.dummy) {
|
815 | w = g.successors(v)[0];
|
816 | g.removeNode(v);
|
817 | origLabel.points.push({ x: node.x, y: node.y });
|
818 | if (node.dummy === "edge-label") {
|
819 | origLabel.x = node.x;
|
820 | origLabel.y = node.y;
|
821 | origLabel.width = node.width;
|
822 | origLabel.height = node.height;
|
823 | }
|
824 | v = w;
|
825 | node = g.node(v);
|
826 | }
|
827 | });
|
828 | }
|
829 | function longestPath(g) {
|
830 | var visited = {};
|
831 | function dfs2(v) {
|
832 | var label = g.node(v);
|
833 | if (has(visited, v)) {
|
834 | return label.rank;
|
835 | }
|
836 | visited[v] = true;
|
837 | var rank2 = min(
|
838 | map(g.outEdges(v), function(e) {
|
839 | return dfs2(e.w) - g.edge(e).minlen;
|
840 | })
|
841 | );
|
842 | if (rank2 === Number.POSITIVE_INFINITY ||
|
843 | rank2 === void 0 ||
|
844 | rank2 === null) {
|
845 | rank2 = 0;
|
846 | }
|
847 | return label.rank = rank2;
|
848 | }
|
849 | forEach(g.sources(), dfs2);
|
850 | }
|
851 | function slack(g, e) {
|
852 | return g.node(e.w).rank - g.node(e.v).rank - g.edge(e).minlen;
|
853 | }
|
854 | function feasibleTree(g) {
|
855 | var t = new Graph({ directed: false });
|
856 | var start = g.nodes()[0];
|
857 | var size = g.nodeCount();
|
858 | t.setNode(start, {});
|
859 | var edge, delta;
|
860 | while (tightTree(t, g) < size) {
|
861 | edge = findMinSlackEdge(t, g);
|
862 | delta = t.hasNode(edge.v) ? slack(g, edge) : -slack(g, edge);
|
863 | shiftRanks(t, g, delta);
|
864 | }
|
865 | return t;
|
866 | }
|
867 | function tightTree(t, g) {
|
868 | function dfs2(v) {
|
869 | forEach(g.nodeEdges(v), function(e) {
|
870 | var edgeV = e.v, w = v === edgeV ? e.w : edgeV;
|
871 | if (!t.hasNode(w) && !slack(g, e)) {
|
872 | t.setNode(w, {});
|
873 | t.setEdge(v, w, {});
|
874 | dfs2(w);
|
875 | }
|
876 | });
|
877 | }
|
878 | forEach(t.nodes(), dfs2);
|
879 | return t.nodeCount();
|
880 | }
|
881 | function findMinSlackEdge(t, g) {
|
882 | return minBy(g.edges(), function(e) {
|
883 | if (t.hasNode(e.v) !== t.hasNode(e.w)) {
|
884 | return slack(g, e);
|
885 | }
|
886 | });
|
887 | }
|
888 | function shiftRanks(t, g, delta) {
|
889 | forEach(t.nodes(), function(v) {
|
890 | g.node(v).rank += delta;
|
891 | });
|
892 | }
|
893 | function CycleException() {
|
894 | }
|
895 | CycleException.prototype = new Error();
|
896 | function dfs$1(g, vs, order2) {
|
897 | if (!isArray(vs)) {
|
898 | vs = [vs];
|
899 | }
|
900 | var navigation = (g.isDirected() ? g.successors : g.neighbors).bind(g);
|
901 | var acc = [];
|
902 | var visited = {};
|
903 | forEach(vs, function(v) {
|
904 | if (!g.hasNode(v)) {
|
905 | throw new Error("Graph does not have node: " + v);
|
906 | }
|
907 | doDfs(g, v, order2 === "post", visited, navigation, acc);
|
908 | });
|
909 | return acc;
|
910 | }
|
911 | function doDfs(g, v, postorder2, visited, navigation, acc) {
|
912 | if (!has(visited, v)) {
|
913 | visited[v] = true;
|
914 | if (!postorder2) {
|
915 | acc.push(v);
|
916 | }
|
917 | forEach(navigation(v), function(w) {
|
918 | doDfs(g, w, postorder2, visited, navigation, acc);
|
919 | });
|
920 | if (postorder2) {
|
921 | acc.push(v);
|
922 | }
|
923 | }
|
924 | }
|
925 | function postorder$1(g, vs) {
|
926 | return dfs$1(g, vs, "post");
|
927 | }
|
928 | function preorder(g, vs) {
|
929 | return dfs$1(g, vs, "pre");
|
930 | }
|
931 | networkSimplex.initLowLimValues = initLowLimValues;
|
932 | networkSimplex.initCutValues = initCutValues;
|
933 | networkSimplex.calcCutValue = calcCutValue;
|
934 | networkSimplex.leaveEdge = leaveEdge;
|
935 | networkSimplex.enterEdge = enterEdge;
|
936 | networkSimplex.exchangeEdges = exchangeEdges;
|
937 | function networkSimplex(g) {
|
938 | g = simplify(g);
|
939 | longestPath(g);
|
940 | var t = feasibleTree(g);
|
941 | initLowLimValues(t);
|
942 | initCutValues(t, g);
|
943 | var e, f;
|
944 | while (e = leaveEdge(t)) {
|
945 | f = enterEdge(t, g, e);
|
946 | exchangeEdges(t, g, e, f);
|
947 | }
|
948 | }
|
949 | function initCutValues(t, g) {
|
950 | var vs = postorder$1(t, t.nodes());
|
951 | vs = vs.slice(0, vs.length - 1);
|
952 | forEach(vs, function(v) {
|
953 | assignCutValue(t, g, v);
|
954 | });
|
955 | }
|
956 | function assignCutValue(t, g, child) {
|
957 | var childLab = t.node(child);
|
958 | var parent = childLab.parent;
|
959 | t.edge(child, parent).cutvalue = calcCutValue(t, g, child);
|
960 | }
|
961 | function calcCutValue(t, g, child) {
|
962 | var childLab = t.node(child);
|
963 | var parent = childLab.parent;
|
964 | var childIsTail = true;
|
965 | var graphEdge = g.edge(child, parent);
|
966 | var cutValue = 0;
|
967 | if (!graphEdge) {
|
968 | childIsTail = false;
|
969 | graphEdge = g.edge(parent, child);
|
970 | }
|
971 | cutValue = graphEdge.weight;
|
972 | forEach(g.nodeEdges(child), function(e) {
|
973 | var isOutEdge = e.v === child, other = isOutEdge ? e.w : e.v;
|
974 | if (other !== parent) {
|
975 | var pointsToHead = isOutEdge === childIsTail, otherWeight = g.edge(e).weight;
|
976 | cutValue += pointsToHead ? otherWeight : -otherWeight;
|
977 | if (isTreeEdge(t, child, other)) {
|
978 | var otherCutValue = t.edge(child, other).cutvalue;
|
979 | cutValue += pointsToHead ? -otherCutValue : otherCutValue;
|
980 | }
|
981 | }
|
982 | });
|
983 | return cutValue;
|
984 | }
|
985 | function initLowLimValues(tree, root2) {
|
986 | if (arguments.length < 2) {
|
987 | root2 = tree.nodes()[0];
|
988 | }
|
989 | dfsAssignLowLim(tree, {}, 1, root2);
|
990 | }
|
991 | function dfsAssignLowLim(tree, visited, nextLim, v, parent) {
|
992 | var low = nextLim;
|
993 | var label = tree.node(v);
|
994 | visited[v] = true;
|
995 | forEach(tree.neighbors(v), function(w) {
|
996 | if (!has(visited, w)) {
|
997 | nextLim = dfsAssignLowLim(tree, visited, nextLim, w, v);
|
998 | }
|
999 | });
|
1000 | label.low = low;
|
1001 | label.lim = nextLim++;
|
1002 | if (parent) {
|
1003 | label.parent = parent;
|
1004 | } else {
|
1005 | delete label.parent;
|
1006 | }
|
1007 | return nextLim;
|
1008 | }
|
1009 | function leaveEdge(tree) {
|
1010 | return find$1(tree.edges(), function(e) {
|
1011 | return tree.edge(e).cutvalue < 0;
|
1012 | });
|
1013 | }
|
1014 | function enterEdge(t, g, edge) {
|
1015 | var v = edge.v;
|
1016 | var w = edge.w;
|
1017 | if (!g.hasEdge(v, w)) {
|
1018 | v = edge.w;
|
1019 | w = edge.v;
|
1020 | }
|
1021 | var vLabel = t.node(v);
|
1022 | var wLabel = t.node(w);
|
1023 | var tailLabel = vLabel;
|
1024 | var flip = false;
|
1025 | if (vLabel.lim > wLabel.lim) {
|
1026 | tailLabel = wLabel;
|
1027 | flip = true;
|
1028 | }
|
1029 | var candidates = filter(g.edges(), function(edge2) {
|
1030 | return flip === isDescendant(t, t.node(edge2.v), tailLabel) && flip !== isDescendant(t, t.node(edge2.w), tailLabel);
|
1031 | });
|
1032 | return minBy(candidates, function(edge2) {
|
1033 | return slack(g, edge2);
|
1034 | });
|
1035 | }
|
1036 | function exchangeEdges(t, g, e, f) {
|
1037 | var v = e.v;
|
1038 | var w = e.w;
|
1039 | t.removeEdge(v, w);
|
1040 | t.setEdge(f.v, f.w, {});
|
1041 | initLowLimValues(t);
|
1042 | initCutValues(t, g);
|
1043 | updateRanks(t, g);
|
1044 | }
|
1045 | function updateRanks(t, g) {
|
1046 | var root2 = find$1(t.nodes(), function(v) {
|
1047 | return !g.node(v).parent;
|
1048 | });
|
1049 | var vs = preorder(t, root2);
|
1050 | vs = vs.slice(1);
|
1051 | forEach(vs, function(v) {
|
1052 | var parent = t.node(v).parent, edge = g.edge(v, parent), flipped = false;
|
1053 | if (!edge) {
|
1054 | edge = g.edge(parent, v);
|
1055 | flipped = true;
|
1056 | }
|
1057 | g.node(v).rank = g.node(parent).rank + (flipped ? edge.minlen : -edge.minlen);
|
1058 | });
|
1059 | }
|
1060 | function isTreeEdge(tree, u, v) {
|
1061 | return tree.hasEdge(u, v);
|
1062 | }
|
1063 | function isDescendant(tree, vLabel, rootLabel) {
|
1064 | return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim;
|
1065 | }
|
1066 | function rank(g) {
|
1067 | switch (g.graph().ranker) {
|
1068 | case "network-simplex":
|
1069 | networkSimplexRanker(g);
|
1070 | break;
|
1071 | case "tight-tree":
|
1072 | tightTreeRanker(g);
|
1073 | break;
|
1074 | case "longest-path":
|
1075 | longestPathRanker(g);
|
1076 | break;
|
1077 | default:
|
1078 | networkSimplexRanker(g);
|
1079 | }
|
1080 | }
|
1081 | var longestPathRanker = longestPath;
|
1082 | function tightTreeRanker(g) {
|
1083 | longestPath(g);
|
1084 | feasibleTree(g);
|
1085 | }
|
1086 | function networkSimplexRanker(g) {
|
1087 | networkSimplex(g);
|
1088 | }
|
1089 | function run(g) {
|
1090 | var root2 = addDummyNode(g, "root", {}, "_root");
|
1091 | var depths = treeDepths(g);
|
1092 | var height = max(values(depths)) - 1;
|
1093 | var nodeSep = 2 * height + 1;
|
1094 | g.graph().nestingRoot = root2;
|
1095 | forEach(g.edges(), function(e) {
|
1096 | g.edge(e).minlen *= nodeSep;
|
1097 | });
|
1098 | var weight = sumWeights(g) + 1;
|
1099 | forEach(g.children(), function(child) {
|
1100 | dfs(g, root2, nodeSep, weight, height, depths, child);
|
1101 | });
|
1102 | g.graph().nodeRankFactor = nodeSep;
|
1103 | }
|
1104 | function dfs(g, root2, nodeSep, weight, height, depths, v) {
|
1105 | var children = g.children(v);
|
1106 | if (!children.length) {
|
1107 | if (v !== root2) {
|
1108 | g.setEdge(root2, v, { weight: 0, minlen: nodeSep });
|
1109 | }
|
1110 | return;
|
1111 | }
|
1112 | var top = addBorderNode$1(g, "_bt");
|
1113 | var bottom = addBorderNode$1(g, "_bb");
|
1114 | var label = g.node(v);
|
1115 | g.setParent(top, v);
|
1116 | label.borderTop = top;
|
1117 | g.setParent(bottom, v);
|
1118 | label.borderBottom = bottom;
|
1119 | forEach(children, function(child) {
|
1120 | dfs(g, root2, nodeSep, weight, height, depths, child);
|
1121 | var childNode = g.node(child);
|
1122 | var childTop = childNode.borderTop ? childNode.borderTop : child;
|
1123 | var childBottom = childNode.borderBottom ? childNode.borderBottom : child;
|
1124 | var thisWeight = childNode.borderTop ? weight : 2 * weight;
|
1125 | var minlen = childTop !== childBottom ? 1 : height - depths[v] + 1;
|
1126 | g.setEdge(top, childTop, {
|
1127 | weight: thisWeight,
|
1128 | minlen,
|
1129 | nestingEdge: true
|
1130 | });
|
1131 | g.setEdge(childBottom, bottom, {
|
1132 | weight: thisWeight,
|
1133 | minlen,
|
1134 | nestingEdge: true
|
1135 | });
|
1136 | });
|
1137 | if (!g.parent(v)) {
|
1138 | g.setEdge(root2, top, { weight: 0, minlen: height + depths[v] });
|
1139 | }
|
1140 | }
|
1141 | function treeDepths(g) {
|
1142 | var depths = {};
|
1143 | function dfs2(v, depth) {
|
1144 | var children = g.children(v);
|
1145 | if (children && children.length) {
|
1146 | forEach(children, function(child) {
|
1147 | dfs2(child, depth + 1);
|
1148 | });
|
1149 | }
|
1150 | depths[v] = depth;
|
1151 | }
|
1152 | forEach(g.children(), function(v) {
|
1153 | dfs2(v, 1);
|
1154 | });
|
1155 | return depths;
|
1156 | }
|
1157 | function sumWeights(g) {
|
1158 | return reduce(
|
1159 | g.edges(),
|
1160 | function(acc, e) {
|
1161 | return acc + g.edge(e).weight;
|
1162 | },
|
1163 | 0
|
1164 | );
|
1165 | }
|
1166 | function cleanup(g) {
|
1167 | var graphLabel = g.graph();
|
1168 | g.removeNode(graphLabel.nestingRoot);
|
1169 | delete graphLabel.nestingRoot;
|
1170 | forEach(g.edges(), function(e) {
|
1171 | var edge = g.edge(e);
|
1172 | if (edge.nestingEdge) {
|
1173 | g.removeEdge(e);
|
1174 | }
|
1175 | });
|
1176 | }
|
1177 | function addSubgraphConstraints(g, cg, vs) {
|
1178 | var prev = {}, rootPrev;
|
1179 | forEach(vs, function(v) {
|
1180 | var child = g.parent(v), parent, prevChild;
|
1181 | while (child) {
|
1182 | parent = g.parent(child);
|
1183 | if (parent) {
|
1184 | prevChild = prev[parent];
|
1185 | prev[parent] = child;
|
1186 | } else {
|
1187 | prevChild = rootPrev;
|
1188 | rootPrev = child;
|
1189 | }
|
1190 | if (prevChild && prevChild !== child) {
|
1191 | cg.setEdge(prevChild, child);
|
1192 | return;
|
1193 | }
|
1194 | child = parent;
|
1195 | }
|
1196 | });
|
1197 | }
|
1198 | function buildLayerGraph(g, rank2, relationship) {
|
1199 | var root2 = createRootNode(g), result = new Graph({ compound: true }).setGraph({ root: root2 }).setDefaultNodeLabel(function(v) {
|
1200 | return g.node(v);
|
1201 | });
|
1202 | forEach(g.nodes(), function(v) {
|
1203 | var node = g.node(v), parent = g.parent(v);
|
1204 | if (node.rank === rank2 || node.minRank <= rank2 && rank2 <= node.maxRank) {
|
1205 | result.setNode(v);
|
1206 | result.setParent(v, parent || root2);
|
1207 | forEach(g[relationship](v), function(e) {
|
1208 | var u = e.v === v ? e.w : e.v, edge = result.edge(u, v), weight = !isUndefined(edge) ? edge.weight : 0;
|
1209 | result.setEdge(u, v, { weight: g.edge(e).weight + weight });
|
1210 | });
|
1211 | if (has(node, "minRank")) {
|
1212 | result.setNode(v, {
|
1213 | borderLeft: node.borderLeft[rank2],
|
1214 | borderRight: node.borderRight[rank2]
|
1215 | });
|
1216 | }
|
1217 | }
|
1218 | });
|
1219 | return result;
|
1220 | }
|
1221 | function createRootNode(g) {
|
1222 | var v;
|
1223 | while (g.hasNode(v = uniqueId("_root")))
|
1224 | ;
|
1225 | return v;
|
1226 | }
|
1227 | function crossCount(g, layering) {
|
1228 | var cc = 0;
|
1229 | for (var i = 1; i < layering.length; ++i) {
|
1230 | cc += twoLayerCrossCount(g, layering[i - 1], layering[i]);
|
1231 | }
|
1232 | return cc;
|
1233 | }
|
1234 | function twoLayerCrossCount(g, northLayer, southLayer) {
|
1235 | var southPos = zipObject(
|
1236 | southLayer,
|
1237 | map(southLayer, function(v, i) {
|
1238 | return i;
|
1239 | })
|
1240 | );
|
1241 | var southEntries = flatten(
|
1242 | map(northLayer, function(v) {
|
1243 | return sortBy$1(
|
1244 | map(g.outEdges(v), function(e) {
|
1245 | return { pos: southPos[e.w], weight: g.edge(e).weight };
|
1246 | }),
|
1247 | "pos"
|
1248 | );
|
1249 | })
|
1250 | );
|
1251 | var firstIndex = 1;
|
1252 | while (firstIndex < southLayer.length)
|
1253 | firstIndex <<= 1;
|
1254 | var treeSize = 2 * firstIndex - 1;
|
1255 | firstIndex -= 1;
|
1256 | var tree = map(new Array(treeSize), function() {
|
1257 | return 0;
|
1258 | });
|
1259 | var cc = 0;
|
1260 | forEach(
|
1261 |
|
1262 | southEntries.forEach(function(entry) {
|
1263 | var index = entry.pos + firstIndex;
|
1264 | tree[index] += entry.weight;
|
1265 | var weightSum = 0;
|
1266 | while (index > 0) {
|
1267 | if (index % 2) {
|
1268 | weightSum += tree[index + 1];
|
1269 | }
|
1270 | index = index - 1 >> 1;
|
1271 | tree[index] += entry.weight;
|
1272 | }
|
1273 | cc += entry.weight * weightSum;
|
1274 | })
|
1275 | );
|
1276 | return cc;
|
1277 | }
|
1278 | function initOrder(g) {
|
1279 | var visited = {};
|
1280 | var simpleNodes = filter(g.nodes(), function(v) {
|
1281 | return !g.children(v).length;
|
1282 | });
|
1283 | var maxRank2 = max(
|
1284 | map(simpleNodes, function(v) {
|
1285 | return g.node(v).rank;
|
1286 | })
|
1287 | );
|
1288 | var layers = map(range$1(maxRank2 + 1), function() {
|
1289 | return [];
|
1290 | });
|
1291 | function dfs2(v) {
|
1292 | if (has(visited, v))
|
1293 | return;
|
1294 | visited[v] = true;
|
1295 | var node = g.node(v);
|
1296 | layers[node.rank].push(v);
|
1297 | forEach(g.successors(v), dfs2);
|
1298 | }
|
1299 | var orderedVs = sortBy$1(simpleNodes, function(v) {
|
1300 | return g.node(v).rank;
|
1301 | });
|
1302 | forEach(orderedVs, dfs2);
|
1303 | return layers;
|
1304 | }
|
1305 | function barycenter(g, movable) {
|
1306 | return map(movable, function(v) {
|
1307 | var inV = g.inEdges(v);
|
1308 | if (!inV.length) {
|
1309 | return { v };
|
1310 | } else {
|
1311 | var result = reduce(
|
1312 | inV,
|
1313 | function(acc, e) {
|
1314 | var edge = g.edge(e), nodeU = g.node(e.v);
|
1315 | return {
|
1316 | sum: acc.sum + edge.weight * nodeU.order,
|
1317 | weight: acc.weight + edge.weight
|
1318 | };
|
1319 | },
|
1320 | { sum: 0, weight: 0 }
|
1321 | );
|
1322 | return {
|
1323 | v,
|
1324 | barycenter: result.sum / result.weight,
|
1325 | weight: result.weight
|
1326 | };
|
1327 | }
|
1328 | });
|
1329 | }
|
1330 | function resolveConflicts(entries, cg) {
|
1331 | var mappedEntries = {};
|
1332 | forEach(entries, function(entry, i) {
|
1333 | var tmp = mappedEntries[entry.v] = {
|
1334 | indegree: 0,
|
1335 | in: [],
|
1336 | out: [],
|
1337 | vs: [entry.v],
|
1338 | i
|
1339 | };
|
1340 | if (!isUndefined(entry.barycenter)) {
|
1341 | tmp.barycenter = entry.barycenter;
|
1342 | tmp.weight = entry.weight;
|
1343 | }
|
1344 | });
|
1345 | forEach(cg.edges(), function(e) {
|
1346 | var entryV = mappedEntries[e.v];
|
1347 | var entryW = mappedEntries[e.w];
|
1348 | if (!isUndefined(entryV) && !isUndefined(entryW)) {
|
1349 | entryW.indegree++;
|
1350 | entryV.out.push(mappedEntries[e.w]);
|
1351 | }
|
1352 | });
|
1353 | var sourceSet = filter(mappedEntries, function(entry) {
|
1354 | return !entry.indegree;
|
1355 | });
|
1356 | return doResolveConflicts(sourceSet);
|
1357 | }
|
1358 | function doResolveConflicts(sourceSet) {
|
1359 | var entries = [];
|
1360 | function handleIn(vEntry) {
|
1361 | return function(uEntry) {
|
1362 | if (uEntry.merged) {
|
1363 | return;
|
1364 | }
|
1365 | if (isUndefined(uEntry.barycenter) || isUndefined(vEntry.barycenter) || uEntry.barycenter >= vEntry.barycenter) {
|
1366 | mergeEntries(vEntry, uEntry);
|
1367 | }
|
1368 | };
|
1369 | }
|
1370 | function handleOut(vEntry) {
|
1371 | return function(wEntry) {
|
1372 | wEntry["in"].push(vEntry);
|
1373 | if (--wEntry.indegree === 0) {
|
1374 | sourceSet.push(wEntry);
|
1375 | }
|
1376 | };
|
1377 | }
|
1378 | while (sourceSet.length) {
|
1379 | var entry = sourceSet.pop();
|
1380 | entries.push(entry);
|
1381 | forEach(entry["in"].reverse(), handleIn(entry));
|
1382 | forEach(entry.out, handleOut(entry));
|
1383 | }
|
1384 | return map(
|
1385 | filter(entries, function(entry2) {
|
1386 | return !entry2.merged;
|
1387 | }),
|
1388 | function(entry2) {
|
1389 | return pick$1(entry2, ["vs", "i", "barycenter", "weight"]);
|
1390 | }
|
1391 | );
|
1392 | }
|
1393 | function mergeEntries(target, source) {
|
1394 | var sum = 0;
|
1395 | var weight = 0;
|
1396 | if (target.weight) {
|
1397 | sum += target.barycenter * target.weight;
|
1398 | weight += target.weight;
|
1399 | }
|
1400 | if (source.weight) {
|
1401 | sum += source.barycenter * source.weight;
|
1402 | weight += source.weight;
|
1403 | }
|
1404 | target.vs = source.vs.concat(target.vs);
|
1405 | target.barycenter = sum / weight;
|
1406 | target.weight = weight;
|
1407 | target.i = Math.min(source.i, target.i);
|
1408 | source.merged = true;
|
1409 | }
|
1410 | function sort(entries, biasRight) {
|
1411 | var parts = partition(entries, function(entry) {
|
1412 | return has(entry, "barycenter");
|
1413 | });
|
1414 | var sortable = parts.lhs, unsortable = sortBy$1(parts.rhs, function(entry) {
|
1415 | return -entry.i;
|
1416 | }), vs = [], sum = 0, weight = 0, vsIndex = 0;
|
1417 | sortable.sort(compareWithBias(!!biasRight));
|
1418 | vsIndex = consumeUnsortable(vs, unsortable, vsIndex);
|
1419 | forEach(sortable, function(entry) {
|
1420 | vsIndex += entry.vs.length;
|
1421 | vs.push(entry.vs);
|
1422 | sum += entry.barycenter * entry.weight;
|
1423 | weight += entry.weight;
|
1424 | vsIndex = consumeUnsortable(vs, unsortable, vsIndex);
|
1425 | });
|
1426 | var result = { vs: flatten(vs) };
|
1427 | if (weight) {
|
1428 | result.barycenter = sum / weight;
|
1429 | result.weight = weight;
|
1430 | }
|
1431 | return result;
|
1432 | }
|
1433 | function consumeUnsortable(vs, unsortable, index) {
|
1434 | var last$1;
|
1435 | while (unsortable.length && (last$1 = last(unsortable)).i <= index) {
|
1436 | unsortable.pop();
|
1437 | vs.push(last$1.vs);
|
1438 | index++;
|
1439 | }
|
1440 | return index;
|
1441 | }
|
1442 | function compareWithBias(bias) {
|
1443 | return function(entryV, entryW) {
|
1444 | if (entryV.barycenter < entryW.barycenter) {
|
1445 | return -1;
|
1446 | } else if (entryV.barycenter > entryW.barycenter) {
|
1447 | return 1;
|
1448 | }
|
1449 | return !bias ? entryV.i - entryW.i : entryW.i - entryV.i;
|
1450 | };
|
1451 | }
|
1452 | function sortSubgraph(g, v, cg, biasRight) {
|
1453 | var movable = g.children(v);
|
1454 | var node = g.node(v);
|
1455 | var bl = node ? node.borderLeft : void 0;
|
1456 | var br = node ? node.borderRight : void 0;
|
1457 | var subgraphs = {};
|
1458 | if (bl) {
|
1459 | movable = filter(movable, function(w) {
|
1460 | return w !== bl && w !== br;
|
1461 | });
|
1462 | }
|
1463 | var barycenters = barycenter(g, movable);
|
1464 | forEach(barycenters, function(entry) {
|
1465 | if (g.children(entry.v).length) {
|
1466 | var subgraphResult = sortSubgraph(g, entry.v, cg, biasRight);
|
1467 | subgraphs[entry.v] = subgraphResult;
|
1468 | if (has(subgraphResult, "barycenter")) {
|
1469 | mergeBarycenters(entry, subgraphResult);
|
1470 | }
|
1471 | }
|
1472 | });
|
1473 | var entries = resolveConflicts(barycenters, cg);
|
1474 | expandSubgraphs(entries, subgraphs);
|
1475 | var result = sort(entries, biasRight);
|
1476 | if (bl) {
|
1477 | result.vs = flatten([bl, result.vs, br]);
|
1478 | if (g.predecessors(bl).length) {
|
1479 | var blPred = g.node(g.predecessors(bl)[0]), brPred = g.node(g.predecessors(br)[0]);
|
1480 | if (!has(result, "barycenter")) {
|
1481 | result.barycenter = 0;
|
1482 | result.weight = 0;
|
1483 | }
|
1484 | result.barycenter = (result.barycenter * result.weight + blPred.order + brPred.order) / (result.weight + 2);
|
1485 | result.weight += 2;
|
1486 | }
|
1487 | }
|
1488 | return result;
|
1489 | }
|
1490 | function expandSubgraphs(entries, subgraphs) {
|
1491 | forEach(entries, function(entry) {
|
1492 | entry.vs = flatten(
|
1493 | entry.vs.map(function(v) {
|
1494 | if (subgraphs[v]) {
|
1495 | return subgraphs[v].vs;
|
1496 | }
|
1497 | return v;
|
1498 | })
|
1499 | );
|
1500 | });
|
1501 | }
|
1502 | function mergeBarycenters(target, other) {
|
1503 | if (!isUndefined(target.barycenter)) {
|
1504 | target.barycenter = (target.barycenter * target.weight + other.barycenter * other.weight) / (target.weight + other.weight);
|
1505 | target.weight += other.weight;
|
1506 | } else {
|
1507 | target.barycenter = other.barycenter;
|
1508 | target.weight = other.weight;
|
1509 | }
|
1510 | }
|
1511 | function order(g) {
|
1512 | var maxRank$1 = maxRank(g), downLayerGraphs = buildLayerGraphs(g, range$1(1, maxRank$1 + 1), "inEdges"), upLayerGraphs = buildLayerGraphs(g, range$1(maxRank$1 - 1, -1, -1), "outEdges");
|
1513 | var layering = initOrder(g);
|
1514 | assignOrder(g, layering);
|
1515 | var bestCC = Number.POSITIVE_INFINITY, best;
|
1516 | for (var i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) {
|
1517 | sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2);
|
1518 | layering = buildLayerMatrix(g);
|
1519 | var cc = crossCount(g, layering);
|
1520 | if (cc < bestCC) {
|
1521 | lastBest = 0;
|
1522 | best = cloneDeep(layering);
|
1523 | bestCC = cc;
|
1524 | }
|
1525 | }
|
1526 | assignOrder(g, best);
|
1527 | }
|
1528 | function buildLayerGraphs(g, ranks, relationship) {
|
1529 | return map(ranks, function(rank2) {
|
1530 | return buildLayerGraph(g, rank2, relationship);
|
1531 | });
|
1532 | }
|
1533 | function sweepLayerGraphs(layerGraphs, biasRight) {
|
1534 | var cg = new Graph();
|
1535 | forEach(layerGraphs, function(lg) {
|
1536 | var root2 = lg.graph().root;
|
1537 | var sorted = sortSubgraph(lg, root2, cg, biasRight);
|
1538 | forEach(sorted.vs, function(v, i) {
|
1539 | lg.node(v).order = i;
|
1540 | });
|
1541 | addSubgraphConstraints(lg, cg, sorted.vs);
|
1542 | });
|
1543 | }
|
1544 | function assignOrder(g, layering) {
|
1545 | forEach(layering, function(layer) {
|
1546 | forEach(layer, function(v, i) {
|
1547 | g.node(v).order = i;
|
1548 | });
|
1549 | });
|
1550 | }
|
1551 | function parentDummyChains(g) {
|
1552 | var postorderNums = postorder(g);
|
1553 | forEach(g.graph().dummyChains, function(v) {
|
1554 | var node = g.node(v);
|
1555 | var edgeObj = node.edgeObj;
|
1556 | var pathData = findPath(g, postorderNums, edgeObj.v, edgeObj.w);
|
1557 | var path = pathData.path;
|
1558 | var lca = pathData.lca;
|
1559 | var pathIdx = 0;
|
1560 | var pathV = path[pathIdx];
|
1561 | var ascending = true;
|
1562 | while (v !== edgeObj.w) {
|
1563 | node = g.node(v);
|
1564 | if (ascending) {
|
1565 | while ((pathV = path[pathIdx]) !== lca && g.node(pathV).maxRank < node.rank) {
|
1566 | pathIdx++;
|
1567 | }
|
1568 | if (pathV === lca) {
|
1569 | ascending = false;
|
1570 | }
|
1571 | }
|
1572 | if (!ascending) {
|
1573 | while (pathIdx < path.length - 1 && g.node(pathV = path[pathIdx + 1]).minRank <= node.rank) {
|
1574 | pathIdx++;
|
1575 | }
|
1576 | pathV = path[pathIdx];
|
1577 | }
|
1578 | g.setParent(v, pathV);
|
1579 | v = g.successors(v)[0];
|
1580 | }
|
1581 | });
|
1582 | }
|
1583 | function findPath(g, postorderNums, v, w) {
|
1584 | var vPath = [];
|
1585 | var wPath = [];
|
1586 | var low = Math.min(postorderNums[v].low, postorderNums[w].low);
|
1587 | var lim = Math.max(postorderNums[v].lim, postorderNums[w].lim);
|
1588 | var parent;
|
1589 | var lca;
|
1590 | parent = v;
|
1591 | do {
|
1592 | parent = g.parent(parent);
|
1593 | vPath.push(parent);
|
1594 | } while (parent && (postorderNums[parent].low > low || lim > postorderNums[parent].lim));
|
1595 | lca = parent;
|
1596 | parent = w;
|
1597 | while ((parent = g.parent(parent)) !== lca) {
|
1598 | wPath.push(parent);
|
1599 | }
|
1600 | return { path: vPath.concat(wPath.reverse()), lca };
|
1601 | }
|
1602 | function postorder(g) {
|
1603 | var result = {};
|
1604 | var lim = 0;
|
1605 | function dfs2(v) {
|
1606 | var low = lim;
|
1607 | forEach(g.children(v), dfs2);
|
1608 | result[v] = { low, lim: lim++ };
|
1609 | }
|
1610 | forEach(g.children(), dfs2);
|
1611 | return result;
|
1612 | }
|
1613 | function findType1Conflicts(g, layering) {
|
1614 | var conflicts = {};
|
1615 | function visitLayer(prevLayer, layer) {
|
1616 | var k0 = 0, scanPos = 0, prevLayerLength = prevLayer.length, lastNode = last(layer);
|
1617 | forEach(layer, function(v, i) {
|
1618 | var w = findOtherInnerSegmentNode(g, v), k1 = w ? g.node(w).order : prevLayerLength;
|
1619 | if (w || v === lastNode) {
|
1620 | forEach(layer.slice(scanPos, i + 1), function(scanNode) {
|
1621 | forEach(g.predecessors(scanNode), function(u) {
|
1622 | var uLabel = g.node(u), uPos = uLabel.order;
|
1623 | if ((uPos < k0 || k1 < uPos) && !(uLabel.dummy && g.node(scanNode).dummy)) {
|
1624 | addConflict(conflicts, u, scanNode);
|
1625 | }
|
1626 | });
|
1627 | });
|
1628 | scanPos = i + 1;
|
1629 | k0 = k1;
|
1630 | }
|
1631 | });
|
1632 | return layer;
|
1633 | }
|
1634 | reduce(layering, visitLayer);
|
1635 | return conflicts;
|
1636 | }
|
1637 | function findType2Conflicts(g, layering) {
|
1638 | var conflicts = {};
|
1639 | function scan(south, southPos, southEnd, prevNorthBorder, nextNorthBorder) {
|
1640 | var v;
|
1641 | forEach(range$1(southPos, southEnd), function(i) {
|
1642 | v = south[i];
|
1643 | if (g.node(v).dummy) {
|
1644 | forEach(g.predecessors(v), function(u) {
|
1645 | var uNode = g.node(u);
|
1646 | if (uNode.dummy && (uNode.order < prevNorthBorder || uNode.order > nextNorthBorder)) {
|
1647 | addConflict(conflicts, u, v);
|
1648 | }
|
1649 | });
|
1650 | }
|
1651 | });
|
1652 | }
|
1653 | function visitLayer(north, south) {
|
1654 | var prevNorthPos = -1, nextNorthPos, southPos = 0;
|
1655 | forEach(south, function(v, southLookahead) {
|
1656 | if (g.node(v).dummy === "border") {
|
1657 | var predecessors = g.predecessors(v);
|
1658 | if (predecessors.length) {
|
1659 | nextNorthPos = g.node(predecessors[0]).order;
|
1660 | scan(south, southPos, southLookahead, prevNorthPos, nextNorthPos);
|
1661 | southPos = southLookahead;
|
1662 | prevNorthPos = nextNorthPos;
|
1663 | }
|
1664 | }
|
1665 | scan(south, southPos, south.length, nextNorthPos, north.length);
|
1666 | });
|
1667 | return south;
|
1668 | }
|
1669 | reduce(layering, visitLayer);
|
1670 | return conflicts;
|
1671 | }
|
1672 | function findOtherInnerSegmentNode(g, v) {
|
1673 | if (g.node(v).dummy) {
|
1674 | return find$1(g.predecessors(v), function(u) {
|
1675 | return g.node(u).dummy;
|
1676 | });
|
1677 | }
|
1678 | }
|
1679 | function addConflict(conflicts, v, w) {
|
1680 | if (v > w) {
|
1681 | var tmp = v;
|
1682 | v = w;
|
1683 | w = tmp;
|
1684 | }
|
1685 | var conflictsV = conflicts[v];
|
1686 | if (!conflictsV) {
|
1687 | conflicts[v] = conflictsV = {};
|
1688 | }
|
1689 | conflictsV[w] = true;
|
1690 | }
|
1691 | function hasConflict(conflicts, v, w) {
|
1692 | if (v > w) {
|
1693 | var tmp = v;
|
1694 | v = w;
|
1695 | w = tmp;
|
1696 | }
|
1697 | return has(conflicts[v], w);
|
1698 | }
|
1699 | function verticalAlignment(g, layering, conflicts, neighborFn) {
|
1700 | var root2 = {}, align = {}, pos = {};
|
1701 | forEach(layering, function(layer) {
|
1702 | forEach(layer, function(v, order2) {
|
1703 | root2[v] = v;
|
1704 | align[v] = v;
|
1705 | pos[v] = order2;
|
1706 | });
|
1707 | });
|
1708 | forEach(layering, function(layer) {
|
1709 | var prevIdx = -1;
|
1710 | forEach(layer, function(v) {
|
1711 | var ws = neighborFn(v);
|
1712 | if (ws.length) {
|
1713 | ws = sortBy$1(ws, function(w2) {
|
1714 | return pos[w2];
|
1715 | });
|
1716 | var mp = (ws.length - 1) / 2;
|
1717 | for (var i = Math.floor(mp), il = Math.ceil(mp); i <= il; ++i) {
|
1718 | var w = ws[i];
|
1719 | if (align[v] === v && prevIdx < pos[w] && !hasConflict(conflicts, v, w)) {
|
1720 | align[w] = v;
|
1721 | align[v] = root2[v] = root2[w];
|
1722 | prevIdx = pos[w];
|
1723 | }
|
1724 | }
|
1725 | }
|
1726 | });
|
1727 | });
|
1728 | return { root: root2, align };
|
1729 | }
|
1730 | function horizontalCompaction(g, layering, root2, align, reverseSep) {
|
1731 | var xs = {}, blockG = buildBlockGraph(g, layering, root2, reverseSep), borderType = reverseSep ? "borderLeft" : "borderRight";
|
1732 | function iterate(setXsFunc, nextNodesFunc) {
|
1733 | var stack = blockG.nodes();
|
1734 | var elem = stack.pop();
|
1735 | var visited = {};
|
1736 | while (elem) {
|
1737 | if (visited[elem]) {
|
1738 | setXsFunc(elem);
|
1739 | } else {
|
1740 | visited[elem] = true;
|
1741 | stack.push(elem);
|
1742 | stack = stack.concat(nextNodesFunc(elem));
|
1743 | }
|
1744 | elem = stack.pop();
|
1745 | }
|
1746 | }
|
1747 | function pass1(elem) {
|
1748 | xs[elem] = blockG.inEdges(elem).reduce(function(acc, e) {
|
1749 | return Math.max(acc, xs[e.v] + blockG.edge(e));
|
1750 | }, 0);
|
1751 | }
|
1752 | function pass2(elem) {
|
1753 | var min2 = blockG.outEdges(elem).reduce(function(acc, e) {
|
1754 | return Math.min(acc, xs[e.w] - blockG.edge(e));
|
1755 | }, Number.POSITIVE_INFINITY);
|
1756 | var node = g.node(elem);
|
1757 | if (min2 !== Number.POSITIVE_INFINITY && node.borderType !== borderType) {
|
1758 | xs[elem] = Math.max(xs[elem], min2);
|
1759 | }
|
1760 | }
|
1761 | iterate(pass1, blockG.predecessors.bind(blockG));
|
1762 | iterate(pass2, blockG.successors.bind(blockG));
|
1763 | forEach(align, function(v) {
|
1764 | xs[v] = xs[root2[v]];
|
1765 | });
|
1766 | return xs;
|
1767 | }
|
1768 | function buildBlockGraph(g, layering, root2, reverseSep) {
|
1769 | var blockGraph = new Graph(), graphLabel = g.graph(), sepFn = sep(graphLabel.nodesep, graphLabel.edgesep, reverseSep);
|
1770 | forEach(layering, function(layer) {
|
1771 | var u;
|
1772 | forEach(layer, function(v) {
|
1773 | var vRoot = root2[v];
|
1774 | blockGraph.setNode(vRoot);
|
1775 | if (u) {
|
1776 | var uRoot = root2[u], prevMax = blockGraph.edge(uRoot, vRoot);
|
1777 | blockGraph.setEdge(uRoot, vRoot, Math.max(sepFn(g, v, u), prevMax || 0));
|
1778 | }
|
1779 | u = v;
|
1780 | });
|
1781 | });
|
1782 | return blockGraph;
|
1783 | }
|
1784 | function findSmallestWidthAlignment(g, xss) {
|
1785 | return minBy(values(xss), function(xs) {
|
1786 | var max2 = Number.NEGATIVE_INFINITY;
|
1787 | var min2 = Number.POSITIVE_INFINITY;
|
1788 | forIn(xs, function(x, v) {
|
1789 | var halfWidth = width(g, v) / 2;
|
1790 | max2 = Math.max(x + halfWidth, max2);
|
1791 | min2 = Math.min(x - halfWidth, min2);
|
1792 | });
|
1793 | return max2 - min2;
|
1794 | });
|
1795 | }
|
1796 | function alignCoordinates(xss, alignTo) {
|
1797 | var alignToVals = values(alignTo), alignToMin = min(alignToVals), alignToMax = max(alignToVals);
|
1798 | forEach(["u", "d"], function(vert) {
|
1799 | forEach(["l", "r"], function(horiz) {
|
1800 | var alignment = vert + horiz, xs = xss[alignment], delta;
|
1801 | if (xs === alignTo)
|
1802 | return;
|
1803 | var xsVals = values(xs);
|
1804 | delta = horiz === "l" ? alignToMin - min(xsVals) : alignToMax - max(xsVals);
|
1805 | if (delta) {
|
1806 | xss[alignment] = mapValues(xs, function(x) {
|
1807 | return x + delta;
|
1808 | });
|
1809 | }
|
1810 | });
|
1811 | });
|
1812 | }
|
1813 | function balance(xss, align) {
|
1814 | return mapValues(xss.ul, function(ignore, v) {
|
1815 | if (align) {
|
1816 | return xss[align.toLowerCase()][v];
|
1817 | } else {
|
1818 | var xs = sortBy$1(map(xss, v));
|
1819 | return (xs[1] + xs[2]) / 2;
|
1820 | }
|
1821 | });
|
1822 | }
|
1823 | function positionX(g) {
|
1824 | var layering = buildLayerMatrix(g);
|
1825 | var conflicts = merge(findType1Conflicts(g, layering), findType2Conflicts(g, layering));
|
1826 | var xss = {};
|
1827 | var adjustedLayering;
|
1828 | forEach(["u", "d"], function(vert) {
|
1829 | adjustedLayering = vert === "u" ? layering : values(layering).reverse();
|
1830 | forEach(["l", "r"], function(horiz) {
|
1831 | if (horiz === "r") {
|
1832 | adjustedLayering = map(adjustedLayering, function(inner) {
|
1833 | return values(inner).reverse();
|
1834 | });
|
1835 | }
|
1836 | var neighborFn = (vert === "u" ? g.predecessors : g.successors).bind(g);
|
1837 | var align = verticalAlignment(g, adjustedLayering, conflicts, neighborFn);
|
1838 | var xs = horizontalCompaction(g, adjustedLayering, align.root, align.align, horiz === "r");
|
1839 | if (horiz === "r") {
|
1840 | xs = mapValues(xs, function(x) {
|
1841 | return -x;
|
1842 | });
|
1843 | }
|
1844 | xss[vert + horiz] = xs;
|
1845 | });
|
1846 | });
|
1847 | var smallestWidth = findSmallestWidthAlignment(g, xss);
|
1848 | alignCoordinates(xss, smallestWidth);
|
1849 | return balance(xss, g.graph().align);
|
1850 | }
|
1851 | function sep(nodeSep, edgeSep, reverseSep) {
|
1852 | return function(g, v, w) {
|
1853 | var vLabel = g.node(v);
|
1854 | var wLabel = g.node(w);
|
1855 | var sum = 0;
|
1856 | var delta;
|
1857 | sum += vLabel.width / 2;
|
1858 | if (has(vLabel, "labelpos")) {
|
1859 | switch (vLabel.labelpos.toLowerCase()) {
|
1860 | case "l":
|
1861 | delta = -vLabel.width / 2;
|
1862 | break;
|
1863 | case "r":
|
1864 | delta = vLabel.width / 2;
|
1865 | break;
|
1866 | }
|
1867 | }
|
1868 | if (delta) {
|
1869 | sum += reverseSep ? delta : -delta;
|
1870 | }
|
1871 | delta = 0;
|
1872 | sum += (vLabel.dummy ? edgeSep : nodeSep) / 2;
|
1873 | sum += (wLabel.dummy ? edgeSep : nodeSep) / 2;
|
1874 | sum += wLabel.width / 2;
|
1875 | if (has(wLabel, "labelpos")) {
|
1876 | switch (wLabel.labelpos.toLowerCase()) {
|
1877 | case "l":
|
1878 | delta = wLabel.width / 2;
|
1879 | break;
|
1880 | case "r":
|
1881 | delta = -wLabel.width / 2;
|
1882 | break;
|
1883 | }
|
1884 | }
|
1885 | if (delta) {
|
1886 | sum += reverseSep ? delta : -delta;
|
1887 | }
|
1888 | delta = 0;
|
1889 | return sum;
|
1890 | };
|
1891 | }
|
1892 | function width(g, v) {
|
1893 | return g.node(v).width;
|
1894 | }
|
1895 | function position(g) {
|
1896 | g = asNonCompoundGraph(g);
|
1897 | positionY(g);
|
1898 | forOwn(positionX(g), function(x, v) {
|
1899 | g.node(v).x = x;
|
1900 | });
|
1901 | }
|
1902 | function positionY(g) {
|
1903 | var layering = buildLayerMatrix(g);
|
1904 | var rankSep = g.graph().ranksep;
|
1905 | var prevY = 0;
|
1906 | forEach(layering, function(layer) {
|
1907 | var maxHeight = max(
|
1908 | map(layer, function(v) {
|
1909 | return g.node(v).height;
|
1910 | })
|
1911 | );
|
1912 | forEach(layer, function(v) {
|
1913 | g.node(v).y = prevY + maxHeight / 2;
|
1914 | });
|
1915 | prevY += maxHeight + rankSep;
|
1916 | });
|
1917 | }
|
1918 | function layout(g, opts) {
|
1919 | var time$1 = opts && opts.debugTiming ? time : notime;
|
1920 | time$1("layout", function() {
|
1921 | var layoutGraph = time$1(" buildLayoutGraph", function() {
|
1922 | return buildLayoutGraph(g);
|
1923 | });
|
1924 | time$1(" runLayout", function() {
|
1925 | runLayout(layoutGraph, time$1);
|
1926 | });
|
1927 | time$1(" updateInputGraph", function() {
|
1928 | updateInputGraph(g, layoutGraph);
|
1929 | });
|
1930 | });
|
1931 | }
|
1932 | function runLayout(g, time2) {
|
1933 | time2(" makeSpaceForEdgeLabels", function() {
|
1934 | makeSpaceForEdgeLabels(g);
|
1935 | });
|
1936 | time2(" removeSelfEdges", function() {
|
1937 | removeSelfEdges(g);
|
1938 | });
|
1939 | time2(" acyclic", function() {
|
1940 | run$2(g);
|
1941 | });
|
1942 | time2(" nestingGraph.run", function() {
|
1943 | run(g);
|
1944 | });
|
1945 | time2(" rank", function() {
|
1946 | rank(asNonCompoundGraph(g));
|
1947 | });
|
1948 | time2(" injectEdgeLabelProxies", function() {
|
1949 | injectEdgeLabelProxies(g);
|
1950 | });
|
1951 | time2(" removeEmptyRanks", function() {
|
1952 | removeEmptyRanks(g);
|
1953 | });
|
1954 | time2(" nestingGraph.cleanup", function() {
|
1955 | cleanup(g);
|
1956 | });
|
1957 | time2(" normalizeRanks", function() {
|
1958 | normalizeRanks(g);
|
1959 | });
|
1960 | time2(" assignRankMinMax", function() {
|
1961 | assignRankMinMax(g);
|
1962 | });
|
1963 | time2(" removeEdgeLabelProxies", function() {
|
1964 | removeEdgeLabelProxies(g);
|
1965 | });
|
1966 | time2(" normalize.run", function() {
|
1967 | run$1(g);
|
1968 | });
|
1969 | time2(" parentDummyChains", function() {
|
1970 | parentDummyChains(g);
|
1971 | });
|
1972 | time2(" addBorderSegments", function() {
|
1973 | addBorderSegments(g);
|
1974 | });
|
1975 | time2(" order", function() {
|
1976 | order(g);
|
1977 | });
|
1978 | time2(" insertSelfEdges", function() {
|
1979 | insertSelfEdges(g);
|
1980 | });
|
1981 | time2(" adjustCoordinateSystem", function() {
|
1982 | adjust(g);
|
1983 | });
|
1984 | time2(" position", function() {
|
1985 | position(g);
|
1986 | });
|
1987 | time2(" positionSelfEdges", function() {
|
1988 | positionSelfEdges(g);
|
1989 | });
|
1990 | time2(" removeBorderNodes", function() {
|
1991 | removeBorderNodes(g);
|
1992 | });
|
1993 | time2(" normalize.undo", function() {
|
1994 | undo(g);
|
1995 | });
|
1996 | time2(" fixupEdgeLabelCoords", function() {
|
1997 | fixupEdgeLabelCoords(g);
|
1998 | });
|
1999 | time2(" undoCoordinateSystem", function() {
|
2000 | undo$1(g);
|
2001 | });
|
2002 | time2(" translateGraph", function() {
|
2003 | translateGraph(g);
|
2004 | });
|
2005 | time2(" assignNodeIntersects", function() {
|
2006 | assignNodeIntersects(g);
|
2007 | });
|
2008 | time2(" reversePoints", function() {
|
2009 | reversePointsForReversedEdges(g);
|
2010 | });
|
2011 | time2(" acyclic.undo", function() {
|
2012 | undo$2(g);
|
2013 | });
|
2014 | }
|
2015 | function updateInputGraph(inputGraph, layoutGraph) {
|
2016 | forEach(inputGraph.nodes(), function(v) {
|
2017 | var inputLabel = inputGraph.node(v);
|
2018 | var layoutLabel = layoutGraph.node(v);
|
2019 | if (inputLabel) {
|
2020 | inputLabel.x = layoutLabel.x;
|
2021 | inputLabel.y = layoutLabel.y;
|
2022 | if (layoutGraph.children(v).length) {
|
2023 | inputLabel.width = layoutLabel.width;
|
2024 | inputLabel.height = layoutLabel.height;
|
2025 | }
|
2026 | }
|
2027 | });
|
2028 | forEach(inputGraph.edges(), function(e) {
|
2029 | var inputLabel = inputGraph.edge(e);
|
2030 | var layoutLabel = layoutGraph.edge(e);
|
2031 | inputLabel.points = layoutLabel.points;
|
2032 | if (has(layoutLabel, "x")) {
|
2033 | inputLabel.x = layoutLabel.x;
|
2034 | inputLabel.y = layoutLabel.y;
|
2035 | }
|
2036 | });
|
2037 | inputGraph.graph().width = layoutGraph.graph().width;
|
2038 | inputGraph.graph().height = layoutGraph.graph().height;
|
2039 | }
|
2040 | var graphNumAttrs = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"];
|
2041 | var graphDefaults = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "tb" };
|
2042 | var graphAttrs = ["acyclicer", "ranker", "rankdir", "align"];
|
2043 | var nodeNumAttrs = ["width", "height"];
|
2044 | var nodeDefaults = { width: 0, height: 0 };
|
2045 | var edgeNumAttrs = ["minlen", "weight", "width", "height", "labeloffset"];
|
2046 | var edgeDefaults = {
|
2047 | minlen: 1,
|
2048 | weight: 1,
|
2049 | width: 0,
|
2050 | height: 0,
|
2051 | labeloffset: 10,
|
2052 | labelpos: "r"
|
2053 | };
|
2054 | var edgeAttrs = ["labelpos"];
|
2055 | function buildLayoutGraph(inputGraph) {
|
2056 | var g = new Graph({ multigraph: true, compound: true });
|
2057 | var graph = canonicalize(inputGraph.graph());
|
2058 | g.setGraph(
|
2059 | merge({}, graphDefaults, selectNumberAttrs(graph, graphNumAttrs), pick$1(graph, graphAttrs))
|
2060 | );
|
2061 | forEach(inputGraph.nodes(), function(v) {
|
2062 | var node = canonicalize(inputGraph.node(v));
|
2063 | g.setNode(v, defaults$1(selectNumberAttrs(node, nodeNumAttrs), nodeDefaults));
|
2064 | g.setParent(v, inputGraph.parent(v));
|
2065 | });
|
2066 | forEach(inputGraph.edges(), function(e) {
|
2067 | var edge = canonicalize(inputGraph.edge(e));
|
2068 | g.setEdge(
|
2069 | e,
|
2070 | merge({}, edgeDefaults, selectNumberAttrs(edge, edgeNumAttrs), pick$1(edge, edgeAttrs))
|
2071 | );
|
2072 | });
|
2073 | return g;
|
2074 | }
|
2075 | function makeSpaceForEdgeLabels(g) {
|
2076 | var graph = g.graph();
|
2077 | graph.ranksep /= 2;
|
2078 | forEach(g.edges(), function(e) {
|
2079 | var edge = g.edge(e);
|
2080 | edge.minlen *= 2;
|
2081 | if (edge.labelpos.toLowerCase() !== "c") {
|
2082 | if (graph.rankdir === "TB" || graph.rankdir === "BT") {
|
2083 | edge.width += edge.labeloffset;
|
2084 | } else {
|
2085 | edge.height += edge.labeloffset;
|
2086 | }
|
2087 | }
|
2088 | });
|
2089 | }
|
2090 | function injectEdgeLabelProxies(g) {
|
2091 | forEach(g.edges(), function(e) {
|
2092 | var edge = g.edge(e);
|
2093 | if (edge.width && edge.height) {
|
2094 | var v = g.node(e.v);
|
2095 | var w = g.node(e.w);
|
2096 | var label = { rank: (w.rank - v.rank) / 2 + v.rank, e };
|
2097 | addDummyNode(g, "edge-proxy", label, "_ep");
|
2098 | }
|
2099 | });
|
2100 | }
|
2101 | function assignRankMinMax(g) {
|
2102 | var maxRank2 = 0;
|
2103 | forEach(g.nodes(), function(v) {
|
2104 | var node = g.node(v);
|
2105 | if (node.borderTop) {
|
2106 | node.minRank = g.node(node.borderTop).rank;
|
2107 | node.maxRank = g.node(node.borderBottom).rank;
|
2108 | maxRank2 = max(maxRank2, node.maxRank);
|
2109 | }
|
2110 | });
|
2111 | g.graph().maxRank = maxRank2;
|
2112 | }
|
2113 | function removeEdgeLabelProxies(g) {
|
2114 | forEach(g.nodes(), function(v) {
|
2115 | var node = g.node(v);
|
2116 | if (node.dummy === "edge-proxy") {
|
2117 | g.edge(node.e).labelRank = node.rank;
|
2118 | g.removeNode(v);
|
2119 | }
|
2120 | });
|
2121 | }
|
2122 | function translateGraph(g) {
|
2123 | var minX = Number.POSITIVE_INFINITY;
|
2124 | var maxX = 0;
|
2125 | var minY = Number.POSITIVE_INFINITY;
|
2126 | var maxY = 0;
|
2127 | var graphLabel = g.graph();
|
2128 | var marginX = graphLabel.marginx || 0;
|
2129 | var marginY = graphLabel.marginy || 0;
|
2130 | function getExtremes(attrs) {
|
2131 | var x = attrs.x;
|
2132 | var y = attrs.y;
|
2133 | var w = attrs.width;
|
2134 | var h = attrs.height;
|
2135 | minX = Math.min(minX, x - w / 2);
|
2136 | maxX = Math.max(maxX, x + w / 2);
|
2137 | minY = Math.min(minY, y - h / 2);
|
2138 | maxY = Math.max(maxY, y + h / 2);
|
2139 | }
|
2140 | forEach(g.nodes(), function(v) {
|
2141 | getExtremes(g.node(v));
|
2142 | });
|
2143 | forEach(g.edges(), function(e) {
|
2144 | var edge = g.edge(e);
|
2145 | if (has(edge, "x")) {
|
2146 | getExtremes(edge);
|
2147 | }
|
2148 | });
|
2149 | minX -= marginX;
|
2150 | minY -= marginY;
|
2151 | forEach(g.nodes(), function(v) {
|
2152 | var node = g.node(v);
|
2153 | node.x -= minX;
|
2154 | node.y -= minY;
|
2155 | });
|
2156 | forEach(g.edges(), function(e) {
|
2157 | var edge = g.edge(e);
|
2158 | forEach(edge.points, function(p) {
|
2159 | p.x -= minX;
|
2160 | p.y -= minY;
|
2161 | });
|
2162 | if (has(edge, "x")) {
|
2163 | edge.x -= minX;
|
2164 | }
|
2165 | if (has(edge, "y")) {
|
2166 | edge.y -= minY;
|
2167 | }
|
2168 | });
|
2169 | graphLabel.width = maxX - minX + marginX;
|
2170 | graphLabel.height = maxY - minY + marginY;
|
2171 | }
|
2172 | function assignNodeIntersects(g) {
|
2173 | forEach(g.edges(), function(e) {
|
2174 | var edge = g.edge(e);
|
2175 | var nodeV = g.node(e.v);
|
2176 | var nodeW = g.node(e.w);
|
2177 | var p1, p2;
|
2178 | if (!edge.points) {
|
2179 | edge.points = [];
|
2180 | p1 = nodeW;
|
2181 | p2 = nodeV;
|
2182 | } else {
|
2183 | p1 = edge.points[0];
|
2184 | p2 = edge.points[edge.points.length - 1];
|
2185 | }
|
2186 | edge.points.unshift(intersectRect(nodeV, p1));
|
2187 | edge.points.push(intersectRect(nodeW, p2));
|
2188 | });
|
2189 | }
|
2190 | function fixupEdgeLabelCoords(g) {
|
2191 | forEach(g.edges(), function(e) {
|
2192 | var edge = g.edge(e);
|
2193 | if (has(edge, "x")) {
|
2194 | if (edge.labelpos === "l" || edge.labelpos === "r") {
|
2195 | edge.width -= edge.labeloffset;
|
2196 | }
|
2197 | switch (edge.labelpos) {
|
2198 | case "l":
|
2199 | edge.x -= edge.width / 2 + edge.labeloffset;
|
2200 | break;
|
2201 | case "r":
|
2202 | edge.x += edge.width / 2 + edge.labeloffset;
|
2203 | break;
|
2204 | }
|
2205 | }
|
2206 | });
|
2207 | }
|
2208 | function reversePointsForReversedEdges(g) {
|
2209 | forEach(g.edges(), function(e) {
|
2210 | var edge = g.edge(e);
|
2211 | if (edge.reversed) {
|
2212 | edge.points.reverse();
|
2213 | }
|
2214 | });
|
2215 | }
|
2216 | function removeBorderNodes(g) {
|
2217 | forEach(g.nodes(), function(v) {
|
2218 | if (g.children(v).length) {
|
2219 | var node = g.node(v);
|
2220 | var t = g.node(node.borderTop);
|
2221 | var b = g.node(node.borderBottom);
|
2222 | var l = g.node(last(node.borderLeft));
|
2223 | var r = g.node(last(node.borderRight));
|
2224 | node.width = Math.abs(r.x - l.x);
|
2225 | node.height = Math.abs(b.y - t.y);
|
2226 | node.x = l.x + node.width / 2;
|
2227 | node.y = t.y + node.height / 2;
|
2228 | }
|
2229 | });
|
2230 | forEach(g.nodes(), function(v) {
|
2231 | if (g.node(v).dummy === "border") {
|
2232 | g.removeNode(v);
|
2233 | }
|
2234 | });
|
2235 | }
|
2236 | function removeSelfEdges(g) {
|
2237 | forEach(g.edges(), function(e) {
|
2238 | if (e.v === e.w) {
|
2239 | var node = g.node(e.v);
|
2240 | if (!node.selfEdges) {
|
2241 | node.selfEdges = [];
|
2242 | }
|
2243 | node.selfEdges.push({ e, label: g.edge(e) });
|
2244 | g.removeEdge(e);
|
2245 | }
|
2246 | });
|
2247 | }
|
2248 | function insertSelfEdges(g) {
|
2249 | var layers = buildLayerMatrix(g);
|
2250 | forEach(layers, function(layer) {
|
2251 | var orderShift = 0;
|
2252 | forEach(layer, function(v, i) {
|
2253 | var node = g.node(v);
|
2254 | node.order = i + orderShift;
|
2255 | forEach(node.selfEdges, function(selfEdge) {
|
2256 | addDummyNode(
|
2257 | g,
|
2258 | "selfedge",
|
2259 | {
|
2260 | width: selfEdge.label.width,
|
2261 | height: selfEdge.label.height,
|
2262 | rank: node.rank,
|
2263 | order: i + ++orderShift,
|
2264 | e: selfEdge.e,
|
2265 | label: selfEdge.label
|
2266 | },
|
2267 | "_se"
|
2268 | );
|
2269 | });
|
2270 | delete node.selfEdges;
|
2271 | });
|
2272 | });
|
2273 | }
|
2274 | function positionSelfEdges(g) {
|
2275 | forEach(g.nodes(), function(v) {
|
2276 | var node = g.node(v);
|
2277 | if (node.dummy === "selfedge") {
|
2278 | var selfNode = g.node(node.e.v);
|
2279 | var x = selfNode.x + selfNode.width / 2;
|
2280 | var y = selfNode.y;
|
2281 | var dx = node.x - x;
|
2282 | var dy = selfNode.height / 2;
|
2283 | g.setEdge(node.e, node.label);
|
2284 | g.removeNode(v);
|
2285 | node.label.points = [
|
2286 | { x: x + 2 * dx / 3, y: y - dy },
|
2287 | { x: x + 5 * dx / 6, y: y - dy },
|
2288 | { x: x + dx, y },
|
2289 | { x: x + 5 * dx / 6, y: y + dy },
|
2290 | { x: x + 2 * dx / 3, y: y + dy }
|
2291 | ];
|
2292 | node.label.x = node.x;
|
2293 | node.label.y = node.y;
|
2294 | }
|
2295 | });
|
2296 | }
|
2297 | function selectNumberAttrs(obj, attrs) {
|
2298 | return mapValues(pick$1(obj, attrs), Number);
|
2299 | }
|
2300 | function canonicalize(attrs) {
|
2301 | var newAttrs = {};
|
2302 | forEach(attrs, function(v, k) {
|
2303 | newAttrs[k.toLowerCase()] = v;
|
2304 | });
|
2305 | return newAttrs;
|
2306 | }
|
2307 | export {
|
2308 | defaults$1 as d,
|
2309 | layout as l,
|
2310 | map as m,
|
2311 | pick$1 as p,
|
2312 | range$1 as r,
|
2313 | uniqueId as u
|
2314 | };
|