trie_alg/
trie.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
152
//! Provides `Trie` and `MultiTrie` implementations space optimized for set of characters in use.

use std::default::Default;

mod member;

#[allow(unused_imports)]
pub mod charset;
pub use self::charset::CharSet;

mod internal;
use self::internal::_Trie;

#[derive(Default)]
pub struct Trie<C, const SIZE: usize>
where
    C: CharSet,
{
    internal: _Trie<bool, C, SIZE>,
}

impl<C, const SIZE: usize> Trie<C, SIZE>
where
    C: CharSet,
{
    /// Creates new Trie
    pub fn new() -> Self {
        Self {
            internal: _Trie::<_, _, SIZE>::new(),
        }
    }

    /// Add a string to Trie
    /// 
    /// Returns `true` if Trie didn't contain the string earlier
    pub fn add(&mut self, s: &str) -> bool {
        self.internal.add(s)
    }

    /// Check if trie contains the string
    pub fn contains(&self, s: &str) -> bool {
        self.internal.contains(s)
    }
}

#[derive(Default)]
pub struct MultiTrie<C, const SIZE: usize>
where
    C: CharSet,
{
    internal: _Trie<u32, C, SIZE>,
}

impl<C, const SIZE: usize> MultiTrie<C, SIZE>
where
    C: CharSet,
{
    /// Creates a new MultiTrie
    pub fn new() -> Self {
        Self {
            internal: _Trie::<_, _, SIZE>::new(),
        }
    }

    /// Adds a new string to the MultiTrie
    /// 
    /// Returns `true`` if MultiTrie didn't contain the string earlier
    pub fn add(&mut self, s: &str) -> bool {
        self.internal.add(s)
    }

    /// Check if MultiTrie contains given string
    pub fn contains(&self, s: &str) -> bool {
        self.internal.contains(s)
    }

    /// Retuns number of strings in the MultiTrie
    pub fn count(&self, s: &str) -> u32 {
        self.internal.count(s)
    }
}

#[macro_export]
/// Create new `Trie`
/// 
/// `trie!(C:CharSet)` creates new trie with given `CharSet`
/// 
/// `trie!()` defaults to `LowerCase`,set of lowercase alphabets
macro_rules! trie {
    () => {
        Trie::<
            self::charset::LowerCase,
            { <self::charset::LowerCase as self::charset::CharSet>::SIZE },
        >::new()
    };
    ($C:ty) => {
        Trie::<$C, { <$C as self::charset::CharSet>::SIZE }>::new()
    };
}

#[macro_export]
/// Create new `MultiTrie`
/// 
/// `multi_trie!(C:CharSet)` creates new trie with given `CharSet`
/// 
/// `multi_trie!()` defaults to `LowerCase`,set of lowercase alphabets
macro_rules! multi_trie {
    () => {
        MultiTrie::<
            self::charset::LowerCase,
            { <self::charset::LowerCase as self::charset::CharSet>::SIZE },
        >::new()
    };
    ($C:ty) => {
        MultiTrie::<$C, { <$C as self::charset::CharSet>::SIZE }>::new()
    };
}

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

    #[test]
    fn test_single_add() {
        let mut trie = trie!();
        trie.add("string");
        assert!(trie.contains("string"));
    }

    #[test]
    fn test_single_eq() {
        let mut trie1 = trie!(LowerCase);
        assert!(trie1.add("string"));
        assert!(!trie1.add("string"));
    }

    #[test]
    fn test_multi_add() {
        let mut trie = multi_trie!();
        trie.add("string");
        assert!(trie.contains("string"));
    }

    #[test]
    fn test_multi_eq() {
        let mut trie = multi_trie!(LowerCase);
        assert!(trie.add("string"));
        assert!(!trie.add("string"));
        assert_eq!(trie.count("string"), 2);
    }
}