Struct bit_vec::BitVec [−][src]
pub struct BitVec<B = u32> { /* fields omitted */ }
The bitvector type.
Examples
use bit_vec::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<u32>
[src]
impl BitVec<u32>
pub fn new() -> Self
[src]
pub fn new() -> Self
pub fn from_elem(nbits: usize, bit: bool) -> Self
[src]
pub fn from_elem(nbits: usize, bit: bool) -> Self
Creates a BitVec
that holds nbits
elements, setting each element
to bit
.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_elem(10, false); assert_eq!(bv.len(), 10); for x in bv.iter() { assert_eq!(x, false); }
pub fn with_capacity(nbits: usize) -> Self
[src]
pub fn with_capacity(nbits: usize) -> Self
Constructs a new, empty BitVec
with the specified capacity.
The bitvector will be able to hold at least capacity
bits 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.
pub fn from_bytes(bytes: &[u8]) -> Self
[src]
pub fn from_bytes(bytes: &[u8]) -> Self
Transforms a byte-vector into a BitVec
. Each byte becomes eight bits,
with the most significant bits of each byte coming first. Each
bit becomes true
if equal to 1 or false
if equal to 0.
Examples
use bit_vec::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]));
pub fn from_fn<F>(len: usize, f: F) -> Self where
F: FnMut(usize) -> bool,
[src]
pub fn from_fn<F>(len: usize, f: F) -> Self where
F: FnMut(usize) -> bool,
Creates a BitVec
of the specified length where the value at each index
is f(index)
.
Examples
use bit_vec::BitVec; let bv = BitVec::from_fn(5, |i| { i % 2 == 0 }); assert!(bv.eq_vec(&[true, false, true, false, true]));
impl<B: BitBlock> BitVec<B>
[src]
impl<B: BitBlock> BitVec<B>
ⓘImportant traits for Blocks<'a, B>pub fn blocks(&self) -> Blocks<B>
[src]
pub fn blocks(&self) -> Blocks<B>
Iterator over the underlying blocks of data
pub fn storage(&self) -> &[B]
[src]
pub fn storage(&self) -> &[B]
Exposes the raw block storage of this BitVec
Only really intended for BitSet.
pub unsafe fn storage_mut(&mut self) -> &mut Vec<B>
[src]
pub unsafe fn storage_mut(&mut self) -> &mut Vec<B>
Exposes the raw block storage of this BitVec
Can probably cause unsafety. Only really intended for BitSet.
pub fn get(&self, i: usize) -> Option<bool>
[src]
pub fn get(&self, i: usize) -> Option<bool>
Retrieves the value at index i
, or None
if the index is out of bounds.
Examples
use bit_vec::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);
pub fn set(&mut self, i: usize, x: bool)
[src]
pub fn set(&mut self, i: usize, x: bool)
Sets the value of a bit at an index i
.
Panics
Panics if i
is out of bounds.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_elem(5, false); bv.set(3, true); assert_eq!(bv[3], true);
pub fn set_all(&mut self)
[src]
pub fn set_all(&mut self)
Sets all bits to 1.
Examples
use bit_vec::BitVec; let before = 0b01100000; let after = 0b11111111; let mut bv = BitVec::from_bytes(&[before]); bv.set_all(); assert_eq!(bv, BitVec::from_bytes(&[after]));
pub fn negate(&mut self)
[src]
pub fn negate(&mut self)
Flips all bits.
Examples
use bit_vec::BitVec; let before = 0b01100000; let after = 0b10011111; let mut bv = BitVec::from_bytes(&[before]); bv.negate(); assert_eq!(bv, BitVec::from_bytes(&[after]));
pub fn union(&mut self, other: &Self) -> bool
[src]
pub fn union(&mut self, other: &Self) -> 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.
Examples
use bit_vec::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]));
pub fn intersect(&mut self, other: &Self) -> bool
[src]
pub fn intersect(&mut self, other: &Self) -> 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.
Examples
use bit_vec::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]));
pub fn difference(&mut self, other: &Self) -> bool
[src]
pub fn difference(&mut self, other: &Self) -> 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.
Examples
use bit_vec::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]));
pub fn all(&self) -> bool
[src]
pub fn all(&self) -> bool
Returns true
if all bits are 1.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_elem(5, true); assert_eq!(bv.all(), true); bv.set(1, false); assert_eq!(bv.all(), false);
ⓘImportant traits for Iter<'a, B>pub fn iter(&self) -> Iter<B>
[src]
pub fn iter(&self) -> Iter<B>
Returns an iterator over the elements of the vector in order.
Examples
use bit_vec::BitVec; let bv = BitVec::from_bytes(&[0b01110100, 0b10010010]); assert_eq!(bv.iter().filter(|x| *x).count(), 7);
pub fn none(&self) -> bool
[src]
pub fn none(&self) -> bool
Returns true
if all bits are 0.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_elem(10, false); assert_eq!(bv.none(), true); bv.set(3, true); assert_eq!(bv.none(), false);
pub fn any(&self) -> bool
[src]
pub fn any(&self) -> bool
Returns true
if any bit is 1.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_elem(10, false); assert_eq!(bv.any(), false); bv.set(3, true); assert_eq!(bv.any(), true);
pub fn to_bytes(&self) -> Vec<u8>
[src]
pub fn to_bytes(&self) -> Vec<u8>
Organises the bits into bytes, such that the first bit in the
BitVec
becomes the high-order bit of the first byte. If the
size of the BitVec
is not a multiple of eight then trailing bits
will be filled-in with false
.
Examples
use bit_vec::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]);
pub fn eq_vec(&self, v: &[bool]) -> bool
[src]
pub fn eq_vec(&self, v: &[bool]) -> bool
Compares a BitVec
to a slice of bool
s.
Both the BitVec
and slice must have the same length.
Panics
Panics if the BitVec
and slice are of different length.
Examples
use bit_vec::BitVec; let bv = BitVec::from_bytes(&[0b10100000]); assert!(bv.eq_vec(&[true, false, true, false, false, false, false, false]));
pub fn truncate(&mut self, len: usize)
[src]
pub fn truncate(&mut self, len: usize)
Shortens a BitVec
, dropping excess elements.
If len
is greater than the vector's current length, this has no
effect.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_bytes(&[0b01001011]); bv.truncate(2); assert!(bv.eq_vec(&[false, true]));
pub fn reserve(&mut self, additional: usize)
[src]
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more bits to be inserted in the given
BitVec
. The collection may reserve more space to avoid frequent reallocations.
Panics
Panics if the new capacity overflows usize
.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_elem(3, false); bv.reserve(10); assert_eq!(bv.len(), 3); assert!(bv.capacity() >= 13);
pub fn reserve_exact(&mut self, additional: usize)
[src]
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional
more bits to be inserted in the
given 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
if future
insertions are expected.
Panics
Panics if the new capacity overflows usize
.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_elem(3, false); bv.reserve(10); assert_eq!(bv.len(), 3); assert!(bv.capacity() >= 13);
pub fn capacity(&self) -> usize
[src]
pub fn capacity(&self) -> usize
Returns the capacity in bits for this bit vector. Inserting any element less than this amount will not trigger a resizing.
Examples
use bit_vec::BitVec; let mut bv = BitVec::new(); bv.reserve(10); assert!(bv.capacity() >= 10);
pub fn grow(&mut self, n: usize, value: bool)
[src]
pub fn grow(&mut self, n: usize, value: bool)
Grows the BitVec
in-place, adding n
copies of value
to the BitVec
.
Panics
Panics if the new len overflows a usize
.
Examples
use bit_vec::BitVec; let mut bv = BitVec::from_bytes(&[0b01001011]); bv.grow(2, true); assert_eq!(bv.len(), 10); assert_eq!(bv.to_bytes(), [0b01001011, 0b11000000]);
pub fn pop(&mut self) -> Option<bool>
[src]
pub fn pop(&mut self) -> Option<bool>
Removes the last bit from the BitVec, and returns it. Returns None if the BitVec is empty.
Examples
use bit_vec::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);
pub fn push(&mut self, elem: bool)
[src]
pub fn push(&mut self, elem: bool)
Pushes a bool
onto the end.
Examples
use bit_vec::BitVec; let mut bv = BitVec::new(); bv.push(true); bv.push(false); assert!(bv.eq_vec(&[true, false]));
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
Returns the total number of bits in this vector
pub unsafe fn set_len(&mut self, len: usize)
[src]
pub unsafe fn set_len(&mut self, len: usize)
Sets the number of bits that this BitVec considers initialized.
Almost certainly can cause bad stuff. Only really intended for BitSet.
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
Returns true if there are no bits in this vector
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
Clears all bits in this vector.
pub fn shrink_to_fit(&mut self)
[src]
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the underlying storage as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the underlying storage that there is space for a few more elements/bits.
Trait Implementations
impl<B: BitBlock> Index<usize> for BitVec<B>
[src]
impl<B: BitBlock> Index<usize> for BitVec<B>
type Output = bool
The returned type after indexing.
fn index(&self, i: usize) -> &bool
[src]
fn index(&self, i: usize) -> &bool
Performs the indexing (container[index]
) operation.
impl<B: BitBlock> Default for BitVec<B>
[src]
impl<B: BitBlock> Default for BitVec<B>
impl<B: BitBlock> FromIterator<bool> for BitVec<B>
[src]
impl<B: BitBlock> FromIterator<bool> for BitVec<B>
fn from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Self
[src]
fn from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Self
Creates a value from an iterator. Read more
impl<B: BitBlock> Extend<bool> for BitVec<B>
[src]
impl<B: BitBlock> Extend<bool> for BitVec<B>
fn extend<I: IntoIterator<Item = bool>>(&mut self, iterable: I)
[src]
fn extend<I: IntoIterator<Item = bool>>(&mut self, iterable: I)
Extends a collection with the contents of an iterator. Read more
impl<B: BitBlock> Clone for BitVec<B>
[src]
impl<B: BitBlock> Clone for BitVec<B>
fn clone(&self) -> Self
[src]
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<B: BitBlock> PartialOrd for BitVec<B>
[src]
impl<B: BitBlock> PartialOrd for BitVec<B>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
fn gt(&self, other: &Rhs) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
fn ge(&self, other: &Rhs) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<B: BitBlock> Ord for BitVec<B>
[src]
impl<B: BitBlock> Ord for BitVec<B>
fn cmp(&self, other: &Self) -> Ordering
[src]
fn cmp(&self, other: &Self) -> Ordering
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
impl<B: BitBlock> Debug for BitVec<B>
[src]
impl<B: BitBlock> Debug for BitVec<B>
fn fmt(&self, fmt: &mut Formatter) -> Result
[src]
fn fmt(&self, fmt: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<B: BitBlock> Hash for BitVec<B>
[src]
impl<B: BitBlock> Hash for BitVec<B>
fn hash<H: Hasher>(&self, state: &mut H)
[src]
fn hash<H: Hasher>(&self, state: &mut H)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<B: BitBlock> PartialEq for BitVec<B>
[src]
impl<B: BitBlock> PartialEq for BitVec<B>
fn eq(&self, other: &Self) -> bool
[src]
fn eq(&self, other: &Self) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl<B: BitBlock> Eq for BitVec<B>
[src]
impl<B: BitBlock> Eq for BitVec<B>
impl<'a, B: BitBlock> IntoIterator for &'a BitVec<B>
[src]
impl<'a, B: BitBlock> IntoIterator for &'a BitVec<B>
type Item = bool
The type of the elements being iterated over.
type IntoIter = Iter<'a, B>
Which kind of iterator are we turning this into?
ⓘImportant traits for Iter<'a, B>fn into_iter(self) -> Iter<'a, B>
[src]
fn into_iter(self) -> Iter<'a, B>
Creates an iterator from a value. Read more
impl<B: BitBlock> IntoIterator for BitVec<B>
[src]
impl<B: BitBlock> IntoIterator for BitVec<B>