Struct std::collections::BitvExperimental [-]  [+] [src]

pub struct Bitv {
    // some fields omitted
}

The bitvector type.

Example

fn main() { use collections::Bitv; let mut bv = Bitv::with_capacity(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_string()); 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_string()); println!("total bits set to true: {}", bv.iter().filter(|x| *x).count()); // reset bitvector to empty bv.clear(); println!("{}", bv.to_string()); println!("total bits set to true: {}", bv.iter().filter(|x| *x).count()); }
use collections::Bitv;

let mut bv = Bitv::with_capacity(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_string());
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_string());
println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());

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

Methods

impl Bitv

fn new() -> Bitv

Creates an empty Bitv.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::new(); }
use std::collections::Bitv;
let mut bv = Bitv::new();

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

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

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::with_capacity(10u, false); assert_eq!(bv.len(), 10u); for x in bv.iter() { assert_eq!(x, false); } }
use std::collections::Bitv;

let mut bv = Bitv::with_capacity(10u, false);
assert_eq!(bv.len(), 10u);
for x in bv.iter() {
    assert_eq!(x, false);
}

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

Retrieves the value at index i.

Panics

Panics if i is out of bounds.

Example

fn main() { use std::collections::bitv; let bv = bitv::from_bytes(&[0b01100000]); assert_eq!(bv.get(0), false); assert_eq!(bv.get(1), true); // Can also use array indexing assert_eq!(bv[1], true); }
use std::collections::bitv;

let bv = bitv::from_bytes(&[0b01100000]);
assert_eq!(bv.get(0), false);
assert_eq!(bv.get(1), true);

// Can also use array indexing
assert_eq!(bv[1], true);

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

Sets the value of a bit at an index i.

Panics

Panics if i is out of bounds.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::with_capacity(5, false); bv.set(3, true); assert_eq!(bv[3], true); }
use std::collections::Bitv;

let mut bv = Bitv::with_capacity(5, false);
bv.set(3, true);
assert_eq!(bv[3], true);

fn set_all(&mut self)

Sets all bits to 1.

Example

fn main() { use std::collections::bitv; let before = 0b01100000; let after = 0b11111111; let mut bv = bitv::from_bytes(&[before]); bv.set_all(); assert_eq!(bv, bitv::from_bytes(&[after])); }
use std::collections::bitv;

let before = 0b01100000;
let after  = 0b11111111;

let mut bv = bitv::from_bytes(&[before]);
bv.set_all();
assert_eq!(bv, bitv::from_bytes(&[after]));

fn negate(&mut self)

Flips all bits.

Example

fn main() { use std::collections::bitv; let before = 0b01100000; let after = 0b10011111; let mut bv = bitv::from_bytes(&[before]); bv.negate(); assert_eq!(bv, bitv::from_bytes(&[after])); }
use std::collections::bitv;

let before = 0b01100000;
let after  = 0b10011111;

let mut bv = bitv::from_bytes(&[before]);
bv.negate();
assert_eq!(bv, bitv::from_bytes(&[after]));

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

Calculates the union of two bitvectors. This acts like the bitwise or function.

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

Panics

Panics if the bitvectors are of different lengths.

Example

fn main() { use std::collections::bitv; let a = 0b01100100; let b = 0b01011010; let res = 0b01111110; let mut a = bitv::from_bytes(&[a]); let b = bitv::from_bytes(&[b]); assert!(a.union(&b)); assert_eq!(a, bitv::from_bytes(&[res])); }
use std::collections::bitv;

let a   = 0b01100100;
let b   = 0b01011010;
let res = 0b01111110;

let mut a = bitv::from_bytes(&[a]);
let b = bitv::from_bytes(&[b]);

assert!(a.union(&b));
assert_eq!(a, bitv::from_bytes(&[res]));

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

Calculates the intersection of two bitvectors. This acts like the bitwise and function.

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

Panics

Panics if the bitvectors are of different lengths.

Example

fn main() { use std::collections::bitv; let a = 0b01100100; let b = 0b01011010; let res = 0b01000000; let mut a = bitv::from_bytes(&[a]); let b = bitv::from_bytes(&[b]); assert!(a.intersect(&b)); assert_eq!(a, bitv::from_bytes(&[res])); }
use std::collections::bitv;

let a   = 0b01100100;
let b   = 0b01011010;
let res = 0b01000000;

let mut a = bitv::from_bytes(&[a]);
let b = bitv::from_bytes(&[b]);

assert!(a.intersect(&b));
assert_eq!(a, bitv::from_bytes(&[res]));

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

Calculates the difference between two bitvectors.

Sets each element of self to the value of that element minus the element of other at the same index. Both bitvectors must be the same length. Returns true if self changed.

Panics

Panics if the bitvectors are of different length.

Example

fn main() { use std::collections::bitv; let a = 0b01100100; let b = 0b01011010; let a_b = 0b00100100; // a - b let b_a = 0b00011010; // b - a let mut bva = bitv::from_bytes(&[a]); let bvb = bitv::from_bytes(&[b]); assert!(bva.difference(&bvb)); assert_eq!(bva, bitv::from_bytes(&[a_b])); let bva = bitv::from_bytes(&[a]); let mut bvb = bitv::from_bytes(&[b]); assert!(bvb.difference(&bva)); assert_eq!(bvb, bitv::from_bytes(&[b_a])); }
use std::collections::bitv;

let a   = 0b01100100;
let b   = 0b01011010;
let a_b = 0b00100100; // a - b
let b_a = 0b00011010; // b - a

let mut bva = bitv::from_bytes(&[a]);
let bvb = bitv::from_bytes(&[b]);

assert!(bva.difference(&bvb));
assert_eq!(bva, bitv::from_bytes(&[a_b]));

let bva = bitv::from_bytes(&[a]);
let mut bvb = bitv::from_bytes(&[b]);

assert!(bvb.difference(&bva));
assert_eq!(bvb, bitv::from_bytes(&[b_a]));

fn all(&self) -> bool

Returns true if all bits are 1.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::with_capacity(5, true); assert_eq!(bv.all(), true); bv.set(1, false); assert_eq!(bv.all(), false); }
use std::collections::Bitv;

let mut bv = Bitv::with_capacity(5, true);
assert_eq!(bv.all(), true);

bv.set(1, false);
assert_eq!(bv.all(), false);

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

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

Example

fn main() { use std::collections::bitv; let bv = bitv::from_bytes(&[0b01110100, 0b10010010]); assert_eq!(bv.iter().filter(|x| *x).count(), 7); }
use std::collections::bitv;

let bv = bitv::from_bytes(&[0b01110100, 0b10010010]);
assert_eq!(bv.iter().filter(|x| *x).count(), 7);

fn none(&self) -> bool

Returns true if all bits are 0.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::with_capacity(10, false); assert_eq!(bv.none(), true); bv.set(3, true); assert_eq!(bv.none(), false); }
use std::collections::Bitv;

let mut bv = Bitv::with_capacity(10, false);
assert_eq!(bv.none(), true);

bv.set(3, true);
assert_eq!(bv.none(), false);

fn any(&self) -> bool

Returns true if any bit is 1.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::with_capacity(10, false); assert_eq!(bv.any(), false); bv.set(3, true); assert_eq!(bv.any(), true); }
use std::collections::Bitv;

let mut bv = Bitv::with_capacity(10, false);
assert_eq!(bv.any(), false);

bv.set(3, true);
assert_eq!(bv.any(), true);

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

Organises 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 eight then trailing bits will be filled-in with false.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::with_capacity(3, true); bv.set(1, false); assert_eq!(bv.to_bytes(), vec!(0b10100000)); let mut bv = Bitv::with_capacity(9, false); bv.set(2, true); bv.set(8, true); assert_eq!(bv.to_bytes(), vec!(0b00100000, 0b10000000)); }
use std::collections::Bitv;

let mut bv = Bitv::with_capacity(3, true);
bv.set(1, false);

assert_eq!(bv.to_bytes(), vec!(0b10100000));

let mut bv = Bitv::with_capacity(9, false);
bv.set(2, true);
bv.set(8, true);

assert_eq!(bv.to_bytes(), vec!(0b00100000, 0b10000000));

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

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

Example

fn main() { use std::collections::bitv; let bv = bitv::from_bytes(&[0b10100000]); assert_eq!(bv.to_bools(), vec!(true, false, true, false, false, false, false, false)); }
use std::collections::bitv;

let bv = bitv::from_bytes(&[0b10100000]);
assert_eq!(bv.to_bools(), vec!(true, false, true, false,
                               false, false, false, false));

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

Compares a Bitv to a slice of bools. Both the Bitv and slice must have the same length.

Panics

Panics if the Bitv and slice are of different length.

Example

fn main() { use std::collections::bitv; let bv = bitv::from_bytes(&[0b10100000]); assert!(bv.eq_vec(&[true, false, true, false, false, false, false, false])); }
use std::collections::bitv;

let bv = bitv::from_bytes(&[0b10100000]);

assert!(bv.eq_vec(&[true, false, true, false,
                    false, false, false, false]));

fn truncate(&mut self, len: uint)

Shortens a Bitv, dropping excess elements.

If len is greater than the vector's current length, this has no effect.

Example

fn main() { use std::collections::bitv; let mut bv = bitv::from_bytes(&[0b01001011]); bv.truncate(2); assert!(bv.eq_vec(&[false, true])); }
use std::collections::bitv;

let mut bv = bitv::from_bytes(&[0b01001011]);
bv.truncate(2);
assert!(bv.eq_vec(&[false, true]));

fn reserve(&mut self, size: uint)

Grows the vector to be able to store size bits without resizing.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::with_capacity(3, false); bv.reserve(10); assert_eq!(bv.len(), 3); assert!(bv.capacity() >= 10); }
use std::collections::Bitv;

let mut bv = Bitv::with_capacity(3, false);
bv.reserve(10);
assert_eq!(bv.len(), 3);
assert!(bv.capacity() >= 10);

fn capacity(&self) -> uint

Returns the capacity in bits for this bit vector. Inserting any element less than this amount will not trigger a resizing.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::new(); bv.reserve(10); assert!(bv.capacity() >= 10); }
use std::collections::Bitv;

let mut bv = Bitv::new();
bv.reserve(10);
assert!(bv.capacity() >= 10);

fn grow(&mut self, n: uint, value: bool)

Grows the Bitv in-place, adding n copies of value to the Bitv.

Example

fn main() { use std::collections::bitv; let mut bv = bitv::from_bytes(&[0b01001011]); bv.grow(2, true); assert_eq!(bv.len(), 10); assert_eq!(bv.to_bytes(), vec!(0b01001011, 0b11000000)); }
use std::collections::bitv;

let mut bv = bitv::from_bytes(&[0b01001011]);
bv.grow(2, true);
assert_eq!(bv.len(), 10);
assert_eq!(bv.to_bytes(), vec!(0b01001011, 0b11000000));

fn pop(&mut self) -> bool

Shortens by one element and returns the removed element.

Panics

Assert if empty.

Example

fn main() { use std::collections::bitv; let mut bv = bitv::from_bytes(&[0b01001001]); assert_eq!(bv.pop(), true); assert_eq!(bv.pop(), false); assert_eq!(bv.len(), 6); assert_eq!(bv.to_bytes(), vec!(0b01001000)); }
use std::collections::bitv;

let mut bv = bitv::from_bytes(&[0b01001001]);
assert_eq!(bv.pop(), true);
assert_eq!(bv.pop(), false);
assert_eq!(bv.len(), 6);
assert_eq!(bv.to_bytes(), vec!(0b01001000));

fn push(&mut self, elem: bool)

Pushes a bool onto the end.

Example

fn main() { use std::collections::Bitv; let mut bv = Bitv::new(); bv.push(true); bv.push(false); assert!(bv.eq_vec(&[true, false])); }
use std::collections::Bitv;

let mut bv = Bitv::new();
bv.push(true);
bv.push(false);
assert!(bv.eq_vec(&[true, false]));

fn len(&self) -> uint

Return the total number of bits in this vector

fn is_empty(&self) -> bool

Returns true if there are no bits in this vector

fn clear(&mut self)

Clears all bits in this vector.

Trait Implementations

impl Index<uint, bool> for Bitv

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

impl Default for Bitv

fn default() -> Bitv

impl FromIterator<bool> for Bitv

fn from_iter<I: Iterator<bool>>(iterator: I) -> Bitv

impl Extend<bool> for Bitv

fn extend<I: Iterator<bool>>(&mut self, iterator: I)

impl Clone for Bitv

fn clone(&self) -> Bitv

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

fn clone_from(&mut self, &Bitv)

impl PartialOrd for Bitv

fn partial_cmp(&self, other: &Bitv) -> Option<Ordering>

fn lt(&self, &Bitv) -> bool

fn le(&self, &Bitv) -> bool

fn gt(&self, &Bitv) -> bool

fn ge(&self, &Bitv) -> bool

impl Ord for Bitv

fn cmp(&self, other: &Bitv) -> Ordering

impl Show for Bitv

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

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, &Bitv) -> bool

impl Eq for Bitv

fn assert_receiver_is_total_eq(&self)