/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn iter<'a>(&'a self) -> bit_set::Iter<'a> {
+ pub fn iter(&self) -> bit_set::Iter {
SetIter(BlockIter::from_blocks(self.bit_vec.blocks()))
}
#[derive(Clone)]
#[stable(feature = "rust1", since = "1.0.0")]
-struct BlockIter<T> where
- T: Iterator<Item=u32> {
+struct BlockIter<T> where T: Iterator<Item=u32> {
head: u32,
head_offset: usize,
- tail: T
+ tail: T,
}
-impl<'a, T> BlockIter<T> where
- T: Iterator<Item=u32> {
+
+impl<'a, T> BlockIter<T> where T: Iterator<Item=u32> {
fn from_blocks(mut blocks: T) -> BlockIter<T> {
let h = blocks.next().unwrap_or(0);
BlockIter {tail: blocks, head: h, head_offset: 0}
while self.head == 0 {
match self.tail.next() {
Some(w) => self.head = w,
- _ => return None
+ None => return None
}
self.head_offset += u32::BITS;
}
- let t = self.head & !self.head + 1;
- // remove the least significant bit
+ // from the current block, isolate the
+ // LSB and subtract 1, producing k:
+ // a block with a number of set bits
+ // equal to the index of the LSB
+ let k = (self.head & (!self.head + 1)) - 1;
+ // update block, removing the LSB
self.head &= self.head - 1;
- // return index of lsb
- Some(self.head_offset + (u32::count_ones(t-1) as usize))
+ // return offset + (index of LSB)
+ Some(self.head_offset + (u32::count_ones(k) as usize))
}
#[inline]
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
- let (a, al) = self.set.size_hint();
- let (b, bl) = self.set.size_hint();
+ let (a, au) = self.set.size_hint();
+ let (b, bu) = self.other.size_hint();
+
+ let upper = match (au, bu) {
+ (Some(au), Some(bu)) => Some(cmp::max(au, bu)),
+ _ => None
+ };
- assert_eq!(a, b);
- (a, cmp::max(al, bl))
+ (cmp::max(a, b), upper)
}
}