@@ -225,6 +225,32 @@ enum BitvVariant { Big(BigBitv), Small(SmallBitv) }
225
225
enum Op { Union , Intersect , Assign , Difference }
226
226
227
227
/// 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
+ /// ```
228
254
#[ deriving( Clone ) ]
229
255
pub struct Bitv {
230
256
/// Internal representation of the bit vector (small or large)
@@ -264,10 +290,11 @@ impl Bitv {
264
290
}
265
291
}
266
292
}
267
-
268
293
}
269
294
270
295
impl Bitv {
296
+ /// Creates an empty Bitv that holds `nbits` elements, setting each element
297
+ /// to `init`.
271
298
pub fn new ( nbits : uint , init : bool ) -> Bitv {
272
299
let rep = if nbits < uint:: BITS {
273
300
Small ( SmallBitv :: new ( if init { ( 1 <<nbits) -1 } else { 0 } ) )
@@ -419,6 +446,21 @@ impl Bitv {
419
446
}
420
447
}
421
448
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
+ /// ```
422
464
#[ inline]
423
465
pub fn iter < ' a > ( & ' a self ) -> Bits < ' a > {
424
466
Bits { bitv : self , next_idx : 0 , end_idx : self . nbits }
0 commit comments