Struct vers_vecs::bit_vec::BitVec

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

A simple bit vector that does not support rank and select queries. It stores bits densely in 64 bit limbs. The last limb may be partially filled. Other than that, there is no overhead.

Example

use vers_vecs::{BitVec, RsVec};

let mut bit_vec = BitVec::new();
bit_vec.append_bit(0u64);
bit_vec.append_bit_u32(1u32);
bit_vec.append_word(0b1010_1010_1010_1010u64); // appends exactly 64 bits

assert_eq!(bit_vec.len(), 66);
assert_eq!(bit_vec.get(0), Some(0u64));
assert_eq!(bit_vec.get(1), Some(1u64));

Implementations§

source§

impl BitVec

source

pub fn new() -> Self

Create a new empty bit vector.

source

pub fn with_capacity(capacity: usize) -> Self

Create a new empty bit vector with the given capacity. The capacity is measured in bits.

source

pub fn from_zeros(len: usize) -> Self

Create a new bit vector with all zeros and the given length. The length is measured in bits.

source

pub fn from_ones(len: usize) -> Self

Create a new bit vector with all ones and the given length. The length is measured in bits.

source

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

Append a bit to the bit vector. The bit is given as a boolean, where true means 1 and false means 0.

source

pub fn drop_last(&mut self, n: usize)

Drop the last n bits from the bit vector. If more bits are dropped than the bit vector contains, the bit vector is cleared.

source

pub fn append_bit(&mut self, bit: u64)

Append a bit from a u64. The least significant bit is appended to the bit vector. All other bits are ignored.

source

pub fn append_bit_u32(&mut self, bit: u32)

Append a bit from a u32. The least significant bit is appended to the bit vector. All other bits are ignored.

source

pub fn append_bit_u8(&mut self, bit: u8)

Append a bit from a u8. The least significant bit is appended to the bit vector. All other bits are ignored.

source

pub fn append_word(&mut self, word: u64)

Append a word to the bit vector. The bits are appended in little endian order (i.e. the first bit of the word is appended first).

source

pub fn append_bits(&mut self, bits: u64, len: usize)

Append multiple bits to the bit vector. The bits are appended in little-endian order (i.e. the least significant bit is appended first). The number of bits to append is given by len. The bits are taken from the least significant bits of bits. All other bits are ignored.

Panics

Panics if len is larger than 64.

source

pub fn len(&self) -> usize

Return the length of the bit vector. The length is measured in bits.

source

pub fn is_empty(&self) -> bool

Return whether the bit vector is empty (contains no bits).

source

pub fn flip_bit(&mut self, pos: usize)

Flip the bit at the given position.

Panics

If the position is larger than the length of the vector, the function panics.

source

pub fn flip_bit_unchecked(&mut self, pos: usize)

Flip the bit at the given position. If the position is larger than the length of the vector, the behavior is undefined (the function will either modify unused memory or panic. This will not corrupt memory, but will affect invalid unchecked get operations).

source

pub fn get(&self, pos: usize) -> Option<u64>

Return the bit at the given position. The bit takes the least significant bit of the returned u64 word. If the position is larger than the length of the vector, None is returned.

source

pub fn get_unchecked(&self, pos: usize) -> u64

Return the bit at the given position. The bit takes the least significant bit of the returned u64 word. If the position is larger than the length of the vector, the behavior is undefined (the function will either return unpredictable data or panic).

source

pub fn is_bit_set(&self, pos: usize) -> Option<bool>

Return whether the bit at the given position is set. If the position is larger than the length of the vector, None is returned.

source

pub fn is_bit_set_unchecked(&self, pos: usize) -> bool

Return whether the bit at the given position is set. If the position is larger than the length of the vector, the behavior is undefined (the function will either return unpredictable results or panic).

source

pub fn get_bits(&self, pos: usize, len: usize) -> Option<u64>

Return multiple bits at the given position. The number of bits to return is given by len. At most 64 bits can be returned. If the position at the end of the query is larger than the length of the vector, None is returned (even if the query partially overlaps with the vector). If the length of the query is larger than 64, None is returned.

source

pub fn get_bits_unchecked(&self, pos: usize, len: usize) -> u64

Return multiple bits at the given position. The number of bits to return is given by len. At most 64 bits can be returned.

This function is always inlined, because it gains a lot from loop optimization and can utilize the processor pre-fetcher better if it is.

Panics

If the position is larger than the length of the vector, the behavior is undefined (the function will either return any valid results padded with unpredictable memory or panic). If the length of the query is larger than 64, the behavior is undefined.

source

pub fn heap_size(&self) -> usize

Returns the number of bytes on the heap for this vector. Does not include allocated memory that isn’t used.

source§

impl BitVec

source

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

Returns an iterator over the elements of BitVec.

Trait Implementations§

source§

impl Clone for BitVec

source§

fn clone(&self) -> BitVec

Returns a copy of the value. Read more
1.0.0 · 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 Default for BitVec

source§

fn default() -> BitVec

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

impl<'a> IntoIterator for &'a BitVec

§

type Item = u64

The type of the elements being iterated over.
§

type IntoIter = BitVecRefIter<'a>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a> IntoIterator for &'a mut BitVec

§

type Item = u64

The type of the elements being iterated over.
§

type IntoIter = BitVecRefIter<'a>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for BitVec

§

type Item = u64

The type of the elements being iterated over.
§

type IntoIter = BitVecIter

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.