org_rust_parser/
node_pool.rs1use std::fmt::{Debug, Display};
2use std::ops::{Index, IndexMut};
3
4use crate::types::{Expr, Node};
5
6#[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Ord, Eq)]
7pub struct NodeID(u32);
12
13pub(crate) fn make_node_id(id: u32) -> NodeID {
15 NodeID(id)
16}
17
18impl Display for NodeID {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 f.write_fmt(format_args!("{}", self.0))
21 }
22}
23
24impl std::fmt::Debug for NodeID {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 f.write_fmt(format_args!("{}", self.0))
27 }
28}
29
30#[derive(Debug)]
49pub struct NodePool<'a> {
50 pub inner_vec: Vec<Node<'a>>,
51 pub counter: u32,
52}
53
54impl<'a> NodePool<'a> {
55 pub(crate) fn new() -> Self {
56 Self {
57 inner_vec: Vec::new(),
58 counter: 0,
60 }
61 }
62
63 pub(crate) fn alloc<T>(
64 &mut self,
65 obj: T,
66 start: usize,
67 end: usize,
68 parent: Option<NodeID>,
69 ) -> NodeID
70 where
71 Expr<'a>: From<T>,
72 {
73 let prev_id = self.counter;
74 self.inner_vec.push(Node::new(obj, start, end, parent));
75 self.counter += 1;
76 NodeID(prev_id)
77 }
78
79 pub(crate) fn alloc_with_id<T>(
90 &mut self,
91 obj: T,
92 start: usize,
93 end: usize,
94 parent: Option<NodeID>,
95 target_id: NodeID,
96 ) -> NodeID
97 where
98 Expr<'a>: From<T>,
99 {
100 self.inner_vec[target_id.0 as usize] = Node::new(obj, start, end, parent);
101
102 target_id
103 }
104
105 pub fn get(&self, id: NodeID) -> Option<&'a Node> {
106 self.inner_vec.get(id.0 as usize)
107 }
108
109 pub fn get_mut(&mut self, id: NodeID) -> Option<&'a mut Node> {
110 self.inner_vec.get_mut(id.0 as usize)
111 }
112
113 pub(crate) fn reserve_id(&mut self) -> NodeID {
118 self.inner_vec.push(Node::default());
119 let old_counter = self.counter;
120 self.counter += 1;
121 NodeID(old_counter)
122 }
123
124 pub fn iter(&self) -> impl Iterator<Item = &Node<'a>> + DoubleEndedIterator<Item = &Node<'a>> {
125 self.inner_vec.iter()
126 }
127
128 pub fn iter_mut(
129 &mut self,
130 ) -> impl Iterator<Item = &mut Node<'a>> + DoubleEndedIterator<Item = &mut Node<'a>> {
131 self.inner_vec.iter_mut()
132 }
133
134 pub fn print_tree(&self) {
136 self.inner_vec[0].print_tree(self);
137 }
138
139 pub fn root_id(&self) -> NodeID {
141 NodeID(0)
142 }
143
144 pub fn delete_node(&mut self, index_id: NodeID) {
149 let par_id = self[index_id].parent.unwrap();
150 let par_node = &mut self[par_id];
151
152 let children = par_node.obj.children_mut().unwrap();
153 let index = children.iter().position(|&x| x == index_id).unwrap();
154 children.remove(index);
155 }
156}
157
158impl<'a> Index<NodeID> for NodePool<'a> {
159 type Output = Node<'a>;
160
161 fn index(&self, index: NodeID) -> &Self::Output {
162 &self.inner_vec[index.0 as usize]
163 }
164}
165
166impl<'a> IndexMut<NodeID> for NodePool<'a> {
167 fn index_mut(&mut self, index: NodeID) -> &mut Self::Output {
168 &mut self.inner_vec[index.0 as usize]
169 }
170}