@ac-kit/math-graph 0.0.0-stage → 0.1.0

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 (88) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/LICENSE +28 -0
  3. package/README.md +92 -2
  4. package/dist/index.d.ts +12 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +11 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/layout/_force-forces.d.ts +103 -0
  9. package/dist/layout/_force-forces.d.ts.map +1 -0
  10. package/dist/layout/_force-forces.js +490 -0
  11. package/dist/layout/_force-forces.js.map +1 -0
  12. package/dist/layout/_hierarchy-table.d.ts +34 -0
  13. package/dist/layout/_hierarchy-table.d.ts.map +1 -0
  14. package/dist/layout/_hierarchy-table.js +131 -0
  15. package/dist/layout/_hierarchy-table.js.map +1 -0
  16. package/dist/layout/_index-graph.d.ts +32 -0
  17. package/dist/layout/_index-graph.d.ts.map +1 -0
  18. package/dist/layout/_index-graph.js +51 -0
  19. package/dist/layout/_index-graph.js.map +1 -0
  20. package/dist/layout/_layer-order.d.ts +96 -0
  21. package/dist/layout/_layer-order.d.ts.map +1 -0
  22. package/dist/layout/_layer-order.js +462 -0
  23. package/dist/layout/_layer-order.js.map +1 -0
  24. package/dist/layout/_layered-acyclic.d.ts +28 -0
  25. package/dist/layout/_layered-acyclic.d.ts.map +1 -0
  26. package/dist/layout/_layered-acyclic.js +179 -0
  27. package/dist/layout/_layered-acyclic.js.map +1 -0
  28. package/dist/layout/_layered-position.d.ts +56 -0
  29. package/dist/layout/_layered-position.d.ts.map +1 -0
  30. package/dist/layout/_layered-position.js +369 -0
  31. package/dist/layout/_layered-position.js.map +1 -0
  32. package/dist/layout/_layered-rank.d.ts +54 -0
  33. package/dist/layout/_layered-rank.d.ts.map +1 -0
  34. package/dist/layout/_layered-rank.js +436 -0
  35. package/dist/layout/_layered-rank.js.map +1 -0
  36. package/dist/layout/_tree-common.d.ts +38 -0
  37. package/dist/layout/_tree-common.d.ts.map +1 -0
  38. package/dist/layout/_tree-common.js +73 -0
  39. package/dist/layout/_tree-common.js.map +1 -0
  40. package/dist/layout/_treemap-tiling.d.ts +62 -0
  41. package/dist/layout/_treemap-tiling.d.ts.map +1 -0
  42. package/dist/layout/_treemap-tiling.js +187 -0
  43. package/dist/layout/_treemap-tiling.js.map +1 -0
  44. package/dist/layout/circular-layout.d.ts +27 -0
  45. package/dist/layout/circular-layout.d.ts.map +1 -0
  46. package/dist/layout/circular-layout.js +35 -0
  47. package/dist/layout/circular-layout.js.map +1 -0
  48. package/dist/layout/create-force-step.d.ts +112 -0
  49. package/dist/layout/create-force-step.d.ts.map +1 -0
  50. package/dist/layout/create-force-step.js +134 -0
  51. package/dist/layout/create-force-step.js.map +1 -0
  52. package/dist/layout/dendrogram-layout.d.ts +32 -0
  53. package/dist/layout/dendrogram-layout.d.ts.map +1 -0
  54. package/dist/layout/dendrogram-layout.js +110 -0
  55. package/dist/layout/dendrogram-layout.js.map +1 -0
  56. package/dist/layout/force-directed-layout.d.ts +42 -0
  57. package/dist/layout/force-directed-layout.d.ts.map +1 -0
  58. package/dist/layout/force-directed-layout.js +112 -0
  59. package/dist/layout/force-directed-layout.js.map +1 -0
  60. package/dist/layout/layered-layout.d.ts +96 -0
  61. package/dist/layout/layered-layout.d.ts.map +1 -0
  62. package/dist/layout/layered-layout.js +120 -0
  63. package/dist/layout/layered-layout.js.map +1 -0
  64. package/dist/layout/partition-layout.d.ts +31 -0
  65. package/dist/layout/partition-layout.d.ts.map +1 -0
  66. package/dist/layout/partition-layout.js +78 -0
  67. package/dist/layout/partition-layout.js.map +1 -0
  68. package/dist/layout/phyllotaxis-layout.d.ts +17 -0
  69. package/dist/layout/phyllotaxis-layout.d.ts.map +1 -0
  70. package/dist/layout/phyllotaxis-layout.js +32 -0
  71. package/dist/layout/phyllotaxis-layout.js.map +1 -0
  72. package/dist/layout/sankey-layout.d.ts +104 -0
  73. package/dist/layout/sankey-layout.d.ts.map +1 -0
  74. package/dist/layout/sankey-layout.js +636 -0
  75. package/dist/layout/sankey-layout.js.map +1 -0
  76. package/dist/layout/tidy-tree-layout.d.ts +25 -0
  77. package/dist/layout/tidy-tree-layout.d.ts.map +1 -0
  78. package/dist/layout/tidy-tree-layout.js +197 -0
  79. package/dist/layout/tidy-tree-layout.js.map +1 -0
  80. package/dist/layout/treemap-layout.d.ts +54 -0
  81. package/dist/layout/treemap-layout.d.ts.map +1 -0
  82. package/dist/layout/treemap-layout.js +121 -0
  83. package/dist/layout/treemap-layout.js.map +1 -0
  84. package/dist/layout/types.d.ts +90 -0
  85. package/dist/layout/types.d.ts.map +1 -0
  86. package/dist/layout/types.js +2 -0
  87. package/dist/layout/types.js.map +1 -0
  88. package/package.json +66 -3
@@ -0,0 +1,131 @@
1
+ import { groupIndices, walkTree } from "@ac-kit/algo";
2
+ import { assertNonNegative, describeValue } from "@ac-kit/core";
3
+ import { TypedVector } from "@ac-kit/data";
4
+ /**
5
+ * Flattens the hierarchy under `root` in pre-order over `walkTree`: no
6
+ * recursion, one budget unit per row. A node reachable through several branches
7
+ * is a separate row each time; a node met again below itself is a cycle.
8
+ *
9
+ * Complexity: O(n) time, O(n) memory.
10
+ *
11
+ * @throws {RangeError} A cycle, naming the repeated row and its ancestor row.
12
+ */
13
+ export function buildHierarchyTable(root, childrenOf, options = {}, functionName = "layout") {
14
+ const nodes = [];
15
+ const parentIndices = new TypedVector(Int32Array);
16
+ const depths = new TypedVector(Uint32Array);
17
+ // Row of the latest node seen at each depth: the parent of the next one.
18
+ const rowAtDepth = [];
19
+ const ancestorRows = new Map();
20
+ walkTree(root, childrenOf, (node, _parent, _siblingIndex, depth) => {
21
+ while (rowAtDepth.length > depth) {
22
+ ancestorRows.delete(nodes[rowAtDepth.pop()]);
23
+ }
24
+ const row = nodes.length;
25
+ const ancestorRow = ancestorRows.get(node);
26
+ if (ancestorRow !== undefined) {
27
+ throw new RangeError(`${functionName}: row ${row} repeats its ancestor row ${ancestorRow}`);
28
+ }
29
+ nodes.push(node);
30
+ parentIndices.pushBack(depth === 0 ? -1 : rowAtDepth[depth - 1]);
31
+ depths.pushBack(depth);
32
+ rowAtDepth[depth] = row;
33
+ ancestorRows.set(node, row);
34
+ return undefined;
35
+ }, { budget: options.budget });
36
+ return {
37
+ nodes,
38
+ parentIndices: parentIndices.toTypedArray(),
39
+ depths: depths.toTypedArray(),
40
+ };
41
+ }
42
+ /**
43
+ * {@link buildHierarchyTable} plus the summed value per row (d3 `sum`/`count`)
44
+ * and, for `"valueDescending"`, children reordered by value, stable.
45
+ *
46
+ * `ownValueOf` is read once per row, in pre-order of the input order, and
47
+ * checked finite and ≥ 0. The sum of a row is its own value plus its
48
+ * children's, added from the last child to the first.
49
+ *
50
+ * Complexity: O(n) time and memory, plus O(k log k) per child list of k
51
+ * children when reordering.
52
+ *
53
+ * @throws {RangeError} A cycle, a value that is not finite and ≥ 0, a sum that
54
+ * overflows (naming the row), or an unknown `order`.
55
+ */
56
+ export function buildValuedHierarchyTable(root, traits, options = {}, functionName = "layout") {
57
+ const { order } = options;
58
+ if (order !== undefined && order !== "input" && order !== "valueDescending") {
59
+ throw new RangeError(`${functionName}: order must be "input" or "valueDescending", got ${describeValue(order)}`);
60
+ }
61
+ const table = buildHierarchyTable(root, traits.childrenOf, options, functionName);
62
+ const { parentIndices } = table;
63
+ const count = table.nodes.length;
64
+ const values = new Float64Array(count);
65
+ const hasChildren = new Uint8Array(count);
66
+ for (let row = 1; row < count; row++) {
67
+ hasChildren[parentIndices[row]] = 1;
68
+ }
69
+ const { ownValueOf } = traits;
70
+ for (let row = 0; row < count; row++) {
71
+ const value = ownValueOf === undefined
72
+ ? hasChildren[row] === 1
73
+ ? 0
74
+ : 1
75
+ : ownValueOf(table.nodes[row], row);
76
+ assertNonNegative(value, `${functionName}: value of row ${row}`);
77
+ values[row] = value;
78
+ }
79
+ // Children have higher rows than their parent: one reverse pass sums bottom-up.
80
+ for (let row = count - 1; row > 0; row--) {
81
+ const parent = parentIndices[row];
82
+ const sum = values[parent] + values[row];
83
+ if (!Number.isFinite(sum)) {
84
+ throw new RangeError(`${functionName}: sum of row ${parent} overflows`);
85
+ }
86
+ values[parent] = sum;
87
+ }
88
+ if (order === "valueDescending" && count > 1) {
89
+ return sortByValueDescending_(table, values);
90
+ }
91
+ return { ...table, values };
92
+ }
93
+ function sortByValueDescending_(table, values) {
94
+ const { nodes, parentIndices, depths } = table;
95
+ const count = nodes.length;
96
+ const { offsets: childOffsets, indices: children } = groupIndices(parentIndices, (parent) => parent, count);
97
+ for (let row = 0; row < count; row++) {
98
+ const start = childOffsets[row];
99
+ const end = childOffsets[row + 1];
100
+ if (end - start > 1) {
101
+ // TypedArray.prototype.sort is stable.
102
+ children.subarray(start, end).sort((a, b) => {
103
+ const valueA = values[a];
104
+ const valueB = values[b];
105
+ return valueA < valueB ? 1 : valueA > valueB ? -1 : 0;
106
+ });
107
+ }
108
+ }
109
+ const sortedNodes = [];
110
+ const sortedParents = new Int32Array(count);
111
+ const sortedDepths = new Uint32Array(count);
112
+ const sortedValues = new Float64Array(count);
113
+ const newRows = new Uint32Array(count);
114
+ walkTree(0, (row) => children.subarray(childOffsets[row], childOffsets[row + 1]), (row) => {
115
+ const newRow = sortedNodes.length;
116
+ newRows[row] = newRow;
117
+ sortedNodes.push(nodes[row]);
118
+ const parent = parentIndices[row];
119
+ sortedParents[newRow] = parent < 0 ? -1 : newRows[parent];
120
+ sortedDepths[newRow] = depths[row];
121
+ sortedValues[newRow] = values[row];
122
+ return undefined;
123
+ });
124
+ return {
125
+ nodes: sortedNodes,
126
+ parentIndices: sortedParents,
127
+ depths: sortedDepths,
128
+ values: sortedValues,
129
+ };
130
+ }
131
+ //# sourceMappingURL=_hierarchy-table.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_hierarchy-table.js","sourceRoot":"","sources":["../../src/layout/_hierarchy-table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,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MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC;IAEvC,QAAQ,CACP,CAAC,EACD,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,YAAY,CAAC,GAAG,CAAE,EAAE,YAAY,CAAC,GAAG,GAAG,CAAC,CAAE,CAAC,EACtE,CAAC,GAAG,EAAE,EAAE;QACP,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC;QAClC,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;QACtB,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAE,CAAC,CAAC;QAC9B,MAAM,MAAM,GAAG,aAAa,CAAC,GAAG,CAAE,CAAC;QACnC,aAAa,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAE,CAAC;QAC3D,YAAY,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAE,CAAC;QACpC,YAAY,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAE,CAAC;QACpC,OAAO,SAAS,CAAC;IAClB,CAAC,CACD,CAAC;IAEF,OAAO;QACN,KAAK,EAAE,WAAW;QAClB,aAAa,EAAE,aAAa;QAC5B,MAAM,EAAE,YAAY;QACpB,MAAM,EAAE,YAAY;KACpB,CAAC;AACH,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { NetworkTraits } from "./types.js";
2
+ /** Links validated once, with out and in adjacency in CSR form. */
3
+ export type IndexGraph = {
4
+ readonly nodeCount: number;
5
+ readonly linkCount: number;
6
+ /** Per link, in input order. */
7
+ readonly sources: Uint32Array;
8
+ readonly targets: Uint32Array;
9
+ /**
10
+ * `nodeCount + 1` offsets: the links leaving node i are
11
+ * `outLinks[outOffsets[i] … outOffsets[i + 1])`, in input order.
12
+ */
13
+ readonly outOffsets: Uint32Array;
14
+ /** Link indices grouped by source. */
15
+ readonly outLinks: Uint32Array;
16
+ readonly inOffsets: Uint32Array;
17
+ /** Link indices grouped by target. */
18
+ readonly inLinks: Uint32Array;
19
+ readonly outDegrees: Uint32Array;
20
+ readonly inDegrees: Uint32Array;
21
+ };
22
+ /**
23
+ * Reads `sourceOf` then `targetOf` once per link, in link order, and builds the
24
+ * adjacency. Self-loops and parallel links are kept.
25
+ *
26
+ * Complexity: O(n + m) time and memory.
27
+ *
28
+ * @throws {RangeError} A source or target that is not an integer in `[0,
29
+ * nodeCount)`, naming `functionName` and the link.
30
+ */
31
+ export declare function buildIndexGraph<TNode, TLink>(nodeCount: number, links: readonly TLink[], traits: NetworkTraits<TNode, TLink>, functionName?: string): IndexGraph;
32
+ //# sourceMappingURL=_index-graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_index-graph.d.ts","sourceRoot":"","sources":["../../src/layout/_index-graph.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,mEAAmE;AACnE,MAAM,MAAM,UAAU,GAAG;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,gCAAgC;IAChC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAC;IACjC,sCAAsC;IACtC,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC;IAC/B,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC;IAChC,sCAAsC;IACtC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAC;IACjC,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC;CAChC,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,EAC3C,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,MAAM,EAAE,aAAa,CAAC,KAAK,EAAE,KAAK,CAAC,EACnC,YAAY,SAAW,GACrB,UAAU,CA8CZ"}
@@ -0,0 +1,51 @@
1
+ import { groupIndices } from "@ac-kit/algo";
2
+ import { assertInteger, describeValue, isIntegerInRange } from "@ac-kit/core";
3
+ /**
4
+ * Reads `sourceOf` then `targetOf` once per link, in link order, and builds the
5
+ * adjacency. Self-loops and parallel links are kept.
6
+ *
7
+ * Complexity: O(n + m) time and memory.
8
+ *
9
+ * @throws {RangeError} A source or target that is not an integer in `[0,
10
+ * nodeCount)`, naming `functionName` and the link.
11
+ */
12
+ export function buildIndexGraph(nodeCount, links, traits, functionName = "layout") {
13
+ const linkCount = links.length;
14
+ assertInteger(nodeCount, `${functionName}: nodeCount`, 0, 4294967295);
15
+ const sources = new Uint32Array(linkCount);
16
+ const targets = new Uint32Array(linkCount);
17
+ for (let link = 0; link < linkCount; link++) {
18
+ const source = traits.sourceOf(links[link], link);
19
+ if (!isIntegerInRange(source, 0, nodeCount)) {
20
+ throw new RangeError(`${functionName}: source of link ${link} must be an integer in [0, ${nodeCount}), got ${describeValue(source)}`);
21
+ }
22
+ const target = traits.targetOf(links[link], link);
23
+ if (!isIntegerInRange(target, 0, nodeCount)) {
24
+ throw new RangeError(`${functionName}: target of link ${link} must be an integer in [0, ${nodeCount}), got ${describeValue(target)}`);
25
+ }
26
+ sources[link] = source;
27
+ targets[link] = target;
28
+ }
29
+ const { offsets: outOffsets, indices: outLinks } = groupIndices(sources, (source) => source, nodeCount);
30
+ const { offsets: inOffsets, indices: inLinks } = groupIndices(targets, (target) => target, nodeCount);
31
+ return {
32
+ nodeCount,
33
+ linkCount,
34
+ sources,
35
+ targets,
36
+ outOffsets,
37
+ outLinks,
38
+ inOffsets,
39
+ inLinks,
40
+ outDegrees: degreesOf_(outOffsets),
41
+ inDegrees: degreesOf_(inOffsets),
42
+ };
43
+ }
44
+ function degreesOf_(offsets) {
45
+ const degrees = new Uint32Array(offsets.length - 1);
46
+ for (let node = 0; node < degrees.length; node++) {
47
+ degrees[node] = offsets[node + 1] - offsets[node];
48
+ }
49
+ return degrees;
50
+ }
51
+ //# sourceMappingURL=_index-graph.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_index-graph.js","sourceRoot":"","sources":["../../src/layout/_index-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAyB9E;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAC9B,SAAiB,EACjB,KAAuB,EACvB,MAAmC,EACnC,YAAY,GAAG,QAAQ;IAEvB,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC;IAC/B,aAAa,CAAC,SAAS,EAAE,GAAG,YAAY,aAAa,EAAE,CAAC,EAAE,UAAW,CAAC,CAAC;IACvE,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC;IAC3C,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC;IAE3C,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC;QAC7C,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAE,EAAE,IAAI,CAAC,CAAC;QACnD,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC;YAC7C,MAAM,IAAI,UAAU,CACnB,GAAG,YAAY,oBAAoB,IAAI,8BAA8B,SAAS,UAAU,aAAa,CAAC,MAAM,CAAC,EAAE,CAC/G,CAAC;QACH,CAAC;QACD,MAAM,MAAM,GAAG,MAAM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D,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
@@ -0,0 +1,96 @@
1
+ import type { IndexGroups } from "@ac-kit/algo";
2
+ import type { WorkBudget } from "@ac-kit/core";
3
+ import type { IndexGraph } from "./_index-graph.js";
4
+ /**
5
+ * Proper layered graph: every link of the input is cut by dummy vertices into
6
+ * segments between adjacent layers, and the vertices of each layer are put in a
7
+ * crossing-reducing order.
8
+ *
9
+ * Vertices `[0, nodeCount)` are the input nodes; the dummy vertices follow,
10
+ * link after link: the dummies of link `i` are `nodeCount + chainStarts[i]` up
11
+ * to `nodeCount + chainStarts[i + 1]` (excluded), from the endpoint of lower
12
+ * rank to the one of higher rank.
13
+ */
14
+ export type LayerOrder = {
15
+ readonly vertexCount: number;
16
+ readonly layerCount: number;
17
+ /** Layer per vertex. */
18
+ readonly layerOf: Uint32Array;
19
+ /** Position within its layer per vertex, 0 first. */
20
+ readonly orders: Uint32Array;
21
+ /** `layerCount + 1` offsets into `layerVertices`. */
22
+ readonly layerStarts: Uint32Array;
23
+ /** Vertices grouped by layer, each layer in its final order. */
24
+ readonly layerVertices: Uint32Array;
25
+ /** `linkCount + 1` offsets into the dummies, in link order. */
26
+ readonly chainStarts: Uint32Array;
27
+ readonly segmentCount: number;
28
+ /** Per segment: the vertex of the lower layer. */
29
+ readonly segmentSources: Uint32Array;
30
+ /** Per segment: the vertex of the next layer. */
31
+ readonly segmentTargets: Uint32Array;
32
+ /** Per segment: the input link it belongs to. */
33
+ readonly segmentLinks: Uint32Array;
34
+ readonly segmentWeights: Float64Array;
35
+ /** Weighted crossings of the returned order (0 for a drawing with none). */
36
+ readonly crossings: number;
37
+ };
38
+ export type LayerOrderOptions = {
39
+ /** Per link, finite and ≥ 0 (default 1). */
40
+ readonly weights?: ArrayLike<number>;
41
+ /** Sweeps without fewer crossings before stopping (default 4). */
42
+ readonly patience?: number;
43
+ /** Charged `vertexCount + segmentCount` per sweep. */
44
+ readonly budget?: WorkBudget;
45
+ };
46
+ /**
47
+ * Segments grouped by `groupOf`, each group listing its segments by ascending
48
+ * neighbour key (then segment index), so nothing depends on link order. The key
49
+ * of a neighbour vertex is the vertex itself, or `keys[vertex]` below
50
+ * `keyCount` (its position in its layer, say).
51
+ */
52
+ export declare function groupByNeighbour(vertexCount: number, groupOf: Uint32Array, neighbourOf: Uint32Array, keys?: Uint32Array, keyCount?: number): IndexGroups;
53
+ /**
54
+ * Weighted number of crossings between two adjacent layers. Edge `i` joins
55
+ * position `lower[i]` of the lower layer to position `upper[i]` of the upper
56
+ * one; two edges cross when their positions are in opposite orders, and edges
57
+ * sharing an endpoint never cross. Barth, Jünger and Mutzel (2004): edges
58
+ * sorted by lower then upper position, and an accumulator tree over the upper
59
+ * positions counts, for each edge, the weight of the earlier edges that end to
60
+ * its right.
61
+ *
62
+ * Complexity: O(edges + lowerSize + upperSize + edges · log upperSize).
63
+ */
64
+ export declare function countBilayerCrossings(lower: ArrayLike<number>, upper: ArrayLike<number>, lowerSize: number, upperSize: number, weights?: ArrayLike<number>): number;
65
+ /**
66
+ * Orders the vertices of each layer to reduce crossings. Layers are the given
67
+ * ranks and need not be contiguous; a link joins its endpoints of different
68
+ * rank, whichever the direction (reversed links included), through one dummy
69
+ * per intermediate layer. Self-loops and links between equal ranks are left
70
+ * out.
71
+ *
72
+ * The initial order is the pre-order of a depth-first search from the vertices
73
+ * by layer, then index (Gansner et al. 1993). Sweeps alternate down and up;
74
+ * each sweep reorders a layer by the weighted barycentre of its neighbours in
75
+ * the fixed adjacent layer (Sugiyama, Tagawa and Toda 1981), a vertex with no
76
+ * neighbour there keeping its slot, ties alternating between the current and
77
+ * the reversed order to leave plateaus. The fewest-crossing order seen is
78
+ * returned, so the result never has more crossings than the initial order. The
79
+ * search stops after `patience` sweeps without improvement, or at zero
80
+ * crossings.
81
+ *
82
+ * Complexity: O(V′ + E′) to build the layered graph, with D dummies V′ = V + D
83
+ * and E′ = E + D; each sweep O(E′ log V′).
84
+ *
85
+ * @throws {RangeError} A rank that is not a non-negative integer, a wrong
86
+ * `ranks` or `weights` length, a negative or non-finite weight, an invalid
87
+ * `patience`.
88
+ */
89
+ export declare function orderLayers(graph: IndexGraph, ranks: ArrayLike<number>, options?: LayerOrderOptions): LayerOrder;
90
+ /**
91
+ * {@link orderLayers} as steps: yields right after each budget charge (every
92
+ * sweep and every transpose pass), so a runner can stop or give up the thread
93
+ * between them. Returns the layered graph.
94
+ */
95
+ export declare function orderLayerSteps(graph: IndexGraph, ranks: ArrayLike<number>, options?: LayerOrderOptions): Generator<void, LayerOrder, undefined>;
96
+ //# sourceMappingURL=_layer-order.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_layer-order.d.ts","sourceRoot":"","sources":["../../src/layout/_layer-order.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAQhD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;;;;;;;GASG;AACH,MAAM,MAAM,UAAU,GAAG;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,wBAAwB;IACxB,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,qDAAqD;IACrD,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,qDAAqD;IACrD,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,gEAAgE;IAChE,QAAQ,CAAC,aAAa,EAAE,WAAW,CAAC;IACpC,+DAA+D;IAC/D,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,kDAAkD;IAClD,QAAQ,CAAC,cAAc,EAAE,WAAW,CAAC;IACrC,iDAAiD;IACjD,QAAQ,CAAC,cAAc,EAAE,WAAW,CAAC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