@@ -225,6 +225,32 @@ enum BitvVariant { Big(BigBitv), Small(SmallBitv) }
225225enum Op { Union , Intersect , Assign , Difference }
226226
227227/// The bitvector type
228+ ///
229+ /// # Example
230+ ///
231+ /// ```rust
232+ /// use collections::bitv::Bitv;
233+ ///
234+ /// let mut bv = Bitv::new(10, false);
235+ ///
236+ /// // insert all primes less than 10
237+ /// bv.set(2, true);
238+ /// bv.set(3, true);
239+ /// bv.set(5, true);
240+ /// bv.set(7, true);
241+ /// println!("{}", bv.to_str());
242+ /// println!("total bits set to true: {}", bv.iter().count(|x| x));
243+ ///
244+ /// // flip all values in bitvector, producing non-primes less than 10
245+ /// bv.negate();
246+ /// println!("{}", bv.to_str());
247+ /// println!("total bits set to true: {}", bv.iter().count(|x| x));
248+ ///
249+ /// // reset bitvector to empty
250+ /// bv.clear();
251+ /// println!("{}", bv.to_str());
252+ /// println!("total bits set to true: {}", bv.iter().count(|x| x));
253+ /// ```
228254#[ deriving( Clone ) ]
229255pub struct Bitv {
230256 /// Internal representation of the bit vector (small or large)
@@ -264,10 +290,11 @@ impl Bitv {
264290 }
265291 }
266292 }
267-
268293}
269294
270295impl Bitv {
296+ /// Creates an empty Bitv that holds `nbits` elements, setting each element
297+ /// to `init`.
271298 pub fn new ( nbits : uint , init : bool ) -> Bitv {
272299 let rep = if nbits < uint:: BITS {
273300 Small ( SmallBitv :: new ( if init { ( 1 <<nbits) -1 } else { 0 } ) )
@@ -419,6 +446,21 @@ impl Bitv {
419446 }
420447 }
421448
449+ /// Returns an iterator over the elements of the vector in order.
450+ ///
451+ /// # Example
452+ ///
453+ /// ```rust
454+ /// use collections::bitv::Bitv;
455+ /// let mut bv = Bitv::new(10, false);
456+ /// bv.set(1, true);
457+ /// bv.set(2, true);
458+ /// bv.set(3, true);
459+ /// bv.set(5, true);
460+ /// bv.set(8, true);
461+ /// // Count bits set to 1; result should be 5
462+ /// println!("{}", bv.iter().count(|x| x));
463+ /// ```
422464 #[ inline]
423465 pub fn iter < ' a > ( & ' a self ) -> Bits < ' a > {
424466 Bits { bitv : self , next_idx : 0 , end_idx : self . nbits }
0 commit comments