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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::fmt;
use std::ops::{Deref, DerefMut};
use enum_iterator::{IterableEnum, EnumIterator};

pub trait BitmaskTrait {
    type Array: Sized;
    type Index: Copy + IterableEnum;

    fn array_default() -> Self::Array;
    fn array_slice(array: &Self::Array) -> &[u8];
    fn array_slice_mut(array: &mut Self::Array) -> &mut [u8];
    fn index(index: Self::Index) -> usize;
    fn index_valid(_array: &Self::Array, _index: Self::Index) -> bool { true }
}

impl BitmaskTrait for Vec<u8> {
    type Array = Self;
    type Index = u16;

    fn array_default() -> Self::Array { vec![0u8; 0x10] }
    fn array_slice(array: &Self::Array) -> &[u8] { array }
    fn array_slice_mut(array: &mut Self::Array) -> &mut [u8] { array }
    fn index(index: Self::Index) -> usize { index as _ }
    fn index_valid(array: &Self::Array, index: Self::Index) -> bool { array.len() > index as usize }
}

pub type BitmaskVec = Bitmask<Vec<u8>>;

impl Bitmask<Vec<u8>> {
    pub fn resize(&mut self, kind: ::EventKind) {
        self.data_mut().resize((kind.count_bits().unwrap_or(0x80) + 7) / 8, 0);
    }
}

#[derive(Copy, Clone)]
pub struct Bitmask<T: BitmaskTrait> {
    mask: T::Array,
}

impl<T: BitmaskTrait> Default for Bitmask<T> {
    fn default() -> Self {
        Bitmask {
            mask: T::array_default(),
        }
    }
}

impl<T: BitmaskTrait> Bitmask<T> {
    pub fn into_inner(self) -> T::Array {
        self.mask
    }

    pub fn iter(&self) -> BitmaskIterator<T> {
        self.into_iter()
    }

    pub fn data(&self) -> &T::Array {
        &self.mask
    }

    pub fn data_mut(&mut self) -> &mut T::Array {
        &mut self.mask
    }

    pub fn slice(&self) -> &[u8] {
        T::array_slice(&self.mask)
    }

    pub fn slice_mut(&mut self) -> &mut [u8] {
        T::array_slice_mut(&mut self.mask)
    }

    pub fn index(index: T::Index) -> (usize, usize) {
        let index = T::index(index);
        (index / 8, index % 8)
    }

    pub fn index_valid(&self, index: T::Index) -> bool {
        T::index_valid(&self.mask, index)
    }

    pub fn get(&self, index: T::Index) -> bool {
        let (offset, shift) = Self::index(index);
        (self.slice()[offset] & (1u8 << shift)) != 0
    }

    pub fn set(&mut self, index: T::Index) {
        let (offset, shift) = Self::index(index);
        let v = &mut self.slice_mut()[offset];
        *v |= 1u8 << shift;
    }

    pub fn clear(&mut self, index: T::Index) {
        let (offset, shift) = Self::index(index);
        let v = &mut self.slice_mut()[offset];
        *v &= !(1u8 << shift);
    }

    pub fn flip(&mut self, index: T::Index) {
        let (offset, shift) = Self::index(index);
        self.slice_mut()[offset] ^= 1u8 << shift;
    }

    pub fn or<I: IntoIterator<Item=T::Index>>(&mut self, indices: I) {
        for index in indices {
            self.set(index);
        }
    }
}

impl<'a, T: BitmaskTrait> IntoIterator for &'a Bitmask<T> {
    type Item = T::Index;
    type IntoIter = BitmaskIterator<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        BitmaskIterator {
            mask: self,
            iter: T::Index::iter(),
        }
    }
}

pub struct BitmaskIterator<'a, T: BitmaskTrait + 'a> {
    mask: &'a Bitmask<T>,
    iter: EnumIterator<T::Index>,
}

impl<'a, T: BitmaskTrait> Iterator for BitmaskIterator<'a, T> {
    type Item = T::Index;

    fn next(&mut self) -> Option<Self::Item> {
        let mask = &self.mask;
        self.iter.by_ref().take_while(|i| mask.index_valid(*i)).filter(|i| mask.get(*i)).next()
    }
}

impl<T: BitmaskTrait> fmt::Debug for Bitmask<T> where T::Index: fmt::Debug {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_list().entries(self.into_iter()).finish()
    }
}

impl<T: BitmaskTrait> Deref for Bitmask<T> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.slice()
    }
}

impl<T: BitmaskTrait> DerefMut for Bitmask<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.slice_mut()
    }
}

impl<T: BitmaskTrait> AsRef<[u8]> for Bitmask<T> {
    fn as_ref(&self) -> &[u8] {
        self.slice()
    }
}

impl<T: BitmaskTrait> AsMut<[u8]> for Bitmask<T> {
    fn as_mut(&mut self) -> &mut [u8] {
        self.slice_mut()
    }
}