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>(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
75impl<BackendT> Default for NodeList<BackendT>
76where
77    BackendT: TreeBackend,
78{
79    fn default() -> Self {
80        Self(Default::default())
81    }
82}
83
84impl<'this, BackendT> IntoIterator for &'this NodeList<BackendT>
85where
86    BackendT: TreeBackend,
87{
88    type Item = &'this Node<BackendT>;
89    type IntoIter = Iter<'this, Node<BackendT>>;
90
91    fn into_iter(self) -> Self::IntoIter {
92        self.0.iter()
93    }
94}
95
96impl<'this, BackendT> IntoIterator for &'this mut NodeList<BackendT>
97where
98    BackendT: TreeBackend,
99{
100    type Item = &'this mut Node<BackendT>;
101    type IntoIter = IterMut<'this, Node<BackendT>>;
102
103    fn into_iter(self) -> Self::IntoIter {
104        self.0.iter_mut()
105    }
106}
107
108impl<BackendT> FromIterator<Node<BackendT>> for NodeList<BackendT>
109where
110    BackendT: TreeBackend,
111{
112    fn from_iter<IteratorT>(iterator: IteratorT) -> Self
113    where
114        IteratorT: IntoIterator<Item = Node<BackendT>>,
115    {
116        Self(Vec::from_iter(iterator))
117    }
118}
119
120impl<BackendT> From<Vec<Node<BackendT>>> for NodeList<BackendT>
121where
122    BackendT: TreeBackend,
123{
124    fn from(vector: Vec<Node<BackendT>>) -> Self {
125        Self(vector)
126    }
127}