Commit ea7b43e8 authored by Mike Bostock's avatar Mike Bostock
Browse files

Abbreviate internal variables.

parent b567b5d1
...@@ -6882,98 +6882,98 @@ ...@@ -6882,98 +6882,98 @@
} }
} }
d3.layout.tree = function() { d3.layout.tree = function() {
var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = null;
function tree(d, i) { function tree(d, i) {
var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0); var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0);
d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.mod = -root1.prelim; d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z;
d3_layout_hierarchyVisitBefore(root1, secondWalk); d3_layout_hierarchyVisitBefore(root1, secondWalk);
if (nodeSize) { if (nodeSize) d3_layout_hierarchyVisitBefore(root1, sizeNode); else {
d3_layout_hierarchyVisitBefore(root1, function(node) {
node.node.x *= size[0];
node.node.y = node.node.depth * size[1];
});
} else {
var left = d3_layout_treeSearch(root0, d3_layout_treeLeftmost), right = d3_layout_treeSearch(root0, d3_layout_treeRightmost), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2, y1 = d3_layout_treeSearch(root0, d3_layout_treeDeepest).depth || 1; var left = d3_layout_treeSearch(root0, d3_layout_treeLeftmost), right = d3_layout_treeSearch(root0, d3_layout_treeRightmost), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2, y1 = d3_layout_treeSearch(root0, d3_layout_treeDeepest).depth || 1;
d3_layout_hierarchyVisitBefore(root1, function(node) { d3_layout_hierarchyVisitBefore(root1, function(node) {
node.node.x = (node.node.x - x0) / (x1 - x0) * size[0]; node._.x = (node._.x - x0) / (x1 - x0) * size[0];
node.node.y = node.node.depth / y1 * size[1]; node._.y = node._.depth / y1 * size[1];
}); });
} }
return nodes; return nodes;
} }
function wrapTree(root0) { function wrapTree(root0) {
var root1 = { var root1 = {
defaultAncestor: null, A: null,
children: [ root0 ] children: [ root0 ]
}, queue = [ root1 ], node1; }, queue = [ root1 ], node1;
while ((node1 = queue.pop()) != null) { while ((node1 = queue.pop()) != null) {
for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) { for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) {
queue.push((children[i] = child = { queue.push((children[i] = child = {
node: children[i], _: children[i],
parent: node1, parent: node1,
children: (child = children[i].children) && child.slice() || [], children: (child = children[i].children) && child.slice() || [],
defaultAncestor: null, A: null,
ancestor: null, a: null,
prelim: 0, z: 0,
mod: 0, m: 0,
change: 0, c: 0,
shift: 0, s: 0,
number: i t: null,
}).ancestor = child); i: i
}).a = child);
} }
} }
return root1.children[0]; return root1.children[0];
} }
function firstWalk(v) { function firstWalk(v) {
var children = v.children, siblings = v.parent.children, w = v.number ? siblings[v.number - 1] : null; var children = v.children, siblings = v.parent.children, w = v.i ? siblings[v.i - 1] : null;
if (children.length) { if (children.length) {
d3_layout_treeShift(v); d3_layout_treeShift(v);
var midpoint = (children[0].prelim + children[children.length - 1].prelim) / 2; var midpoint = (children[0].z + children[children.length - 1].z) / 2;
if (w) { if (w) {
v.prelim = w.prelim + separation(v.node, w.node); v.z = w.z + separation(v._, w._);
v.mod = v.prelim - midpoint; v.m = v.z - midpoint;
} else { } else {
v.prelim = midpoint; v.z = midpoint;
} }
} else if (w) { } else if (w) {
v.prelim = w.prelim + separation(v.node, w.node); v.z = w.z + separation(v._, w._);
} }
v.parent.defaultAncestor = apportion(v, w, v.parent.defaultAncestor || siblings[0]); v.parent.A = apportion(v, w, v.parent.A || siblings[0]);
} }
function secondWalk(v) { function secondWalk(v) {
v.node.x = v.prelim + v.parent.mod; v._.x = v.z + v.parent.m;
v.mod += v.parent.mod; v.m += v.parent.m;
} }
function apportion(v, w, ancestor) { function apportion(v, w, ancestor) {
if (w) { if (w) {
var vip = v, vop = v, vim = w, vom = vip.parent.children[0], sip = vip.mod, sop = vop.mod, sim = vim.mod, som = vom.mod, shift; var vip = v, vop = v, vim = w, vom = vip.parent.children[0], sip = vip.m, sop = vop.m, sim = vim.m, som = vom.m, shift;
while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) { while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) {
vom = d3_layout_treeLeft(vom); vom = d3_layout_treeLeft(vom);
vop = d3_layout_treeRight(vop); vop = d3_layout_treeRight(vop);
vop.ancestor = v; vop.a = v;
shift = vim.prelim + sim - vip.prelim - sip + separation(vim.node, vip.node); shift = vim.z + sim - vip.z - sip + separation(vim._, vip._);
if (shift > 0) { if (shift > 0) {
d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift); d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift);
sip += shift; sip += shift;
sop += shift; sop += shift;
} }
sim += vim.mod; sim += vim.m;
sip += vip.mod; sip += vip.m;
som += vom.mod; som += vom.m;
sop += vop.mod; sop += vop.m;
} }
if (vim && !d3_layout_treeRight(vop)) { if (vim && !d3_layout_treeRight(vop)) {
vop.thread = vim; vop.t = vim;
vop.mod += sim - sop; vop.m += sim - sop;
} }
if (vip && !d3_layout_treeLeft(vom)) { if (vip && !d3_layout_treeLeft(vom)) {
vom.thread = vip; vom.t = vip;
vom.mod += sip - som; vom.m += sip - som;
ancestor = v; ancestor = v;
} }
} }
return ancestor; return ancestor;
} }
function sizeNode(node) {
node._.x *= size[0];
node._.y = node._.depth * size[1];
}
tree.separation = function(x) { tree.separation = function(x) {
if (!arguments.length) return separation; if (!arguments.length) return separation;
separation = x; separation = x;
...@@ -6981,12 +6981,12 @@ ...@@ -6981,12 +6981,12 @@
}; };
tree.size = function(x) { tree.size = function(x) {
if (!arguments.length) return nodeSize ? null : size; if (!arguments.length) return nodeSize ? null : size;
nodeSize = (size = x) == null; nodeSize = (size = x) == null ? sizeNode : null;
return tree; return tree;
}; };
tree.nodeSize = function(x) { tree.nodeSize = function(x) {
if (!arguments.length) return nodeSize ? size : null; if (!arguments.length) return nodeSize ? size : null;
nodeSize = (size = x) != null; nodeSize = (size = x) == null ? null : sizeNode;
return tree; return tree;
}; };
return d3_layout_hierarchyRebind(tree, hierarchy); return d3_layout_hierarchyRebind(tree, hierarchy);
...@@ -7017,31 +7017,31 @@ ...@@ -7017,31 +7017,31 @@
} }
function d3_layout_treeLeft(v) { function d3_layout_treeLeft(v) {
var children = v.children; var children = v.children;
return children.length ? children[0] : v.thread; return children.length ? children[0] : v.t;
} }
function d3_layout_treeRight(v) { function d3_layout_treeRight(v) {
var children = v.children, n; var children = v.children, n;
return (n = children.length) ? children[n - 1] : v.thread; return (n = children.length) ? children[n - 1] : v.t;
} }
function d3_layout_treeMove(wm, wp, shift) { function d3_layout_treeMove(wm, wp, shift) {
var change = shift / (wp.number - wm.number); var change = shift / (wp.i - wm.i);
wp.change -= change; wp.c -= change;
wp.shift += shift; wp.s += shift;
wm.change += change; wm.c += change;
wp.prelim += shift; wp.z += shift;
wp.mod += shift; wp.m += shift;
} }
function d3_layout_treeShift(v) { function d3_layout_treeShift(v) {
var shift = 0, change = 0, children = v.children, i = children.length, w; var shift = 0, change = 0, children = v.children, i = children.length, w;
while (--i >= 0) { while (--i >= 0) {
w = children[i]; w = children[i];
w.prelim += shift; w.z += shift;
w.mod += shift; w.m += shift;
shift += w.shift + (change += w.change); shift += w.s + (change += w.c);
} }
} }
function d3_layout_treeAncestor(vim, v, ancestor) { function d3_layout_treeAncestor(vim, v, ancestor) {
return vim.ancestor.parent === v.parent ? vim.ancestor : ancestor; return vim.a.parent === v.parent ? vim.a : ancestor;
} }
d3.layout.cluster = function() { d3.layout.cluster = function() {
var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -82,6 +82,7 @@ function d3_layout_hierarchyRebind(object, hierarchy) { ...@@ -82,6 +82,7 @@ function d3_layout_hierarchyRebind(object, hierarchy) {
return object; return object;
} }
// Pre-order traversal.
function d3_layout_hierarchyVisitBefore(node, callback) { function d3_layout_hierarchyVisitBefore(node, callback) {
var nodes = [node]; var nodes = [node];
while ((node = nodes.pop()) != null) { while ((node = nodes.pop()) != null) {
...@@ -93,6 +94,7 @@ function d3_layout_hierarchyVisitBefore(node, callback) { ...@@ -93,6 +94,7 @@ function d3_layout_hierarchyVisitBefore(node, callback) {
} }
} }
// Post-order traversal.
function d3_layout_hierarchyVisitAfter(node, callback) { function d3_layout_hierarchyVisitAfter(node, callback) {
var nodes = [node], nodes2 = []; var nodes = [node], nodes2 = [];
while ((node = nodes.pop()) != null) { while ((node = nodes.pop()) != null) {
......
...@@ -6,7 +6,7 @@ d3.layout.tree = function() { ...@@ -6,7 +6,7 @@ d3.layout.tree = function() {
var hierarchy = d3.layout.hierarchy().sort(null).value(null), var hierarchy = d3.layout.hierarchy().sort(null).value(null),
separation = d3_layout_treeSeparation, separation = d3_layout_treeSeparation,
size = [1, 1], // width, height size = [1, 1], // width, height
nodeSize = false; nodeSize = null;
function tree(d, i) { function tree(d, i) {
var nodes = hierarchy.call(this, d, i), var nodes = hierarchy.call(this, d, i),
...@@ -14,16 +14,11 @@ d3.layout.tree = function() { ...@@ -14,16 +14,11 @@ d3.layout.tree = function() {
root1 = wrapTree(root0); root1 = wrapTree(root0);
// Compute the layout using Buchheim et al.'s algorithm. // Compute the layout using Buchheim et al.'s algorithm.
d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.mod = -root1.prelim; d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z;
d3_layout_hierarchyVisitBefore(root1, secondWalk); d3_layout_hierarchyVisitBefore(root1, secondWalk);
// If a fixed node size is specified, scale x and y. // If a fixed node size is specified, scale x and y.
if (nodeSize) { if (nodeSize) d3_layout_hierarchyVisitBefore(root1, sizeNode);
d3_layout_hierarchyVisitBefore(root1, function(node) {
node.node.x *= size[0];
node.node.y = node.node.depth * size[1];
});
}
// If a fixed tree size is specified, scale x and y based on the extent. // If a fixed tree size is specified, scale x and y based on the extent.
// Compute the left-most, right-most, and depth-most nodes for extents. // Compute the left-most, right-most, and depth-most nodes for extents.
...@@ -34,8 +29,8 @@ d3.layout.tree = function() { ...@@ -34,8 +29,8 @@ d3.layout.tree = function() {
x1 = right.x + separation(right, left) / 2, x1 = right.x + separation(right, left) / 2,
y1 = d3_layout_treeSearch(root0, d3_layout_treeDeepest).depth || 1; y1 = d3_layout_treeSearch(root0, d3_layout_treeDeepest).depth || 1;
d3_layout_hierarchyVisitBefore(root1, function(node) { d3_layout_hierarchyVisitBefore(root1, function(node) {
node.node.x = (node.node.x - x0) / (x1 - x0) * size[0]; node._.x = (node._.x - x0) / (x1 - x0) * size[0];
node.node.y = node.node.depth / y1 * size[1]; node._.y = node._.depth / y1 * size[1];
}); });
} }
...@@ -43,24 +38,25 @@ d3.layout.tree = function() { ...@@ -43,24 +38,25 @@ d3.layout.tree = function() {
} }
function wrapTree(root0) { function wrapTree(root0) {
var root1 = {defaultAncestor: null, children: [root0]}, var root1 = {A: null, children: [root0]},
queue = [root1], queue = [root1],
node1; node1;
while ((node1 = queue.pop()) != null) { while ((node1 = queue.pop()) != null) {
for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) { for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) {
queue.push((children[i] = child = { queue.push((children[i] = child = {
node: children[i], _: children[i], // source node
parent: node1, parent: node1,
children: (child = children[i].children) && child.slice() || [], children: (child = children[i].children) && child.slice() || [],
defaultAncestor: null, A: null, // default ancestor
ancestor: null, a: null, // ancestor
prelim: 0, z: 0, // prelim
mod: 0, m: 0, // mod
change: 0, c: 0, // change
shift: 0, s: 0, // shift
number: i t: null, // thread
}).ancestor = child); i: i // number
}).a = child);
} }
} }
...@@ -75,27 +71,27 @@ d3.layout.tree = function() { ...@@ -75,27 +71,27 @@ d3.layout.tree = function() {
function firstWalk(v) { function firstWalk(v) {
var children = v.children, var children = v.children,
siblings = v.parent.children, siblings = v.parent.children,
w = v.number ? siblings[v.number - 1] : null; w = v.i ? siblings[v.i - 1] : null;
if (children.length) { if (children.length) {
d3_layout_treeShift(v); d3_layout_treeShift(v);
var midpoint = (children[0].prelim + children[children.length - 1].prelim) / 2; var midpoint = (children[0].z + children[children.length - 1].z) / 2;
if (w) { if (w) {
v.prelim = w.prelim + separation(v.node, w.node); v.z = w.z + separation(v._, w._);
v.mod = v.prelim - midpoint; v.m = v.z - midpoint;
} else { } else {
v.prelim = midpoint; v.z = midpoint;
} }
} else if (w) { } else if (w) {
v.prelim = w.prelim + separation(v.node, w.node); v.z = w.z + separation(v._, w._);
} }
v.parent.defaultAncestor = apportion(v, w, v.parent.defaultAncestor || siblings[0]); v.parent.A = apportion(v, w, v.parent.A || siblings[0]);
} }
// SECOND WALK // SECOND WALK
// Computes all real x-coordinates by summing up the modifiers recursively. // Computes all real x-coordinates by summing up the modifiers recursively.
function secondWalk(v) { function secondWalk(v) {
v.node.x = v.prelim + v.parent.mod; v._.x = v.z + v.parent.m;
v.mod += v.parent.mod; v.m += v.parent.m;
} }
// APPORTION // APPORTION
...@@ -116,39 +112,44 @@ d3.layout.tree = function() { ...@@ -116,39 +112,44 @@ d3.layout.tree = function() {
vop = v, vop = v,
vim = w, vim = w,
vom = vip.parent.children[0], vom = vip.parent.children[0],
sip = vip.mod, sip = vip.m,
sop = vop.mod, sop = vop.m,
sim = vim.mod, sim = vim.m,
som = vom.mod, som = vom.m,
shift; shift;
while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) { while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) {
vom = d3_layout_treeLeft(vom); vom = d3_layout_treeLeft(vom);
vop = d3_layout_treeRight(vop); vop = d3_layout_treeRight(vop);
vop.ancestor = v; vop.a = v;
shift = vim.prelim + sim - vip.prelim - sip + separation(vim.node, vip.node); shift = vim.z + sim - vip.z - sip + separation(vim._, vip._);
if (shift > 0) { if (shift > 0) {
d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift); d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift);
sip += shift; sip += shift;
sop += shift; sop += shift;
} }
sim += vim.mod; sim += vim.m;
sip += vip.mod; sip += vip.m;
som += vom.mod; som += vom.m;
sop += vop.mod; sop += vop.m;
} }
if (vim && !d3_layout_treeRight(vop)) { if (vim && !d3_layout_treeRight(vop)) {
vop.thread = vim; vop.t = vim;
vop.mod += sim - sop; vop.m += sim - sop;
} }
if (vip && !d3_layout_treeLeft(vom)) { if (vip && !d3_layout_treeLeft(vom)) {
vom.thread = vip; vom.t = vip;
vom.mod += sip - som; vom.m += sip - som;
ancestor = v; ancestor = v;
} }
} }
return ancestor; return ancestor;
} }
function sizeNode(node) {
node._.x *= size[0];
node._.y = node._.depth * size[1];
}
tree.separation = function(x) { tree.separation = function(x) {
if (!arguments.length) return separation; if (!arguments.length) return separation;
separation = x; separation = x;
...@@ -157,13 +158,13 @@ d3.layout.tree = function() { ...@@ -157,13 +158,13 @@ d3.layout.tree = function() {
tree.size = function(x) { tree.size = function(x) {
if (!arguments.length) return nodeSize ? null : size; if (!arguments.length) return nodeSize ? null : size;
nodeSize = (size = x) == null; nodeSize = (size = x) == null ? sizeNode : null;
return tree; return tree;
}; };
tree.nodeSize = function(x) { tree.nodeSize = function(x) {
if (!arguments.length) return nodeSize ? size : null; if (!arguments.length) return nodeSize ? size : null;
nodeSize = (size = x) != null; nodeSize = (size = x) == null ? null : sizeNode;
return tree; return tree;
}; };
...@@ -212,26 +213,26 @@ function d3_layout_treeDeepest(a, b) { ...@@ -212,26 +213,26 @@ function d3_layout_treeDeepest(a, b) {
// returns null if and only if v is on the highest level of its subtree. // returns null if and only if v is on the highest level of its subtree.
function d3_layout_treeLeft(v) { function d3_layout_treeLeft(v) {
var children = v.children; var children = v.children;
return children.length ? children[0] : v.thread; return children.length ? children[0] : v.t;
} }
// NEXT RIGHT // NEXT RIGHT
// This function works analogously to NEXT LEFT. // This function works analogously to NEXT LEFT.
function d3_layout_treeRight(v) { function d3_layout_treeRight(v) {
var children = v.children, n; var children = v.children, n;
return (n = children.length) ? children[n - 1] : v.thread; return (n = children.length) ? children[n - 1] : v.t;
} }
// MOVE SUBTREE // MOVE SUBTREE
// Shifts the current subtree rooted at w+. This is done by increasing // Shifts the current subtree rooted at w+. This is done by increasing
// prelim(w+) and mod(w+) by shift. // prelim(w+) and mod(w+) by shift.
function d3_layout_treeMove(wm, wp, shift) { function d3_layout_treeMove(wm, wp, shift) {
var change = shift / (wp.number - wm.number); var change = shift / (wp.i - wm.i);
wp.change -= change; wp.c -= change;
wp.shift += shift; wp.s += shift;
wm.change += change; wm.c += change;
wp.prelim += shift; wp.z += shift;
wp.mod += shift; wp.m += shift;
} }
// EXECUTE SHIFTS // EXECUTE SHIFTS
...@@ -246,9 +247,9 @@ function d3_layout_treeShift(v) { ...@@ -246,9 +247,9 @@ function d3_layout_treeShift(v) {
w; w;
while (--i >= 0) { while (--i >= 0) {
w = children[i]; w = children[i];
w.prelim += shift; w.z += shift;
w.mod += shift; w.m += shift;
shift += w.shift + (change += w.change); shift += w.s + (change += w.c);