| 1 | import node_count from "./count.js";
|
|---|
| 2 | import node_each from "./each.js";
|
|---|
| 3 | import node_eachBefore from "./eachBefore.js";
|
|---|
| 4 | import node_eachAfter from "./eachAfter.js";
|
|---|
| 5 | import node_find from "./find.js";
|
|---|
| 6 | import node_sum from "./sum.js";
|
|---|
| 7 | import node_sort from "./sort.js";
|
|---|
| 8 | import node_path from "./path.js";
|
|---|
| 9 | import node_ancestors from "./ancestors.js";
|
|---|
| 10 | import node_descendants from "./descendants.js";
|
|---|
| 11 | import node_leaves from "./leaves.js";
|
|---|
| 12 | import node_links from "./links.js";
|
|---|
| 13 | import node_iterator from "./iterator.js";
|
|---|
| 14 |
|
|---|
| 15 | export default function hierarchy(data, children) {
|
|---|
| 16 | if (data instanceof Map) {
|
|---|
| 17 | data = [undefined, data];
|
|---|
| 18 | if (children === undefined) children = mapChildren;
|
|---|
| 19 | } else if (children === undefined) {
|
|---|
| 20 | children = objectChildren;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | var root = new Node(data),
|
|---|
| 24 | node,
|
|---|
| 25 | nodes = [root],
|
|---|
| 26 | child,
|
|---|
| 27 | childs,
|
|---|
| 28 | i,
|
|---|
| 29 | n;
|
|---|
| 30 |
|
|---|
| 31 | while (node = nodes.pop()) {
|
|---|
| 32 | if ((childs = children(node.data)) && (n = (childs = Array.from(childs)).length)) {
|
|---|
| 33 | node.children = childs;
|
|---|
| 34 | for (i = n - 1; i >= 0; --i) {
|
|---|
| 35 | nodes.push(child = childs[i] = new Node(childs[i]));
|
|---|
| 36 | child.parent = node;
|
|---|
| 37 | child.depth = node.depth + 1;
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | return root.eachBefore(computeHeight);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | function node_copy() {
|
|---|
| 46 | return hierarchy(this).eachBefore(copyData);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | function objectChildren(d) {
|
|---|
| 50 | return d.children;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | function mapChildren(d) {
|
|---|
| 54 | return Array.isArray(d) ? d[1] : null;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | function copyData(node) {
|
|---|
| 58 | if (node.data.value !== undefined) node.value = node.data.value;
|
|---|
| 59 | node.data = node.data.data;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | export function computeHeight(node) {
|
|---|
| 63 | var height = 0;
|
|---|
| 64 | do node.height = height;
|
|---|
| 65 | while ((node = node.parent) && (node.height < ++height));
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | export function Node(data) {
|
|---|
| 69 | this.data = data;
|
|---|
| 70 | this.depth =
|
|---|
| 71 | this.height = 0;
|
|---|
| 72 | this.parent = null;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | Node.prototype = hierarchy.prototype = {
|
|---|
| 76 | constructor: Node,
|
|---|
| 77 | count: node_count,
|
|---|
| 78 | each: node_each,
|
|---|
| 79 | eachAfter: node_eachAfter,
|
|---|
| 80 | eachBefore: node_eachBefore,
|
|---|
| 81 | find: node_find,
|
|---|
| 82 | sum: node_sum,
|
|---|
| 83 | sort: node_sort,
|
|---|
| 84 | path: node_path,
|
|---|
| 85 | ancestors: node_ancestors,
|
|---|
| 86 | descendants: node_descendants,
|
|---|
| 87 | leaves: node_leaves,
|
|---|
| 88 | links: node_links,
|
|---|
| 89 | copy: node_copy,
|
|---|
| 90 | [Symbol.iterator]: node_iterator
|
|---|
| 91 | };
|
|---|