Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update maxRank to workaround limit on number of arguments of function #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/nesting-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = {
function run(g) {
let root = util.addDummyNode(g, "root", {}, "_root");
let depths = treeDepths(g);
let height = Math.max(...Object.values(depths)) - 1; // Note: depths is an Object not an array
let depthsArr = Object.values(depths);
let height = util.applyWithChunking(Math.max, depthsArr) - 1; // Note: depths is an Object not an array
let nodeSep = 2 * height + 1;

g.graph().nestingRoot = root;
Expand Down
3 changes: 2 additions & 1 deletion lib/order/init-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = initOrder;
function initOrder(g) {
let visited = {};
let simpleNodes = g.nodes().filter(v => !g.children(v).length);
let maxRank = Math.max(...simpleNodes.map(v => g.node(v).rank));
let simpleNodesRanks = simpleNodes.map(v => g.node(v).rank);
let maxRank = util.applyWithChunking(Math.max, simpleNodesRanks);
let layers = util.range(maxRank + 1).map(() => []);

function dfs(v) {
Expand Down
8 changes: 4 additions & 4 deletions lib/position/bk.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ function findSmallestWidthAlignment(g, xss) {
*/
function alignCoordinates(xss, alignTo) {
let alignToVals = Object.values(alignTo),
alignToMin = Math.min(...alignToVals),
alignToMax = Math.max(...alignToVals);
alignToMin = util.applyWithChunking(Math.min, alignToVals),
alignToMax = util.applyWithChunking(Math.max, alignToVals);

["u", "d"].forEach(vert => {
["l", "r"].forEach(horiz => {
Expand All @@ -324,9 +324,9 @@ function alignCoordinates(xss, alignTo) {
if (xs === alignTo) return;

let xsVals = Object.values(xs);
let delta = alignToMin - Math.min(...xsVals);
let delta = alignToMin - util.applyWithChunking(Math.min, xsVals);
if (horiz !== "l") {
delta = alignToMax - Math.max(...xsVals);
delta = alignToMax - util.applyWithChunking(Math.max,xsVals);
}

if (delta) {
Expand Down
8 changes: 6 additions & 2 deletions lib/rank/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

const { applyWithChunking } = require("../util");

module.exports = {
longestPath: longestPath,
slack: slack
Expand Down Expand Up @@ -36,13 +38,15 @@ function longestPath(g) {
}
visited[v] = true;

var rank = Math.min(...g.outEdges(v).map(e => {
let outEdgesMinLens = g.outEdges(v).map(e => {
if (e == null) {
return Number.POSITIVE_INFINITY;
}

return dfs(e.w) - g.edge(e).minlen;
}));
});

var rank = applyWithChunking(Math.min, outEdgesMinLens);

if (rank === Number.POSITIVE_INFINITY) {
rank = 0;
Expand Down
37 changes: 31 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let Graph = require("@dagrejs/graphlib").Graph;
module.exports = {
addBorderNode,
addDummyNode,
applyWithChunking,
asNonCompoundGraph,
buildLayerMatrix,
intersectRect,
Expand Down Expand Up @@ -153,14 +154,15 @@ function buildLayerMatrix(g) {
* rank(v) >= 0 and at least one node w has rank(w) = 0.
*/
function normalizeRanks(g) {
let min = Math.min(...g.nodes().map(v => {
let nodeRanks = g.nodes().map(v => {
let rank = g.node(v).rank;
if (rank === undefined) {
return Number.MAX_VALUE;
}

return rank;
}));
});
let min = applyWithChunking(Math.min, nodeRanks);
g.nodes().forEach(v => {
let node = g.node(v);
if (node.hasOwnProperty("rank")) {
Expand All @@ -171,7 +173,8 @@ function normalizeRanks(g) {

function removeEmptyRanks(g) {
// Ranks may not start at 0, so we need to offset them
let offset = Math.min(...g.nodes().map(v => g.node(v).rank));
let nodeRanks = g.nodes().map(v => g.node(v).rank);
let offset = applyWithChunking(Math.min, nodeRanks);

let layers = [];
g.nodes().forEach(v => {
Expand Down Expand Up @@ -205,15 +208,37 @@ function addBorderNode(g, prefix, rank, order) {
return addDummyNode(g, "border", node, prefix);
}

function splitToChunks(array, chunkSize = CHUNKING_THRESHOLD) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for readibility i would move these functions after maxRank

applyWithChunking first, followed by splitToChunks.

since we are not using const myFn = () => {} we can get away with this and makes it easier to read

const chunks = [];
for (let i = 0; i < array.length; i += chunkSize) {
const chunk = array.slice(i, i + chunkSize);
chunks.push(chunk);
}
return chunks;
}

const CHUNKING_THRESHOLD = 65535;

function applyWithChunking(fn, argsArray) {
if(argsArray.length > CHUNKING_THRESHOLD) {
const chunks = splitToChunks(argsArray);
return fn.apply(null, chunks.map(chunk => fn.apply(null, chunk)));
} else {
return fn.apply(null, argsArray);
}
}

function maxRank(g) {
return Math.max(...g.nodes().map(v => {
const nodes = g.nodes();
const nodeRanks = nodes.map(v => {
let rank = g.node(v).rank;
if (rank === undefined) {
return Number.MIN_VALUE;
}

return rank;
}));
});

return applyWithChunking(Math.max, nodeRanks);
}

/*
Expand Down