trie_alg/trie/
charset.rs

1//! contains trait definitions for space optimizing your trie to optimize for space. This trait is also re-exported for better usability
2
3/// Implement this trait to space optimize your trie according to your character set
4pub trait CharSet {
5    /// number of characters in the CharSet
6    const SIZE: usize;
7    /// provide one to one mapping from `char` to `usize` 
8    fn map(ch: char) -> usize;
9    /// provide one to one mapping from `usize` to `char`
10    fn unmap(hash: usize) -> char;
11}
12
13/// Set of ASCII lowercase alphabets
14#[derive(Default)]
15pub struct LowerCase();
16
17impl CharSet for LowerCase {
18    const SIZE: usize = 26;
19    fn map(ch: char) -> usize {
20        ch as usize - 'a' as usize
21    }
22    fn unmap(hash: usize) -> char {
23        (b'a' + hash as u8) as char
24    }
25}
26
27/// Set of ASCII uppercase alphabets
28#[derive(Default)]
29#[allow(unused_imports)]
30pub struct UpperCase();
31
32impl CharSet for UpperCase {
33    const SIZE: usize = 26;
34    fn map(ch: char) -> usize {
35        ch as usize - 'A' as usize
36    }
37    fn unmap(hash: usize) -> char {
38        (b'A' + hash as u8) as char
39    }
40}
41
42/// Set of ASCII characters
43#[derive(Default)]
44#[allow(unused_imports)]
45pub struct Ascii();
46
47impl CharSet for Ascii {
48    const SIZE: usize = 256;
49    fn map(ch: char) -> usize {
50        ch as usize
51    }
52    fn unmap(hash: usize) -> char {
53        (hash as u8) as char
54    }
55}