@knowvah/dot-engine 2.12.2 → 2.12.3

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 (190) hide show
  1. package/dist/common/compass-port.d.ts.map +1 -1
  2. package/dist/common/graph-init.d.ts.map +1 -1
  3. package/dist/common/htmltable-label.d.ts.map +1 -1
  4. package/dist/common/htmltable-lex.d.ts +3 -1
  5. package/dist/common/htmltable-lex.d.ts.map +1 -1
  6. package/dist/common/htmltable-parse-image.d.ts +23 -0
  7. package/dist/common/htmltable-parse-image.d.ts.map +1 -0
  8. package/dist/common/htmltable-parse-state.d.ts +4 -0
  9. package/dist/common/htmltable-parse-state.d.ts.map +1 -1
  10. package/dist/common/htmltable-parse.d.ts +33 -2
  11. package/dist/common/htmltable-parse.d.ts.map +1 -1
  12. package/dist/common/poly-inside.d.ts.map +1 -1
  13. package/dist/common/splines-geom.d.ts +2 -1
  14. package/dist/common/splines-geom.d.ts.map +1 -1
  15. package/dist/common/splines-routespl.d.ts +3 -0
  16. package/dist/common/splines-routespl.d.ts.map +1 -1
  17. package/dist/index.js +21301 -21229
  18. package/dist/index.js.map +4 -4
  19. package/dist/layout/dot/classify.d.ts.map +1 -1
  20. package/dist/layout/dot/edge-route-chain-views.d.ts +35 -0
  21. package/dist/layout/dot/edge-route-chain-views.d.ts.map +1 -0
  22. package/dist/layout/dot/edge-route-chain.d.ts +20 -16
  23. package/dist/layout/dot/edge-route-chain.d.ts.map +1 -1
  24. package/dist/layout/dot/edge-route-faithful.d.ts.map +1 -1
  25. package/dist/layout/dot/flat.d.ts +22 -7
  26. package/dist/layout/dot/flat.d.ts.map +1 -1
  27. package/dist/layout/dot/mincross-flat.d.ts +10 -1
  28. package/dist/layout/dot/mincross-flat.d.ts.map +1 -1
  29. package/dist/layout/dot/mincross.d.ts +6 -1
  30. package/dist/layout/dot/mincross.d.ts.map +1 -1
  31. package/dist/layout/dot/ns-subtree.d.ts +9 -6
  32. package/dist/layout/dot/ns-subtree.d.ts.map +1 -1
  33. package/dist/layout/dot/pack-components.d.ts +4 -3
  34. package/dist/layout/dot/pack-components.d.ts.map +1 -1
  35. package/dist/layout/dot/position-aux.d.ts +62 -20
  36. package/dist/layout/dot/position-aux.d.ts.map +1 -1
  37. package/dist/layout/dot/position-bbox.d.ts.map +1 -1
  38. package/dist/layout/dot/position-ycoords.d.ts.map +1 -1
  39. package/dist/layout/dot/position.d.ts.map +1 -1
  40. package/dist/layout/dot/rank-dot2.d.ts +7 -25
  41. package/dist/layout/dot/rank-dot2.d.ts.map +1 -1
  42. package/dist/layout/dot/self-loop.d.ts +11 -4
  43. package/dist/layout/dot/self-loop.d.ts.map +1 -1
  44. package/dist/layout/dot/splines-flat-multi.d.ts +11 -3
  45. package/dist/layout/dot/splines-flat-multi.d.ts.map +1 -1
  46. package/dist/layout/dot/splines-route.d.ts +6 -3
  47. package/dist/layout/dot/splines-route.d.ts.map +1 -1
  48. package/dist/layout/dot/straight-edges.d.ts.map +1 -1
  49. package/dist/layout/fdp/tlayout.d.ts +3 -0
  50. package/dist/layout/fdp/tlayout.d.ts.map +1 -1
  51. package/dist/layout/neato/__fixtures__/hier-imds.d.ts +5 -4
  52. package/dist/layout/neato/__fixtures__/hier-imds.d.ts.map +1 -1
  53. package/dist/layout/neato/__fixtures__/hier-majorization.d.ts +6 -5
  54. package/dist/layout/neato/__fixtures__/hier-majorization.d.ts.map +1 -1
  55. package/dist/layout/neato/conjgrad.d.ts +7 -6
  56. package/dist/layout/neato/conjgrad.d.ts.map +1 -1
  57. package/dist/layout/neato/matrix-ops-dense.d.ts +1 -6
  58. package/dist/layout/neato/matrix-ops-dense.d.ts.map +1 -1
  59. package/dist/layout/neato/matrix-ops.d.ts +0 -6
  60. package/dist/layout/neato/matrix-ops.d.ts.map +1 -1
  61. package/dist/layout/neato/quad-prog-vpsc.d.ts.map +1 -1
  62. package/dist/model/rankEntry.d.ts +3 -1
  63. package/dist/model/rankEntry.d.ts.map +1 -1
  64. package/dist/ortho/index.d.ts +0 -1
  65. package/dist/ortho/index.d.ts.map +1 -1
  66. package/dist/ortho/partition.d.ts +2 -4
  67. package/dist/ortho/partition.d.ts.map +1 -1
  68. package/dist/pathplan/route.d.ts +93 -0
  69. package/dist/pathplan/route.d.ts.map +1 -1
  70. package/dist/render/dot/agwrite.d.ts +21 -9
  71. package/dist/render/dot/agwrite.d.ts.map +1 -1
  72. package/dist/render/dot/types.d.ts +7 -2
  73. package/dist/render/dot/types.d.ts.map +1 -1
  74. package/dist/render.js +21301 -21229
  75. package/dist/render.js.map +4 -4
  76. package/dist/util/overflow.d.ts +37 -0
  77. package/dist/util/overflow.d.ts.map +1 -0
  78. package/package.json +3 -2
  79. package/src/common/compass-port.ts +36 -20
  80. package/src/common/graph-init.ts +11 -1
  81. package/src/common/htmltable-label.ts +9 -4
  82. package/src/common/htmltable-lex.ts +5 -2
  83. package/src/common/htmltable-parse-image.ts +69 -0
  84. package/src/common/htmltable-parse-state.ts +6 -0
  85. package/src/common/htmltable-parse.ts +34 -44
  86. package/src/common/poly-inside.ts +11 -5
  87. package/src/common/splines-geom.ts +24 -6
  88. package/src/common/splines-routespl.ts +32 -0
  89. package/src/gvc/__fixtures__/rerender/abom-shift-pack.dot.plain.nofree.1 +19 -19
  90. package/src/gvc/__fixtures__/rerender/abom-shift-pack.dot.plain.nofree.2 +19 -19
  91. package/src/gvc/__fixtures__/rerender/abom-shift-pack.dot.plain.nofree.3 +19 -19
  92. package/src/gvc/__fixtures__/rerender/abom-shift-parity.dot.plain.nofree.2 +18 -18
  93. package/src/gvc/__fixtures__/rerender/abom-shift-parity.dot.plain.nofree.3 +12 -12
  94. package/src/gvc/__fixtures__/rerender/flat-rev-stale.dot.plain.nofree.1 +20 -20
  95. package/src/gvc/__fixtures__/rerender/flat-rev-stale.dot.plain.nofree.2 +31 -31
  96. package/src/gvc/__fixtures__/rerender/flat-rev-stale.dot.plain.nofree.3 +33 -33
  97. package/src/gvc/__fixtures__/rerender/multi-edge.dot.plain.nofree.1 +10 -10
  98. package/src/gvc/__fixtures__/rerender/multi-edge.dot.plain.nofree.2 +28 -28
  99. package/src/gvc/__fixtures__/rerender/multi-edge.dot.plain.nofree.3 +28 -28
  100. package/src/gvc/__fixtures__/rerender/multi-edge.dot.svg.nofree.1 +21 -28
  101. package/src/gvc/__fixtures__/rerender/multi-edge.dot.svg.nofree.2 +100 -102
  102. package/src/gvc/__fixtures__/rerender/multi-edge.dot.svg.nofree.3 +137 -139
  103. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.1 +1 -1
  104. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.2 +1 -1
  105. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.3 +1 -1
  106. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.1 +3 -3
  107. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.2 +3 -3
  108. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.3 +3 -3
  109. package/src/gvc/__fixtures__/rerender/regen.sh +6 -0
  110. package/src/layout/dot/classify.ts +5 -2
  111. package/src/layout/dot/edge-route-chain-views.ts +120 -0
  112. package/src/layout/dot/edge-route-chain.ts +103 -117
  113. package/src/layout/dot/edge-route-faithful.ts +20 -5
  114. package/src/layout/dot/flat.ts +38 -17
  115. package/src/layout/dot/mincross-flat.ts +15 -5
  116. package/src/layout/dot/mincross.ts +6 -2
  117. package/src/layout/dot/ns-subtree.ts +11 -7
  118. package/src/layout/dot/pack-components.ts +6 -4
  119. package/src/layout/dot/position-aux.ts +112 -39
  120. package/src/layout/dot/position-bbox.ts +14 -1
  121. package/src/layout/dot/position-ycoords.ts +7 -1
  122. package/src/layout/dot/position.ts +4 -1
  123. package/src/layout/dot/rank-dot2.ts +13 -94
  124. package/src/layout/dot/self-loop.ts +29 -11
  125. package/src/layout/dot/splines-flat-multi.ts +12 -4
  126. package/src/layout/dot/splines-route.ts +10 -11
  127. package/src/layout/dot/straight-edges.ts +5 -3
  128. package/src/layout/fdp/tlayout.ts +6 -5
  129. package/src/layout/neato/__fixtures__/conjgrad-d.json +1 -0
  130. package/src/layout/neato/__fixtures__/dim/hier-big-dim3.dot +141 -141
  131. package/src/layout/neato/__fixtures__/dim/hier-big-dim3.xdot +339 -339
  132. package/src/layout/neato/__fixtures__/dim/hier-big-dim4.dot +140 -140
  133. package/src/layout/neato/__fixtures__/dim/hier-big-dim4.xdot +323 -323
  134. package/src/layout/neato/__fixtures__/dim/hier-big-subset-dim3.dot +141 -141
  135. package/src/layout/neato/__fixtures__/dim/hier-big-subset-dim3.xdot +339 -339
  136. package/src/layout/neato/__fixtures__/dim/hier-big110-dim3.dot +251 -251
  137. package/src/layout/neato/__fixtures__/dim/hier-big110-dim3.xdot +535 -535
  138. package/src/layout/neato/__fixtures__/dim/hier-circuit-dim3.dot +14 -14
  139. package/src/layout/neato/__fixtures__/dim/hier-circuit-dim3.xdot +32 -32
  140. package/src/layout/neato/__fixtures__/dim/hier-dim4.dot +8 -8
  141. package/src/layout/neato/__fixtures__/dim/hier-dim4.xdot +14 -14
  142. package/src/layout/neato/__fixtures__/dim/hier-len-dim3.dot +17 -17
  143. package/src/layout/neato/__fixtures__/dim/hier-len-dim3.xdot +35 -35
  144. package/src/layout/neato/__fixtures__/dim/hier-noedge-big.dot +2 -2
  145. package/src/layout/neato/__fixtures__/dim/hier-noedge-big.xdot +2 -2
  146. package/src/layout/neato/__fixtures__/dim/hier-ring-dim3.dot +11 -11
  147. package/src/layout/neato/__fixtures__/dim/hier-ring-dim3.xdot +21 -21
  148. package/src/layout/neato/__fixtures__/dim/hier-star-dim3.dot +14 -14
  149. package/src/layout/neato/__fixtures__/dim/hier-star-dim3.xdot +26 -26
  150. package/src/layout/neato/__fixtures__/dim/hier-stresswt1-dim4.dot +13 -13
  151. package/src/layout/neato/__fixtures__/dim/hier-stresswt1-dim4.xdot +30 -30
  152. package/src/layout/neato/__fixtures__/dim/hier-subset-dim3.dot +10 -10
  153. package/src/layout/neato/__fixtures__/dim/hier-subset-dim3.xdot +19 -19
  154. package/src/layout/neato/__fixtures__/dim/smart-units.json +1 -1
  155. package/src/layout/neato/__fixtures__/hier/cases/dirnone.plain +10 -10
  156. package/src/layout/neato/__fixtures__/hier/cases/tdist.plain +12 -12
  157. package/src/layout/neato/__fixtures__/hier/hier-probe.plain +1 -1
  158. package/src/layout/neato/__fixtures__/hier/hier-probe.xdot +19 -19
  159. package/src/layout/neato/__fixtures__/hier-imds.ts +14 -13
  160. package/src/layout/neato/__fixtures__/hier-majorization.ts +24 -23
  161. package/src/layout/neato/__fixtures__/ipsep/ipsep-grid.xdot +20 -20
  162. package/src/layout/neato/__fixtures__/ipsep/ipsep-k6.xdot +8 -8
  163. package/src/layout/neato/__fixtures__/ipsep/ipsep-rand-len.xdot +34 -34
  164. package/src/layout/neato/__fixtures__/ipsep/ipsep-rand.xdot +32 -32
  165. package/src/layout/neato/__fixtures__/ipsep/ipsep-tree.xdot +18 -18
  166. package/src/layout/neato/__fixtures__/ipsep/kd-ipsep-len.xdot +13 -13
  167. package/src/layout/neato/__fixtures__/ipsep/kd-ipsep.xdot +12 -12
  168. package/src/layout/neato/__fixtures__/models/hier-circuit.xdot +33 -33
  169. package/src/layout/neato/__fixtures__/models/hier-default.xdot +30 -30
  170. package/src/layout/neato/__fixtures__/models/hier-mds-nolen.xdot +30 -30
  171. package/src/layout/neato/__fixtures__/models/hier-pin-disc-circuit.xdot +35 -35
  172. package/src/layout/neato/__fixtures__/models/hier-shortpath.xdot +30 -30
  173. package/src/layout/neato/conjgrad.ts +38 -35
  174. package/src/layout/neato/matrix-ops-dense.ts +2 -20
  175. package/src/layout/neato/matrix-ops.ts +0 -20
  176. package/src/layout/neato/quad-prog-vpsc.ts +7 -5
  177. package/src/layout/neato/smart-ini-x.ts +15 -15
  178. package/src/layout/neato/stress-subspace.ts +5 -5
  179. package/src/layout/sfdp/__fixtures__/dim/dim3-dimen3.dot +6 -6
  180. package/src/layout/sfdp/__fixtures__/dim/dim3.dot +6 -6
  181. package/src/layout/sfdp/__fixtures__/dim/dim4-dimen4.dot +6 -6
  182. package/src/layout/sfdp/__fixtures__/dim/overlap-prism-dim3.dot +6 -6
  183. package/src/layout/sfdp/__fixtures__/dim/regen.sh +2 -0
  184. package/src/model/rankEntry.ts +3 -1
  185. package/src/ortho/index.ts +0 -1
  186. package/src/ortho/partition.ts +10 -22
  187. package/src/pathplan/route.ts +14 -4
  188. package/src/render/dot/agwrite.ts +35 -15
  189. package/src/render/dot/types.ts +7 -2
  190. package/src/util/overflow.ts +74 -0
@@ -7,23 +7,19 @@
7
7
  * (via perp()). Intersection of the two decompositions yields cells
8
8
  * bounded on all four sides.
9
9
  *
10
- * SEED is fixed at 173 — matches lib/ortho/partition.c:srand48(173).
10
+ * The segment permutation draws from the process-global libc rand() stream
11
+ * (graphviz 16.1.1, b99490ece): partition no longer seeds srand48(173).
11
12
  *
12
13
  * @see lib/ortho/partition.c
13
14
  */
14
15
 
15
- import { srand48, drand48 } from "../common/random.js";
16
+ import { gvPermutation } from "../common/crand.js";
16
17
  import type { OrthoBox, OrthoPoint, Cell } from "./types.js";
17
18
  import {
18
19
  constructTrapezoids, fpEqual, greaterThan, isValidTrap, C_EPS, equalTo,
19
20
  } from "./trapezoid.js";
20
21
  import type { SegmentT, TrapT } from "./trapezoid.js";
21
22
 
22
- /** Fixed RNG seed — matches lib/ortho/partition.c:srand48(173) */
23
- const SEED = 173;
24
-
25
- export { SEED };
26
-
27
23
  const NPOINTS = 4;
28
24
 
29
25
  /** @see lib/ortho/partition.c:#define TR_FROM_UP/TR_FROM_DN */
@@ -86,20 +82,14 @@ class PartitionHelper {
86
82
  }
87
83
 
88
84
  /**
89
- * Generate a random permutation of the segments 1..n, drawing from the
90
- * process-global POSIX drand48 stream (seeded by partition's srand48).
85
+ * Generate a random permutation of the segments 1..n: gv_permutation's
86
+ * Fisher-Yates shuffle over the global libc rand() stream, made 1-based.
91
87
  * @see lib/ortho/partition.c:generateRandomOrdering
92
88
  */
93
89
  static generateRandomOrdering(n: number): number[] {
94
- const permute: number[] = [];
95
- for (let i = 0; i < n; i++) permute.push(i + 1);
96
- for (let i = 0; i < n; i++) {
97
- // C: j = (size_t)((double)i + drand48() * (double)(n - i));
98
- const j = Math.floor(i + drand48() * (n - i));
99
- if (j !== i) {
100
- const tmp = permute[i]; permute[i] = permute[j]; permute[j] = tmp;
101
- }
102
- }
90
+ const permute = gvPermutation(n);
91
+ // make indexing 1-based
92
+ for (let i = 0; i < n; i++) permute[i]!++;
103
93
  return permute;
104
94
  }
105
95
 
@@ -271,10 +261,8 @@ export function partition(cells: Cell[], bb: OrthoBox): OrthoBox[] | null {
271
261
  const h = PartitionHelper;
272
262
  const nsegs = 4 * (cells.length + 1);
273
263
 
274
- // C: srand48(173) once, then two generateRandomOrdering calls share the
275
- // continuing drand48 stream (partition.c:737-763).
276
- srand48(SEED);
277
-
264
+ // C: the two generateRandomOrdering calls share the continuing rand()
265
+ // stream (partition.c:677, :693).
278
266
  const hSeg = h.genSegments(cells, bb, false);
279
267
  const hPerm = h.generateRandomOrdering(nsegs);
280
268
  const hTr = constructTrapezoids(nsegs, hSeg, hPerm);
@@ -15,7 +15,8 @@ interface LineCoeff { xc0: number; xc1: number; yc0: number; yc1: number; }
15
15
  interface RootAccum { roots: number[]; n: number; }
16
16
  interface RouteState { ops: Point[]; barriers: Edge[]; }
17
17
 
18
- class RouteHelper {
18
+ /** @internal — exported for route.branch.test.ts */
19
+ export class RouteHelper {
19
20
  static B0(t: number): number { const u = 1-t; return u*u*u; }
20
21
  static B1(t: number): number { const u = 1-t; return 3*t*u*u; }
21
22
  static B2(t: number): number { const u = 1-t; return 3*t*t*u; }
@@ -71,6 +72,15 @@ class RouteHelper {
71
72
  c[0] = v0;
72
73
  }
73
74
 
75
+ /**
76
+ * `a == b` within a tolerance (16.1.1 `d3ab9ba47`; the same 1e-7 as solve3).
77
+ * @see lib/pathplan/route.c:eq
78
+ */
79
+ static eq(a: number, b: number): boolean {
80
+ const EPS = 1e-7;
81
+ return Math.abs(a - b) < EPS;
82
+ }
83
+
74
84
  static addRoot(root: number, acc: RootAccum): void {
75
85
  if (root >= 0 && root <= 1) { acc.roots[acc.n] = root; acc.n++; }
76
86
  }
@@ -91,7 +101,7 @@ class RouteHelper {
91
101
  } else {
92
102
  for (let i = 0; i < xn; i++)
93
103
  for (let j = 0; j < yn; j++)
94
- if (xr[i] === yr[j]) RouteHelper.addRoot(xr[i], acc);
104
+ if (RouteHelper.eq(xr[i], yr[j])) RouteHelper.addRoot(xr[i], acc);
95
105
  }
96
106
  return acc.n;
97
107
  }
@@ -144,8 +154,8 @@ class RouteHelper {
144
154
  yc0: lps[0].y, yc1: lps[1].y - lps[0].y,
145
155
  };
146
156
  const acc: RootAccum = { roots, n: 0 };
147
- if (lc.xc1 === 0) {
148
- if (lc.yc1 === 0) return RouteHelper.silBothZero(sps, lc, acc);
157
+ if (RouteHelper.eq(lc.xc1, 0)) {
158
+ if (RouteHelper.eq(lc.yc1, 0)) return RouteHelper.silBothZero(sps, lc, acc);
149
159
  return RouteHelper.silXzeroYnonzero(sps, lc, acc);
150
160
  }
151
161
  return RouteHelper.silGeneral(sps, lc, acc);
@@ -61,19 +61,22 @@ export abstract class DotWriterBase {
61
61
  /**
62
62
  * Serialize the whole laid-out graph to xdot DOT text — a faithful port of
63
63
  * cgraph's agwrite (lib/cgraph/write.c). Recurses the subgraph tree
64
- * (write_subgs/write_body), scoping each node/edge to the subgraph(s) it
65
- * belongs to via preorder numbers (write_node_test/write_edge_test), and
66
- * re-emits an object bare (no attrs) on any scope after the first
67
- * (attrs_written). This reproduces native's per-subgraph edge re-declarations
68
- * — e.g. an edge in a rank=same subgraph is drawn once, then re-declared bare.
64
+ * (write_subgs/write_body). A node is scoped to the subgraph(s) it belongs
65
+ * to via preorder numbers (write_node_test), and is re-emitted bare (no
66
+ * attrs) on any scope after the first (attrs_written). An edge is written
67
+ * once: each body pends its out-edges during the node walk and writes them
68
+ * after it in creation order (write_edges), and an edge already written is
69
+ * never pended again, so no later scope re-declares it (graphviz 16.1.1).
69
70
  * @see lib/cgraph/write.c:agwrite
71
+ * @see lib/cgraph/write.c:before_write
70
72
  */
71
73
  protected serialize(g: Graph): string {
72
74
  const ctx: SerCtx = {
73
75
  out: [],
74
76
  preorder: new Map<Graph, number>(),
75
77
  nodeLW: new Map<Node, number>(),
76
- edgeLW: new Map<Edge, number>(),
78
+ edgesTodo: new Map<number, Edge>(),
79
+ edgesDone: new Set<number>(),
77
80
  attrsWritten: new Set<Node | Edge>(),
78
81
  level: 0,
79
82
  };
@@ -260,7 +263,10 @@ export abstract class DotWriterBase {
260
263
  }
261
264
  }
262
265
 
263
- /** @see write.c:write_body — subgraphs, then this scope's nodes and edges. */
266
+ /** Subgraphs, then this scope's nodes, then the edges the node walk pended.
267
+ * The walk still writes an out-edge's head node right after its tail, but
268
+ * the edge itself waits for writeEdges unless an earlier body wrote it.
269
+ * @see lib/cgraph/write.c:write_body */
264
270
  private writeBody(g: Graph, ctx: SerCtx): void {
265
271
  this.writeSubgs(g, ctx);
266
272
  // agfstnode/agnxtnode walk a subgraph's node set in AGSEQ (root creation)
@@ -275,9 +281,24 @@ export abstract class DotWriterBase {
275
281
  this.writeNode(g, e.head, ctx);
276
282
  prev = e.head;
277
283
  }
278
- if (this.writeEdgeTest(g, e, ctx)) this.writeEdge(g, e, ctx);
284
+ // pend this edge to be emitted later
285
+ if (!ctx.edgesDone.has(e.seq)) ctx.edgesTodo.set(e.seq, e);
279
286
  }
280
287
  }
288
+ // flush pending edges to the output
289
+ this.writeEdges(g, ctx);
290
+ }
291
+
292
+ /** Write the pending edges in ascending `Edge.seq` (the C walks a Dtoset
293
+ * keyed on AGSEQ), mark each done, and empty the set for the next body.
294
+ * @see lib/cgraph/write.c:write_edges */
295
+ private writeEdges(g: Graph, ctx: SerCtx): void {
296
+ const seqs = [...ctx.edgesTodo.keys()].sort((a, b) => a - b);
297
+ for (const seq of seqs) {
298
+ this.writeEdge(g, ctx.edgesTodo.get(seq)!, ctx);
299
+ ctx.edgesDone.add(seq);
300
+ }
301
+ ctx.edgesTodo.clear();
281
302
  }
282
303
 
283
304
  /** @see write.c:write_node_test — every xdot node carries pos/size, so it is
@@ -299,11 +320,6 @@ export abstract class DotWriterBase {
299
320
  return n.name;
300
321
  }
301
322
 
302
- /** @see write.c:write_edge_test */
303
- private writeEdgeTest(g: Graph, e: Edge, ctx: SerCtx): boolean {
304
- return (ctx.edgeLW.get(e) ?? 0) < ctx.preorder.get(g)!;
305
- }
306
-
307
323
  /** @see write.c:write_node */
308
324
  private writeNode(g: Graph, n: Node, ctx: SerCtx): void {
309
325
  let s = this.indent(ctx) + xdotId(this.emitNodeName(n));
@@ -315,7 +331,6 @@ export abstract class DotWriterBase {
315
331
  ctx.nodeLW.set(n, ctx.preorder.get(g)!);
316
332
  }
317
333
 
318
- /** @see write.c:write_edge — attrs only on first emission, bare thereafter. */
319
334
  /**
320
335
  * The `:port` / `:port:compass` suffix C writes after an endpoint name.
321
336
  *
@@ -341,6 +356,12 @@ export abstract class DotWriterBase {
341
356
  return ':' + agstrcanonText(val.slice(0, i)) + ':' + agstrcanonText(val.slice(i + 1));
342
357
  }
343
358
 
359
+ /** One edge statement. The C's `else` arm (write_edge_name: a bare `[key=…]`
360
+ * for an edge whose attributes were already written) is not ported: since
361
+ * 16.1.1 edges_done makes write_body write each edge once per agwrite, and
362
+ * before_write's set_attrwf clears every flag first, so the arm cannot run.
363
+ * The attrsWritten gate stays, as the C's `if (!attrs_written(e))` does.
364
+ * @see lib/cgraph/write.c:write_edge */
344
365
  private writeEdge(g: Graph, e: Edge, ctx: SerCtx): void {
345
366
  const conn = edgeConnector(isDirected(g));
346
367
  let s = this.indent(ctx) + xdotId(this.emitNodeName(e.tail)) +
@@ -351,7 +372,6 @@ export abstract class DotWriterBase {
351
372
  ctx.attrsWritten.add(e);
352
373
  }
353
374
  ctx.out.push(s + ';\n');
354
- ctx.edgeLW.set(e, ctx.preorder.get(g)!);
355
375
  }
356
376
 
357
377
  /** Root-graph attribute block: `_draw_`, `_ldraw_`, `bb`, `xdotversion`. */
@@ -23,8 +23,13 @@ export interface SerCtx {
23
23
  preorder: Map<Graph, number>;
24
24
  /** node → preorder of the subgraph it was last written in (node_last_written). */
25
25
  nodeLW: Map<Node, number>;
26
- /** edge → preorder of the subgraph it was last written in (edge_last_written). */
27
- edgeLW: Map<Edge, number>;
26
+ /** Edges the current body has pended, keyed on `Edge.seq` (AGSEQ), which dedupes as the C's
27
+ * ordered dictionary does. Empty at the start of every body's node walk.
28
+ * @see lib/cgraph/write.c:info_t (edges_todo) */
29
+ edgesTodo: Map<number, Edge>;
30
+ /** `Edge.seq` of every edge already written in this agwrite.
31
+ * @see lib/cgraph/write.c:info_t (edges_done) */
32
+ edgesDone: Set<number>;
28
33
  /** objects whose attributes have already been emitted (AGATTRWF/attrs_written). */
29
34
  attrsWritten: Set<Node | Edge>;
30
35
  /** current indentation depth. */
@@ -0,0 +1,74 @@
1
+ // SPDX-License-Identifier: EPL-2.0
2
+ /**
3
+ * Overflow-checked arithmetic helpers.
4
+ *
5
+ * @see lib/util/overflow.h
6
+ */
7
+
8
+ // C's INT_MAX / INT_MIN for a 32-bit int.
9
+ const INT_MAX = 2_147_483_647;
10
+ const INT_MIN = -2_147_483_648;
11
+
12
+ /**
13
+ * Add two integers, clamping to lower and upper type bounds.
14
+ *
15
+ * Returns the sum `a + b`, or `INT_MAX`/`INT_MIN` on overflow/underflow. C's
16
+ * operands are `int`; the port applies the same three branches to its numbers
17
+ * as they are, with no int32 wrap of the inputs.
18
+ * @see lib/util/overflow.h:sadd_saturate
19
+ */
20
+ export function saddSaturate(a: number, b: number): number {
21
+ // overflow?
22
+ if (b > 0 && INT_MAX - b < a) {
23
+ return INT_MAX;
24
+ }
25
+
26
+ // underflow?
27
+ if (b < 0 && INT_MIN - b > a) {
28
+ return INT_MIN;
29
+ }
30
+
31
+ return a + b;
32
+ }
33
+
34
+ /**
35
+ * Divide two double floating-point values, checking for overflow.
36
+ *
37
+ * Returns the quotient `n / d`, or `null` where C returns `true` and leaves
38
+ * `*res` as it was: the quotient is not finite (a zero or subnormal divisor,
39
+ * a non-finite operand, or a result past `DBL_MAX`). A call site writes C's
40
+ * "assign the fallback, then overwrite on success" as `fdivOverflow(n, d) ??
41
+ * fallback`; a zero quotient is a result, and `??` keeps it.
42
+ * @see lib/util/overflow.h:fdiv_overflow
43
+ */
44
+ export function fdivOverflow(n: number, d: number): number | null {
45
+ const r = n / d;
46
+
47
+ // did we overflow?
48
+ if (!Number.isFinite(r)) {
49
+ return null;
50
+ }
51
+
52
+ return r;
53
+ }
54
+
55
+ /**
56
+ * Divide two floating-point values, checking for overflow.
57
+ *
58
+ * The `float` twin of {@link fdivOverflow}. C's parameters and quotient are
59
+ * `float`, so the operands and the quotient are rounded to float32: a
60
+ * quotient past `FLT_MAX` is an overflow here though it is finite as a
61
+ * double. Dividing two float32 values in double and rounding once gives
62
+ * IEEE single division's result (53 >= 2 * 24 + 2 bits).
63
+ * @see lib/util/overflow.h:fdivf_overflow
64
+ */
65
+ export function fdivfOverflow(n: number, d: number): number | null {
66
+ const r = Math.fround(Math.fround(n) / Math.fround(d));
67
+
68
+ // did we overflow?
69
+ if (!Number.isFinite(r)) {
70
+ return null;
71
+ }
72
+
73
+ return r;
74
+ }