idlize

Форк
0
86 строк · 2.5 Кб
1
/*
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
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
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.
14
 */
15

16
import { int32, uint32 } from "@koalaui/compat"
17
import { TreePath } from "./TreePath"
18

19
/**
20
 * This class provides basic functionality for tree building or incremental tree updating.
21
 */
22
export abstract class TreeUpdater<Node> {
23
    private current: TreePath<Node>
24
    readonly reverse: boolean
25

26
    /**
27
     * @param root     the tree root that always exists
28
     * @param reverse  `false` builds a tree top-down, `true` build a tree bottom-up
29
     */
30
    constructor(root: Node, reverse: boolean = false) {
31
        this.current = new TreePath<Node>(root)
32
        this.reverse = reverse
33
    }
34

35
    /**
36
     * Returns the path to the selected node.
37
     */
38
    get path(): TreePath<Node> {
39
        return this.current
40
    }
41

42
    /**
43
     * Returns the selected node to perform tree change operations.
44
     */
45
    get node(): Node {
46
        return this.current.node
47
    }
48

49
    /**
50
     * Selects the specified child node when the updater is getting traversed down the tree.
51
     */
52
    down(node: Node): void {
53
        this.current = this.current.child(node)
54
    }
55

56
    /**
57
     * Selects the parent node when the updater is getting traversed up the tree.
58
     */
59
    up(): void {
60
        let parent = this.current.parent
61
        if (parent === undefined) throw new Error("unexpected")
62
        this.current = parent
63
    }
64

65
    /**
66
     * Selects the root node to prepare the updater to traversing a tree.
67
     */
68
    clear(): void {
69
        this.current = this.current.root
70
    }
71

72
    /**
73
     * Inserts the specified node as a child of a selected node at the given index.
74
     */
75
    abstract insert(index: int32, node: Node): void
76

77
    /**
78
     * Removes the specified amount of children from the given index.
79
     */
80
    abstract remove(index: int32, amount: uint32): void
81

82
    /**
83
     * Moves the specified amount of children from the indexFrom to the indexTo.
84
     */
85
    abstract move(indexFrom: int32, indexTo: int32, amount: uint32): void
86
}
87

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.