generational_indextree/
node.rs

1//! Node.
2
3#[cfg(not(feature = "std"))]
4use core::fmt;
5
6#[cfg(feature = "deser")]
7use serde::{Deserialize, Serialize};
8
9#[cfg(feature = "std")]
10use std::fmt;
11
12use crate::NodeId;
13
14#[derive(PartialEq, Eq, Clone, Debug)]
15#[cfg_attr(feature = "deser", derive(Deserialize, Serialize))]
16/// A node within a particular `Arena`.
17pub struct Node<T> {
18    // Keep these private (with read-only accessors) so that we can keep them
19    // consistent. E.g. the parent of a node’s child is that node.
20    pub(crate) parent: Option<NodeId>,
21    pub(crate) previous_sibling: Option<NodeId>,
22    pub(crate) next_sibling: Option<NodeId>,
23    pub(crate) first_child: Option<NodeId>,
24    pub(crate) last_child: Option<NodeId>,
25    /// The actual data which will be stored within the tree.
26    pub(crate) data: T,
27}
28
29impl<T> Node<T> {
30    /// Returns a reference to the node data.
31    pub fn get(&self) -> &T {
32        &self.data
33    }
34
35    /// Returns a mutable reference to the node data.
36    pub fn get_mut(&mut self) -> &mut T {
37        &mut self.data
38    }
39
40    /// Creates a new `Node` with the default state and the given data.
41    pub(crate) fn new(data: T) -> Self {
42        Self {
43            parent: None,
44            previous_sibling: None,
45            next_sibling: None,
46            first_child: None,
47            last_child: None,
48            data,
49        }
50    }
51
52    /// Returns the ID of the parent node, unless this node is the root of the
53    /// tree.
54    ///
55    /// # Examples
56    ///
57    /// ```
58    /// # use generational_indextree::Arena;
59    /// # let mut arena = Arena::new();
60    /// # let n1 = arena.new_node("1");
61    /// # let n1_1 = arena.new_node("1_1");
62    /// # let n1_2 = arena.new_node("1_2");
63    /// # n1.append(n1_2, &mut arena);
64    /// # let n1_3 = arena.new_node("1_3");
65    /// # n1.append(n1_3, &mut arena);
66    /// # n1.append(n1_1, &mut arena);
67    /// // arena
68    /// // `-- 1
69    /// //     |-- 1_1
70    /// //     |-- 1_2
71    /// //     `-- 1_3
72    /// assert_eq!(arena[n1].parent(), None);
73    /// assert_eq!(arena[n1_1].parent(), Some(n1));
74    /// assert_eq!(arena[n1_2].parent(), Some(n1));
75    /// assert_eq!(arena[n1_3].parent(), Some(n1));
76    /// ```
77    pub fn parent(&self) -> Option<NodeId> {
78        self.parent
79    }
80
81    /// Returns the ID of the first child of this node, unless it has no child.
82    ///
83    /// # Examples
84    ///
85    /// ```
86    /// # use generational_indextree::Arena;
87    /// # let mut arena = Arena::new();
88    /// # let n1 = arena.new_node("1");
89    /// # let n1_1 = arena.new_node("1_1");
90    /// # n1.append(n1_1, &mut arena);
91    /// # let n1_2 = arena.new_node("1_2");
92    /// # n1.append(n1_2, &mut arena);
93    /// # let n1_3 = arena.new_node("1_3");
94    /// # n1.append(n1_3, &mut arena);
95    /// // arena
96    /// // `-- 1
97    /// //     |-- 1_1
98    /// //     |-- 1_2
99    /// //     `-- 1_3
100    /// assert_eq!(arena[n1].first_child(), Some(n1_1));
101    /// assert_eq!(arena[n1_1].first_child(), None);
102    /// assert_eq!(arena[n1_2].first_child(), None);
103    /// assert_eq!(arena[n1_3].first_child(), None);
104    /// ```
105    pub fn first_child(&self) -> Option<NodeId> {
106        self.first_child
107    }
108
109    /// Returns the ID of the last child of this node, unless it has no child.
110    ///
111    /// # Examples
112    ///
113    /// ```
114    /// # use generational_indextree::Arena;
115    /// # let mut arena = Arena::new();
116    /// # let n1 = arena.new_node("1");
117    /// # let n1_1 = arena.new_node("1_1");
118    /// # n1.append(n1_1, &mut arena);
119    /// # let n1_2 = arena.new_node("1_2");
120    /// # n1.append(n1_2, &mut arena);
121    /// # let n1_3 = arena.new_node("1_3");
122    /// # n1.append(n1_3, &mut arena);
123    /// // arena
124    /// // `-- 1
125    /// //     |-- 1_1
126    /// //     |-- 1_2
127    /// //     `-- 1_3
128    /// assert_eq!(arena[n1].last_child(), Some(n1_3));
129    /// assert_eq!(arena[n1_1].last_child(), None);
130    /// assert_eq!(arena[n1_2].last_child(), None);
131    /// assert_eq!(arena[n1_3].last_child(), None);
132    /// ```
133    pub fn last_child(&self) -> Option<NodeId> {
134        self.last_child
135    }
136
137    /// Returns the ID of the previous sibling of this node, unless it is a
138    /// first child.
139    ///
140    /// # Examples
141    ///
142    /// ```
143    /// # use generational_indextree::Arena;
144    /// # let mut arena = Arena::new();
145    /// # let n1 = arena.new_node("1");
146    /// # let n1_1 = arena.new_node("1_1");
147    /// # n1.append(n1_1, &mut arena);
148    /// # let n1_2 = arena.new_node("1_2");
149    /// # n1.append(n1_2, &mut arena);
150    /// # let n1_3 = arena.new_node("1_3");
151    /// # n1.append(n1_3, &mut arena);
152    /// // arena
153    /// // `-- 1
154    /// //     |-- 1_1
155    /// //     |-- 1_2
156    /// //     `-- 1_3
157    /// assert_eq!(arena[n1].previous_sibling(), None);
158    /// assert_eq!(arena[n1_1].previous_sibling(), None);
159    /// assert_eq!(arena[n1_2].previous_sibling(), Some(n1_1));
160    /// assert_eq!(arena[n1_3].previous_sibling(), Some(n1_2));
161    /// ```
162    ///
163    /// Note that newly created nodes are independent toplevel nodes, and they
164    /// are not siblings by default.
165    ///
166    /// ```
167    /// # use generational_indextree::Arena;
168    /// let mut arena = Arena::new();
169    /// let n1 = arena.new_node("1");
170    /// let n2 = arena.new_node("2");
171    /// // arena
172    /// // |-- (implicit)
173    /// // |   `-- 1
174    /// // `-- (implicit)
175    /// //     `-- 2
176    /// assert_eq!(arena[n1].previous_sibling(), None);
177    /// assert_eq!(arena[n2].previous_sibling(), None);
178    ///
179    /// n1.insert_after(n2, &mut arena);
180    /// // arena
181    /// // `-- (implicit)
182    /// //     |-- 1
183    /// //     `-- 2
184    /// assert_eq!(arena[n1].previous_sibling(), None);
185    /// assert_eq!(arena[n2].previous_sibling(), Some(n1));
186    /// ```
187    pub fn previous_sibling(&self) -> Option<NodeId> {
188        self.previous_sibling
189    }
190
191    /// Returns the ID of the next sibling of this node, unless it is a
192    /// last child.
193    ///
194    /// # Examples
195    ///
196    /// ```
197    /// # use generational_indextree::Arena;
198    /// # let mut arena = Arena::new();
199    /// # let n1 = arena.new_node("1");
200    /// # let n1_1 = arena.new_node("1_1");
201    /// # n1.append(n1_1, &mut arena);
202    /// # let n1_2 = arena.new_node("1_2");
203    /// # n1.append(n1_2, &mut arena);
204    /// # let n1_3 = arena.new_node("1_3");
205    /// # n1.append(n1_3, &mut arena);
206    /// // arena
207    /// // `-- 1
208    /// //     |-- 1_1
209    /// //     |-- 1_2
210    /// //     `-- 1_3
211    /// assert_eq!(arena[n1].next_sibling(), None);
212    /// assert_eq!(arena[n1_1].next_sibling(), Some(n1_2));
213    /// assert_eq!(arena[n1_2].next_sibling(), Some(n1_3));
214    /// assert_eq!(arena[n1_3].next_sibling(), None);
215    /// ```
216    ///
217    /// Note that newly created nodes are independent toplevel nodes, and they
218    /// are not siblings by default.
219    ///
220    /// ```
221    /// # use generational_indextree::Arena;
222    /// let mut arena = Arena::new();
223    /// let n1 = arena.new_node("1");
224    /// let n2 = arena.new_node("2");
225    /// // arena
226    /// // |-- (implicit)
227    /// // |   `-- 1
228    /// // `-- (implicit)
229    /// //     `-- 2
230    /// assert_eq!(arena[n1].next_sibling(), None);
231    /// assert_eq!(arena[n2].next_sibling(), None);
232    ///
233    /// n1.insert_after(n2, &mut arena);
234    /// // arena
235    /// // `-- (implicit)
236    /// //     |-- 1
237    /// //     `-- 2
238    /// assert_eq!(arena[n1].next_sibling(), Some(n2));
239    /// assert_eq!(arena[n2].next_sibling(), None);
240    /// ```
241    pub fn next_sibling(&self) -> Option<NodeId> {
242        self.next_sibling
243    }
244
245    /// Checks if the node is detached.
246    pub(crate) fn is_detached(&self) -> bool {
247        self.parent.is_none() && self.previous_sibling.is_none() && self.next_sibling.is_none()
248    }
249}
250
251impl<T> fmt::Display for Node<T> {
252    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253        if let Some(parent) = self.parent {
254            write!(f, "parent: {}; ", parent)?;
255        } else {
256            write!(f, "no parent; ")?;
257        }
258        if let Some(previous_sibling) = self.previous_sibling {
259            write!(f, "previous sibling: {}; ", previous_sibling)?;
260        } else {
261            write!(f, "no previous sibling; ")?;
262        }
263        if let Some(next_sibling) = self.next_sibling {
264            write!(f, "next sibling: {}; ", next_sibling)?;
265        } else {
266            write!(f, "no next sibling; ")?;
267        }
268        if let Some(first_child) = self.first_child {
269            write!(f, "first child: {}; ", first_child)?;
270        } else {
271            write!(f, "no first child; ")?;
272        }
273        if let Some(last_child) = self.last_child {
274            write!(f, "last child: {}; ", last_child)?;
275        } else {
276            write!(f, "no last child; ")?;
277        }
278        Ok(())
279    }
280}