tree.js
3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export var Tree = (function () {
/**
* @param {?} root
*/
function Tree(root) {
this._root = root;
}
Object.defineProperty(Tree.prototype, "root", {
/**
* @return {?}
*/
get: function () { return this._root.value; },
enumerable: true,
configurable: true
});
/**
* \@internal
* @param {?} t
* @return {?}
*/
Tree.prototype.parent = function (t) {
var /** @type {?} */ p = this.pathFromRoot(t);
return p.length > 1 ? p[p.length - 2] : null;
};
/**
* \@internal
* @param {?} t
* @return {?}
*/
Tree.prototype.children = function (t) {
var /** @type {?} */ n = findNode(t, this._root);
return n ? n.children.map(function (t) { return t.value; }) : [];
};
/**
* \@internal
* @param {?} t
* @return {?}
*/
Tree.prototype.firstChild = function (t) {
var /** @type {?} */ n = findNode(t, this._root);
return n && n.children.length > 0 ? n.children[0].value : null;
};
/**
* \@internal
* @param {?} t
* @return {?}
*/
Tree.prototype.siblings = function (t) {
var /** @type {?} */ p = findPath(t, this._root, []);
if (p.length < 2)
return [];
var /** @type {?} */ c = p[p.length - 2].children.map(function (c) { return c.value; });
return c.filter(function (cc) { return cc !== t; });
};
/**
* \@internal
* @param {?} t
* @return {?}
*/
Tree.prototype.pathFromRoot = function (t) { return findPath(t, this._root, []).map(function (s) { return s.value; }); };
return Tree;
}());
function Tree_tsickle_Closure_declarations() {
/**
* \@internal
* @type {?}
*/
Tree.prototype._root;
}
/**
* @param {?} expected
* @param {?} c
* @return {?}
*/
function findNode(expected, c) {
if (expected === c.value)
return c;
for (var _i = 0, _a = c.children; _i < _a.length; _i++) {
var cc = _a[_i];
var /** @type {?} */ r = findNode(expected, cc);
if (r)
return r;
}
return null;
}
/**
* @param {?} expected
* @param {?} c
* @param {?} collected
* @return {?}
*/
function findPath(expected, c, collected) {
collected.push(c);
if (expected === c.value)
return collected;
for (var _i = 0, _a = c.children; _i < _a.length; _i++) {
var cc = _a[_i];
var /** @type {?} */ cloned = collected.slice(0);
var /** @type {?} */ r = findPath(expected, cc, cloned);
if (r.length > 0)
return r;
}
return [];
}
export var TreeNode = (function () {
/**
* @param {?} value
* @param {?} children
*/
function TreeNode(value, children) {
this.value = value;
this.children = children;
}
/**
* @return {?}
*/
TreeNode.prototype.toString = function () { return "TreeNode(" + this.value + ")"; };
return TreeNode;
}());
function TreeNode_tsickle_Closure_declarations() {
/** @type {?} */
TreeNode.prototype.value;
/** @type {?} */
TreeNode.prototype.children;
}
//# sourceMappingURL=tree.js.map