1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use std::{
io::{self, Write},
marker::PhantomData,
mem::size_of,
ops::Deref,
};
pub type TreeSize = u64;
const TREE_SIZE_SIZE: usize = size_of::<TreeSize>();
pub trait Node {
type Value;
fn write_value<W>(writer: &mut W, value: &Self::Value) -> io::Result<usize>
where
W: Write;
fn read_value(bytes: &[u8]) -> (usize, Self::Value);
}
pub struct TreeBuilder<N, W> {
_node_type: PhantomData<N>,
open_node_sizes: Vec<TreeSize>,
writer: W,
}
impl<N, W> TreeBuilder<N, W> {
pub fn new(writer: W) -> Self {
Self {
_node_type: PhantomData,
open_node_sizes: Vec::new(),
writer,
}
}
pub fn write_node(&mut self, value: &N::Value, num_children: usize) -> io::Result<()>
where
N: Node,
W: Write,
{
let size_value: TreeSize = N::write_value(&mut self.writer, value)? as TreeSize;
let size_children: TreeSize = self
.open_node_sizes
.drain((self.open_node_sizes.len() - num_children)..)
.sum();
let total_size = size_value + size_children;
self.writer.write_all(&total_size.to_le_bytes())?;
self.open_node_sizes
.push(total_size + TREE_SIZE_SIZE as TreeSize);
Ok(())
}
}
pub struct TreeVec<N> {
_node_type: PhantomData<N>,
bytes: Vec<u8>,
}
impl<N> TreeVec<N> {
pub fn new(bytes: Vec<u8>) -> TreeVec<N> {
TreeVec {
_node_type: PhantomData,
bytes,
}
}
pub fn as_tree_slice(&self) -> &TreeSlice<N> {
TreeSlice::from_slice(&self.bytes)
}
}
impl<N> Deref for TreeVec<N> {
type Target = TreeSlice<N>;
fn deref(&self) -> &Self::Target {
self.as_tree_slice()
}
}
pub struct TreeSlice<N> {
_node_type: PhantomData<N>,
bytes: [u8],
}
impl<N> TreeSlice<N> {
pub fn from_slice(slice: &[u8]) -> &Self {
let ptr: *const [u8] = slice;
unsafe { &*(ptr as *const TreeSlice<N>) }
}
pub fn read_node(&self) -> (N::Value, Branches<'_, N>)
where
N: Node,
{
let total_size = self.bytes.len();
let (size_value, value) = N::read_value(&self.bytes[..(total_size - TREE_SIZE_SIZE)]);
let branches = Branches {
_node_type: PhantomData,
bytes: &self.bytes[..(total_size - TREE_SIZE_SIZE - size_value)],
};
(value, branches)
}
}
pub struct Branches<'a, N> {
_node_type: PhantomData<N>,
bytes: &'a [u8],
}
impl<'a, N: 'a> Iterator for Branches<'a, N> {
type Item = &'a TreeSlice<N>;
fn next(&mut self) -> Option<Self::Item> {
if self.bytes.is_empty() {
None
} else {
let total_size = self.bytes.len();
let tree_size_bytes: &[u8; TREE_SIZE_SIZE] = self.bytes
[(total_size - TREE_SIZE_SIZE)..]
.try_into()
.unwrap();
let tree_size = TreeSize::from_le_bytes(*tree_size_bytes) as usize;
let (remainder, tree_slice) =
self.bytes.split_at(total_size - tree_size - TREE_SIZE_SIZE);
let tree_slice = TreeSlice::from_slice(tree_slice);
self.bytes = remainder;
Some(tree_slice)
}
}
}
pub struct LeI32;
impl Node for LeI32 {
type Value = i32;
fn write_value<W>(writer: &mut W, value: &Self::Value) -> std::io::Result<usize>
where
W: Write,
{
let bytes = value.to_le_bytes();
writer.write_all(&bytes)?;
Ok(bytes.len())
}
fn read_value(bytes: &[u8]) -> (usize, i32) {
let total_len = bytes.len();
let last_four_bytes: &[u8; 4] = bytes[(total_len - 4)..].try_into().unwrap();
(4, i32::from_le_bytes(*last_four_bytes))
}
}
pub struct U8;
impl Node for U8 {
type Value = u8;
fn write_value<W>(writer: &mut W, value: &Self::Value) -> std::io::Result<usize>
where
W: Write,
{
let bytes = value.to_le_bytes();
writer.write_all(&bytes)?;
Ok(bytes.len())
}
fn read_value(bytes: &[u8]) -> (usize, u8) {
let total_len = bytes.len();
let last_four_bytes: &[u8; 1] = bytes[(total_len - 1)..].try_into().unwrap();
(1, u8::from_le_bytes(*last_four_bytes))
}
}