@@ -2,11 +2,19 @@ use alloc::vec::Vec;
2
2
use core:: fmt:: { Debug , Formatter } ;
3
3
use core:: num:: NonZeroUsize ;
4
4
use std:: collections:: BTreeMap ;
5
+ use crate :: inner_node:: { ChildLink , StorageNode } ;
5
6
6
7
7
8
type NodeID = usize ;
8
9
10
+ /// A node in the syntax tree.
11
+ #[ derive( Copy , Clone , Debug ) ]
12
+ pub struct SyntaxNode < M > {
13
+ pub node : NodeID ,
14
+ pub data : M ,
15
+ }
9
16
17
+ #[ derive( Clone ) ]
10
18
pub struct SyntaxTree < M > {
11
19
store : Vec < StorageNode < M > > ,
12
20
first_empty : Option < NonZeroUsize > ,
@@ -36,10 +44,7 @@ impl<M> SyntaxTree<M> {
36
44
}
37
45
pub fn create_root ( & mut self , data : M ) -> NodeID {
38
46
self . store . clear ( ) ;
39
- self . store . push ( StorageNode :: Root {
40
- children : None ,
41
- data,
42
- } ) ;
47
+ self . store . push ( StorageNode :: new ( data) ) ;
43
48
0
44
49
}
45
50
pub fn create_place_holder ( & mut self ) -> NodeID {
@@ -49,52 +54,35 @@ impl<M> SyntaxTree<M> {
49
54
let new_id = s. get ( ) ;
50
55
debug_assert ! ( self . store. get( new_id) . is_some( ) ) ;
51
56
unsafe {
52
- match self . store . get_unchecked_mut ( new_id) {
53
- StorageNode :: Empty { next_empty, is_placeholder } => {
54
- self . first_empty = * next_empty;
55
- * is_placeholder = true
56
- }
57
- _ => {
58
- unreachable ! ( "Node {s} is not empty" )
59
- }
60
- }
57
+ let hole = self . store . get_unchecked_mut ( new_id) ;
58
+ debug_assert ! ( hole. data. is_none( ) ) ;
59
+ // hole.next = s;
61
60
}
62
61
new_id
63
62
}
64
63
// no remaining holes
65
64
None => {
66
- self . store . push ( StorageNode :: Empty { is_placeholder : true , next_empty : None } ) ;
65
+ self . store . push ( StorageNode :: empty ( ) ) ;
67
66
self . store . len ( ) . saturating_sub ( 1 )
68
67
}
69
68
}
70
69
}
71
70
72
71
unsafe fn insert_node ( & mut self , id : usize , data : M ) {
73
72
let node = self . store . get_unchecked_mut ( id) ;
74
- * node = StorageNode :: Node {
75
- parent : None ,
76
- previous : None ,
77
- next : None ,
78
- children : None ,
79
- data,
80
- } ;
73
+ * node = StorageNode :: new ( data) ;
81
74
}
82
75
83
76
pub fn update_node ( & mut self , node : SyntaxNode < M > ) {
84
77
match self . store . get_mut ( node. node ) {
85
- Some ( StorageNode :: Root { data, .. } ) => {
86
- * data = node. data ;
87
- }
88
- Some ( StorageNode :: Node { data, .. } ) => {
89
- * data = node. data ;
78
+ Some ( s) => {
79
+ s. data = Some ( node. data ) ;
90
80
}
91
- Some ( StorageNode :: Empty { next_empty, is_placeholder : _ } ) => {
92
- self . first_empty = * next_empty;
93
- unsafe {
94
- self . insert_node ( node. node , node. data )
81
+ None => {
82
+ if cfg ! ( debug_assertions) {
83
+ panic ! ( "Node {} does not exist" , node. node) ;
95
84
}
96
85
}
97
- None => { }
98
86
}
99
87
}
100
88
pub fn create_node ( & mut self , data : M ) -> NodeID {
@@ -103,42 +91,22 @@ impl<M> SyntaxTree<M> {
103
91
node
104
92
}
105
93
106
- fn mark_empty ( & mut self , node : NodeID ) {
107
- self . store . insert ( node, StorageNode :: Empty { is_placeholder : false , next_empty : self . first_empty } )
108
- }
109
-
110
94
/// remove a single node
111
95
pub fn delete_node ( & mut self , node : NodeID ) {
112
96
match self . store . get_mut ( node) {
113
- Some ( StorageNode :: Root { children, data } ) => { }
114
- Some ( StorageNode :: Node { parent, previous, next, children, data } ) => { }
115
- Some ( StorageNode :: Empty { is_placeholder, next_empty } ) => { }
97
+ Some ( s) => { }
116
98
None => { }
117
99
}
118
100
self . delete_node ( node)
119
101
}
120
- pub fn make_parent ( & mut self , parent : NodeID , child : NodeID ) {
121
- debug_assert ! ( child != 0 , "Root node cannot be its own child" ) ;
102
+ pub unsafe fn make_parent_unchecked ( & mut self , parent : NodeID , child : NodeID ) {
122
103
match self . store . get_mut ( parent) {
123
- Some ( StorageNode :: Root { children, data } ) => {
124
- match children {
125
- Some ( s) => {
126
- s. last = unsafe {
127
- ChildLink :: new ( child)
128
- }
129
- }
130
- None => {
131
- unsafe {
132
- ChildLink :: new ( child)
133
- }
134
- }
135
- }
104
+ Some ( s) => {
105
+ todo ! ( )
136
106
}
137
- Some ( StorageNode :: Node { parent, previous, next, children, data } ) => { }
138
- Some ( StorageNode :: Empty { is_placeholder, .. } ) => {
139
- panic ! ( "Cannot make empty node {parent} as parent of {child}" )
107
+ None => {
108
+ panic ! ( "Cannot make node {parent} as parent of {child}" )
140
109
}
141
- None => { }
142
110
}
143
111
}
144
112
0 commit comments