BitVec

Struct BitVec 

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

BitVec represents a bit vector that supports 4 operations:

  1. Marking a position as set.
  2. Checking if a position is set.
  3. Count set bits.
  4. Get the index of the last set bit.

Internally, it stores a vector of u8’s (as Vec).

  • The first 8 positions of the bit vector are encoded in the first element of the vector, the next 8 are encoded in the second element, and so on.
  • Bits are read from left to right. For instance, in the following bitvec [0b0001_0000, 0b0000_0000, 0b0000_0000, 0b0000_0001], the 3rd and 31st positions are set.
  • Each bit of a u8 is set to 1 if the position is set and to 0 if it’s not.
  • We only allow setting positions upto u16::MAX. As a result, the size of the inner vector is limited to 8192 (= 65536 / 8).
  • Once a bit has been set, it cannot be unset. As a result, the inner vector cannot shrink.
  • The positions can be set in any order.
  • A position can set more than once – it remains set after the first time.

§Examples:

use aptos_bitvec::BitVec;
use std::ops::BitAnd;

let mut bv = BitVec::default();
bv.set(2);
bv.set(5);
assert!(bv.is_set(2));
assert!(bv.is_set(5));
assert_eq!(false, bv.is_set(0));
assert_eq!(bv.count_ones(), 2);
assert_eq!(bv.last_set_bit(), Some(5));

// A bitwise AND of BitVec can be performed by using the `&` operator.
let mut bv1 = BitVec::default();
bv1.set(2);
bv1.set(3);
let mut bv2 = BitVec::default();
bv2.set(2);
let intersection = bv1.bitand(&bv2);
assert!(intersection.is_set(2));
assert_eq!(false, intersection.is_set(3));

Implementations§

Source§

impl BitVec

Source

pub fn with_num_bits(num_bits: u16) -> Self

Initialize with buckets that can fit in num_bits.

Source

pub fn set(&mut self, pos: u16)

Sets the bit at position @pos.

Source

pub fn is_set(&self, pos: u16) -> bool

Checks if the bit at position @pos is set.

Source

pub fn all_zeros(&self) -> bool

Return true if the BitVec is all zeros.

Source

pub fn count_ones(&self) -> u32

Returns the number of set bits.

Source

pub fn last_set_bit(&self) -> Option<u16>

Returns the index of the last set bit.

Source

pub fn iter_ones(&self) -> impl Iterator<Item = usize> + '_

Return an Iterator over all ‘1’ bit indexes.

Source

pub fn num_buckets(&self) -> usize

Return the number of buckets.

Source

pub fn required_buckets(num_bits: u16) -> usize

Number of buckets require for num_bits.

Trait Implementations§

Source§

impl BitAnd for &BitVec

Source§

fn bitand(self, other: Self) -> Self::Output

Returns a new BitVec that is a bitwise AND of two BitVecs.

Source§

type Output = BitVec

The resulting type after applying the & operator.
Source§

impl BitOr for &BitVec

Source§

fn bitor(self, other: Self) -> Self::Output

Returns a new BitVec that is a bitwise OR of two BitVecs.

Source§

type Output = BitVec

The resulting type after applying the | operator.
Source§

impl Clone for BitVec

Source§

fn clone(&self) -> BitVec

Returns a duplicate 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<'de> Deserialize<'de> for BitVec

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<BitVec> for Vec<u8>

Source§

fn from(bitvec: BitVec) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<bool>> for BitVec

Source§

fn from(bits: Vec<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for BitVec

Source§

fn from(raw_bytes: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<u8> for BitVec

Source§

fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl PartialEq for BitVec

Source§

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

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

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 Serialize for BitVec

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 Eq for BitVec

Source§

impl StructuralPartialEq for BitVec

Auto Trait Implementations§

§

impl Freeze for BitVec

§

impl RefUnwindSafe for BitVec

§

impl Send for BitVec

§

impl Sync for BitVec

§

impl Unpin for BitVec

§

impl UnwindSafe for BitVec

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.
Source§

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