2
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
import { uint32 } from "@koalaui/compat"
19
* This class represents a path to a node in a tree.
21
export class TreePath<Node> {
23
readonly parent: TreePath<Node> | undefined
26
* Creates a tree path to the given node relative to the specified parent path.
27
* The path represents a root node if parent path is undefined.
29
* @param node a node in a tree
30
* @param parent a path to a parent node or undefined for the root
32
constructor(node: Node, parent?: TreePath<Node>) {
38
* Creates a tree path to the specified child node.
40
* @param node a node in a tree
42
child(node: Node): TreePath<Node> {
43
return new TreePath<Node>(node, this)
47
* Returns the tree path that represents the root node.
48
* The root path returns itself.
50
get root(): TreePath<Node> {
51
return this.parent?.root ?? this
55
* Returns the depth of the tree path relative to the root node.
56
* The root path returns 0.
60
let parent = this.parent
61
while (parent !== undefined) {
62
parent = parent!.parent
69
let parent = this.parent ?? ""
70
return `${parent}/${this.node}`