-
Notifications
You must be signed in to change notification settings - Fork 18
/
radix_tree.rs
151 lines (125 loc) · 4.42 KB
/
radix_tree.rs
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
use crate::vwmap::NamespaceDescriptor;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct NamespaceDescriptorWithHash {
pub(crate) descriptor: NamespaceDescriptor,
pub(crate) hash_seed: u32,
}
impl NamespaceDescriptorWithHash {
pub(crate) fn new(descriptor: NamespaceDescriptor, hash_seed: u32) -> Self {
Self {
descriptor,
hash_seed,
}
}
}
#[derive(Clone, Debug)]
struct RadixTreeNode {
children: Vec<Option<RadixTreeNode>>,
value: Option<NamespaceDescriptorWithHash>,
}
impl Default for RadixTreeNode {
fn default() -> Self {
Self {
children: vec![None; 256],
value: None,
}
}
}
#[derive(Clone, Default, Debug)]
pub struct RadixTree {
root: RadixTreeNode,
}
impl RadixTree {
pub(crate) fn insert(&mut self, key: &[u8], value: NamespaceDescriptorWithHash) {
let mut node = &mut self.root;
for &byte in key {
let child = &mut node.children[byte as usize];
node = child.get_or_insert_with(RadixTreeNode::default);
}
node.value = Some(value);
}
pub(crate) fn get(&self, key: &[u8]) -> Option<&NamespaceDescriptorWithHash> {
let mut node = &self.root;
for &byte in key {
let maybe_child = &node.children[byte as usize];
if let Some(child) = maybe_child {
node = child;
} else {
return None.as_ref();
}
}
node.value.as_ref()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vwmap::{NamespaceFormat, NamespaceType};
#[test]
fn test_insert_and_get() {
let mut tree = RadixTree::default();
let namespace_descriptor_with_hash_1 = NamespaceDescriptorWithHash {
descriptor: NamespaceDescriptor {
namespace_index: 0,
namespace_type: NamespaceType::Primitive,
namespace_format: NamespaceFormat::Categorical,
},
hash_seed: 1,
};
let namespace_descriptor_with_hash_2 = NamespaceDescriptorWithHash {
descriptor: NamespaceDescriptor {
namespace_index: 10,
namespace_type: NamespaceType::Primitive,
namespace_format: NamespaceFormat::Categorical,
},
hash_seed: 2,
};
let namespace_descriptor_with_hash_3 = NamespaceDescriptorWithHash {
descriptor: NamespaceDescriptor {
namespace_index: 20,
namespace_type: NamespaceType::Primitive,
namespace_format: NamespaceFormat::Categorical,
},
hash_seed: 3,
};
tree.insert(b"A", namespace_descriptor_with_hash_1);
tree.insert(b"AB", namespace_descriptor_with_hash_2);
tree.insert(b"ABC", namespace_descriptor_with_hash_3);
assert_eq!(tree.get(b"A"), Some(&namespace_descriptor_with_hash_1));
assert_eq!(tree.get(b"AB"), Some(&namespace_descriptor_with_hash_2));
assert_eq!(tree.get(b"ABC"), Some(&namespace_descriptor_with_hash_3));
assert_eq!(tree.get(b"ABCD"), None);
}
#[test]
fn test_insert_and_get_empty_key() {
let mut tree = RadixTree::default();
let namespace_descriptor_with_hash = NamespaceDescriptorWithHash {
descriptor: NamespaceDescriptor {
namespace_index: 0,
namespace_type: NamespaceType::Primitive,
namespace_format: NamespaceFormat::Categorical,
},
hash_seed: 1,
};
tree.insert(b"", namespace_descriptor_with_hash);
assert_eq!(tree.get(b""), Some(&namespace_descriptor_with_hash));
assert_eq!(tree.get(b"A"), None);
}
#[test]
fn test_insert_and_get_long_key() {
let mut tree = RadixTree::default();
let namespace_descriptor_with_hash = NamespaceDescriptorWithHash {
descriptor: NamespaceDescriptor {
namespace_index: 0,
namespace_type: NamespaceType::Primitive,
namespace_format: NamespaceFormat::Categorical,
},
hash_seed: 1,
};
tree.insert(b"AB", namespace_descriptor_with_hash);
assert_eq!(tree.get(b"AB"), Some(&namespace_descriptor_with_hash));
assert_eq!(tree.get(b"A"), None);
assert_eq!(tree.get(b"B"), None);
assert_eq!(tree.get(b"ABC"), None);
}
}