Skip to main content

BitVec

Struct BitVec 

Source
pub struct BitVec { /* private fields */ }
Expand description

A growable list of bits implemented using a Vec<u8>

§Examples

use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(5, false);

bv.set(0, true);
bv.set(1, true);
bv.set(2, true);
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, true, true, false, false],
);

bv.set_all(true);
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, true, true, true, true],
);

bv.flip(0);
bv.flip_all();
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, false, false, false, false],
);

bv.push(true);
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, false, false, false, false, true],
);
assert_eq!(bv.pop(), Some(true));

let clone = bv.clone();
bv.flip_all();
bv.union(&clone);
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, true, true, true, true],
);

Implementations§

Source§

impl BitVec

Source

pub fn new(len: usize) -> Self

Constructs a new BitVec with a certain number of bits. All bits are initialized to false.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let bv = BitVec::new(5);
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![false, false, false, false, false],
);
Source

pub fn from_elem(len: usize, bit: bool) -> Self

Constructs a new BitVec with a certain number of bits. All bits are initialized to bit.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let bv = BitVec::from_elem(5, true);
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, true, true, true, true],
);
Source

pub fn from_bytes(bytes: &[u8]) -> Self

Constructs a BitVec from a byte slice. Each byte becomes eight bits, with the most signficant bits of each byte coming first.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let bv = BitVec::from_bytes(&[0b11010000]);
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, true, false, true, false, false, false, false],
);
Source

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

Returns the byte-vec representation of the BitVec with the first bit in the BitVec becoming the high-order bit of the first byte.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::new(8);

bv.set(0, true);
bv.set(1, true);
bv.set(3, true);

assert_eq!(bv.to_bytes(), vec![0b11010000]);
Source

pub fn with_capacity(len: usize) -> Self

Constructs a new, empty BitVec with a certain capacity.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let bv = BitVec::with_capacity(5);
Source

pub fn set(&mut self, index: usize, bit: bool)

Sets the value at index index to bit.

§Panics

Panics if attempt to set an index out-of-bounds.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::new(5);
bv.set(1, true);

assert_eq!(bv.get(0), Some(false));
assert_eq!(bv.get(1), Some(true));
Source

pub fn get(&self, index: usize) -> Option<bool>

Returns the value at index index, or None if index is out of bounds.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::new(5);
bv.set(1, true);

assert_eq!(bv.get(0), Some(false));
assert_eq!(bv.get(1), Some(true));
Source

pub fn set_all(&mut self, bit: bool)

Sets all values in the BitVec to bit.

§Panics

Panics if attempting to set a bit out-of-bounds.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(5, false);
bv.set_all(true);

assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, true, true, true, true],
);
Source

pub fn flip(&mut self, index: usize)

Flip the value at index index.

§Panics

Panics if attempt to flip an index out-of-bounds.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(5, false);

bv.flip(0);
assert_eq!(bv.get(0), Some(true));

bv.flip(1);
assert_eq!(bv.get(0), Some(true));
Source

pub fn flip_all(&mut self)

Flips all values in the BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(5, false);

bv.flip_all();
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![true, true, true, true, true],
);

bv.flip_all();
assert_eq!(
    bv.iter().collect::<Vec<bool>>(),
    vec![false, false, false, false, false],
);
Source

pub fn union(&mut self, other: &Self)

Sets self to the union of self and other.

§Panics

Panics if the two BitVec are of different lengths.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);

let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);

bv1.union(&bv2);
assert_eq!(
    bv1.iter().collect::<Vec<bool>>(),
    vec![true, true, true, false],
);
Source

pub fn intersection(&mut self, other: &Self)

Sets self to the intersection of self and other.

§Panics

Panics if the two BitVec are of different lengths.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);

let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);

bv1.intersection(&bv2);
assert_eq!(
    bv1.iter().collect::<Vec<bool>>(),
    vec![true, false, false, false],
);
Source

pub fn difference(&mut self, other: &Self)

Sets self to the difference of self and other.

§Panics

Panics if the two BitVec are of different lengths.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);

let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);

bv1.difference(&bv2);
assert_eq!(
    bv1.iter().collect::<Vec<bool>>(),
    vec![false, true, false, false],
);
Source

pub fn symmetric_difference(&mut self, other: &Self)

Sets self to the symmetric difference of self and other.

§Panics

Panics if the two BitVec are of different lengths.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);

let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);

bv1.symmetric_difference(&bv2);
assert_eq!(
    bv1.iter().collect::<Vec<bool>>(),
    vec![false, true, true, false],
);
Source

pub fn truncate(&mut self, len: usize)

Truncates a BitVec, dropping excess elements.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(5, false);

bv.truncate(2);
assert_eq!(bv.iter().collect::<Vec<bool>>(), vec![false, false]);
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more bits to be inserted in the given BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(5, false);

bv.reserve(10);
assert_eq!(bv.len(), 5);
assert!(bv.capacity() >= 16);
Source

pub fn reserve_exact(&mut self, additional: usize)

Reserves capacity for exactly additional more bits to be inserted in the given BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(5, false);
bv.reserve_exact(10);
assert_eq!(bv.len(), 5);
assert_eq!(bv.capacity(), 16);
Source

pub fn pop(&mut self) -> Option<bool>

Returns and removes the last element of the BitVec. Returns None if the BitVec is empty.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(1, false);

assert_eq!(bv.pop(), Some(false));
assert_eq!(bv.pop(), None);
Source

pub fn push(&mut self, bit: bool)

Pushes an element into the BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(1, false);

bv.push(true);
assert_eq!(bv.get(1), Some(true));
Source

pub fn iter(&self) -> BitVecIter<'_>

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

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(1, false);

bv.push(true);
assert_eq!(bv.iter().collect::<Vec<bool>>(), vec![false, true]);
Source

pub fn is_empty(&self) -> bool

Returns true if the BitVec is empty.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(1, false);

assert!(!bv.is_empty());
bv.pop();
assert!(bv.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of elements in the BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::from_elem(1, false);

assert_eq!(bv.len(), 1);
bv.pop();
assert_eq!(bv.len(), 0);
Source

pub fn capacity(&self) -> usize

Returns the capacity of the BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let mut bv = BitVec::new(0);

bv.reserve_exact(10);
assert_eq!(bv.capacity(), 16);
Source

pub fn count_ones(&self) -> usize

Returns the number of set bits in the BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let bv = BitVec::from_bytes(&[0b11010000]);
assert_eq!(bv.count_ones(), 3);
Source

pub fn count_zeros(&self) -> usize

Returns the number of unset bits in the BitVec.

§Examples
use probabilistic_collections::bit_vec::BitVec;

let bv = BitVec::from_bytes(&[0b11010000]);
assert_eq!(bv.count_zeros(), 5);

Trait Implementations§

Source§

impl Clone for BitVec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BitVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Index<usize> for BitVec

Source§

type Output = bool

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &bool

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a BitVec

Source§

type IntoIter = BitVecIter<'a>

Which kind of iterator are we turning this into?
Source§

type Item = bool

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for BitVec

Source§

type IntoIter = BitVecIntoIter

Which kind of iterator are we turning this into?
Source§

type Item = bool

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for BitVec

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for BitVec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V