Skip to main content

cursive_tree/model/
list.rs

1use super::{super::backend::*, depth::*, iterator::*, kind::*, node::*, path::*, representation::*};
2
3use std::{ptr, slice::*};
4
5//
6// NodeList
7//
8
9/// Tree node list.
10pub struct NodeList<BackendT>(pub Vec<Node<BackendT>>)
11where
12    BackendT: TreeBackend;
13
14impl<BackendT> NodeList<BackendT>
15where
16    BackendT: TreeBackend,
17{
18    /// Iterate nodes in visual order from top to bottom.
19    ///
20    /// When only_expanded is true will skip the children of collapsed branches.
21    pub fn iter(&self, only_expanded: bool) -> NodeIterator<'_, BackendT> {
22        NodeIterator::new(self, only_expanded)
23    }
24
25    /// Get node at path.
26    pub fn at_path(&self, mut path: NodePath) -> Option<&Node<BackendT>> {
27        path.pop_front().and_then(|index| self.0.get(index)).and_then(|node| node.at_path(path))
28    }
29
30    /// Get node at path.
31    pub fn at_path_mut(&mut self, mut path: NodePath) -> Option<&mut Node<BackendT>> {
32        path.pop_front().and_then(|index| self.0.get_mut(index)).and_then(|node| node.at_path_mut(path))
33    }
34
35    /// Fill path to node.
36    ///
37    /// Returns true if found.
38    pub fn fill_path(&self, path: &mut NodePath, node: &Node<BackendT>) -> bool {
39        if self.0.is_empty() {
40            return false;
41        }
42
43        for (index, node_) in self.0.iter().enumerate() {
44            path.push_back(index);
45            if ptr::eq(node, node_) {
46                return true;
47            } else if node_.fill_path(path, node) {
48                return true;
49            } else {
50                path.pop_back();
51            }
52        }
53
54        false
55    }
56
57    /// Add a node.
58    pub fn add(&mut self, depth: usize, kind: NodeKind, id: BackendT::ID, representation: Representation) {
59        self.0.push(Node::new(depth, kind, id, representation));
60    }
61
62    /// Insert a node.
63    pub fn insert(
64        &mut self,
65        index: usize,
66        depth: usize,
67        kind: NodeKind,
68        id: BackendT::ID,
69        representation: Representation,
70    ) {
71        self.0.insert(index, Node::new(depth, kind, id, representation));
72    }
73
74    /// Expand branch nodes.
75    ///
76    /// If depth is [None] will expand all depths.
77    ///
78    /// If depth is 0 will do nothing.
79    ///
80    /// Note that this *will* populate expanded nodes from the backend.
81    pub fn expand(&mut self, mut depth: Option<usize>, context: BackendT::Context) -> Result<(), BackendT::Error>
82    where
83        BackendT::Context: Clone,
84    {
85        if depth.is_zero() {
86            return Ok(());
87        }
88
89        depth.decrease();
90        if !depth.is_zero() {
91            for node in self {
92                node.expand(depth, context.clone())?;
93            }
94        }
95
96        Ok(())
97    }
98
99    /// Collapse branch nodes.
100    ///
101    /// If depth is [None] will collapse all depths.
102    ///
103    /// If depth is 0 will do nothing.
104    pub fn collapse(&mut self, mut depth: Option<usize>) {
105        if depth.is_zero() {
106            return;
107        }
108
109        depth.decrease();
110        if !depth.is_zero() {
111            for node in self {
112                node.collapse(depth);
113            }
114        }
115    }
116}
117
118impl<BackendT> Default for NodeList<BackendT>
119where
120    BackendT: TreeBackend,
121{
122    fn default() -> Self {
123        Self(Default::default())
124    }
125}
126
127impl<'this, BackendT> IntoIterator for &'this NodeList<BackendT>
128where
129    BackendT: TreeBackend,
130{
131    type Item = &'this Node<BackendT>;
132    type IntoIter = Iter<'this, Node<BackendT>>;
133
134    fn into_iter(self) -> Self::IntoIter {
135        self.0.iter()
136    }
137}
138
139impl<'this, BackendT> IntoIterator for &'this mut NodeList<BackendT>
140where
141    BackendT: TreeBackend,
142{
143    type Item = &'this mut Node<BackendT>;
144    type IntoIter = IterMut<'this, Node<BackendT>>;
145
146    fn into_iter(self) -> Self::IntoIter {
147        self.0.iter_mut()
148    }
149}
150
151impl<BackendT> FromIterator<Node<BackendT>> for NodeList<BackendT>
152where
153    BackendT: TreeBackend,
154{
155    fn from_iter<IteratorT>(iterator: IteratorT) -> Self
156    where
157        IteratorT: IntoIterator<Item = Node<BackendT>>,
158    {
159        Self(Vec::from_iter(iterator))
160    }
161}
162
163impl<BackendT> From<Vec<Node<BackendT>>> for NodeList<BackendT>
164where
165    BackendT: TreeBackend,
166{
167    fn from(vector: Vec<Node<BackendT>>) -> Self {
168        Self(vector)
169    }
170}