Skip to content

Commit

Permalink
Revert "remove some lodash" (#42)
Browse files Browse the repository at this point in the history
This reverts commit 58b441f.

These changes are causing unintended visual changes, and we're not 100%
sure why.
  • Loading branch information
aloisklink authored Oct 1, 2024
1 parent dd5b6fe commit e20dc06
Show file tree
Hide file tree
Showing 35 changed files with 563 additions and 610 deletions.
12 changes: 6 additions & 6 deletions src/dagre/acyclic.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as _ from 'lodash-es';
import { greedyFAS } from './greedy-fas.js';
import { uniqueId } from './util.js';

export { run, undo };

function run(g) {
var fas = g.graph().acyclicer === 'greedy' ? greedyFAS(g, weightFn(g)) : dfsFAS(g);
fas.forEach((e) => {
_.forEach(fas, function (e) {
var label = g.edge(e);
g.removeEdge(e);
label.forwardName = e.name;
label.reversed = true;
g.setEdge(e.w, e.v, label, uniqueId('rev'));
g.setEdge(e.w, e.v, label, _.uniqueId('rev'));
});

function weightFn(g) {
Expand All @@ -31,7 +31,7 @@ function dfsFAS(g) {
}
visited[v] = true;
stack[v] = true;
g.outEdges(v).forEach((e) => {
_.forEach(g.outEdges(v), function (e) {
if (Object.prototype.hasOwnProperty.call(stack, e.w)) {
fas.push(e);
} else {
Expand All @@ -41,12 +41,12 @@ function dfsFAS(g) {
delete stack[v];
}

g.nodes().forEach(dfs);
_.forEach(g.nodes(), dfs);
return fas;
}

function undo(g) {
g.edges().forEach((e) => {
_.forEach(g.edges(), function (e) {
var label = g.edge(e);
if (label.reversed) {
g.removeEdge(e);
Expand Down
5 changes: 3 additions & 2 deletions src/dagre/add-border-segments.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as _ from 'lodash-es';
import * as util from './util.js';

export { addBorderSegments };
Expand All @@ -7,7 +8,7 @@ function addBorderSegments(g) {
var children = g.children(v);
var node = g.node(v);
if (children.length) {
children.forEach(dfs);
_.forEach(children, dfs);
}

if (Object.prototype.hasOwnProperty.call(node, 'minRank')) {
Expand All @@ -20,7 +21,7 @@ function addBorderSegments(g) {
}
}

g.children().forEach(dfs);
_.forEach(g.children(), dfs);
}

function addBorderNode(g, prop, prefix, sg, sgNode, rank) {
Expand Down
26 changes: 18 additions & 8 deletions src/dagre/coordinate-system.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as _ from 'lodash-es';

export { adjust, undo };

function adjust(g) {
Expand All @@ -20,8 +22,12 @@ function undo(g) {
}

function swapWidthHeight(g) {
g.nodes().forEach((v) => swapWidthHeightOne(g.node(v)));
g.edges().forEach((e) => swapWidthHeightOne(g.edge(e)));
_.forEach(g.nodes(), function (v) {
swapWidthHeightOne(g.node(v));
});
_.forEach(g.edges(), function (e) {
swapWidthHeightOne(g.edge(e));
});
}

function swapWidthHeightOne(attrs) {
Expand All @@ -31,11 +37,13 @@ function swapWidthHeightOne(attrs) {
}

function reverseY(g) {
g.nodes().forEach((v) => reverseYOne(g.node(v)));
_.forEach(g.nodes(), function (v) {
reverseYOne(g.node(v));
});

g.edges().forEach((e) => {
_.forEach(g.edges(), function (e) {
var edge = g.edge(e);
edge.points.forEach(reverseYOne);
_.forEach(edge.points, reverseYOne);
if (Object.prototype.hasOwnProperty.call(edge, 'y')) {
reverseYOne(edge);
}
Expand All @@ -47,11 +55,13 @@ function reverseYOne(attrs) {
}

function swapXY(g) {
g.nodes().forEach((v) => swapXYOne(g.node(v)));
_.forEach(g.nodes(), function (v) {
swapXYOne(g.node(v));
});

g.edges().forEach((e) => {
_.forEach(g.edges(), function (e) {
var edge = g.edge(e);
edge.points.forEach(swapXYOne);
_.forEach(edge.points, swapXYOne);
if (Object.prototype.hasOwnProperty.call(edge, 'x')) {
swapXYOne(edge);
}
Expand Down
19 changes: 10 additions & 9 deletions src/dagre/debug.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as _ from 'lodash-es';
import { Graph } from '../graphlib/index.js';
import * as util from './util.js';

Expand All @@ -9,22 +10,22 @@ function debugOrdering(g) {

var h = new Graph({ compound: true, multigraph: true }).setGraph({});

g.nodes().forEach((v) => {
_.forEach(g.nodes(), function (v) {
h.setNode(v, { label: v });
h.setParent(v, 'layer' + g.node(v).rank);
});

g.edges().forEach((e) => h.setEdge(e.v, e.w, {}, e.name));
_.forEach(g.edges(), function (e) {
h.setEdge(e.v, e.w, {}, e.name);
});

layerMatrix.forEach((layer, i) => {
_.forEach(layerMatrix, function (layer, i) {
var layerV = 'layer' + i;
h.setNode(layerV, { rank: 'same' });
if (layer.length > 0) {
layer.reduce((u, v) => {
h.setEdge(u, v, { style: 'invis' });
return v;
});
}
_.reduce(layer, function (u, v) {
h.setEdge(u, v, { style: 'invis' });
return v;
});
});

return h;
Expand Down
26 changes: 17 additions & 9 deletions src/dagre/greedy-fas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { range } from './util.js';
import * as _ from 'lodash-es';
import { Graph } from '../graphlib/index.js';
import { List } from './data/list.js';

Expand All @@ -11,7 +11,7 @@ import { List } from './data/list.js';
*/
export { greedyFAS };

var DEFAULT_WEIGHT_FN = () => 1;
var DEFAULT_WEIGHT_FN = _.constant(1);

function greedyFAS(g, weightFn) {
if (g.nodeCount() <= 1) {
Expand All @@ -21,7 +21,11 @@ function greedyFAS(g, weightFn) {
var results = doGreedyFAS(state.graph, state.buckets, state.zeroIdx);

// Expand multi-edges
return results.flatMap((e) => g.outEdges(e.v, e.w));
return _.flatten(
_.map(results, function (e) {
return g.outEdges(e.v, e.w);
}),
);
}

function doGreedyFAS(g, buckets, zeroIdx) {
Expand Down Expand Up @@ -54,7 +58,7 @@ function doGreedyFAS(g, buckets, zeroIdx) {
function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) {
var results = collectPredecessors ? [] : undefined;

g.inEdges(entry.v).forEach((edge) => {
_.forEach(g.inEdges(entry.v), function (edge) {
var weight = g.edge(edge);
var uEntry = g.node(edge.v);

Expand All @@ -66,7 +70,7 @@ function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) {
assignBucket(buckets, zeroIdx, uEntry);
});

g.outEdges(entry.v).forEach((edge) => {
_.forEach(g.outEdges(entry.v), function (edge) {
var weight = g.edge(edge);
var w = edge.w;
var wEntry = g.node(w);
Expand All @@ -84,11 +88,13 @@ function buildState(g, weightFn) {
var maxIn = 0;
var maxOut = 0;

g.nodes().forEach((v) => fasGraph.setNode(v, { v: v, in: 0, out: 0 }));
_.forEach(g.nodes(), function (v) {
fasGraph.setNode(v, { v: v, in: 0, out: 0 });
});

// Aggregate weights on nodes, but also sum the weights across multi-edges
// into a single edge for the fasGraph.
g.edges().forEach((e) => {
_.forEach(g.edges(), function (e) {
var prevWeight = fasGraph.edge(e.v, e.w) || 0;
var weight = weightFn(e);
var edgeWeight = prevWeight + weight;
Expand All @@ -97,10 +103,12 @@ function buildState(g, weightFn) {
maxIn = Math.max(maxIn, (fasGraph.node(e.w)['in'] += weight));
});

var buckets = range(maxOut + maxIn + 3).map(() => new List());
var buckets = _.range(maxOut + maxIn + 3).map(function () {
return new List();
});
var zeroIdx = maxIn + 1;

fasGraph.nodes().forEach((v) => {
_.forEach(fasGraph.nodes(), function (v) {
assignBucket(buckets, zeroIdx, fasGraph.node(v));
});

Expand Down
Loading

0 comments on commit e20dc06

Please sign in to comment.