Add BitIter::new().

This factors out some duplicated code.
This commit is contained in:
Nicholas Nethercote 2019-10-15 10:02:55 +11:00
parent 237d54ff6c
commit 2918a7d5a9

View file

@ -168,11 +168,7 @@ impl<T: Idx> BitSet<T> {
/// Iterates over the indices of set bits in a sorted order.
#[inline]
pub fn iter(&self) -> BitIter<'_, T> {
BitIter {
cur: None,
iter: self.words.iter().enumerate(),
marker: PhantomData,
}
BitIter::new(&self.words)
}
/// Duplicates the set as a hybrid set.
@ -296,6 +292,17 @@ pub struct BitIter<'a, T: Idx> {
marker: PhantomData<T>
}
impl<'a, T: Idx> BitIter<'a, T> {
#[inline]
fn new(words: &'a [Word]) -> BitIter<'a, T> {
BitIter {
cur: None,
iter: words.iter().enumerate(),
marker: PhantomData,
}
}
}
impl<'a, T: Idx> Iterator for BitIter<'a, T> {
type Item = T;
fn next(&mut self) -> Option<T> {
@ -851,11 +858,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
pub fn iter(&self, row: R) -> BitIter<'_, C> {
assert!(row.index() < self.num_rows);
let (start, end) = self.range(row);
BitIter {
cur: None,
iter: self.words[start..end].iter().enumerate(),
marker: PhantomData,
}
BitIter::new(&self.words[start..end])
}
/// Returns the number of elements in `row`.