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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use Bigwise;
use BitsIter;
use std::fmt::{self, Debug, Formatter};
use std::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
use rand::{Rand, Rng};

/// A `Bigwise` type composed of 64 bits.
///
/// Wraps a `u64`
///
#[derive(Copy, Clone, Eq, PartialEq, Hash, Default, Ord, PartialOrd)]
pub struct Bw64(u64);

impl Bigwise for Bw64 {

    fn size() -> u32 {
        64
    }

    fn empty() -> Self {
        Bw64(0)
    }

    fn full() -> Self {
        Bw64(!0)
    }

    fn from_bytes(bytes: &[u8]) -> Self {
        let mut val = 0u64;
        for (i, &b) in bytes.iter().rev().take(8).enumerate() {
            val |= (b as u64) << (8*i);
        }
        Bw64(val)
    }

    fn to_bytes(self) -> Vec<u8> {
        let nb_bytes = (Self::size() / 8) as usize;
        let mut res = Vec::with_capacity(nb_bytes);
        for i in (0..nb_bytes).rev() {
            res.push(((self.0 >> (8*i)) & 0xFF) as u8);
        }
        res
    }

    fn get(self, i: u32) -> bool {
        if i >= Self::size() {
            false
        } else {
            (self.0 >> i) & 1 > 0
        }
    }

    fn set(&mut self, i: u32, v: bool) {
        if i >= Self::size() {
            panic!("index overflow");
        }
        if v {
            self.0 |= 1 << i;
        } else {
            self.0 &= !(1 << i);
        }
    }

    fn rotate_left(self, n: u32) -> Self {
        Bw64(self.0.rotate_left(n))
    }

    fn rotate_right(self, n: u32) -> Self {
        Bw64(self.0.rotate_right(n))
    }
}

impl Rand for Bw64 {
    fn rand<R: Rng>(rng: &mut R) -> Self {
        Bw64(rng.next_u64())
    }
}

/// The sequence of zeroes and ones, the MSB (most significant bit)
/// appearing on the left.
///
impl Debug for Bw64 {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        let bytes = self.to_bytes();
        for b in bytes {
            try!(f.write_str(&format!("{:08b}", b)));
        }
        Ok(())
    }
}

impl BitAnd for Bw64 {

    type Output = Self;

    fn bitand(self, rhs: Self) -> Self {
        Bw64(self.0 & rhs.0)
    }
}

impl BitOr for Bw64 {

    type Output = Self;

    fn bitor(self, rhs: Self) -> Self {
        Bw64(self.0 | rhs.0)
    }
}

impl BitXor for Bw64 {

    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self {
        Bw64(self.0 ^ rhs.0)
    }
}

impl Not for Bw64 {

    type Output = Self;

    fn not(self) -> Self {
        Bw64(!self.0)
    }
}

impl Shl<u32> for Bw64 {

    type Output = Self;

    fn shl(self, rhs: u32) -> Self {
        if rhs >= Self::size() {
            Self::empty()
        } else {
            Bw64(self.0 << rhs)
        }
    }
}

impl Shr<u32> for Bw64 {

    type Output = Self;

    fn shr(self, rhs: u32) -> Self {
        if rhs >= Self::size() {
            Self::empty()
        } else {
            Bw64(self.0 >> rhs)
        }
    }
}

/// An iterator over the bits.
///
/// Bits are iterated from the least significant one to the most
/// significant one.
///
/// # Examples
///
/// ```
/// use bigwise::{Bigwise, Bw64};
/// let b = Bw64::from_bytes(&[0b11111000]);
/// let mut it = b.into_iter();
/// assert_eq!(Some(false), it.next());
/// assert_eq!(Some(false), it.next());
/// assert_eq!(Some(false), it.next());
/// assert_eq!(Some(true), it.next());
/// assert_eq!(Some(true), it.next());
/// assert_eq!(Some(true), it.next());
/// assert_eq!(Some(true), it.next());
/// assert_eq!(Some(true), it.next());
/// assert_eq!(Some(false), it.next());
/// assert_eq!(Some(false), it.next());
/// assert!(it.take(200).all(|v| v == false));
/// ```
///
impl IntoIterator for Bw64 {
    type Item = bool;
    type IntoIter = BitsIter<Self>;

    fn into_iter(self) -> Self::IntoIter {
        BitsIter::new(self)
    }
}