Struct BitVector

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

This is a space-efficient, resizeable bitvector class. In the common case it occupies one word, but if necessary, it will inflate this one word to point to a single chunk of out-of-line allocated storage to store an arbitrary number of bits.

  • The bitvector remembers the bound of how many bits can be stored, but this may be slightly greater (by as much as some platform-specific constant) than the last argument passed to ensureSize().

  • The bitvector can resize itself automatically (set, clear, get) or can be used in a manual mode, which is faster (quick_set, quick_clear, quick_get, ensure_size).

  • Accesses assert! that you are within bounds.

  • Bits are automatically initialized to zero.

On the other hand, this BitVector class may not be the fastest around, since it does conditionals on every get/set/clear. But it is great if you need to juggle a lot of variable-length BitVectors and you’re worried about wasting space.

Implementations§

Source§

impl BitVector

Source

pub fn new() -> Self

Source

pub fn with_capacity(num_bits: usize) -> Self

Source

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

Merge other into self, equal to bit-or.

Source

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

Filter self by other, keeping only the bits that are set in both, equal to bit-and.

Source

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

Exclude the bits in other from self, equal to bit-and-not.

Source

pub fn is_empty(&self) -> bool

Source

pub fn bit_count(&self) -> usize

Return number of set bits.

Source

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

Search after index for the next bit with value value, returns index if no such bit is found.

Source

pub fn len(&self) -> usize

Return number of bits in the bitvector.

Source

pub fn quick_clear(&mut self, bit: usize) -> bool

Quick clear a bit. Does not reallocate.

§Panics

Panics if bit is out of bounds.

Source

pub fn quick_set(&mut self, bit: usize, value: bool) -> bool

Quick set bit. Does not reallocate.

§Panics

Panics if bit is out of bounds.

Source

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

Quick get bit.

§Panics

Panics if bit is out of bounds.

Source

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

Get bit at index, or false if index is out of bounds.

Source

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

Same as get

Source

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

Clear bit at index, or return false if index is out of bounds.

Source

pub fn set(&mut self, index: usize, value: bool) -> bool

Set bit at index. Resizes bitvector if necessary.

Source

pub fn ensure_size(&mut self, num_bits: usize)

Ensure that the bitvector can hold at least num_bits bits.

Source

pub fn resize(&mut self, num_bits: usize)

Resize the bitvector to num_bits bits.

Source

pub fn clear_all(&mut self)

Set all bits to zero.

Source

pub fn shift_right_by_multiple_of_64(&mut self, shift_in_bits: usize)

Shift right by shift_in_bits bits. Resizes bitvector if necessary.

Source

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

Creates a new iterator over the bitvector.

Trait Implementations§

Source§

impl Clone for BitVector

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BitVector

Source§

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

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

impl Default for BitVector

Source§

fn default() -> Self

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

impl Drop for BitVector

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Hash for BitVector

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 PartialEq for BitVector

Source§

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

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

const 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 Eq for BitVector

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.