Skip to main content

cursive_tree/model/
list.rs

1use super::{super::backend::*, iterator::*, kind::*, node::*, path::*, representation::*};
2
3use std::{ptr, slice::*};
4
5//
6// NodeList
7//
8
9/// Tree node list.
10pub struct NodeList<BackendT, ContextT, ErrorT, IdT, DataT>(pub Vec<Node<BackendT, ContextT, ErrorT, IdT, DataT>>);
11
12impl<BackendT, ContextT, ErrorT, IdT, DataT> NodeList<BackendT, ContextT, ErrorT, IdT, DataT> {
13    /// Iterate nodes in visual order from top to bottom.
14    ///
15    /// When only_expanded is true will skip the children of collapsed branches.
16    pub fn iter(&self, only_expanded: bool) -> NodeIterator<'_, BackendT, ContextT, ErrorT, IdT, DataT> {
17        NodeIterator::new(self, only_expanded)
18    }
19
20    /// Get node at path.
21    pub fn at_path(&self, mut path: NodePath) -> Option<&Node<BackendT, ContextT, ErrorT, IdT, DataT>>
22    where
23        BackendT: TreeBackend<ContextT, ErrorT, IdT, DataT>,
24    {
25        path.pop_front().and_then(|index| self.0.get(index)).and_then(|node| node.at_path(path))
26    }
27
28    /// Get node at path.
29    pub fn at_path_mut(&mut self, mut path: NodePath) -> Option<&mut Node<BackendT, ContextT, ErrorT, IdT, DataT>>
30    where
31        BackendT: TreeBackend<ContextT, ErrorT, IdT, DataT>,
32    {
33        path.pop_front().and_then(|index| self.0.get_mut(index)).and_then(|node| node.at_path_mut(path))
34    }
35
36    /// Fill path to node.
37    ///
38    /// Returns true if found.
39    pub fn fill_path(&self, path: &mut NodePath, node: &Node<BackendT, ContextT, ErrorT, IdT, DataT>) -> bool
40    where
41        BackendT: TreeBackend<ContextT, ErrorT, IdT, DataT>,
42    {
43        if self.0.is_empty() {
44            return false;
45        }
46
47        for (index, node_) in self.0.iter().enumerate() {
48            path.push_back(index);
49            if ptr::eq(node, node_) {
50                return true;
51            } else if node_.fill_path(path, node) {
52                return true;
53            } else {
54                path.pop_back();
55            }
56        }
57
58        false
59    }
60
61    /// Add a node.
62    pub fn add(&mut self, depth: usize, kind: NodeKind, id: IdT, representation: Representation)
63    where
64        BackendT: TreeBackend<ContextT, ErrorT, IdT, DataT>,
65    {
66        self.0.push(Node::new(depth, kind, id, representation));
67    }
68
69    /// Insert a node.
70    pub fn insert(&mut self, index: usize, depth: usize, kind: NodeKind, id: IdT, representation: Representation)
71    where
72        BackendT: TreeBackend<ContextT, ErrorT, IdT, DataT>,
73    {
74        self.0.insert(index, Node::new(depth, kind, id, representation));
75    }
76}
77
78impl<BackendT, ContextT, ErrorT, IdT, DataT> Default for NodeList<BackendT, ContextT, ErrorT, IdT, DataT> {
79    fn default() -> Self {
80        Self(Default::default())
81    }
82}
83
84impl<'this, BackendT, ContextT, ErrorT, IdT, DataT> IntoIterator
85    for &'this NodeList<BackendT, ContextT, ErrorT, IdT, DataT>
86{
87    type Item = &'this Node<BackendT, ContextT, ErrorT, IdT, DataT>;
88    type IntoIter = Iter<'this, Node<BackendT, ContextT, ErrorT, IdT, DataT>>;
89
90    fn into_iter(self) -> Self::IntoIter {
91        self.0.iter()
92    }
93}
94
95impl<'this, BackendT, ContextT, ErrorT, IdT, DataT> IntoIterator
96    for &'this mut NodeList<BackendT, ContextT, ErrorT, IdT, DataT>
97{
98    type Item = &'this mut Node<BackendT, ContextT, ErrorT, IdT, DataT>;
99    type IntoIter = IterMut<'this, Node<BackendT, ContextT, ErrorT, IdT, DataT>>;
100
101    fn into_iter(self) -> Self::IntoIter {
102        self.0.iter_mut()
103    }
104}
105
106impl<BackendT, ContextT, ErrorT, IdT, DataT> FromIterator<Node<BackendT, ContextT, ErrorT, IdT, DataT>>
107    for NodeList<BackendT, ContextT, ErrorT, IdT, DataT>
108{
109    fn from_iter<IteratorT>(iterator: IteratorT) -> Self
110    where
111        IteratorT: IntoIterator<Item = Node<BackendT, ContextT, ErrorT, IdT, DataT>>,
112    {
113        Self(Vec::from_iter(iterator))
114    }
115}
116
117impl<BackendT, ContextT, ErrorT, IdT, DataT> From<Vec<Node<BackendT, ContextT, ErrorT, IdT, DataT>>>
118    for NodeList<BackendT, ContextT, ErrorT, IdT, DataT>
119{
120    fn from(vector: Vec<Node<BackendT, ContextT, ErrorT, IdT, DataT>>) -> Self {
121        Self(vector)
122    }
123}