Move BitIterator into root of crate.

This commit is contained in:
Sean Bowe
2017-06-17 19:46:40 -06:00
parent 5cf6acd21a
commit fd3774118a
3 changed files with 34 additions and 33 deletions

View File

@@ -181,3 +181,35 @@ impl<T, E> Convert<T, E> for T {
Cow::Borrowed(self)
}
}
pub struct BitIterator<T> {
t: T,
n: usize
}
impl<T: AsRef<[u64]>> BitIterator<T> {
fn new(t: T) -> Self {
let bits = 64 * t.as_ref().len();
BitIterator {
t: t,
n: bits
}
}
}
impl<T: AsRef<[u64]>> Iterator for BitIterator<T> {
type Item = bool;
fn next(&mut self) -> Option<bool> {
if self.n == 0 {
None
} else {
self.n -= 1;
let part = self.n / 64;
let bit = self.n - (64 * part);
Some(self.t.as_ref()[part] & (1 << bit) > 0)
}
}
}