Struct diem_bitvec::BitVec[][src]

pub struct BitVec { /* fields omitted */ }
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;
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

Sets the bit at position @pos.

Checks if the bit at position @pos is set.

Return true if the BitVec is all zeros.

Returns the number of set bits.

Returns the index of the last set bit.

Return an Iterator over all ‘1’ bit indexes.

Trait Implementations

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

The type of Strategy used to generate values of type Self. Read more

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

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

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

The resulting type after applying the & operator.

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

The resulting type after applying the | operator.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

Creates a value from an iterator. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.