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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! 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);

        if Some(parent_index) == self.last_node {
            let mut cursor = self.cursor(child).unwrap();

            while cursor.move_next().is_ok() {}

            let id = &cursor.id();
            self.last_node = self.index(id);
        }

        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));

        if Some(sibling_index) == self.first_node {
            self.first_node = Some(node_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);

        if Some(sibling_index) == self.last_node {
            self.last_node = Some(node_index);
        }

        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(())
    }
}

#[cfg(test)]
mod test {
    use crate::tree::Tree;

    #[test]
    fn should_update_last_node_make_child() {
        let mut tree = Tree::new();

        let parent = tree.append_child(1);

        let node = tree.create_node(2);

        assert_eq!(Some(parent), tree.first_node_id());
        assert_eq!(Some(parent), tree.last_node_id());

        tree.make_child(&node, &parent).unwrap();

        assert_eq!(Some(parent), tree.first_node_id());
        assert_eq!(Some(node), tree.last_node_id());
    }

    #[test]
    fn should_update_first_node_make_sibling() {
        let mut tree = Tree::new();

        let sibling = tree.append_child(1);

        let node = tree.create_node(2);

        assert_eq!(Some(sibling), tree.first_node_id());
        assert_eq!(Some(sibling), tree.last_node_id());

        tree.make_prev_siblings(&node, &sibling).unwrap();

        assert_eq!(Some(node), tree.first_node_id());
        assert_eq!(Some(sibling), tree.last_node_id());
    }

    #[test]
    fn should_update_last_node_make_sibling() {
        let mut tree = Tree::new();

        tree.append_child(1);
        let sibling = tree.append_child(2);

        let node = tree.create_node(3);

        assert_eq!(Some(sibling), tree.last_node_id());

        tree.make_next_siblings(&node, &sibling).unwrap();

        assert_eq!(Some(node), tree.last_node_id());
    }
}