Struct diem_bitvec::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 u8::MAX. As a result, the size of the inner vector is limited to 32 (= 256 / 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 diem_bitvec::BitVec;
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 & bv2;
assert!(intersection.is_set(2));
assert_eq!(false, intersection.is_set(3));

Implementations§

source§

impl BitVec

source

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

Sets the bit at position @pos.

source

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

Checks if the bit at position @pos is set.

source

pub fn count_ones(&self) -> u32

Returns the number of set bits.

source

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

Returns the index of the last set bit.

Trait Implementations§

source§

impl Arbitrary for BitVec

§

type Parameters = <Vec<u8, Global> as Arbitrary>::Parameters

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
§

type Strategy = Map<<Vec<u8, Global> as Arbitrary>::Strategy, fn(_: Vec<u8, Global>) -> BitVec>

The type of Strategy used to generate values of type Self.
source§

fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
source§

impl BitAnd<BitVec> for BitVec

source§

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

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

§

type Output = BitVec

The resulting type after applying the & operator.
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<'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 PartialEq<BitVec> for BitVec

source§

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

source§

impl StructuralPartialEq for BitVec

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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.
const: unstable · 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.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

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

§

fn vzip(self) -> V

source§

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