Skip to content

Commit 943d118

Browse files
committed
Unlink data in syntax tree
1 parent c91dd4f commit 943d118

File tree

2 files changed

+78
-56
lines changed

2 files changed

+78
-56
lines changed

projects/syntax-tree/src/inner_node/mod.rs

+24-36
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::num::NonZeroUsize;
55
pub struct StorageNode<M> {
66
pub(crate) parent: Option<usize>,
77
pub(crate) previous: Option<NonZeroUsize>,
8+
// if data is some, this means next sa
89
pub(crate) next: Option<NonZeroUsize>,
910
pub(crate) children: Option<ChildLink>,
1011
pub(crate) data: Option<M>,
@@ -16,42 +17,24 @@ pub struct ChildLink {
1617
last: NonZeroUsize,
1718
}
1819

19-
2020
impl<M> StorageNode<M> {
2121
pub fn empty() -> Self {
22-
Self {
23-
parent: None,
24-
previous: None,
25-
next: None,
26-
children: None,
27-
data: None,
28-
}
22+
Self { parent: None, previous: None, next: None, children: None, data: None }
2923
}
3024
pub fn new(data: M) -> Self {
31-
Self {
32-
parent: None,
33-
previous: None,
34-
next: None,
35-
children: None,
36-
data: Some(data),
37-
}
25+
Self { parent: None, previous: None, next: None, children: None, data: Some(data) }
3826
}
39-
pub fn make_child(&mut self, new: usize) -> Option<NonZeroUsize> {
27+
pub fn append_child(&mut self, new: usize) -> Option<NonZeroUsize> {
4028
debug_assert_ne!(new, 0, "Root node can't be child");
41-
let new = unsafe {
42-
NonZeroUsize::new_unchecked(new)
43-
};
29+
let new = unsafe { NonZeroUsize::new_unchecked(new) };
4430
match &mut self.children {
4531
Some(s) => {
4632
let old = s.last;
4733
s.last = new;
4834
Some(old)
4935
}
5036
None => {
51-
self.children = Some(ChildLink {
52-
first: new,
53-
last: new,
54-
});
37+
self.children = Some(ChildLink { first: new, last: new });
5538
None
5639
}
5740
}
@@ -60,36 +43,41 @@ impl<M> StorageNode<M> {
6043

6144
impl ChildLink {
6245
pub unsafe fn new(first: usize) -> Self {
63-
Self {
64-
first: NonZeroUsize::new_unchecked(first),
65-
last: NonZeroUsize::new_unchecked(first),
66-
}
46+
Self { first: NonZeroUsize::new_unchecked(first), last: NonZeroUsize::new_unchecked(first) }
6747
}
6848
}
6949

7050
impl<M: Debug> Debug for StorageNode<M> {
7151
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
7252
let kind = if self.data.is_none() {
7353
"Placeholder"
74-
} else if self.children.is_none() {
54+
}
55+
else if self.children.is_none() {
7556
"Leaf"
76-
} else {
57+
}
58+
else {
7759
"Node"
7860
};
7961
let fmt = &mut f.debug_struct(kind);
8062
match &self.parent {
81-
Some(s) => {
82-
fmt.field("parent", s)
83-
}
84-
None => {
85-
fmt.field("parent", &Option::<()>::None)
86-
}
63+
Some(s) => fmt.field("parent", s),
64+
None => fmt.field("parent", &Option::<()>::None),
8765
};
8866

67+
if let Some(s) = &self.previous {
68+
fmt.field("previous", s);
69+
}
70+
if let Some(s) = &self.next {
71+
fmt.field("next", s);
72+
}
73+
if let Some(s) = &self.children {
74+
fmt.field("head_child", &s.first);
75+
fmt.field("tail_child", &s.last);
76+
}
8977
if let Some(s) = &self.data {
9078
fmt.field("data", s);
9179
}
9280

9381
fmt.finish()
9482
}
95-
}
83+
}

projects/syntax-tree/src/syntax_tree/mod.rs

+54-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
use crate::inner_node::StorageNode;
12
use alloc::vec::Vec;
2-
use core::fmt::{Debug, Formatter};
3-
use core::num::NonZeroUsize;
3+
use core::{
4+
fmt::{Debug, Formatter},
5+
num::NonZeroUsize,
6+
};
47
use std::collections::BTreeMap;
5-
use crate::inner_node::{ChildLink, StorageNode};
6-
78

89
type NodeID = usize;
910

1011
/// A node in the syntax tree.
1112
#[derive(Copy, Clone, Debug)]
1213
pub struct SyntaxNode<M> {
14+
/// A node in the syntax tree.
1315
pub node: NodeID,
16+
/// A node in the syntax tree.
1417
pub data: M,
1518
}
1619

20+
/// A node in the syntax tree.
1721
#[derive(Clone)]
1822
pub struct SyntaxTree<M> {
1923
store: Vec<StorageNode<M>>,
@@ -36,17 +40,15 @@ impl<M> SyntaxTree<M> {
3640
/// Create a new syntax tree with a given capacity.
3741
pub fn new(capacity: usize) -> Self {
3842
let store = Vec::with_capacity(capacity);
39-
Self {
40-
store,
41-
first_empty: None,
42-
last_empty: None,
43-
}
43+
Self { store, first_empty: None, last_empty: None }
4444
}
45+
/// Create the root node, note this will erase all data in the tree
4546
pub fn create_root(&mut self, data: M) -> NodeID {
4647
self.store.clear();
4748
self.store.push(StorageNode::new(data));
4849
0
4950
}
51+
/// Create a place holder node but not fill data
5052
pub fn create_place_holder(&mut self) -> NodeID {
5153
match self.first_empty {
5254
// find a hole
@@ -67,12 +69,12 @@ impl<M> SyntaxTree<M> {
6769
}
6870
}
6971
}
70-
72+
/// Insert a node with data
7173
unsafe fn insert_node(&mut self, id: usize, data: M) {
7274
let node = self.store.get_unchecked_mut(id);
7375
*node = StorageNode::new(data);
7476
}
75-
77+
/// Delete a node
7678
pub fn update_node(&mut self, node: SyntaxNode<M>) {
7779
match self.store.get_mut(node.node) {
7880
Some(s) => {
@@ -85,6 +87,7 @@ impl<M> SyntaxTree<M> {
8587
}
8688
}
8789
}
90+
/// Get a node
8891
pub fn create_node(&mut self, data: M) -> NodeID {
8992
let node = self.create_place_holder();
9093
self.update_node(SyntaxNode { node, data });
@@ -99,18 +102,47 @@ impl<M> SyntaxTree<M> {
99102
}
100103
self.delete_node(node)
101104
}
102-
pub unsafe fn make_parent_unchecked(&mut self, parent: NodeID, child: NodeID) {
103-
match self.store.get_mut(parent) {
105+
106+
unsafe fn unlink(&mut self, node: &StorageNode<M>) {
107+
todo!()
108+
}
109+
110+
pub fn make_parent(&mut self, parent: NodeID, child: NodeID) -> Option<()> {
111+
// check this node non null
112+
let _this_node = self.store.get(child)?;
113+
// check parent non null
114+
let parent_node = self.store.get(parent)?;
115+
match parent_node.previous {
104116
Some(s) => {
105-
todo!()
106-
}
107-
None => {
108-
panic!("Cannot make node {parent} as parent of {child}")
117+
// check previous non null if exists
118+
self.store.get(s.get())?;
109119
}
120+
None => {}
110121
}
122+
unsafe {
123+
self.make_parent_unchecked(parent, child);
124+
}
125+
Some(())
111126
}
112127

113-
//
128+
/// make a node as the parent of another node
129+
pub unsafe fn make_parent_unchecked(&mut self, parent: NodeID, child: NodeID) {
130+
let this_id = NonZeroUsize::new_unchecked(child);
131+
let this_node = {
132+
// erase lifetime, for double &mut borrow below
133+
let node = self.store.get_unchecked_mut(child);
134+
&mut *(node as *mut StorageNode<M>)
135+
};
136+
this_node.parent = Some(parent);
137+
let parent_node = self.store.get_unchecked_mut(parent);
138+
let prev_id = match parent_node.append_child(child) {
139+
Some(s) => s,
140+
None => return,
141+
};
142+
let prev_node = self.store.get_unchecked_mut(prev_id.get());
143+
prev_node.next = Some(this_id);
144+
this_node.previous = Some(prev_id);
145+
}
114146
}
115147

116148
#[test]
@@ -121,7 +153,9 @@ fn test() {
121153
let child1 = tree.create_place_holder();
122154
let child2 = tree.create_place_holder();
123155
tree.update_node(SyntaxNode { node: child2, data: "Root-Child2" });
124-
let child3 = tree.create_node("Root-Child3");
125-
// let a = tree.create_place_holder();
156+
let child21 = tree.create_node("Root-Child2-Child1");
157+
tree.make_parent(root, child1);
158+
tree.make_parent(root, child2);
159+
tree.make_parent(child2, child21);
126160
println!("{:#?}", tree);
127161
}

0 commit comments

Comments
 (0)