yansi/
set.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
use core::fmt;
use core::marker::PhantomData;

pub struct Set<T>(PhantomData<T>, pub(crate) u16);

pub trait SetMember: Copy + fmt::Debug {
    const MAX_VALUE: u8;

    fn bit_mask(self) -> u16;
    fn from_bit_mask(value: u16) -> Option<Self>;
}

impl<T: SetMember> Set<T> {
    pub const EMPTY: Self = Set(PhantomData, 0);

    pub fn contains(self, value: T) -> bool {
        (value.bit_mask() & self.1) == value.bit_mask()
    }

    pub const fn iter(self) -> Iter<T> {
        Iter { index: 0, set: self }
    }
}

pub struct Iter<T> {
    index: u8,
    set: Set<T>,
}

impl<T: SetMember> Iterator for Iter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        while self.index <= T::MAX_VALUE {
            let mask: u16 = 1 << self.index;

            self.index += 1;
            if let Some(v) = T::from_bit_mask(mask) {
                if self.set.contains(v) {
                    return Some(v);
                }
            }
        }

        None
    }
}

impl<T: SetMember> Default for Set<T> {
    fn default() -> Self {
        Set::EMPTY
    }
}

impl<T: SetMember> fmt::Debug for Set<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_set().entries(self.iter()).finish()
    }
}

impl<T> Copy for Set<T> { }

impl<T> Clone for Set<T> {
    fn clone(&self) -> Self {
        Self(self.0, self.1)
    }
}

impl<T> PartialEq for Set<T> {
    fn eq(&self, other: &Self) -> bool {
        self.1 == other.1
    }
}

impl<T> Eq for Set<T> { }

impl<T> PartialOrd for Set<T> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.1.partial_cmp(&other.1)
    }
}

impl<T> Ord for Set<T> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.1.cmp(&other.1)
    }
}

impl<T> core::hash::Hash for Set<T> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.1.hash(state);
    }
}