aboutsummaryrefslogtreecommitdiffstats
path: root/src/trie.rs
blob: 87eeaae581375a494bcaa4112b0c54b908a0a2ff (plain) (blame)
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
use core::util::swap;

// XXX turn this into a radix trie, probably
struct Trie<T> {
    priv root: ~TrieNode<T>,
}

pub fn Trie<T> () -> Trie<T> {
    Trie { root: ~TrieNode() }
}

struct TrieNode<T> {
    priv value: Option<T>,
    priv children: [Option<~TrieNode<T>>, ..256],
}

fn TrieNode<T> () -> TrieNode<T> {
    TrieNode {
        value: None,
        // XXX can't just use [None, ..256] because of #5244
        children: [
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
            None, None, None, None, None, None, None, None,
        ],
    }
}

impl<T> Trie<T> {
    pub fn insert (&mut self, s: &str, v: T) {
        if s.len() == 0 {
            self.root.value = Some(v);
        }
        else {
            let bytes = str::as_bytes_slice(s);
            self.insert_vec(
                &mut self.root.children[bytes[0]],
                bytes.tail(),
                v
            );
        }
    }

    pub fn find (&self, s: &str) -> &'self Option<T> {
        let bytes = str::as_bytes_slice(s);
        let (prefix_length, node) = self.root.find_prefix_trie(bytes);

        if prefix_length == bytes.len() {
            &node.value
        }
        else {
            &None
        }
    }

    pub fn has_prefix (&self, s: &str) -> bool {
        let bytes = str::as_bytes_slice(s);
        let (prefix_length, node) = self.root.find_prefix_trie(bytes);
        if prefix_length == bytes.len() {
            node.children.any(|child| { child.is_some() })
        }
        else {
            false
        }
    }

    fn insert_vec (&self, loc: &mut Option<~TrieNode<T>>, bytes: &[u8], v: T) {
        let mut tmp = None;
        swap(&mut tmp, loc);

        let mut new = match tmp {
            Some(node) => node,
            None       => ~TrieNode(),
        };

        if bytes.len() == 0 {
            new.value = Some(v);
        }
        else {
            self.insert_vec(&mut new.children[bytes[0]], bytes.tail(), v);
        }

        *loc = Some(new);
    }
}

impl<T> TrieNode<T> {
    fn find_prefix_trie (&self, bytes: &[u8]) -> (uint, &'self TrieNode<T>) {
        if bytes.len() == 0 {
            (0u, self)
        }
        else {
            match self.children[bytes[0]] {
                Some(ref t) => {
                    let (prefix_length, node) = t.find_prefix_trie(
                        bytes.tail()
                    );
                    (prefix_length + 1, node)
                }
                None => {
                    (0u, self)
                }
            }
        }
    }
}

#[test]
fn test_trie1 () {
    let mut trie = Trie();

    trie.insert("foo", 1);
    trie.insert("bar", 2);
    trie.insert("baz", 3);

    check_not_exists(&trie, "");

    check_not_exists(&trie, "f");
    check_not_exists(&trie, "fo");
    check_exists(&trie, "foo", 1);
    check_not_exists(&trie, "foe");
    check_not_exists(&trie, "food");

    check_not_exists(&trie, "b");
    check_not_exists(&trie, "ba");
    check_exists(&trie, "bar", 2);
    check_exists(&trie, "baz", 3);
    check_not_exists(&trie, "bat");
    check_not_exists(&trie, "bart");
    check_not_exists(&trie, "barz");

    check_not_exists(&trie, "quux");

    check_has_prefix(&trie, "");

    check_has_prefix(&trie, "f");
    check_has_prefix(&trie, "b");
    check_not_has_prefix(&trie, "q");

    check_has_prefix(&trie, "fo");
    check_has_prefix(&trie, "ba");
    check_not_has_prefix(&trie, "fa");
    check_not_has_prefix(&trie, "bo");
    check_not_has_prefix(&trie, "qu");

    check_not_has_prefix(&trie, "foo");
    check_not_has_prefix(&trie, "bar");
    check_not_has_prefix(&trie, "baz");
    check_not_has_prefix(&trie, "for");
    check_not_has_prefix(&trie, "bao");
    check_not_has_prefix(&trie, "quu");
}

#[cfg(test)]
fn check_exists (trie: &Trie<int>, find: &str, value: int) {
    match trie.find(find) {
        &Some(v) => { assert!(v == value) }
        &None    => { fail!(fmt!("didn't find %?", find)) }
    }
}

#[cfg(test)]
fn check_not_exists (trie: &Trie<int>, find: &str) {
    match trie.find(find) {
        &Some(_) => { fail!(fmt!("shouldn't find %?", find)) }
        &None    => ()
    }
}

#[cfg(test)]
fn check_has_prefix (trie: &Trie<int>, find: &str) {
    assert!(trie.has_prefix(find));
}

#[cfg(test)]
fn check_not_has_prefix (trie: &Trie<int>, find: &str) {
    assert!(!trie.has_prefix(find));
}