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
pub struct NodeChildIterator<'a, N: NodeType, T: Tree<N>> {
pub index: usize,
pub key: Option<&'a N::Key>,
pub node: &'a T,
}
pub trait NodeType: Default {
type Key: std::ops::Index<usize>;
type Value;
}
pub trait Tree<N: NodeType>: Sized {
fn is_leaf(&self) -> bool;
fn is_empty(&self) -> bool;
fn num_children(&self) -> usize;
fn ith_child(&self, index: usize) -> Option<&Self>;
fn set_ith_child(&mut self, index: usize, child: &Self);
fn children(&self) -> NodeChildIterator<N, Self> {
NodeChildIterator {
index: 0,
key: None,
node: &self,
}
}
fn insert(&mut self, key: &N::Key, value: N::Value) -> Result< (), String>;
fn has_key(&self, key: &N::Key) -> bool;
fn value(&self) -> Option<&N::Value>;
fn value_length(&self) -> Option<usize>;
fn from_hash(h: Vec<u8>) -> Self;
fn new_empty() -> Self;
fn new_extension(ext: Vec<u8>, child: Self) -> Self;
fn new_branch() -> Self;
fn new_leaf(key: Vec<u8>, value: Vec<u8>) -> Self;
}
impl<'a, N: NodeType, T: Tree<N>> std::iter::Iterator for NodeChildIterator<'a, N, T> {
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.node.num_children() {
self.index += 1;
self.node.ith_child(self.index - 1)
} else {
None
}
}
}