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
//wtf was I thinking here
/*
use std::collections::HashMap;
pub struct Trie {
root: TrieNode,
}
impl Trie {
pub fn new() -> Self {
Self { root: TrieNode::new() }
}
pub fn insert(&mut self, slice: &str) {
self.root.insert(slice);
}
pub fn search(&self, slice: &str) -> bool {
self.root.search(slice)
}
}
//performance wise, this data structure seems kinda retarded honestly
struct TrieNode {
children: HashMap<char, TrieNode>,
is_tail: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode { children: HashMap::new(), is_tail: false }
}
fn insert(&mut self, slice: &str) {
let ch = slice.as_bytes()[0] as char;
let node = match self.children.get_mut(&ch) {
Some(node) => node,
//is this right?
None => &mut self.children.insert(ch, TrieNode::new()).unwrap(),
};
if slice.len() == 1 {
node.is_tail = true;
} else {
node.insert(&slice[1..]);
}
}
fn search(&self, slice: &str) -> bool {
let ch = slice.as_bytes()[0] as char;
match self.children.get(&ch) {
Some(node) => {
if slice.len() == 1 {
return node.is_tail
} else {
node.search(&slice[1..])
}
},
None => false,
}
}
}
*/