Skip to content

Commit

Permalink
Main bitv example: prime sieve.
Browse files Browse the repository at this point in the history
  • Loading branch information
treeman authored and alexcrichton committed Jul 21, 2014
1 parent 26047f1 commit 4574b2f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/libcollections/bitv.rs
Expand Up @@ -8,6 +8,55 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Collections implemented with bit vectors.
//!
//! # Example
//!
//! This is a simple example of the [Sieve of Eratosthenes][sieve]
//! which calculates prime numbers up to a given limit.
//!
//! [sieve]: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
//!
//! ```
//! use std::collections::{BitvSet, Bitv};
//! use std::iter;
//!
//! let max_prime = 10000;
//!
//! // Store the primes as a BitvSet
//! let primes = {
//! let mut bv = Bitv::with_capacity(max_prime, true);
//!
//! // Neither 0 nor 1 are prime
//! bv.set(0, false);
//! bv.set(1, false);
//!
//! for i in range(2, max_prime) {
//! // if i is a prime
//! if bv.get(i) {
//! // mark all multiples of i as non-prime (any multiples below i * i
//! // will have been marked as non-prime previously)
//! for j in iter::range_step(i * i, max_prime, i) { bv.set(j, false) }
//! }
//! }
//! BitvSet::from_bitv(bv)
//! };
//!
//! // Simple primality tests below our max bound
//! let print_primes = 20;
//! print!("The primes below {} are: ", print_primes);
//! for x in range(0, print_primes) {
//! if primes.contains(&x) {
//! print!("{} ", x);
//! }
//! }
//! println!("");
//!
//! // We can manipulate the internal Bitv
//! let num_primes = primes.get_ref().iter().filter(|x| *x).count();
//! println!("There are {} primes below {}", num_primes, max_prime);
//! ```

#![allow(missing_doc)]

use core::prelude::*;
Expand Down

0 comments on commit 4574b2f

Please sign in to comment.