forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
88 lines (74 loc) · 2.27 KB
/
util.ts
File metadata and controls
88 lines (74 loc) · 2.27 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
/**
* @license
* Copyright Google LLC 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
*/
import {getIntParameter} from '../util';
export class TreeNode {
transitiveChildCount: number;
children: TreeNode[];
constructor(
public value: string,
public depth: number,
public maxDepth: number,
public left: TreeNode | null,
public right: TreeNode | null,
) {
this.transitiveChildCount = Math.pow(2, this.maxDepth - this.depth + 1) - 1;
this.children = this.left ? [this.left, this.right!] : [];
}
// Needed for Polymer as it does not support ternary nor modulo operator
// in expressions
get style(): string {
return this.depth % 2 === 0 ? 'background-color: grey' : '';
}
}
let treeCreateCount: number;
let maxDepth: number;
let numberData: TreeNode;
let charData: TreeNode;
export function getMaxDepth() {
return maxDepth;
}
export function initTreeUtils() {
maxDepth = getIntParameter('depth');
treeCreateCount = 0;
numberData = _buildTree(0, numberValues);
charData = _buildTree(0, charValues);
}
function _buildTree(currDepth: number, valueFn: (depth: number) => string): TreeNode {
const children = currDepth < maxDepth ? _buildTree(currDepth + 1, valueFn) : null;
return new TreeNode(valueFn(currDepth), currDepth, maxDepth, children, children);
}
export const emptyTree = new TreeNode('', 0, 0, null, null);
export function buildTree(): TreeNode {
treeCreateCount++;
return treeCreateCount % 2 ? numberData : charData;
}
function numberValues(depth: number): string {
return depth.toString();
}
function charValues(depth: number): string {
return String.fromCharCode('A'.charCodeAt(0) + (depth % 26));
}
export function flattenTree(node: TreeNode, target: TreeNode[] = []): TreeNode[] {
target.push(node);
if (node.left) {
flattenTree(node.left, target);
}
if (node.right) {
flattenTree(node.right, target);
}
return target;
}
export function newArray<T = any>(size: number): T[];
export function newArray<T>(size: number, value: T): T[];
export function newArray<T>(size: number, value?: T): T[] {
const list: T[] = [];
for (let i = 0; i < size; i++) {
list.push(value!);
}
return list;
}