Skip to content

Commit c91dd4f

Browse files
committed
Reuse next node info
1 parent 5f14f99 commit c91dd4f

File tree

4 files changed

+124
-155
lines changed

4 files changed

+124
-155
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use core::fmt::{Debug, Formatter};
2+
use std::num::NonZeroUsize;
3+
4+
#[derive(Clone)]
5+
pub struct StorageNode<M> {
6+
pub(crate) parent: Option<usize>,
7+
pub(crate) previous: Option<NonZeroUsize>,
8+
pub(crate) next: Option<NonZeroUsize>,
9+
pub(crate) children: Option<ChildLink>,
10+
pub(crate) data: Option<M>,
11+
}
12+
13+
#[derive(Copy, Clone)]
14+
pub struct ChildLink {
15+
first: NonZeroUsize,
16+
last: NonZeroUsize,
17+
}
18+
19+
20+
impl<M> StorageNode<M> {
21+
pub fn empty() -> Self {
22+
Self {
23+
parent: None,
24+
previous: None,
25+
next: None,
26+
children: None,
27+
data: None,
28+
}
29+
}
30+
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+
}
38+
}
39+
pub fn make_child(&mut self, new: usize) -> Option<NonZeroUsize> {
40+
debug_assert_ne!(new, 0, "Root node can't be child");
41+
let new = unsafe {
42+
NonZeroUsize::new_unchecked(new)
43+
};
44+
match &mut self.children {
45+
Some(s) => {
46+
let old = s.last;
47+
s.last = new;
48+
Some(old)
49+
}
50+
None => {
51+
self.children = Some(ChildLink {
52+
first: new,
53+
last: new,
54+
});
55+
None
56+
}
57+
}
58+
}
59+
}
60+
61+
impl ChildLink {
62+
pub unsafe fn new(first: usize) -> Self {
63+
Self {
64+
first: NonZeroUsize::new_unchecked(first),
65+
last: NonZeroUsize::new_unchecked(first),
66+
}
67+
}
68+
}
69+
70+
impl<M: Debug> Debug for StorageNode<M> {
71+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
72+
let kind = if self.data.is_none() {
73+
"Placeholder"
74+
} else if self.children.is_none() {
75+
"Leaf"
76+
} else {
77+
"Node"
78+
};
79+
let fmt = &mut f.debug_struct(kind);
80+
match &self.parent {
81+
Some(s) => {
82+
fmt.field("parent", s)
83+
}
84+
None => {
85+
fmt.field("parent", &Option::<()>::None)
86+
}
87+
};
88+
89+
if let Some(s) = &self.data {
90+
fmt.field("data", s);
91+
}
92+
93+
fmt.finish()
94+
}
95+
}

projects/syntax-tree/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
#![doc(html_logo_url = "https://raw.githubusercontent.com/oovm/shape-rs/dev/projects/images/Trapezohedron.svg")]
66
#![doc(html_favicon_url = "https://raw.githubusercontent.com/oovm/shape-rs/dev/projects/images/Trapezohedron.svg")]
77

8-
extern crate alloc;
9-
extern crate core;
108
extern crate core;
9+
extern crate alloc;
1110

1211
mod errors;
1312
mod syntax_tree;
14-
mod syntax_node;
13+
mod inner_node;
14+
15+
pub use syntax_tree::{SyntaxTree, SyntaxNode};

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

Lines changed: 0 additions & 95 deletions
This file was deleted.

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

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ use alloc::vec::Vec;
22
use core::fmt::{Debug, Formatter};
33
use core::num::NonZeroUsize;
44
use std::collections::BTreeMap;
5+
use crate::inner_node::{ChildLink, StorageNode};
56

67

78
type NodeID = usize;
89

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+
}
916

17+
#[derive(Clone)]
1018
pub struct SyntaxTree<M> {
1119
store: Vec<StorageNode<M>>,
1220
first_empty: Option<NonZeroUsize>,
@@ -36,10 +44,7 @@ impl<M> SyntaxTree<M> {
3644
}
3745
pub fn create_root(&mut self, data: M) -> NodeID {
3846
self.store.clear();
39-
self.store.push(StorageNode::Root {
40-
children: None,
41-
data,
42-
});
47+
self.store.push(StorageNode::new(data));
4348
0
4449
}
4550
pub fn create_place_holder(&mut self) -> NodeID {
@@ -49,52 +54,35 @@ impl<M> SyntaxTree<M> {
4954
let new_id = s.get();
5055
debug_assert!(self.store.get(new_id).is_some());
5156
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;
6160
}
6261
new_id
6362
}
6463
// no remaining holes
6564
None => {
66-
self.store.push(StorageNode::Empty { is_placeholder: true, next_empty: None });
65+
self.store.push(StorageNode::empty());
6766
self.store.len().saturating_sub(1)
6867
}
6968
}
7069
}
7170

7271
unsafe fn insert_node(&mut self, id: usize, data: M) {
7372
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);
8174
}
8275

8376
pub fn update_node(&mut self, node: SyntaxNode<M>) {
8477
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);
9080
}
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);
9584
}
9685
}
97-
None => {}
9886
}
9987
}
10088
pub fn create_node(&mut self, data: M) -> NodeID {
@@ -103,42 +91,22 @@ impl<M> SyntaxTree<M> {
10391
node
10492
}
10593

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-
11094
/// remove a single node
11195
pub fn delete_node(&mut self, node: NodeID) {
11296
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) => {}
11698
None => {}
11799
}
118100
self.delete_node(node)
119101
}
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) {
122103
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!()
136106
}
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}")
140109
}
141-
None => {}
142110
}
143111
}
144112

0 commit comments

Comments
 (0)