1
+ use crate :: inner_node:: StorageNode ;
1
2
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
+ } ;
4
7
use std:: collections:: BTreeMap ;
5
- use crate :: inner_node:: { ChildLink , StorageNode } ;
6
-
7
8
8
9
type NodeID = usize ;
9
10
10
11
/// A node in the syntax tree.
11
12
#[ derive( Copy , Clone , Debug ) ]
12
13
pub struct SyntaxNode < M > {
14
+ /// A node in the syntax tree.
13
15
pub node : NodeID ,
16
+ /// A node in the syntax tree.
14
17
pub data : M ,
15
18
}
16
19
20
+ /// A node in the syntax tree.
17
21
#[ derive( Clone ) ]
18
22
pub struct SyntaxTree < M > {
19
23
store : Vec < StorageNode < M > > ,
@@ -36,17 +40,15 @@ impl<M> SyntaxTree<M> {
36
40
/// Create a new syntax tree with a given capacity.
37
41
pub fn new ( capacity : usize ) -> Self {
38
42
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 }
44
44
}
45
+ /// Create the root node, note this will erase all data in the tree
45
46
pub fn create_root ( & mut self , data : M ) -> NodeID {
46
47
self . store . clear ( ) ;
47
48
self . store . push ( StorageNode :: new ( data) ) ;
48
49
0
49
50
}
51
+ /// Create a place holder node but not fill data
50
52
pub fn create_place_holder ( & mut self ) -> NodeID {
51
53
match self . first_empty {
52
54
// find a hole
@@ -67,12 +69,12 @@ impl<M> SyntaxTree<M> {
67
69
}
68
70
}
69
71
}
70
-
72
+ /// Insert a node with data
71
73
unsafe fn insert_node ( & mut self , id : usize , data : M ) {
72
74
let node = self . store . get_unchecked_mut ( id) ;
73
75
* node = StorageNode :: new ( data) ;
74
76
}
75
-
77
+ /// Delete a node
76
78
pub fn update_node ( & mut self , node : SyntaxNode < M > ) {
77
79
match self . store . get_mut ( node. node ) {
78
80
Some ( s) => {
@@ -85,6 +87,7 @@ impl<M> SyntaxTree<M> {
85
87
}
86
88
}
87
89
}
90
+ /// Get a node
88
91
pub fn create_node ( & mut self , data : M ) -> NodeID {
89
92
let node = self . create_place_holder ( ) ;
90
93
self . update_node ( SyntaxNode { node, data } ) ;
@@ -99,18 +102,47 @@ impl<M> SyntaxTree<M> {
99
102
}
100
103
self . delete_node ( node)
101
104
}
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 {
104
116
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 ( ) ) ?;
109
119
}
120
+ None => { }
110
121
}
122
+ unsafe {
123
+ self . make_parent_unchecked ( parent, child) ;
124
+ }
125
+ Some ( ( ) )
111
126
}
112
127
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
+ }
114
146
}
115
147
116
148
#[ test]
@@ -121,7 +153,9 @@ fn test() {
121
153
let child1 = tree. create_place_holder ( ) ;
122
154
let child2 = tree. create_place_holder ( ) ;
123
155
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) ;
126
160
println ! ( "{:#?}" , tree) ;
127
161
}
0 commit comments