Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add documentation for Bitv.
  • Loading branch information
jbcrail committed May 7, 2014
1 parent b299231 commit 2fc3b3a
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/libcollections/bitv.rs
Expand Up @@ -225,6 +225,32 @@ enum BitvVariant { Big(BigBitv), Small(SmallBitv) }
enum Op {Union, Intersect, Assign, Difference}

/// The bitvector type
///
/// # Example
///
/// ```rust
/// use collections::bitv::Bitv;
///
/// let mut bv = Bitv::new(10, false);
///
/// // insert all primes less than 10
/// bv.set(2, true);
/// bv.set(3, true);
/// bv.set(5, true);
/// bv.set(7, true);
/// println!("{}", bv.to_str());
/// println!("total bits set to true: {}", bv.iter().count(|x| x));
///
/// // flip all values in bitvector, producing non-primes less than 10
/// bv.negate();
/// println!("{}", bv.to_str());
/// println!("total bits set to true: {}", bv.iter().count(|x| x));
///
/// // reset bitvector to empty
/// bv.clear();
/// println!("{}", bv.to_str());
/// println!("total bits set to true: {}", bv.iter().count(|x| x));
/// ```
#[deriving(Clone)]
pub struct Bitv {
/// Internal representation of the bit vector (small or large)
Expand Down Expand Up @@ -264,10 +290,11 @@ impl Bitv {
}
}
}

}

impl Bitv {
/// Creates an empty Bitv that holds `nbits` elements, setting each element
/// to `init`.
pub fn new(nbits: uint, init: bool) -> Bitv {
let rep = if nbits < uint::BITS {
Small(SmallBitv::new(if init {(1<<nbits)-1} else {0}))
Expand Down Expand Up @@ -419,6 +446,21 @@ impl Bitv {
}
}

/// Returns an iterator over the elements of the vector in order.
///
/// # Example
///
/// ```rust
/// use collections::bitv::Bitv;
/// let mut bv = Bitv::new(10, false);
/// bv.set(1, true);
/// bv.set(2, true);
/// bv.set(3, true);
/// bv.set(5, true);
/// bv.set(8, true);
/// // Count bits set to 1; result should be 5
/// println!("{}", bv.iter().count(|x| x));
/// ```
#[inline]
pub fn iter<'a>(&'a self) -> Bits<'a> {
Bits {bitv: self, next_idx: 0, end_idx: self.nbits}
Expand Down

5 comments on commit 2fc3b3a

@bors
Copy link
Contributor

@bors bors commented on 2fc3b3a May 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at jbcrail@2fc3b3a

@bors
Copy link
Contributor

@bors bors commented on 2fc3b3a May 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jbcrail/rust/add-bitv-doc = 2fc3b3a into auto

@bors
Copy link
Contributor

@bors bors commented on 2fc3b3a May 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jbcrail/rust/add-bitv-doc = 2fc3b3a merged ok, testing candidate = 897b96a

@bors
Copy link
Contributor

@bors bors commented on 2fc3b3a May 7, 2014

@bors
Copy link
Contributor

@bors bors commented on 2fc3b3a May 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 897b96a

Please sign in to comment.