[6a3a178] | 1 | /**
|
---|
| 2 | * @license
|
---|
| 3 | * Copyright Google LLC All Rights Reserved.
|
---|
| 4 | *
|
---|
| 5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 6 | * found in the LICENSE file at https://angular.io/license
|
---|
| 7 | */
|
---|
| 8 | export class Tree {
|
---|
| 9 | constructor(root) {
|
---|
| 10 | this._root = root;
|
---|
| 11 | }
|
---|
| 12 | get root() {
|
---|
| 13 | return this._root.value;
|
---|
| 14 | }
|
---|
| 15 | /**
|
---|
| 16 | * @internal
|
---|
| 17 | */
|
---|
| 18 | parent(t) {
|
---|
| 19 | const p = this.pathFromRoot(t);
|
---|
| 20 | return p.length > 1 ? p[p.length - 2] : null;
|
---|
| 21 | }
|
---|
| 22 | /**
|
---|
| 23 | * @internal
|
---|
| 24 | */
|
---|
| 25 | children(t) {
|
---|
| 26 | const n = findNode(t, this._root);
|
---|
| 27 | return n ? n.children.map(t => t.value) : [];
|
---|
| 28 | }
|
---|
| 29 | /**
|
---|
| 30 | * @internal
|
---|
| 31 | */
|
---|
| 32 | firstChild(t) {
|
---|
| 33 | const n = findNode(t, this._root);
|
---|
| 34 | return n && n.children.length > 0 ? n.children[0].value : null;
|
---|
| 35 | }
|
---|
| 36 | /**
|
---|
| 37 | * @internal
|
---|
| 38 | */
|
---|
| 39 | siblings(t) {
|
---|
| 40 | const p = findPath(t, this._root);
|
---|
| 41 | if (p.length < 2)
|
---|
| 42 | return [];
|
---|
| 43 | const c = p[p.length - 2].children.map(c => c.value);
|
---|
| 44 | return c.filter(cc => cc !== t);
|
---|
| 45 | }
|
---|
| 46 | /**
|
---|
| 47 | * @internal
|
---|
| 48 | */
|
---|
| 49 | pathFromRoot(t) {
|
---|
| 50 | return findPath(t, this._root).map(s => s.value);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | // DFS for the node matching the value
|
---|
| 54 | function findNode(value, node) {
|
---|
| 55 | if (value === node.value)
|
---|
| 56 | return node;
|
---|
| 57 | for (const child of node.children) {
|
---|
| 58 | const node = findNode(value, child);
|
---|
| 59 | if (node)
|
---|
| 60 | return node;
|
---|
| 61 | }
|
---|
| 62 | return null;
|
---|
| 63 | }
|
---|
| 64 | // Return the path to the node with the given value using DFS
|
---|
| 65 | function findPath(value, node) {
|
---|
| 66 | if (value === node.value)
|
---|
| 67 | return [node];
|
---|
| 68 | for (const child of node.children) {
|
---|
| 69 | const path = findPath(value, child);
|
---|
| 70 | if (path.length) {
|
---|
| 71 | path.unshift(node);
|
---|
| 72 | return path;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | return [];
|
---|
| 76 | }
|
---|
| 77 | export class TreeNode {
|
---|
| 78 | constructor(value, children) {
|
---|
| 79 | this.value = value;
|
---|
| 80 | this.children = children;
|
---|
| 81 | }
|
---|
| 82 | toString() {
|
---|
| 83 | return `TreeNode(${this.value})`;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | // Return the list of T indexed by outlet name
|
---|
| 87 | export function nodeChildrenAsMap(node) {
|
---|
| 88 | const map = {};
|
---|
| 89 | if (node) {
|
---|
| 90 | node.children.forEach(child => map[child.value.outlet] = child);
|
---|
| 91 | }
|
---|
| 92 | return map;
|
---|
| 93 | }
|
---|
| 94 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJlZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL3JvdXRlci9zcmMvdXRpbHMvdHJlZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxNQUFNLE9BQU8sSUFBSTtJQUlmLFlBQVksSUFBaUI7UUFDM0IsSUFBSSxDQUFDLEtBQUssR0FBRyxJQUFJLENBQUM7SUFDcEIsQ0FBQztJQUVELElBQUksSUFBSTtRQUNOLE9BQU8sSUFBSSxDQUFDLEtBQUssQ0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