Struct primal_bit::BitVec

source ·
pub struct BitVec { /* private fields */ }
Expand description

The bitvector type.

Implementations§

source§

impl BitVec

source

pub fn new() -> BitVec

Creates an empty BitVec.

§Examples
use std::collections::BitVec;
let mut bv = BitVec::new();
source

pub fn from_bytes(data: Vec<u8>, nbits: usize) -> BitVec

Creates a BitVec from the given bytes.

source

pub fn from_elem(nbits: usize, bit: bool) -> BitVec

Creates a BitVec that holds nbits elements, setting each element to bit.

§Examples
use std::collections::BitVec;

let mut bv = BitVec::from_elem(10, false);
assert_eq!(bv.len(), 10);
for x in bv.iter() {
    assert_eq!(x, false);
}
source

pub fn as_bytes_mut(&mut self) -> &mut [u8]

source

pub fn as_bytes(&self) -> &[u8]

source

pub fn len(&self) -> usize

Returns the total number of bits in this vector

source

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 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);
source

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 std::collections::BitVec;

let mut bv = BitVec::from_elem(5, false);
bv.set(3, true);
assert_eq!(bv[3], true);
source§

impl BitVec

source

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

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

§Examples
use std::collections::BitVec;

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

impl BitVec

source

pub fn ones_from(&self, from: usize) -> Ones<'_>

source

pub fn into_ones(self) -> IntoOnes

source§

impl BitVec

source

pub fn count_ones(&self) -> usize

Count the number of ones in the entire BitVec.

source

pub fn count_ones_before(&self, bit: usize) -> usize

Count the number of ones for the bits up to but not including the bitth bit.

source

pub fn find_nth_bit(&self, n: usize) -> Option<usize>

Find the index of the nth (0-indexed) set bit.

source

pub fn set_all(&mut self)

Sets all bits to 1.

§Examples
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]));
source

pub fn clear(&mut self)

Clears all bits in this vector.

source

pub fn is_empty(&self) -> bool

Returns true if there are no bits in this vector

Trait Implementations§

source§

impl Clone for BitVec

source§

fn clone(&self) -> BitVec

Returns a copy of the value. Read more
source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for BitVec

source§

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

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

impl Default for BitVec

source§

fn default() -> BitVec

Returns the “default value” for a type. Read more
source§

impl Hash for BitVec

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Index<usize> for BitVec

§

type Output = bool

The returned type after indexing.
source§

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

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

impl<'a> IntoIterator for &'a BitVec

§

type Item = bool

The type of the elements being iterated over.
§

type IntoIter = Iter<'a>

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

fn into_iter(self) -> Iter<'a>

Creates an iterator from a value. Read more
source§

impl PartialEq for BitVec

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for BitVec

Auto Trait Implementations§

§

impl Freeze for BitVec

§

impl RefUnwindSafe for BitVec

§

impl Send for BitVec

§

impl Sync for BitVec

§

impl Unpin for BitVec

§

impl UnwindSafe for BitVec

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> 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,

§

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>,

§

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>,

§

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.