Struct qwt::bitvector::BitVector

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

Implementation of an immutable bit vector.

Implementations§

source§

impl BitVector

source

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

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

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

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

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

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

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.

source

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
source

pub fn is_empty(&self) -> bool

Checks if the bit vector is empty.

§Returns

Returns true if the bit vector is empty, false otherwise.

§Examples
use qwt::BitVector;

let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();

assert!(!bv.is_empty());
source

pub fn len(&self) -> usize

Returns the number of bits in the bit vector.

§Returns

The number of bits 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.len(), 6);
source

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

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

source§

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

Returns the bit at the given position index, or None if index is out of bounds.

§Examples
use qwt::{BitVector, AccessBin};

let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();

assert_eq!(bv.get(5), Some(true));
assert_eq!(bv.get(1), Some(false));
assert_eq!(bv.get(10), None);
source§

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

Returns the bit at position index.

§Safety

Calling this method with an out-of-bounds index is undefined behavior.

§Examples
use qwt::{BitVector, AccessBin};

let v = vec![0,2,3,4,5];
let bv: BitVector = v.into_iter().collect();

assert_eq!(unsafe{bv.get_unchecked(5)}, true);
source§

impl AsRef<BitVector> for BitVector

source§

fn as_ref(&self) -> &BitVector

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for BitVector

source§

fn clone(&self) -> BitVector

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 BitVector

source§

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

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

impl Default for BitVector

source§

fn default() -> BitVector

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

impl<'de> Deserialize<'de> for BitVector

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

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§

fn from(bv: BitVector) -> Self

Converts to this type from the input type.
source§

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

Converts to this type from the input type.
source§

impl<V> FromIterator<V> for BitVector
where V: MyPrimInt, <V as TryInto<usize>>::Error: Debug,

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§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = V>, <V as TryInto<usize>>::Error: Debug,

Creates a value from an iterator. Read more
source§

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§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = bool>,

Creates a value from an iterator. Read more
source§

impl<'a> IntoIterator for &'a BitVector

§

type IntoIter = BitVectorIter<'a>

Which kind of iterator are we turning this into?
§

type Item = bool

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for BitVector

§

type IntoIter = BitVectorIntoIter

Which kind of iterator are we turning this into?
§

type Item = bool

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for BitVector

source§

fn eq(&self, other: &BitVector) -> 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 Serialize for BitVector

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl SpaceUsage for BitVector

source§

fn space_usage_byte(&self) -> usize

Returns the space usage in bytes.

source§

fn space_usage_KiB(&self) -> f64

Gives the space usage of the data structure in KiB.
source§

fn space_usage_MiB(&self) -> f64

Gives the space usage of the data structure in MiB.
source§

fn space_usage_GiB(&self) -> f64

Gives the space usage of the data structure in GiB.
source§

impl Eq for BitVector

source§

impl StructuralPartialEq 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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,