Struct collections::BitVec
[−]
[src]
pub struct BitVec { // some fields omitted }
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
The bitvector type.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(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); 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); println!("total bits set to true: {}", bv.iter().filter(|x| *x).count()); // reset bitvector to empty bv.clear(); println!("{:?}", bv); println!("total bits set to true: {}", bv.iter().filter(|x| *x).count()); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(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); 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); println!("total bits set to true: {}", bv.iter().filter(|x| *x).count()); // reset bitvector to empty bv.clear(); println!("{:?}", bv); println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
Methods
impl BitVec
fn new() -> BitVec
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Creates an empty BitVec
`BitVec`.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::new(); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::new();
fn from_elem(nbits: usize, bit: bool) -> BitVec
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Creates a BitVec
`BitVecthat holds
nbitselements, setting each element to
bit`.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let bv = BitVec::from_elem(10, false); assert_eq!(bv.len(), 10); for x in &bv { assert_eq!(x, false); } }#![feature(bitvec)] use std::collections::BitVec; let bv = BitVec::from_elem(10, false); assert_eq!(bv.len(), 10); for x in &bv { assert_eq!(x, false); }
fn with_capacity(nbits: usize) -> BitVec
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Constructs a new, empty BitVec
`BitVec` with the specified capacity.
The bitvector will be able to hold at least capacity
`capacitybits without reallocating. If
capacity` is 0, it will not allocate.
It is important to note that this function does not specify the length of the returned bitvector, but only the capacity.
fn from_bytes(bytes: &[u8]) -> BitVec
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Transforms a byte-vector into a BitVec
`BitVec. Each byte becomes eight bits, with the most significant bits of each byte coming first. Each bit becomes
trueif equal to 1 or
false` if equal to 0.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b10100000, 0b00010010]); assert!(bv.eq_vec(&[true, false, true, false, false, false, false, false, false, false, false, true, false, false, true, false])); }#![feature(bitvec)] use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b10100000, 0b00010010]); assert!(bv.eq_vec(&[true, false, true, false, false, false, false, false, false, false, false, true, false, false, true, false]));
fn from_fn<F>(len: usize, f: F) -> BitVec where F: FnMut(usize) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Creates a BitVec
`BitVecof the specified length where the value at each index is
f(index)`.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let bv = BitVec::from_fn(5, |i| { i % 2 == 0 }); assert!(bv.eq_vec(&[true, false, true, false, true])); }#![feature(bitvec)] use std::collections::BitVec; let bv = BitVec::from_fn(5, |i| { i % 2 == 0 }); assert!(bv.eq_vec(&[true, false, true, false, true]));
fn get(&self, i: usize) -> Option<bool>
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Retrieves the value at index i
`i, or
`, or None
`None` if the index is out of bounds.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b01100000]); assert_eq!(bv.get(0), Some(false)); assert_eq!(bv.get(1), Some(true)); assert_eq!(bv.get(100), None); // Can also use array indexing assert_eq!(bv[1], true); }#![feature(bitvec)] use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b01100000]); assert_eq!(bv.get(0), Some(false)); assert_eq!(bv.get(1), Some(true)); assert_eq!(bv.get(100), None); // Can also use array indexing assert_eq!(bv[1], true);
fn set(&mut self, i: usize, x: bool)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Sets the value of a bit at an index i
`i`.
Panics
Panics if i
`i` is out of bounds.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(5, false); bv.set(3, true); assert_eq!(bv[3], true); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(5, false); bv.set(3, true); assert_eq!(bv[3], true);
fn set_all(&mut self)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Sets all bits to 1.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let before = 0b01100000; let after = 0b11111111; let mut bv = BitVec::from_bytes(&[before]); bv.set_all(); assert_eq!(bv, BitVec::from_bytes(&[after])); }#![feature(bitvec)] use std::collections::BitVec; let before = 0b01100000; let after = 0b11111111; let mut bv = BitVec::from_bytes(&[before]); bv.set_all(); assert_eq!(bv, BitVec::from_bytes(&[after]));
fn negate(&mut self)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Flips all bits.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let before = 0b01100000; let after = 0b10011111; let mut bv = BitVec::from_bytes(&[before]); bv.negate(); assert_eq!(bv, BitVec::from_bytes(&[after])); }#![feature(bitvec)] use std::collections::BitVec; let before = 0b01100000; let after = 0b10011111; let mut bv = BitVec::from_bytes(&[before]); bv.negate(); assert_eq!(bv, BitVec::from_bytes(&[after]));
fn union(&mut self, other: &BitVec) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Calculates the union of two bitvectors. This acts like the bitwise or
`or`
function.
Sets self
`selfto the union of
selfand
` and other
`other. Both bitvectors must be the same length. Returns
trueif
` if self
`self` changed.
Panics
Panics if the bitvectors are of different lengths.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01111110; let mut a = BitVec::from_bytes(&[a]); let b = BitVec::from_bytes(&[b]); assert!(a.union(&b)); assert_eq!(a, BitVec::from_bytes(&[res])); }#![feature(bitvec)] use std::collections::BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01111110; let mut a = BitVec::from_bytes(&[a]); let b = BitVec::from_bytes(&[b]); assert!(a.union(&b)); assert_eq!(a, BitVec::from_bytes(&[res]));
fn intersect(&mut self, other: &BitVec) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Calculates the intersection of two bitvectors. This acts like the
bitwise and
`and` function.
Sets self
`selfto the intersection of
selfand
` and other
`other. Both bitvectors must be the same length. Returns
trueif
` if self
`self` changed.
Panics
Panics if the bitvectors are of different lengths.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01000000; let mut a = BitVec::from_bytes(&[a]); let b = BitVec::from_bytes(&[b]); assert!(a.intersect(&b)); assert_eq!(a, BitVec::from_bytes(&[res])); }#![feature(bitvec)] use std::collections::BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01000000; let mut a = BitVec::from_bytes(&[a]); let b = BitVec::from_bytes(&[b]); assert!(a.intersect(&b)); assert_eq!(a, BitVec::from_bytes(&[res]));
fn difference(&mut self, other: &BitVec) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Calculates the difference between two bitvectors.
Sets each element of self
`selfto the value of that element minus the element of
otherat the same index. Both bitvectors must be the same length. Returns
trueif
` if self
`self` changed.
Panics
Panics if the bitvectors are of different length.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let a = 0b01100100; let b = 0b01011010; let a_b = 0b00100100; // a - b let b_a = 0b00011010; // b - a let mut bva = BitVec::from_bytes(&[a]); let bvb = BitVec::from_bytes(&[b]); assert!(bva.difference(&bvb)); assert_eq!(bva, BitVec::from_bytes(&[a_b])); let bva = BitVec::from_bytes(&[a]); let mut bvb = BitVec::from_bytes(&[b]); assert!(bvb.difference(&bva)); assert_eq!(bvb, BitVec::from_bytes(&[b_a])); }#![feature(bitvec)] use std::collections::BitVec; let a = 0b01100100; let b = 0b01011010; let a_b = 0b00100100; // a - b let b_a = 0b00011010; // b - a let mut bva = BitVec::from_bytes(&[a]); let bvb = BitVec::from_bytes(&[b]); assert!(bva.difference(&bvb)); assert_eq!(bva, BitVec::from_bytes(&[a_b])); let bva = BitVec::from_bytes(&[a]); let mut bvb = BitVec::from_bytes(&[b]); assert!(bvb.difference(&bva)); assert_eq!(bvb, BitVec::from_bytes(&[b_a]));
fn all(&self) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Returns true
`true` if all bits are 1.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(5, true); assert_eq!(bv.all(), true); bv.set(1, false); assert_eq!(bv.all(), false); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(5, true); assert_eq!(bv.all(), true); bv.set(1, false); assert_eq!(bv.all(), false);
fn iter(&self) -> Iter
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Returns an iterator over the elements of the vector in order.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b01110100, 0b10010010]); assert_eq!(bv.iter().filter(|x| *x).count(), 7); }#![feature(bitvec)] use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b01110100, 0b10010010]); assert_eq!(bv.iter().filter(|x| *x).count(), 7);
fn append(&mut self, other: &mut Self)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Moves all bits from other
`otherinto
` into Self
`Self, leaving
other` empty.
Examples
#![feature(bitvec, append)] extern crate collections; fn main() { use std::collections::BitVec; let mut a = BitVec::from_bytes(&[0b10000000]); let mut b = BitVec::from_bytes(&[0b01100001]); a.append(&mut b); assert_eq!(a.len(), 16); assert_eq!(b.len(), 0); assert!(a.eq_vec(&[true, false, false, false, false, false, false, false, false, true, true, false, false, false, false, true])); }#![feature(bitvec, append)] use std::collections::BitVec; let mut a = BitVec::from_bytes(&[0b10000000]); let mut b = BitVec::from_bytes(&[0b01100001]); a.append(&mut b); assert_eq!(a.len(), 16); assert_eq!(b.len(), 0); assert!(a.eq_vec(&[true, false, false, false, false, false, false, false, false, true, true, false, false, false, false, true]));
fn split_off(&mut self, at: usize) -> Self
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Splits the BitVec
`BitVec` into two at the given bit,
retaining the first half in-place and returning the second one.
Panics
Panics if at
`at` is out of bounds.
Examples
#![feature(bitvec, split_off)] extern crate collections; fn main() { use std::collections::BitVec; let mut a = BitVec::new(); a.push(true); a.push(false); a.push(false); a.push(true); let b = a.split_off(2); assert_eq!(a.len(), 2); assert_eq!(b.len(), 2); assert!(a.eq_vec(&[true, false])); assert!(b.eq_vec(&[false, true])); }#![feature(bitvec, split_off)] use std::collections::BitVec; let mut a = BitVec::new(); a.push(true); a.push(false); a.push(false); a.push(true); let b = a.split_off(2); assert_eq!(a.len(), 2); assert_eq!(b.len(), 2); assert!(a.eq_vec(&[true, false])); assert!(b.eq_vec(&[false, true]));
fn none(&self) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Returns true
`true` if all bits are 0.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(10, false); assert_eq!(bv.none(), true); bv.set(3, true); assert_eq!(bv.none(), false); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(10, false); assert_eq!(bv.none(), true); bv.set(3, true); assert_eq!(bv.none(), false);
fn any(&self) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Returns true
`true` if any bit is 1.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(10, false); assert_eq!(bv.any(), false); bv.set(3, true); assert_eq!(bv.any(), true); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(10, false); assert_eq!(bv.any(), false); bv.set(3, true); assert_eq!(bv.any(), true);
fn to_bytes(&self) -> Vec<u8>
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Organises the bits into bytes, such that the first bit in the
BitVec
`BitVecbecomes the high-order bit of the first byte. If the size of the
BitVecis not a multiple of eight then trailing bits will be filled-in with
false`.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(3, true); bv.set(1, false); assert_eq!(bv.to_bytes(), [0b10100000]); let mut bv = BitVec::from_elem(9, false); bv.set(2, true); bv.set(8, true); assert_eq!(bv.to_bytes(), [0b00100000, 0b10000000]); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(3, true); bv.set(1, false); assert_eq!(bv.to_bytes(), [0b10100000]); let mut bv = BitVec::from_elem(9, false); bv.set(2, true); bv.set(8, true); assert_eq!(bv.to_bytes(), [0b00100000, 0b10000000]);
fn eq_vec(&self, v: &[bool]) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Compares a BitVec
`BitVecto a slice of
bools. Both the
BitVec` and slice must have the same length.
Panics
Panics if the BitVec
`BitVec` and slice are of different length.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b10100000]); assert!(bv.eq_vec(&[true, false, true, false, false, false, false, false])); }#![feature(bitvec)] use std::collections::BitVec; let bv = BitVec::from_bytes(&[0b10100000]); assert!(bv.eq_vec(&[true, false, true, false, false, false, false, false]));
fn truncate(&mut self, len: usize)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Shortens a BitVec
`BitVec`, dropping excess elements.
If len
`len` is greater than the vector's current length, this has no
effect.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_bytes(&[0b01001011]); bv.truncate(2); assert!(bv.eq_vec(&[false, true])); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_bytes(&[0b01001011]); bv.truncate(2); assert!(bv.eq_vec(&[false, true]));
fn reserve(&mut self, additional: usize)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Reserves capacity for at least additional
more bits to be inserted in the given
BitVec
`BitVec`. The collection may reserve more space to avoid frequent reallocations.
Panics
Panics if the new capacity overflows usize
`usize`.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(3, false); bv.reserve(10); assert_eq!(bv.len(), 3); assert!(bv.capacity() >= 13); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(3, false); bv.reserve(10); assert_eq!(bv.len(), 3); assert!(bv.capacity() >= 13);
fn reserve_exact(&mut self, additional: usize)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Reserves the minimum capacity for exactly additional
more bits to be inserted in the
given BitVec
`BitVec`. Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests. Therefore
capacity can not be relied upon to be precisely minimal. Prefer reserve
`reserve` if future
insertions are expected.
Panics
Panics if the new capacity overflows usize
`usize`.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_elem(3, false); bv.reserve(10); assert_eq!(bv.len(), 3); assert!(bv.capacity() >= 13); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_elem(3, false); bv.reserve(10); assert_eq!(bv.len(), 3); assert!(bv.capacity() >= 13);
fn capacity(&self) -> usize
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Returns the capacity in bits for this bit vector. Inserting any element less than this amount will not trigger a resizing.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::new(); bv.reserve(10); assert!(bv.capacity() >= 10); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::new(); bv.reserve(10); assert!(bv.capacity() >= 10);
fn grow(&mut self, n: usize, value: bool)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Grows the BitVec
`BitVecin-place, adding
ncopies of
valueto the
` to the BitVec
`BitVec`.
Panics
Panics if the new len overflows a usize
`usize`.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_bytes(&[0b01001011]); bv.grow(2, true); assert_eq!(bv.len(), 10); assert_eq!(bv.to_bytes(), [0b01001011, 0b11000000]); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_bytes(&[0b01001011]); bv.grow(2, true); assert_eq!(bv.len(), 10); assert_eq!(bv.to_bytes(), [0b01001011, 0b11000000]);
fn pop(&mut self) -> Option<bool>
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Removes the last bit from the BitVec, and returns it. Returns None if the BitVec is empty.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::from_bytes(&[0b01001001]); assert_eq!(bv.pop(), Some(true)); assert_eq!(bv.pop(), Some(false)); assert_eq!(bv.len(), 6); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::from_bytes(&[0b01001001]); assert_eq!(bv.pop(), Some(true)); assert_eq!(bv.pop(), Some(false)); assert_eq!(bv.len(), 6);
fn push(&mut self, elem: bool)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Pushes a bool
`bool` onto the end.
Examples
#![feature(bitvec)] extern crate collections; fn main() { use std::collections::BitVec; let mut bv = BitVec::new(); bv.push(true); bv.push(false); assert!(bv.eq_vec(&[true, false])); }#![feature(bitvec)] use std::collections::BitVec; let mut bv = BitVec::new(); bv.push(true); bv.push(false); assert!(bv.eq_vec(&[true, false]));
fn len(&self) -> usize
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Returns the total number of bits in this vector
fn is_empty(&self) -> bool
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Returns true if there are no bits in this vector
fn clear(&mut self)
: BitVec and BitSet have been migrated to cargo as bit-vec and bit-set
Clears all bits in this vector.