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
//! Creates and changes relationships between nodes

use crate::{error::Error, id::NodeId, tree::Tree};

impl<T> Tree<T> {
    /// Makes three nodes related to each other
    fn relate(
        &mut self,
        node_index: usize,
        parent_index: Option<usize>,
        prev_sibling_index: Option<usize>,
        next_sibling_index: Option<usize>,
    ) {
        let node = self.nodes[node_index].unwrap_mut();
        node.parent = parent_index;
        node.prev_sibling = prev_sibling_index;
        node.next_sibling = next_sibling_index;

        // If the parent doesn't have children set the node as first and last
        if let Some(parent_index) = parent_index {
            let parent = self.nodes[parent_index].unwrap_mut();

            if prev_sibling_index.is_none() {
                parent.first_child = Some(node_index);
            }

            if next_sibling_index.is_none() {
                parent.last_child = Some(node_index);
            }
        }

        if let Some(prev_sibling_index) = prev_sibling_index {
            let prev_sibling = self.nodes[prev_sibling_index].unwrap_mut();

            debug_assert!(prev_sibling.next_sibling.is_none());
            debug_assert_eq!(prev_sibling.parent, parent_index);

            prev_sibling.next_sibling = Some(node_index);
        }

        if let Some(next_sibling_index) = next_sibling_index {
            let next_sibling = self.nodes[next_sibling_index].unwrap_mut();

            debug_assert!(next_sibling.prev_sibling.is_none());
            debug_assert_eq!(next_sibling.parent, parent_index);

            next_sibling.prev_sibling = Some(node_index);
        }
    }

    /// Make the `child` nodes as the last child of the `parent` node.
    ///
    /// # Errors
    ///
    /// - Fails of the same `NodeId` is passed
    /// - TODO: Fail if the child node is parent of the parent node.
    pub fn make_child(&mut self, child: &NodeId, parent: &NodeId) -> Result<(), Error> {
        let child_index = self.index(child).ok_or(Error::Invalid("for child"))?;
        let parent_index = self.index(parent).ok_or(Error::Invalid("for parent"))?;

        if child_index == parent_index {
            return Err(Error::SameNode);
        }

        // TODO: search if the child has the parent as child

        let parent_node = self.nodes[parent_index].unwrap_ref();
        let last_child = parent_node.last_child;

        self.relate(child_index, Some(parent_index), last_child, None);

        Ok(())
    }

    /// Make the `node` as the previous sibling of `sibling`
    ///
    /// # Errors
    ///
    /// - Fails of the same `NodeId` is passed
    /// - TODO: Fail if the child node is parent of the parent node.
    pub fn make_prev_siblings(&mut self, node: &NodeId, sibling: &NodeId) -> Result<(), Error> {
        let node_index = self.index(node).ok_or(Error::Invalid("for node"))?;
        let sibling_index = self.index(sibling).ok_or(Error::Invalid("for sibling"))?;

        if node_index == sibling_index {
            return Err(Error::SameNode);
        }

        // TODO: search if the child has the parent as child

        let sibling_node = self.nodes[sibling_index].unwrap_ref();
        let parent_index = sibling_node.parent;
        let prev_sibling = sibling_node.prev_sibling;

        self.relate(node_index, parent_index, prev_sibling, Some(sibling_index));

        Ok(())
    }

    /// Make the `node` as the next sibling of `sibling`
    ///
    /// # Errors
    ///
    /// - Fails of the same `NodeId` is passed
    /// - TODO: Fail if the child node is parent of the parent node.
    pub fn make_next_siblings(&mut self, node: &NodeId, sibling: &NodeId) -> Result<(), Error> {
        let node_index = self.index(node).ok_or(Error::Invalid("for node"))?;
        let sibling_index = self.index(sibling).ok_or(Error::Invalid("for sibling"))?;

        if node_index == sibling_index {
            return Err(Error::SameNode);
        }

        // TODO: search if the child has the parent as child

        let sibling_node = self.nodes[sibling_index].unwrap_ref();
        let parent_index = sibling_node.parent;
        let next_sibling = sibling_node.next_sibling;

        self.relate(node_index, parent_index, Some(sibling_index), next_sibling);

        Ok(())
    }

    /// Detach the node from it's parent
    ///
    /// # Errors
    ///
    /// - Fails of the `node` was removed
    pub fn detach(&mut self, node: &NodeId) -> Result<(), Error> {
        let node_index = self.index(node).ok_or(Error::Invalid("for node"))?;
        let node = self.nodes[node_index].unwrap_mut();

        let parent = node.parent;
        let prev_sibling = node.prev_sibling;
        let next_sibling = node.next_sibling;

        node.parent = None;

        if let Some(parent_index) = parent {
            let parent = self.nodes[parent_index].unwrap_mut();

            if parent.last_child == Some(node_index) {
                parent.last_child = prev_sibling;
            }

            if parent.first_child == Some(node_index) {
                parent.last_child = next_sibling;
            }
        }

        if let Some(next_sibling_index) = next_sibling {
            let next = self.nodes[next_sibling_index].unwrap_mut();

            next.prev_sibling = prev_sibling;
        }

        if let Some(prev_sibling_index) = prev_sibling {
            let prev = self.nodes[prev_sibling_index].unwrap_mut();

            prev.next_sibling = next_sibling;
        }

        Ok(())
    }
}