Skip to content

Commit 307fab1

Browse files
committed
fix to size_hint(); documentation for bit-twiddle;
1 parent 3a62f87 commit 307fab1

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/libcollections/bit.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ impl BitSet {
14511451
/// ```
14521452
#[inline]
14531453
#[stable(feature = "rust1", since = "1.0.0")]
1454-
pub fn iter<'a>(&'a self) -> bit_set::Iter<'a> {
1454+
pub fn iter(&self) -> bit_set::Iter {
14551455
SetIter(BlockIter::from_blocks(self.bit_vec.blocks()))
14561456
}
14571457

@@ -1803,14 +1803,13 @@ impl hash::Hash for BitSet {
18031803

18041804
#[derive(Clone)]
18051805
#[stable(feature = "rust1", since = "1.0.0")]
1806-
struct BlockIter<T> where
1807-
T: Iterator<Item=u32> {
1806+
struct BlockIter<T> where T: Iterator<Item=u32> {
18081807
head: u32,
18091808
head_offset: usize,
1810-
tail: T
1809+
tail: T,
18111810
}
1812-
impl<'a, T> BlockIter<T> where
1813-
T: Iterator<Item=u32> {
1811+
1812+
impl<'a, T> BlockIter<T> where T: Iterator<Item=u32> {
18141813
fn from_blocks(mut blocks: T) -> BlockIter<T> {
18151814
let h = blocks.next().unwrap_or(0);
18161815
BlockIter {tail: blocks, head: h, head_offset: 0}
@@ -1850,16 +1849,20 @@ impl<'a, T> Iterator for BlockIter<T> where T: Iterator<Item=u32> {
18501849
while self.head == 0 {
18511850
match self.tail.next() {
18521851
Some(w) => self.head = w,
1853-
_ => return None
1852+
None => return None
18541853
}
18551854
self.head_offset += u32::BITS;
18561855
}
18571856

1858-
let t = self.head & !self.head + 1;
1859-
// remove the least significant bit
1857+
// from the current block, isolate the
1858+
// LSB and subtract 1, producing k:
1859+
// a block with a number of set bits
1860+
// equal to the index of the LSB
1861+
let k = (self.head & (!self.head + 1)) - 1;
1862+
// update block, removing the LSB
18601863
self.head &= self.head - 1;
1861-
// return index of lsb
1862-
Some(self.head_offset + (u32::count_ones(t-1) as usize))
1864+
// return offset + (index of LSB)
1865+
Some(self.head_offset + (u32::count_ones(k) as usize))
18631866
}
18641867

18651868
#[inline]
@@ -1886,11 +1889,15 @@ impl<'a> Iterator for TwoBitPositions<'a> {
18861889

18871890
#[inline]
18881891
fn size_hint(&self) -> (usize, Option<usize>) {
1889-
let (a, al) = self.set.size_hint();
1890-
let (b, bl) = self.set.size_hint();
1892+
let (a, au) = self.set.size_hint();
1893+
let (b, bu) = self.other.size_hint();
1894+
1895+
let upper = match (au, bu) {
1896+
(Some(au), Some(bu)) => Some(cmp::max(au, bu)),
1897+
_ => None
1898+
};
18911899

1892-
assert_eq!(a, b);
1893-
(a, cmp::max(al, bl))
1900+
(cmp::max(a, b), upper)
18941901
}
18951902
}
18961903

0 commit comments

Comments
 (0)