aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/util/tree.rs
blob: 94ceb5e1b36c2b15a1a1bb696aa33601be22c362 (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Helper functions for garbage collected doubly-linked trees.

/// The basic trait. This function is meant to encapsulate a clonable reference to a tree node.
pub trait TreeNodeRef<N> : Clone {
    /// Borrows this node as immutable.
    fn with_base<R>(&self, callback: &fn(&N) -> R) -> R;

    /// Borrows this node as mutable.
    fn with_mut_base<R>(&self, callback: &fn(&mut N) -> R) -> R;
}

/// The contents of a tree node.
pub trait TreeNode<NR> {
    /// Returns the parent of this node.
    fn parent_node(&self) -> Option<NR>;

    /// Returns the first child of this node.
    fn first_child(&self) -> Option<NR>;

    /// Returns the last child of this node.
    fn last_child(&self) -> Option<NR>;

    /// Returns the previous sibling of this node.
    fn prev_sibling(&self) -> Option<NR>;

    /// Returns the next sibling of this node.
    fn next_sibling(&self) -> Option<NR>;

    /// Sets the parent of this node.
    fn set_parent_node(&mut self, new_parent: Option<NR>);

    /// Sets the first child of this node.
    fn set_first_child(&mut self, new_first_child: Option<NR>);

    /// Sets the last child of this node.
    fn set_last_child(&mut self, new_last_child: Option<NR>);

    /// Sets the previous sibling of this node.
    fn set_prev_sibling(&mut self, new_prev_sibling: Option<NR>);

    /// Sets the next sibling of this node.
    fn set_next_sibling(&mut self, new_next_sibling: Option<NR>);
}

/// A set of helper functions useful for operating on trees.
pub trait TreeUtils {
    /// Returns true if this node is disconnected from the tree or has no children.
    fn is_leaf(&self) -> bool;

    /// Adds a new child to the end of this node's list of children.
    ///
    /// Fails unless `new_child` is disconnected from the tree.
    fn add_child(&self, new_child: Self);

    /// Removes the given child from this node's list of children.
    ///
    /// Fails unless `child` is a child of this node. (FIXME: This is not yet checked.)
    fn remove_child(&self, child: Self);

    /// Iterates over all children of this node.
    fn each_child(&self, callback: &fn(Self) -> bool) -> bool;

    /// Iterates over this node and all its descendants, in preorder.
    fn traverse_preorder(&self, callback: &fn(Self) -> bool) -> bool;

    /// Iterates over this node and all its descendants, in postorder.
    fn traverse_postorder(&self, callback: &fn(Self) -> bool) -> bool;
}

impl<NR:TreeNodeRef<N>,N:TreeNode<NR>> TreeUtils for NR {
    fn is_leaf(&self) -> bool {
        do self.with_base |this_node| {
            this_node.first_child().is_none()
        }
    }

    fn add_child(&self, new_child: NR) {
        do self.with_mut_base |this_node| {
            do new_child.with_mut_base |new_child_node| {
                assert!(new_child_node.parent_node().is_none());
                assert!(new_child_node.prev_sibling().is_none());
                assert!(new_child_node.next_sibling().is_none());

                match this_node.last_child() {
                    None => this_node.set_first_child(Some(new_child.clone())),
                    Some(last_child) => {
                        do last_child.with_mut_base |last_child_node| {
                            assert!(last_child_node.next_sibling().is_none());
                            last_child_node.set_next_sibling(Some(new_child.clone()));
                            new_child_node.set_prev_sibling(Some(last_child.clone()));
                        }
                    }
                }

                this_node.set_last_child(Some(new_child.clone()));
                new_child_node.set_parent_node(Some((*self).clone()));
            }
        }
    }

    fn remove_child(&self, child: NR) {
        do self.with_mut_base |this_node| {
            do child.with_mut_base |child_node| {
                assert!(child_node.parent_node().is_some());

                match child_node.prev_sibling() {
                    None => this_node.set_first_child(child_node.next_sibling()),
                    Some(prev_sibling) => {
                        do prev_sibling.with_mut_base |prev_sibling_node| {
                            prev_sibling_node.set_next_sibling(child_node.next_sibling());
                        }
                    }
                }

                match child_node.next_sibling() {
                    None => this_node.set_last_child(child_node.prev_sibling()),
                    Some(next_sibling) => {
                        do next_sibling.with_mut_base |next_sibling_node| {
                            next_sibling_node.set_prev_sibling(child_node.prev_sibling());
                        }
                    }
                }

                child_node.set_prev_sibling(None);
                child_node.set_next_sibling(None);
                child_node.set_parent_node(None);
            }
        }
    }

    fn each_child(&self, callback: &fn(NR) -> bool) -> bool {
        let mut maybe_current = self.with_base(|n| n.first_child());
        while !maybe_current.is_none() {
            let current = maybe_current.get_ref().clone();
            if !callback(current.clone()) {
                break;
            }

            maybe_current = current.with_base(|n| n.next_sibling());
        }

        true
    }

    fn traverse_preorder(&self, callback: &fn(NR) -> bool) -> bool {
        if !callback((*self).clone()) {
            return false;
        }

        for self.each_child |kid| {
            // FIXME: Work around rust#2202. We should be able to pass the callback directly.
            if !kid.traverse_preorder(|a| callback(a)) {
                return false;
            }
        }

        true
    }

    fn traverse_postorder(&self, callback: &fn(NR) -> bool) -> bool {
        for self.each_child |kid| {
            // FIXME: Work around rust#2202. We should be able to pass the callback directly.
            if !kid.traverse_postorder(|a| callback(a)) {
                return false;
            }
        }

        callback((*self).clone())
    }
}