pub struct BitVector { /* private fields */ }
Expand description
Implementation of an immutable bit vector.
Implementations§
source§impl BitVector
impl BitVector
sourcepub fn get_bits(&self, index: usize, len: usize) -> Option<u64>
pub fn get_bits(&self, index: usize, len: usize) -> Option<u64>
Accesses len
bits, with 1 <= len
<= 64, starting at position index
.
Returns None
if index
+len
is out of bounds,
if len
is 0, or if len
is greater than 64.
§Examples
use qwt::{BitVector, AccessBin};
let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();
assert_eq!(bv.get(1), Some(false));
assert_eq!(bv.get_bits(1, 3), Some(0b110)); // Accesses bits from index 1 to 3
// Accessing bits from index 1 to 8, which is out of bounds
assert_eq!(bv.get_bits(1, 8), None);
// Accessing more than 64 bits
assert_eq!(bv.get_bits(0, 65), None);
// Accessing more than 0 bits
assert_eq!(bv.get_bits(0, 0), None);
sourcepub unsafe fn get_bits_unchecked(&self, index: usize, len: usize) -> u64
pub unsafe fn get_bits_unchecked(&self, index: usize, len: usize) -> u64
Accesses len
bits, starting at position index
, without performing bounds checking.
§Safety
This method is unsafe because it does not perform bounds checking.
It is the caller’s responsibility to ensure that the provided index
and len
are within the bounds of the bit vector.
§Examples
use qwt::{BitVector};
let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();
// This is unsafe because it does not perform bounds checking
unsafe {
assert_eq!(bv.get_bits_unchecked(1, 3), 0b110);
}
sourcepub fn get_word(&self, i: usize) -> u64
pub fn get_word(&self, i: usize) -> u64
Gets a whole 64-bit word from the bit vector at index i
in the underlying vector of u64.
§Examples
use qwt::BitVector;
let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();
// Get the 64-bit word at index 0
let word = bv.get_word(0);
assert_eq!(word, 0b111101);
sourcepub fn ones(&self) -> BitVectorBitPositionsIter<'_, true> ⓘ
pub fn ones(&self) -> BitVectorBitPositionsIter<'_, true> ⓘ
Returns a non-consuming iterator over positions of bits set to 1 in the bit vector.
§Examples
use qwt::BitVector;
let vv: Vec<usize> = vec![0, 63, 128, 129, 254, 1026];
let bv: BitVector = vv.iter().copied().collect();
sourcepub fn ones_with_pos(&self, pos: usize) -> BitVectorBitPositionsIter<'_, true> ⓘ
pub fn ones_with_pos(&self, pos: usize) -> BitVectorBitPositionsIter<'_, true> ⓘ
Returns a non-consuming iterator over positions of bits set to 1 in the bit vector, starting at a specified bit position.
§Examples
use qwt::BitVector;
let vv: Vec<usize> = vec![0, 63, 128, 129, 254, 1026];
let bv: BitVector = vv.iter().copied().collect();
let v: Vec<usize> = bv.ones_with_pos(2).collect();
assert_eq!(v, vec![63, 128, 129, 254, 1026]);
sourcepub fn zeros(&self) -> BitVectorBitPositionsIter<'_, false> ⓘ
pub fn zeros(&self) -> BitVectorBitPositionsIter<'_, false> ⓘ
Returns a non-consuming iterator over positions of bits set to 0 in the bit vector.
§Examples
use qwt::BitVector;
use qwt::perf_and_test_utils::negate_vector;
let vv: Vec<usize> = vec![0, 63, 128, 129, 254, 1026];
let bv: BitVector = vv.iter().copied().collect();
let v: Vec<usize> = bv.zeros().collect();
assert_eq!(v, negate_vector(&vv));
sourcepub fn zeros_with_pos(&self, pos: usize) -> BitVectorBitPositionsIter<'_, false> ⓘ
pub fn zeros_with_pos(&self, pos: usize) -> BitVectorBitPositionsIter<'_, false> ⓘ
Returns a non-consuming iterator over positions of bits set to 0 in the bit vector, starting at a specified bit position.
sourcepub fn iter(&self) -> BitVectorIter<'_> ⓘ
pub fn iter(&self) -> BitVectorIter<'_> ⓘ
Returns a non-consuming iterator over bits of the bit vector.
§Examples
use qwt::BitVector;
let v = vec![0,2,3,5];
let bv: BitVector = v.into_iter().collect();
let mut iter = bv.iter();
assert_eq!(iter.next(), Some(true)); // First bit is true
assert_eq!(iter.next(), Some(false)); // Second bit is false
assert_eq!(iter.next(), Some(true)); // Third bit is true
assert_eq!(iter.next(), Some(true)); // Fourth bit is true
assert_eq!(iter.next(), Some(false)); // Fifth bit is false
assert_eq!(iter.next(), Some(true)); // Sixth bit is true
assert_eq!(iter.next(), None); // End of the iterator
sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Counts the number of ones (bits set to 1) in the bit vector.
§Examples
use qwt::BitVector;
let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();
assert_eq!(bv.count_ones(), 5);
sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Counts the number of zeros (bits set to 0) in the bit vector.
§Examples
use qwt::BitVector;
let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();
assert_eq!(bv.count_zeros(), 1);
Trait Implementations§
source§impl AccessBin for BitVector
impl AccessBin for BitVector
source§impl<'de> Deserialize<'de> for BitVector
impl<'de> Deserialize<'de> for BitVector
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl From<BitVector> for BitVectorMut
impl From<BitVector> for BitVectorMut
Implements conversion from immutable BitVector
to mutable BitVectorMut
.
This conversion takes ownership of the original BitVector
and creates a mutable version.
§Examples
use qwt::{BitVector, BitVectorMut, AccessBin};
let v = vec![0,2,3,4,5];
let mut bv: BitVector = v.into_iter().collect();
// Convert immutable BitVector to mutable BitVectorMut
let mut bvm: BitVectorMut = bv.into();
assert_eq!(bvm.get(0), Some(true));
assert_eq!(bvm.len(), 6);
bvm.push(true);
assert_eq!(bvm.len(), 7);
source§impl From<BitVectorMut> for BitVector
impl From<BitVectorMut> for BitVector
Implements conversion from mutable BitVectorMut
to immutable BitVector
.
This conversion consumes the original BitVectorMut
and creates an immutable version.
§Examples
use qwt::{BitVector, BitVectorMut, AccessBin};
let mut bvm = BitVectorMut::new();
bvm.push(true);
bvm.push(false);
// Convert mutable BitVectorMut to immutable BitVector
let bv: BitVector = bvm.into();
assert_eq!(bv.get(0), Some(true));
source§fn from(bvm: BitVectorMut) -> Self
fn from(bvm: BitVectorMut) -> Self
source§impl<V> FromIterator<V> for BitVector
impl<V> FromIterator<V> for BitVector
Creates a BitVector
from an iterator over non-negative integer values.
§Panics
Panics if any value of the sequence cannot be converted to usize.
§Examples
use qwt::{AccessBin, BitVector};
// Create a bit vector from an iterator over usize values
let bv: BitVector = vec![0, 1, 3, 5].into_iter().collect();
assert_eq!(bv.len(), 6);
assert_eq!(bv.get(3), Some(true));
source§impl FromIterator<bool> for BitVector
impl FromIterator<bool> for BitVector
Creates a BitVector
from an iterator over bool
values.
§Examples
use qwt::{AccessBin, BitVector};
// Create a bit vector from an iterator over bool values
let bv: BitVector = vec![true, false, true].into_iter().collect();
assert_eq!(bv.len(), 3);
assert_eq!(bv.get(1), Some(false));
source§impl<'a> IntoIterator for &'a BitVector
impl<'a> IntoIterator for &'a BitVector
source§impl IntoIterator for BitVector
impl IntoIterator for BitVector
source§impl PartialEq for BitVector
impl PartialEq for BitVector
source§impl SpaceUsage for BitVector
impl SpaceUsage for BitVector
source§fn space_usage_byte(&self) -> usize
fn space_usage_byte(&self) -> usize
Returns the space usage in bytes.
source§fn space_usage_KiB(&self) -> f64
fn space_usage_KiB(&self) -> f64
source§fn space_usage_MiB(&self) -> f64
fn space_usage_MiB(&self) -> f64
source§fn space_usage_GiB(&self) -> f64
fn space_usage_GiB(&self) -> f64
impl Eq for BitVector
impl StructuralPartialEq for BitVector
Auto Trait Implementations§
impl Freeze for BitVector
impl RefUnwindSafe for BitVector
impl Send for BitVector
impl Sync for BitVector
impl Unpin for BitVector
impl UnwindSafe for BitVector
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)