@statewalker/fsm-charts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE +21 -0
  3. package/README.md +1 -0
  4. package/dist/StateChart-DNGQd1XG.d.ts +53 -0
  5. package/dist/chunk-3GGM3ZWL.js +2685 -0
  6. package/dist/chunk-NXWOFQMO.js +10 -0
  7. package/dist/chunk-TPNWHU7Y.js +437 -0
  8. package/dist/index-layout.d.ts +96 -0
  9. package/dist/index-layout.js +2 -0
  10. package/dist/index-svg.d.ts +25 -0
  11. package/dist/index-svg.js +2 -0
  12. package/dist/index.d.ts +3 -0
  13. package/dist/index.js +3 -0
  14. package/package.json +60 -0
  15. package/src/FsmStateConfig.ts +5 -0
  16. package/src/dagre/acyclic.d.ts +2 -0
  17. package/src/dagre/acyclic.js +60 -0
  18. package/src/dagre/add-border-segments.d.ts +1 -0
  19. package/src/dagre/add-border-segments.js +36 -0
  20. package/src/dagre/coordinate-system.d.ts +2 -0
  21. package/src/dagre/coordinate-system.js +75 -0
  22. package/src/dagre/data/list.d.ts +12 -0
  23. package/src/dagre/data/list.js +55 -0
  24. package/src/dagre/debug.d.ts +2 -0
  25. package/src/dagre/debug.js +32 -0
  26. package/src/dagre/greedy-fas.d.ts +1 -0
  27. package/src/dagre/greedy-fas.js +126 -0
  28. package/src/dagre/index.d.ts +5 -0
  29. package/src/dagre/index.js +6 -0
  30. package/src/dagre/layout.d.ts +1 -0
  31. package/src/dagre/layout.js +466 -0
  32. package/src/dagre/nesting-graph.d.ts +2 -0
  33. package/src/dagre/nesting-graph.js +137 -0
  34. package/src/dagre/normalize.d.ts +2 -0
  35. package/src/dagre/normalize.js +91 -0
  36. package/src/dagre/order/add-subgraph-constraints.d.ts +1 -0
  37. package/src/dagre/order/add-subgraph-constraints.js +53 -0
  38. package/src/dagre/order/barycenter.d.ts +15 -0
  39. package/src/dagre/order/barycenter.js +31 -0
  40. package/src/dagre/order/build-layer-graph.d.ts +2 -0
  41. package/src/dagre/order/build-layer-graph.js +76 -0
  42. package/src/dagre/order/cross-count.d.ts +1 -0
  43. package/src/dagre/order/cross-count.js +82 -0
  44. package/src/dagre/order/index.d.ts +1 -0
  45. package/src/dagre/order/index.js +77 -0
  46. package/src/dagre/order/init-order.d.ts +1 -0
  47. package/src/dagre/order/init-order.js +44 -0
  48. package/src/dagre/order/resolve-conflicts.d.ts +1 -0
  49. package/src/dagre/order/resolve-conflicts.js +128 -0
  50. package/src/dagre/order/sort-subgraph.d.ts +8 -0
  51. package/src/dagre/order/sort-subgraph.js +78 -0
  52. package/src/dagre/order/sort.d.ts +6 -0
  53. package/src/dagre/order/sort.js +59 -0
  54. package/src/dagre/parent-dummy-chains.d.ts +1 -0
  55. package/src/dagre/parent-dummy-chains.js +86 -0
  56. package/src/dagre/position/bk.d.ts +31 -0
  57. package/src/dagre/position/bk.js +423 -0
  58. package/src/dagre/position/index.d.ts +1 -0
  59. package/src/dagre/position/index.js +31 -0
  60. package/src/dagre/rank/feasible-tree.d.ts +2 -0
  61. package/src/dagre/rank/feasible-tree.js +87 -0
  62. package/src/dagre/rank/index.d.ts +1 -0
  63. package/src/dagre/rank/index.js +52 -0
  64. package/src/dagre/rank/network-simplex.d.ts +16 -0
  65. package/src/dagre/rank/network-simplex.js +236 -0
  66. package/src/dagre/rank/util.d.ts +2 -0
  67. package/src/dagre/rank/util.js +63 -0
  68. package/src/dagre/util.d.ts +27 -0
  69. package/src/dagre/util.js +250 -0
  70. package/src/graphlib/alg/components.d.ts +1 -0
  71. package/src/graphlib/alg/components.js +27 -0
  72. package/src/graphlib/alg/dfs.d.ts +1 -0
  73. package/src/graphlib/alg/dfs.js +46 -0
  74. package/src/graphlib/alg/dijkstra-all.d.ts +1 -0
  75. package/src/graphlib/alg/dijkstra-all.js +14 -0
  76. package/src/graphlib/alg/dijkstra.d.ts +1 -0
  77. package/src/graphlib/alg/dijkstra.js +65 -0
  78. package/src/graphlib/alg/find-cycles.d.ts +1 -0
  79. package/src/graphlib/alg/find-cycles.js +10 -0
  80. package/src/graphlib/alg/floyd-warshall.d.ts +1 -0
  81. package/src/graphlib/alg/floyd-warshall.js +55 -0
  82. package/src/graphlib/alg/index.d.ts +24 -0
  83. package/src/graphlib/alg/index.js +25 -0
  84. package/src/graphlib/alg/is-acyclic.d.ts +1 -0
  85. package/src/graphlib/alg/is-acyclic.js +15 -0
  86. package/src/graphlib/alg/postorder.d.ts +1 -0
  87. package/src/graphlib/alg/postorder.js +7 -0
  88. package/src/graphlib/alg/preorder.d.ts +1 -0
  89. package/src/graphlib/alg/preorder.js +7 -0
  90. package/src/graphlib/alg/prim.d.ts +2 -0
  91. package/src/graphlib/alg/prim.js +52 -0
  92. package/src/graphlib/alg/tarjan.d.ts +1 -0
  93. package/src/graphlib/alg/tarjan.js +47 -0
  94. package/src/graphlib/alg/topsort.d.ts +5 -0
  95. package/src/graphlib/alg/topsort.js +36 -0
  96. package/src/graphlib/data/priority-queue.d.ts +59 -0
  97. package/src/graphlib/data/priority-queue.js +152 -0
  98. package/src/graphlib/graph.d.ts +55 -0
  99. package/src/graphlib/graph.js +510 -0
  100. package/src/graphlib/index.d.ts +3 -0
  101. package/src/graphlib/index.js +7 -0
  102. package/src/graphlib/json.d.ts +16 -0
  103. package/src/graphlib/json.js +63 -0
  104. package/src/index-layout.ts +1 -0
  105. package/src/index-svg.ts +1 -0
  106. package/src/index.ts +2 -0
  107. package/src/layout/Dimensions.ts +8 -0
  108. package/src/layout/GraphParamsProvider.ts +17 -0
  109. package/src/layout/Padding.ts +10 -0
  110. package/src/layout/Position.ts +4 -0
  111. package/src/layout/StateChart.ts +8 -0
  112. package/src/layout/StateGraphEdge.ts +14 -0
  113. package/src/layout/StateGraphNode.ts +12 -0
  114. package/src/layout/Transition.ts +5 -0
  115. package/src/layout/TransitionsGraph.ts +13 -0
  116. package/src/layout/buildCharts.ts +74 -0
  117. package/src/layout/buildFlatCharts.ts +142 -0
  118. package/src/layout/getGraphParamsProvider.ts +63 -0
  119. package/src/layout/getLabelDimensions.ts +59 -0
  120. package/src/layout/index.ts +15 -0
  121. package/src/lodash-es/_deleteme.js +291 -0
  122. package/src/lodash-es/index.ts +9 -0
  123. package/src/lodash-es/types.ts +35 -0
  124. package/src/svg/buildStatechartCss.ts +120 -0
  125. package/src/svg/buildStatechartSvg.ts +256 -0
  126. package/src/svg/buildStatechartsPanel.ts +38 -0
  127. package/src/svg/index.ts +3 -0
  128. package/src/utils/BasisCurve.ts +89 -0
  129. package/src/utils/getPadding.ts +13 -0
  130. package/src/utils/serializeAttrs.ts +28 -0
  131. package/src/utils/serializeStyle.ts +11 -0
  132. package/src/utils/toKebabCase.ts +18 -0
@@ -0,0 +1,53 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+
3
+ export { addSubgraphConstraints };
4
+
5
+ function addSubgraphConstraints(g, cg, vs) {
6
+ var prev = {},
7
+ rootPrev;
8
+
9
+ _.forEach(vs, function (v) {
10
+ var child = g.parent(v),
11
+ parent,
12
+ prevChild;
13
+ while (child) {
14
+ parent = g.parent(child);
15
+ if (parent) {
16
+ prevChild = prev[parent];
17
+ prev[parent] = child;
18
+ } else {
19
+ prevChild = rootPrev;
20
+ rootPrev = child;
21
+ }
22
+ if (prevChild && prevChild !== child) {
23
+ cg.setEdge(prevChild, child);
24
+ return;
25
+ }
26
+ child = parent;
27
+ }
28
+ });
29
+
30
+ /*
31
+ function dfs(v) {
32
+ var children = v ? g.children(v) : g.children();
33
+ if (children.length) {
34
+ var min = Number.POSITIVE_INFINITY,
35
+ subgraphs = [];
36
+ _.each(children, function(child) {
37
+ var childMin = dfs(child);
38
+ if (g.children(child).length) {
39
+ subgraphs.push({ v: child, order: childMin });
40
+ }
41
+ min = Math.min(min, childMin);
42
+ });
43
+ _.reduce(_.sortBy(subgraphs, "order"), function(prev, curr) {
44
+ cg.setEdge(prev.v, curr.v);
45
+ return curr;
46
+ });
47
+ return min;
48
+ }
49
+ return g.node(v).order;
50
+ }
51
+ dfs(undefined);
52
+ */
53
+ }
@@ -0,0 +1,15 @@
1
+ export function barycenter(
2
+ g: any,
3
+ movable: any
4
+ ): (
5
+ | {
6
+ v: any;
7
+ barycenter?: undefined;
8
+ weight?: undefined;
9
+ }
10
+ | {
11
+ v: any;
12
+ barycenter: number;
13
+ weight: any;
14
+ }
15
+ )[];
@@ -0,0 +1,31 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+
3
+ export { barycenter };
4
+
5
+ function barycenter(g, movable) {
6
+ return _.map(movable, function (v) {
7
+ var inV = g.inEdges(v);
8
+ if (!inV.length) {
9
+ return { v: v };
10
+ } else {
11
+ var result = _.reduce(
12
+ inV,
13
+ function (acc, e) {
14
+ var edge = g.edge(e),
15
+ nodeU = g.node(e.v);
16
+ return {
17
+ sum: acc.sum + edge.weight * nodeU.order,
18
+ weight: acc.weight + edge.weight,
19
+ };
20
+ },
21
+ { sum: 0, weight: 0 }
22
+ );
23
+
24
+ return {
25
+ v: v,
26
+ barycenter: result.sum / result.weight,
27
+ weight: result.weight,
28
+ };
29
+ }
30
+ });
31
+ }
@@ -0,0 +1,2 @@
1
+ export function buildLayerGraph(g: any, rank: any, relationship: any): Graph;
2
+ import { Graph } from '../../graphlib/index.js';
@@ -0,0 +1,76 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+ import { Graph } from '../../graphlib/index.js';
3
+
4
+ export { buildLayerGraph };
5
+
6
+ /*
7
+ * Constructs a graph that can be used to sort a layer of nodes. The graph will
8
+ * contain all base and subgraph nodes from the request layer in their original
9
+ * hierarchy and any edges that are incident on these nodes and are of the type
10
+ * requested by the "relationship" parameter.
11
+ *
12
+ * Nodes from the requested rank that do not have parents are assigned a root
13
+ * node in the output graph, which is set in the root graph attribute. This
14
+ * makes it easy to walk the hierarchy of movable nodes during ordering.
15
+ *
16
+ * Pre-conditions:
17
+ *
18
+ * 1. Input graph is a DAG
19
+ * 2. Base nodes in the input graph have a rank attribute
20
+ * 3. Subgraph nodes in the input graph has minRank and maxRank attributes
21
+ * 4. Edges have an assigned weight
22
+ *
23
+ * Post-conditions:
24
+ *
25
+ * 1. Output graph has all nodes in the movable rank with preserved
26
+ * hierarchy.
27
+ * 2. Root nodes in the movable layer are made children of the node
28
+ * indicated by the root attribute of the graph.
29
+ * 3. Non-movable nodes incident on movable nodes, selected by the
30
+ * relationship parameter, are included in the graph (without hierarchy).
31
+ * 4. Edges incident on movable nodes, selected by the relationship
32
+ * parameter, are added to the output graph.
33
+ * 5. The weights for copied edges are aggregated as need, since the output
34
+ * graph is not a multi-graph.
35
+ */
36
+ function buildLayerGraph(g, rank, relationship) {
37
+ var root = createRootNode(g),
38
+ result = new Graph({ compound: true })
39
+ .setGraph({ root: root })
40
+ .setDefaultNodeLabel(function (v) {
41
+ return g.node(v);
42
+ });
43
+
44
+ _.forEach(g.nodes(), function (v) {
45
+ var node = g.node(v),
46
+ parent = g.parent(v);
47
+
48
+ if (node.rank === rank || (node.minRank <= rank && rank <= node.maxRank)) {
49
+ result.setNode(v);
50
+ result.setParent(v, parent || root);
51
+
52
+ // This assumes we have only short edges!
53
+ _.forEach(g[relationship](v), function (e) {
54
+ var u = e.v === v ? e.w : e.v,
55
+ edge = result.edge(u, v),
56
+ weight = !_.isUndefined(edge) ? edge.weight : 0;
57
+ result.setEdge(u, v, { weight: g.edge(e).weight + weight });
58
+ });
59
+
60
+ if (_.has(node, 'minRank')) {
61
+ result.setNode(v, {
62
+ borderLeft: node.borderLeft[rank],
63
+ borderRight: node.borderRight[rank],
64
+ });
65
+ }
66
+ }
67
+ });
68
+
69
+ return result;
70
+ }
71
+
72
+ function createRootNode(g) {
73
+ var v;
74
+ while (g.hasNode((v = _.uniqueId('_root'))));
75
+ return v;
76
+ }
@@ -0,0 +1 @@
1
+ export function crossCount(g: any, layering: any): number;
@@ -0,0 +1,82 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+
3
+ export { crossCount };
4
+
5
+ /*
6
+ * A function that takes a layering (an array of layers, each with an array of
7
+ * ordererd nodes) and a graph and returns a weighted crossing count.
8
+ *
9
+ * Pre-conditions:
10
+ *
11
+ * 1. Input graph must be simple (not a multigraph), directed, and include
12
+ * only simple edges.
13
+ * 2. Edges in the input graph must have assigned weights.
14
+ *
15
+ * Post-conditions:
16
+ *
17
+ * 1. The graph and layering matrix are left unchanged.
18
+ *
19
+ * This algorithm is derived from Barth, et al., "Bilayer Cross Counting."
20
+ */
21
+ function crossCount(g, layering) {
22
+ var cc = 0;
23
+ for (var i = 1; i < layering.length; ++i) {
24
+ cc += twoLayerCrossCount(g, layering[i - 1], layering[i]);
25
+ }
26
+ return cc;
27
+ }
28
+
29
+ function twoLayerCrossCount(g, northLayer, southLayer) {
30
+ // Sort all of the edges between the north and south layers by their position
31
+ // in the north layer and then the south. Map these edges to the position of
32
+ // their head in the south layer.
33
+ var southPos = _.zipObject(
34
+ southLayer,
35
+ _.map(southLayer, function (v, i) {
36
+ return i;
37
+ })
38
+ );
39
+ var southEntries = _.flatten(
40
+ _.map(northLayer, function (v) {
41
+ return _.sortBy(
42
+ _.map(g.outEdges(v), function (e) {
43
+ return { pos: southPos[e.w], weight: g.edge(e).weight };
44
+ }),
45
+ 'pos'
46
+ );
47
+ })
48
+ );
49
+
50
+ // Build the accumulator tree
51
+ var firstIndex = 1;
52
+ while (firstIndex < southLayer.length) firstIndex <<= 1;
53
+ var treeSize = 2 * firstIndex - 1;
54
+ firstIndex -= 1;
55
+ var tree = _.map(new Array(treeSize), function () {
56
+ return 0;
57
+ });
58
+
59
+ // Calculate the weighted crossings
60
+ var cc = 0;
61
+ _.forEach(
62
+ // @ts-expect-error
63
+ southEntries.forEach(function (entry) {
64
+ var index = entry.pos + firstIndex;
65
+ tree[index] += entry.weight;
66
+ var weightSum = 0;
67
+ // @ts-expect-error
68
+ while (index > 0) {
69
+ // @ts-expect-error
70
+ if (index % 2) {
71
+ weightSum += tree[index + 1];
72
+ }
73
+ // @ts-expect-error
74
+ index = (index - 1) >> 1;
75
+ tree[index] += entry.weight;
76
+ }
77
+ cc += entry.weight * weightSum;
78
+ })
79
+ );
80
+
81
+ return cc;
82
+ }
@@ -0,0 +1 @@
1
+ export function order(g: any): void;
@@ -0,0 +1,77 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+ import { Graph } from '../../graphlib/index.js';
3
+ import * as util from '../util.js';
4
+ import { addSubgraphConstraints } from './add-subgraph-constraints.js';
5
+ import { buildLayerGraph } from './build-layer-graph.js';
6
+ import { crossCount } from './cross-count.js';
7
+ import { initOrder } from './init-order.js';
8
+ import { sortSubgraph } from './sort-subgraph.js';
9
+
10
+ export { order };
11
+
12
+ /*
13
+ * Applies heuristics to minimize edge crossings in the graph and sets the best
14
+ * order solution as an order attribute on each node.
15
+ *
16
+ * Pre-conditions:
17
+ *
18
+ * 1. Graph must be DAG
19
+ * 2. Graph nodes must be objects with a "rank" attribute
20
+ * 3. Graph edges must have the "weight" attribute
21
+ *
22
+ * Post-conditions:
23
+ *
24
+ * 1. Graph nodes will have an "order" attribute based on the results of the
25
+ * algorithm.
26
+ */
27
+ function order(g) {
28
+ var maxRank = util.maxRank(g),
29
+ downLayerGraphs = buildLayerGraphs(g, _.range(1, maxRank + 1), 'inEdges'),
30
+ upLayerGraphs = buildLayerGraphs(g, _.range(maxRank - 1, -1, -1), 'outEdges');
31
+
32
+ var layering = initOrder(g);
33
+ assignOrder(g, layering);
34
+
35
+ var bestCC = Number.POSITIVE_INFINITY,
36
+ best;
37
+
38
+ for (var i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) {
39
+ sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2);
40
+
41
+ layering = util.buildLayerMatrix(g);
42
+ var cc = crossCount(g, layering);
43
+ if (cc < bestCC) {
44
+ lastBest = 0;
45
+ best = _.cloneDeep(layering);
46
+ bestCC = cc;
47
+ }
48
+ }
49
+
50
+ assignOrder(g, best);
51
+ }
52
+
53
+ function buildLayerGraphs(g, ranks, relationship) {
54
+ return _.map(ranks, function (rank) {
55
+ return buildLayerGraph(g, rank, relationship);
56
+ });
57
+ }
58
+
59
+ function sweepLayerGraphs(layerGraphs, biasRight) {
60
+ var cg = new Graph();
61
+ _.forEach(layerGraphs, function (lg) {
62
+ var root = lg.graph().root;
63
+ var sorted = sortSubgraph(lg, root, cg, biasRight);
64
+ _.forEach(sorted.vs, function (v, i) {
65
+ lg.node(v).order = i;
66
+ });
67
+ addSubgraphConstraints(lg, cg, sorted.vs);
68
+ });
69
+ }
70
+
71
+ function assignOrder(g, layering) {
72
+ _.forEach(layering, function (layer) {
73
+ _.forEach(layer, function (v, i) {
74
+ g.node(v).order = i;
75
+ });
76
+ });
77
+ }
@@ -0,0 +1 @@
1
+ export function initOrder(g: any): any[][];
@@ -0,0 +1,44 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+
3
+ export { initOrder };
4
+
5
+ /*
6
+ * Assigns an initial order value for each node by performing a DFS search
7
+ * starting from nodes in the first rank. Nodes are assigned an order in their
8
+ * rank as they are first visited.
9
+ *
10
+ * This approach comes from Gansner, et al., "A Technique for Drawing Directed
11
+ * Graphs."
12
+ *
13
+ * Returns a layering matrix with an array per layer and each layer sorted by
14
+ * the order of its nodes.
15
+ */
16
+ function initOrder(g) {
17
+ var visited = {};
18
+ var simpleNodes = _.filter(g.nodes(), function (v) {
19
+ return !g.children(v).length;
20
+ });
21
+ var maxRank = _.max(
22
+ _.map(simpleNodes, function (v) {
23
+ return g.node(v).rank;
24
+ })
25
+ );
26
+ var layers = _.map(_.range(maxRank + 1), function () {
27
+ return [];
28
+ });
29
+
30
+ function dfs(v) {
31
+ if (_.has(visited, v)) return;
32
+ visited[v] = true;
33
+ var node = g.node(v);
34
+ layers[node.rank].push(v);
35
+ _.forEach(g.successors(v), dfs);
36
+ }
37
+
38
+ var orderedVs = _.sortBy(simpleNodes, function (v) {
39
+ return g.node(v).rank;
40
+ });
41
+ _.forEach(orderedVs, dfs);
42
+
43
+ return layers;
44
+ }
@@ -0,0 +1 @@
1
+ export function resolveConflicts(entries: any, cg: any): Pick<any, string>[];
@@ -0,0 +1,128 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+
3
+ export { resolveConflicts };
4
+
5
+ /*
6
+ * Given a list of entries of the form {v, barycenter, weight} and a
7
+ * constraint graph this function will resolve any conflicts between the
8
+ * constraint graph and the barycenters for the entries. If the barycenters for
9
+ * an entry would violate a constraint in the constraint graph then we coalesce
10
+ * the nodes in the conflict into a new node that respects the contraint and
11
+ * aggregates barycenter and weight information.
12
+ *
13
+ * This implementation is based on the description in Forster, "A Fast and
14
+ * Simple Hueristic for Constrained Two-Level Crossing Reduction," thought it
15
+ * differs in some specific details.
16
+ *
17
+ * Pre-conditions:
18
+ *
19
+ * 1. Each entry has the form {v, barycenter, weight}, or if the node has
20
+ * no barycenter, then {v}.
21
+ *
22
+ * Returns:
23
+ *
24
+ * A new list of entries of the form {vs, i, barycenter, weight}. The list
25
+ * `vs` may either be a singleton or it may be an aggregation of nodes
26
+ * ordered such that they do not violate constraints from the constraint
27
+ * graph. The property `i` is the lowest original index of any of the
28
+ * elements in `vs`.
29
+ */
30
+ function resolveConflicts(entries, cg) {
31
+ var mappedEntries = {};
32
+ _.forEach(entries, function (entry, i) {
33
+ var tmp = (mappedEntries[entry.v] = {
34
+ indegree: 0,
35
+ in: [],
36
+ out: [],
37
+ vs: [entry.v],
38
+ i: i,
39
+ });
40
+ if (!_.isUndefined(entry.barycenter)) {
41
+ // @ts-expect-error
42
+ tmp.barycenter = entry.barycenter;
43
+ // @ts-expect-error
44
+ tmp.weight = entry.weight;
45
+ }
46
+ });
47
+
48
+ _.forEach(cg.edges(), function (e) {
49
+ var entryV = mappedEntries[e.v];
50
+ var entryW = mappedEntries[e.w];
51
+ if (!_.isUndefined(entryV) && !_.isUndefined(entryW)) {
52
+ entryW.indegree++;
53
+ entryV.out.push(mappedEntries[e.w]);
54
+ }
55
+ });
56
+
57
+ var sourceSet = _.filter(mappedEntries, function (entry) {
58
+ // @ts-expect-error
59
+ return !entry.indegree;
60
+ });
61
+
62
+ return doResolveConflicts(sourceSet);
63
+ }
64
+
65
+ function doResolveConflicts(sourceSet) {
66
+ var entries = [];
67
+
68
+ function handleIn(vEntry) {
69
+ return function (uEntry) {
70
+ if (uEntry.merged) {
71
+ return;
72
+ }
73
+ if (
74
+ _.isUndefined(uEntry.barycenter) ||
75
+ _.isUndefined(vEntry.barycenter) ||
76
+ uEntry.barycenter >= vEntry.barycenter
77
+ ) {
78
+ mergeEntries(vEntry, uEntry);
79
+ }
80
+ };
81
+ }
82
+
83
+ function handleOut(vEntry) {
84
+ return function (wEntry) {
85
+ wEntry['in'].push(vEntry);
86
+ if (--wEntry.indegree === 0) {
87
+ sourceSet.push(wEntry);
88
+ }
89
+ };
90
+ }
91
+
92
+ while (sourceSet.length) {
93
+ var entry = sourceSet.pop();
94
+ entries.push(entry);
95
+ _.forEach(entry['in'].reverse(), handleIn(entry));
96
+ _.forEach(entry.out, handleOut(entry));
97
+ }
98
+
99
+ return _.map(
100
+ _.filter(entries, function (entry) {
101
+ return !entry.merged;
102
+ }),
103
+ function (entry) {
104
+ return _.pick(entry, ['vs', 'i', 'barycenter', 'weight']);
105
+ }
106
+ );
107
+ }
108
+
109
+ function mergeEntries(target, source) {
110
+ var sum = 0;
111
+ var weight = 0;
112
+
113
+ if (target.weight) {
114
+ sum += target.barycenter * target.weight;
115
+ weight += target.weight;
116
+ }
117
+
118
+ if (source.weight) {
119
+ sum += source.barycenter * source.weight;
120
+ weight += source.weight;
121
+ }
122
+
123
+ target.vs = source.vs.concat(target.vs);
124
+ target.barycenter = sum / weight;
125
+ target.weight = weight;
126
+ target.i = Math.min(source.i, target.i);
127
+ source.merged = true;
128
+ }
@@ -0,0 +1,8 @@
1
+ export function sortSubgraph(
2
+ g: any,
3
+ v: any,
4
+ cg: any,
5
+ biasRight: any
6
+ ): {
7
+ vs: any[];
8
+ };
@@ -0,0 +1,78 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+ import { barycenter } from './barycenter.js';
3
+ import { resolveConflicts } from './resolve-conflicts.js';
4
+ import { sort } from './sort.js';
5
+
6
+ export { sortSubgraph };
7
+
8
+ function sortSubgraph(g, v, cg, biasRight) {
9
+ var movable = g.children(v);
10
+ var node = g.node(v);
11
+ var bl = node ? node.borderLeft : undefined;
12
+ var br = node ? node.borderRight : undefined;
13
+ var subgraphs = {};
14
+
15
+ if (bl) {
16
+ movable = _.filter(movable, function (w) {
17
+ return w !== bl && w !== br;
18
+ });
19
+ }
20
+
21
+ var barycenters = barycenter(g, movable);
22
+ _.forEach(barycenters, function (entry) {
23
+ if (g.children(entry.v).length) {
24
+ var subgraphResult = sortSubgraph(g, entry.v, cg, biasRight);
25
+ subgraphs[entry.v] = subgraphResult;
26
+ if (_.has(subgraphResult, 'barycenter')) {
27
+ mergeBarycenters(entry, subgraphResult);
28
+ }
29
+ }
30
+ });
31
+
32
+ var entries = resolveConflicts(barycenters, cg);
33
+ expandSubgraphs(entries, subgraphs);
34
+
35
+ var result = sort(entries, biasRight);
36
+
37
+ if (bl) {
38
+ result.vs = _.flatten([bl, result.vs, br]);
39
+ if (g.predecessors(bl).length) {
40
+ var blPred = g.node(g.predecessors(bl)[0]),
41
+ brPred = g.node(g.predecessors(br)[0]);
42
+ if (!_.has(result, 'barycenter')) {
43
+ result.barycenter = 0;
44
+ result.weight = 0;
45
+ }
46
+ result.barycenter =
47
+ (result.barycenter * result.weight + blPred.order + brPred.order) / (result.weight + 2);
48
+ result.weight += 2;
49
+ }
50
+ }
51
+
52
+ return result;
53
+ }
54
+
55
+ function expandSubgraphs(entries, subgraphs) {
56
+ _.forEach(entries, function (entry) {
57
+ entry.vs = _.flatten(
58
+ entry.vs.map(function (v) {
59
+ if (subgraphs[v]) {
60
+ return subgraphs[v].vs;
61
+ }
62
+ return v;
63
+ })
64
+ );
65
+ });
66
+ }
67
+
68
+ function mergeBarycenters(target, other) {
69
+ if (!_.isUndefined(target.barycenter)) {
70
+ target.barycenter =
71
+ (target.barycenter * target.weight + other.barycenter * other.weight) /
72
+ (target.weight + other.weight);
73
+ target.weight += other.weight;
74
+ } else {
75
+ target.barycenter = other.barycenter;
76
+ target.weight = other.weight;
77
+ }
78
+ }
@@ -0,0 +1,6 @@
1
+ export function sort(
2
+ entries: any,
3
+ biasRight: any
4
+ ): {
5
+ vs: any[];
6
+ };
@@ -0,0 +1,59 @@
1
+ import { _ } from '../../lodash-es/index.ts';
2
+ import * as util from '../util.js';
3
+
4
+ export { sort };
5
+
6
+ function sort(entries, biasRight) {
7
+ var parts = util.partition(entries, function (entry) {
8
+ return _.has(entry, 'barycenter');
9
+ });
10
+ var sortable = parts.lhs,
11
+ unsortable = _.sortBy(parts.rhs, function (entry) {
12
+ return -entry.i;
13
+ }),
14
+ vs = [],
15
+ sum = 0,
16
+ weight = 0,
17
+ vsIndex = 0;
18
+
19
+ sortable.sort(compareWithBias(!!biasRight));
20
+
21
+ vsIndex = consumeUnsortable(vs, unsortable, vsIndex);
22
+
23
+ _.forEach(sortable, function (entry) {
24
+ vsIndex += entry.vs.length;
25
+ vs.push(entry.vs);
26
+ sum += entry.barycenter * entry.weight;
27
+ weight += entry.weight;
28
+ vsIndex = consumeUnsortable(vs, unsortable, vsIndex);
29
+ });
30
+
31
+ var result = { vs: _.flatten(vs) };
32
+ if (weight) {
33
+ result.barycenter = sum / weight;
34
+ result.weight = weight;
35
+ }
36
+ return result;
37
+ }
38
+
39
+ function consumeUnsortable(vs, unsortable, index) {
40
+ var last;
41
+ while (unsortable.length && (last = _.last(unsortable)).i <= index) {
42
+ unsortable.pop();
43
+ vs.push(last.vs);
44
+ index++;
45
+ }
46
+ return index;
47
+ }
48
+
49
+ function compareWithBias(bias) {
50
+ return function (entryV, entryW) {
51
+ if (entryV.barycenter < entryW.barycenter) {
52
+ return -1;
53
+ } else if (entryV.barycenter > entryW.barycenter) {
54
+ return 1;
55
+ }
56
+
57
+ return !bias ? entryV.i - entryW.i : entryW.i - entryV.i;
58
+ };
59
+ }
@@ -0,0 +1 @@
1
+ export function parentDummyChains(g: any): void;