trie_alg/trie/
charset.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
//! contains trait definitions for space optimizing your trie to optimize for space. This trait is also re-exported for better usability

/// Implement this trait to space optimize your trie according to your character set
pub trait CharSet {
    /// number of characters in the CharSet
    const SIZE: usize;
    /// provide one to one mapping from `char` to `usize` 
    fn map(ch: char) -> usize;
    /// provide one to one mapping from `usize` to `char`
    fn unmap(hash: usize) -> char;
}

/// Set of ASCII lowercase alphabets
#[derive(Default)]
pub struct LowerCase();

impl CharSet for LowerCase {
    const SIZE: usize = 26;
    fn map(ch: char) -> usize {
        ch as usize - 'a' as usize
    }
    fn unmap(hash: usize) -> char {
        (b'a' + hash as u8) as char
    }
}

/// Set of ASCII uppercase alphabets
#[derive(Default)]
#[allow(unused_imports)]
pub struct UpperCase();

impl CharSet for UpperCase {
    const SIZE: usize = 26;
    fn map(ch: char) -> usize {
        ch as usize - 'A' as usize
    }
    fn unmap(hash: usize) -> char {
        (b'A' + hash as u8) as char
    }
}

/// Set of ASCII characters
#[derive(Default)]
#[allow(unused_imports)]
pub struct Ascii();

impl CharSet for Ascii {
    const SIZE: usize = 256;
    fn map(ch: char) -> usize {
        ch as usize
    }
    fn unmap(hash: usize) -> char {
        (hash as u8) as char
    }
}