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
impl BitVec
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new empty bit vector with the given capacity. The capacity is measured in bits.
sourcepub fn from_zeros(len: usize) -> Self
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.
sourcepub fn from_ones(len: usize) -> Self
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.
sourcepub fn append(&mut self, bit: bool)
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.
sourcepub fn drop_last(&mut self, n: usize)
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.
sourcepub fn append_bit(&mut self, bit: u64)
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.
sourcepub fn append_bit_u32(&mut self, bit: u32)
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.
sourcepub fn append_bit_u8(&mut self, bit: u8)
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.
sourcepub fn append_word(&mut self, word: u64)
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).
sourcepub fn append_bits(&mut self, bits: u64, len: usize)
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.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the length of the bit vector. The length is measured in bits.
sourcepub fn flip_bit(&mut self, pos: usize)
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.
sourcepub fn flip_bit_unchecked(&mut self, pos: usize)
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).
sourcepub fn get(&self, pos: usize) -> Option<u64>
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.
sourcepub fn get_unchecked(&self, pos: usize) -> u64
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).
sourcepub fn is_bit_set(&self, pos: usize) -> Option<bool>
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.
sourcepub fn is_bit_set_unchecked(&self, pos: usize) -> bool
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).
sourcepub fn get_bits(&self, pos: usize, len: usize) -> Option<u64>
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.
sourcepub fn get_bits_unchecked(&self, pos: usize, len: usize) -> u64
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.