1use crate::{id::NodeId, tree::Tree};
4
5#[derive(Debug)]
11pub struct Cursor<'a, T> {
12 pub(crate) index: usize,
13 tree: &'a mut Tree<T>,
14}
15
16impl<T> Tree<T> {
17 pub fn cursor(&mut self, id: &NodeId) -> Option<Cursor<T>> {
18 self.index(id).map(|index| Cursor { index, tree: self })
19 }
20
21 pub fn cursor_first(&mut self) -> Option<Cursor<T>> {
22 self.first_node.map(|index| Cursor { index, tree: self })
23 }
24
25 pub fn cursor_last(&mut self) -> Option<Cursor<T>> {
26 self.last_node.map(|index| Cursor { index, tree: self })
27 }
28}
29
30impl<T> Cursor<'_, T> {
31 #[must_use]
32 pub fn id(&self) -> NodeId {
33 NodeId::new(self.index)
34 }
35
36 #[must_use]
37 pub fn get(&self) -> &T {
38 &self.tree.nodes[self.index].unwrap_ref().value
39 }
40
41 #[must_use]
42 pub fn get_mut(&mut self) -> &mut T {
43 &mut self.tree.nodes[self.index].unwrap_mut().value
44 }
45
46 pub fn parent(&mut self) -> Result<&mut Self, &mut Self> {
52 match self.tree.nodes[self.index].unwrap_ref().parent {
53 Some(index) => {
54 self.index = index;
55 Ok(self)
56 }
57 None => Err(self),
58 }
59 }
60
61 pub fn first_child(&mut self) -> Result<&mut Self, &mut Self> {
67 match self.tree.nodes[self.index].unwrap_ref().first_child {
68 Some(index) => {
69 self.index = index;
70 Ok(self)
71 }
72 None => Err(self),
73 }
74 }
75
76 pub fn last_child(&mut self) -> Result<&mut Self, &mut Self> {
82 match self.tree.nodes[self.index].unwrap_ref().last_child {
83 Some(index) => {
84 self.index = index;
85 Ok(self)
86 }
87 None => Err(self),
88 }
89 }
90
91 pub fn next_sibling(&mut self) -> Result<&mut Self, &mut Self> {
97 match self.tree.nodes[self.index].unwrap_ref().next_sibling {
98 Some(index) => {
99 self.index = index;
100 Ok(self)
101 }
102 None => Err(self),
103 }
104 }
105
106 pub fn prev_sibling(&mut self) -> Result<&mut Self, &mut Self> {
112 match self.tree.nodes[self.index].unwrap_ref().prev_sibling {
113 Some(index) => {
114 self.index = index;
115 Ok(self)
116 }
117 None => Err(self),
118 }
119 }
120
121 pub fn move_next(&mut self) -> Result<&mut Self, &mut Self> {
127 self.first_child()
128 .or_else(Cursor::next_sibling)
129 .or_else(|cursor| {
130 let mut parent = &cursor.tree.nodes[cursor.index].unwrap_ref().parent;
131
132 while let Some(parent_index) = parent {
134 let node = cursor.tree.nodes[*parent_index].unwrap_ref();
135
136 if let Some(sibling) = node.next_sibling {
137 cursor.index = sibling;
138
139 return Ok(cursor);
140 }
141
142 parent = &node.parent;
143 }
144
145 Err(cursor)
146 })
147 }
148
149 pub fn append_child(&mut self, value: T) -> NodeId {
150 let index = self.tree.insert_child_at(self.index, value);
151
152 NodeId::new(index)
153 }
154
155 pub fn append_sibling(&mut self, value: T) -> NodeId {
156 let index = self.tree.insert_sibling_at(self.index, value);
157
158 NodeId::new(index)
159 }
160
161 #[must_use]
162 pub fn peek_parent(&self) -> Option<&T> {
163 self.tree.nodes[self.index]
164 .unwrap_ref()
165 .parent
166 .and_then(|index| self.tree.nodes[index].map_ref(|node| &node.value))
167 }
168
169 #[must_use]
170 pub fn peek_next_sibling(&self) -> Option<&T> {
171 self.tree.nodes[self.index]
172 .unwrap_ref()
173 .next_sibling
174 .and_then(|index| self.tree.nodes[index].map_ref(|node| &node.value))
175 }
176
177 #[must_use]
178 pub fn peek_prev_sibling(&self) -> Option<&T> {
179 self.tree.nodes[self.index]
180 .unwrap_ref()
181 .prev_sibling
182 .and_then(|index| self.tree.nodes[index].map_ref(|node| &node.value))
183 }
184
185 #[must_use]
186 pub fn peek_first_child(&self) -> Option<&T> {
187 self.tree.nodes[self.index]
188 .unwrap_ref()
189 .first_child
190 .and_then(|index| self.tree.nodes[index].map_ref(|node| &node.value))
191 }
192
193 #[must_use]
194 pub fn peek_last_child(&self) -> Option<&T> {
195 self.tree.nodes[self.index]
196 .unwrap_ref()
197 .first_child
198 .and_then(|index| self.tree.nodes[index].map_ref(|node| &node.value))
199 }
200}
201
202#[cfg(test)]
203mod test {
204 use crate::tree::Tree;
205
206 #[test]
207 fn should_move_next() {
208 let mut tree: Tree<i32> = Tree::new();
209 tree.append_child(0);
211
212 let b = tree.append_child(1);
214
215 tree.append_child(2);
217
218 tree.insert_sibling_after(&b, 3).unwrap();
221
222 let mut cursor = tree.cursor_first().unwrap();
223
224 assert_eq!(0, *cursor.get());
225
226 cursor.move_next().unwrap();
227 assert_eq!(1, *cursor.get());
228
229 cursor.move_next().unwrap();
230 assert_eq!(2, *cursor.get());
231
232 cursor.move_next().unwrap();
233 assert_eq!(3, *cursor.get());
234
235 assert!(cursor.move_next().is_err());
236 }
237}