@@ -7,10 +7,10 @@ import buildLayerGraph from "./build-layer-graph";
77import addSubgraphConstraints from "./add-subgraph-constraints" ;
88import { Graph } from "../graph-lib" ;
99import * as util from "../util" ;
10- import type { Graph as GraphType , OrderConstraint } from '../types' ;
10+ import type { Graph as GraphType , NodeCollection , OrderConstraint } from '../types' ;
1111
1212interface OrderOptions {
13- customOrder ?: ( graph : GraphType , order : ( g : GraphType , opts : OrderOptions ) => void ) => void ;
13+ customOrder ?: ( graph : GraphType , order : ( g : GraphType , opts : OrderOptions , oldNodes : NodeCollection ) => void ) => void ;
1414 disableOptimalOrderHeuristic ?: boolean ;
1515 constraints ?: OrderConstraint [ ] ;
1616}
@@ -30,7 +30,7 @@ interface OrderOptions {
3030 * 1. Graph nodes will have an "order" attribute based on the results of the
3131 * algorithm.
3232 */
33- export default function order ( graph : GraphType , opts : OrderOptions = { } ) : void {
33+ export default function order ( graph : GraphType , opts : OrderOptions = { } , oldNodes : NodeCollection ) : void {
3434 if ( typeof opts . customOrder === 'function' ) {
3535 opts . customOrder ( graph , order ) ;
3636 return ;
@@ -40,7 +40,7 @@ export default function order(graph: GraphType, opts: OrderOptions = {}): void {
4040 const downLayerGraphs = buildLayerGraphs ( graph , util . range ( 1 , maxRank + 1 ) , "inEdges" ) ;
4141 const upLayerGraphs = buildLayerGraphs ( graph , util . range ( maxRank - 1 , - 1 , - 1 ) , "outEdges" ) ;
4242
43- let layering = initOrder ( graph ) ;
43+ let layering = initOrder ( graph , oldNodes ) ;
4444 assignOrder ( graph , layering ) ;
4545
4646 if ( opts . disableOptimalOrderHeuristic ) {
@@ -52,7 +52,7 @@ export default function order(graph: GraphType, opts: OrderOptions = {}): void {
5252
5353 const constraints = opts . constraints || [ ] ;
5454 for ( let i = 0 , lastBest = 0 ; lastBest < 4 ; ++ i , ++ lastBest ) {
55- sweepLayerGraphs ( i % 2 ? downLayerGraphs : upLayerGraphs , i % 4 >= 2 , constraints ) ;
55+ sweepLayerGraphs ( i % 2 ? downLayerGraphs : upLayerGraphs , i % 4 >= 2 , constraints , oldNodes ) ;
5656
5757 layering = util . buildLayerMatrix ( graph ) ;
5858 const cc = crossCount ( graph , layering ) ;
@@ -104,13 +104,17 @@ function buildLayerGraphs(graph: GraphType, ranks: number[], relationship: "inEd
104104 } ) ;
105105}
106106
107- function sweepLayerGraphs ( layerGraphs : GraphType [ ] , biasRight : boolean , constraints : OrderConstraint [ ] ) : void {
107+ function sweepLayerGraphs ( layerGraphs : GraphType [ ] , switchBias : boolean , constraints : OrderConstraint [ ] , oldNodes : NodeCollection ) : void {
108+ let biasRight : boolean = true ;
108109 const cg = new Graph ( ) as GraphType ;
109110 layerGraphs . forEach ( function ( lg ) {
110111 constraints . forEach ( con => cg . setEdge ( con . left , con . right ) ) ;
111112
112113 const root = ( lg . graph ( ) as { root : string } ) . root ;
113- const sorted = sortSubgraph ( lg , root , cg , biasRight ) ;
114+ const { result : sorted , usedBias} = sortSubgraph ( lg , root , cg , oldNodes , biasRight ) ;
115+ if ( switchBias && usedBias ) {
116+ biasRight = ! biasRight ;
117+ }
114118 sorted . vs . forEach ( ( v , i ) => lg . node ( v ) . order = i ) ;
115119 addSubgraphConstraints ( lg , cg , sorted . vs ) ;
116120 } ) ;
0 commit comments