1use super::{super::backend::*, iterator::*, kind::*, node::*, path::*, representation::*};
2
3use std::{ptr, slice::*};
4
5pub 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 pub fn iter(&self, only_expanded: bool) -> NodeIterator<'_, BackendT, ContextT, ErrorT, IdT, DataT> {
17 NodeIterator::new(self, only_expanded)
18 }
19
20 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 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 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 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 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}