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
//mod trie;
mod radix_tree;
mod implementations;

pub use implementations::*;

/// A generic tree based collection storing decomposed items
///
/// A generic tree based fixed width per node tree in which inserted elements are decomposed into
/// their parts and stored such that shared prefixes are reused. Optimization used for nodes with
/// single child such that nodes until a future split are condensed into a single node.
///
/// AKA "prefix tree", "Radix tree"
///
/// # Examples
///
/// ```
/// let mut trie = Trie::new(
///     |c: &char| (c.to_lowercase().next().unwrap() as usize) - ('a' as usize),
///     ('z' as usize) - ('a' as usize),
/// );
/// assert_eq!(trie.contains(&String::from("asd"))), false);
/// trie.insert(String::from("asd")));
/// assert_eq!(trie.contains(&String::from("asd"))), false);
/// ```
pub type Trie<T, FIndex> = radix_tree::Trie<T, FIndex>;

/// Trait that splits T into component parts
///
/// this trait needs to be implemented in order for T to be placed into a trie
pub trait Decomposable<TParts, TIterator: Iterator<Item=TParts>> {
    fn decompose(self) -> TIterator;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_trie_simple() {
        let mut trie = Trie::new(
            |c: &char| (c.to_lowercase().next().unwrap() as usize) - ('a' as usize), // index function
            ('z' as usize) - ('a' as usize),                                      // alphabet size
        );

        assert_eq!(trie.contains(String::from("asd")), false);
        assert_eq!(trie.contains(String::from("dsa")), false);
        trie.insert(String::from("asd"));
        assert_eq!(trie.contains(String::from("dsa")), false);
        assert_eq!(trie.contains(String::from("asd")), true);
        trie.insert(String::from("asd"));
        assert_eq!(trie.contains(String::from("asd")), true);
        assert_eq!(trie.contains(String::from("dsa")), false);
        trie.insert(String::from("dsa"));
        assert_eq!(trie.contains(String::from("asd")), true);
        assert_eq!(trie.contains(String::from("dsa")), true);
    }

    #[test]
    fn test_trie_simple_numeric() {
        let mut trie = Trie::new(
            |c: &u8| (*c as usize),
            u8::max_value() as usize,
        );

        trie.insert(456 as u16);
    }
}