Move BitIterator into root of crate.
This commit is contained in:
32
src/lib.rs
32
src/lib.rs
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user