Struct collections::bitv::Bitv[src]

pub struct Bitv {
    // some fields omitted
}

The bitvector type

Example

extern crate collections; fn main() { 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().filter(|x| *x).count()); // 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().filter(|x| *x).count()); // reset bitvector to empty bv.clear(); println!("{}", bv.to_str()); println!("total bits set to true: {}", bv.iter().filter(|x| *x).count()); }
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().filter(|x| *x).count());

// 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().filter(|x| *x).count());

// reset bitvector to empty
bv.clear();
println!("{}", bv.to_str());
println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());

Methods

impl Bitv

fn new(nbits: uint, init: bool) -> Bitv

Creates an empty Bitv that holds nbits elements, setting each element to init.

fn union(&mut self, v1: &Bitv) -> bool

Calculates the union of two bitvectors

Sets self to the union of self and v1. Both bitvectors must be the same length. Returns true if self changed.

fn intersect(&mut self, v1: &Bitv) -> bool

Calculates the intersection of two bitvectors

Sets self to the intersection of self and v1. Both bitvectors must be the same length. Returns true if self changed.

fn assign(&mut self, v: &Bitv) -> bool

Assigns the value of v1 to self

Both bitvectors must be the same length. Returns true if self was changed

fn get(&self, i: uint) -> bool

Retrieve the value at index i

fn set(&mut self, i: uint, x: bool)

Set the value of a bit at a given index

i must be less than the length of the bitvector.

fn clear(&mut self)

Set all bits to 0

fn set_all(&mut self)

Set all bits to 1

fn negate(&mut self)

Flip all bits

fn difference(&mut self, v: &Bitv) -> bool

Calculate the difference between two bitvectors

Sets each element of v0 to the value of that element minus the element of v1 at the same index. Both bitvectors must be the same length.

Returns true if v0 was changed.

fn all(&self) -> bool

Returns true if all bits are 1

fn iter<'a>(&'a self) -> Bits<'a>

Returns an iterator over the elements of the vector in order.

Example

extern crate collections; fn main() { 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().filter(|x| *x).count()); }
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().filter(|x| *x).count());

fn none(&self) -> bool

Returns true if all bits are 0

fn any(&self) -> bool

Returns true if any bit is 1

fn to_vec(&self) -> Vec<uint>

Converts self to a vector of uint with the same length.

Each uint in the resulting vector has either value 0u or 1u.

fn to_bytes(&self) -> Vec<u8>

Organise the bits into bytes, such that the first bit in the Bitv becomes the high-order bit of the first byte. If the size of the Bitv is not a multiple of 8 then trailing bits will be filled-in with false/0

fn to_bools(&self) -> Vec<bool>

Transform self into a Vec<bool> by turning each bit into a bool.

fn eq_vec(&self, v: &[bool]) -> bool

Compare a bitvector to a vector of bool.

Both the bitvector and vector must have the same length.

fn ones(&self, f: |uint| -> bool) -> bool

Trait Implementations

impl Index<uint, bool> for Bitv

fn index(&self, i: &uint) -> bool

impl Show for Bitv

fn fmt(&self, fmt: &mut Formatter) -> Result

impl<S: Writer> Hash<S> for Bitv

fn hash(&self, state: &mut S)

impl PartialEq for Bitv

fn eq(&self, other: &Bitv) -> bool

fn ne(&self, other: &Self) -> bool

impl Eq for Bitv

Derived Implementations

impl Clone for Bitv

fn clone(&self) -> Bitv

fn clone_from(&mut self, source: &Self)