CountedBitmap

Struct CountedBitmap 

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

A compact list of bits.

Implementations§

Source§

impl CountedBitmap

Source

pub fn new(bitcount: usize) -> Self

Create a new bit vector which can hold bitcount number of bits.

All bits will be initialized to 0.

Source

pub fn len(&self) -> usize

Return the number of bits in bitmap.

use bmap::CountedBitmap;

let bmap = CountedBitmap::new(42);
assert_eq!(bmap.len(), 42);
Source

pub fn is_empty(&self) -> bool

Returns true if no bits have been set.

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(42);
assert!(bmap.is_empty());

bmap.set(11);
assert!(!bmap.is_empty());

// Special case -- a zero-length bitmap is considered to be empty
let bmap = CountedBitmap::new(0);
assert!(bmap.is_empty());
Source

pub fn remain(&self) -> usize

Return number of bits that have not been set. (I.e. number of bits remaining to be set).

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(4);
assert_eq!(bmap.remain(), 4);
bmap.set(1);
assert_eq!(bmap.remain(), 3);
Source

pub fn progress(&self) -> f64

Return how many many bits are set to 1 in relative terms. The returned value will be a value between 0.0 and 1.0 (inclusive-inclusive). For a 0-length bit vector the value 1.0 will be returned.

Source

pub fn is_finished(&self) -> bool

Return a boolean indicating whether all bits have been set.

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(2);
assert!(bmap.is_finished() == false);
bmap.set(0);
assert!(bmap.is_finished() == false);
bmap.set(1);
assert!(bmap.is_finished() == true);

// Special case: zero-length bitmap is always finished.
let mut bmap = CountedBitmap::new(0);
assert!(bmap.is_finished() == true);
Source

pub fn is_set(&self, idx: usize) -> Result<bool, Error>

Given a bit index, return true is the corresponding bit is set. Return false otherwise.

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(2);
assert_eq!(bmap.is_set(0).unwrap(), false);
bmap.set(0).unwrap();
assert_eq!(bmap.is_set(0).unwrap(), true);
Source

pub fn set(&mut self, idx: usize) -> Result<(), Error>

Set a bit.

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(4);
bmap.set(2).unwrap();
Source

pub unsafe fn set_unchecked(&mut self, idx: usize)

Set a bit, where it is assumed that the caller has validated that the idx parameter is valid.

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(4);
unsafe { bmap.set_unchecked(2) };
assert_eq!(bmap.is_set(2).unwrap(), true);
§Safety

The caller must ensure that idx is within bounds.

Source

pub fn unset(&mut self, idx: usize) -> Result<(), Error>

Clear a bit.

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(4);
bmap.set(2).unwrap();
assert_eq!(bmap.is_set(2).unwrap(), true);
bmap.unset(2).unwrap();
assert_eq!(bmap.is_set(2).unwrap(), false);
Source

pub fn clear(&mut self)

Clear the bitmap.

Sets all bits to zero and resets the remaining counter.

Source

pub fn cond_set<F>(&mut self, idx: usize, f: F) -> Result<bool, Error>
where F: FnMut() -> bool,

If bit at the specified index is not set, then call closure. If closure returns true then set bit.

use bmap::CountedBitmap;

let mut bmap = CountedBitmap::new(1);
let mut set_to_true = false;
let ret = bmap
  .cond_set(0, || {
    set_to_true = true;
    true
  })
  .unwrap();
assert_eq!(ret, true);
assert_eq!(bmap.is_finished(), true);
assert_eq!(set_to_true, true);
Source§

impl CountedBitmap

Iterators.

Source

pub fn iter(&self) -> BitIter<'_>

Return an iterator that returns each of the container’s bit values as booleans.

use bmap::CountedBitmap;
let mut bmap = CountedBitmap::new(4);
bmap.set(1).unwrap();
bmap.set(2).unwrap();
let mut it = bmap.iter().enumerate();
assert_eq!(it.next(), Some((0, false)));
assert_eq!(it.next(), Some((1, true)));
assert_eq!(it.next(), Some((2, true)));
assert_eq!(it.next(), Some((3, false)));
assert_eq!(it.next(), None);
Source

pub fn iter_zeroes(&self) -> BitValIter<'_>

Create an iterator that will return the indexes of all zeroes in the bit map.

use bmap::CountedBitmap;
let mut bmap = CountedBitmap::new(4);
bmap.set(1).unwrap();
bmap.set(2).unwrap();
let mut it = bmap.iter_zeroes();
assert_eq!(it.next(), Some(0));
assert_eq!(it.next(), Some(3));
assert_eq!(it.next(), None);
Source

pub fn iter_ones(&self) -> BitValIter<'_>

Create an iterator that will return the indexes of all ones in the bit map.

use bmap::CountedBitmap;
let mut bmap = CountedBitmap::new(4);
bmap.set(1).unwrap();
bmap.set(2).unwrap();
let mut it = bmap.iter_ones();
assert_eq!(it.next(), Some(1));
assert_eq!(it.next(), Some(2));
assert_eq!(it.next(), None);
Source

pub fn iter_zeroes_block(&self) -> BitBlocksIter<'_>

Create an iterator that will return index ranges of contiguous blocks of zeroes.

use bmap::CountedBitmap;
let mut bmap = CountedBitmap::new(4);
bmap.set(0).unwrap();
bmap.set(1).unwrap();
let mut it = bmap.iter_zeroes_block();
assert_eq!(it.next(), Some((2, 3)));
assert_eq!(it.next(), None);
Source

pub fn iter_ones_block(&self) -> BitBlocksIter<'_>

Create an iterator that will return index ranges of contiguous blocks of ones.

use bmap::CountedBitmap;
let mut bmap = CountedBitmap::new(4);
bmap.set(0).unwrap();
bmap.set(1).unwrap();
let mut it = bmap.iter_ones_block();
assert_eq!(it.next(), Some((0, 1)));
assert_eq!(it.next(), None);

Trait Implementations§

Source§

impl Debug for CountedBitmap

Source§

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

Formats the value using the given formatter. Read more

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> 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, 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.