char_bag.rs

 1#[derive(Copy, Clone, Debug)]
 2pub struct CharBag(u64);
 3
 4impl CharBag {
 5    pub fn is_superset(self, other: CharBag) -> bool {
 6        self.0 & other.0 == other.0
 7    }
 8
 9    fn insert(&mut self, c: char) {
10        if c >= 'a' && c <= 'z' {
11            let mut count = self.0;
12            let idx = c as u8 - 'a' as u8;
13            count = count >> (idx * 2);
14            count = ((count << 1) | 1) & 3;
15            count = count << idx * 2;
16            self.0 |= count;
17        } else if c >= '0' && c <= '9' {
18            let idx = c as u8 - '0' as u8;
19            self.0 |= 1 << (idx + 52);
20        } else if c == '-' {
21            self.0 |= 1 << 62;
22        }
23    }
24}
25
26impl From<&str> for CharBag {
27    fn from(s: &str) -> Self {
28        let mut bag = Self(0);
29        for c in s.chars() {
30            bag.insert(c);
31        }
32        bag
33    }
34}
35
36impl From<&[char]> for CharBag {
37    fn from(chars: &[char]) -> Self {
38        let mut bag = Self(0);
39        for c in chars {
40            bag.insert(*c);
41        }
42        bag
43    }
44}