pub struct CountedBitmap { /* private fields */ }Expand description
A compact list of bits.
Implementations§
Source§impl CountedBitmap
impl CountedBitmap
Sourcepub fn new(bitcount: usize) -> Self
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.
Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn remain(&self) -> usize
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);Sourcepub fn progress(&self) -> f64
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.
Sourcepub fn is_finished(&self) -> bool
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);Sourcepub fn is_set(&self, idx: usize) -> Result<bool, Error>
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);Sourcepub fn set(&mut self, idx: usize) -> Result<(), Error>
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();Sourcepub unsafe fn set_unchecked(&mut self, idx: usize)
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.
Sourcepub fn unset(&mut self, idx: usize) -> Result<(), Error>
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);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the bitmap.
Sets all bits to zero and resets the remaining counter.
Sourcepub fn cond_set<F>(&mut self, idx: usize, f: F) -> Result<bool, Error>
pub fn cond_set<F>(&mut self, idx: usize, f: F) -> Result<bool, Error>
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.
impl CountedBitmap
Iterators.
Sourcepub fn iter(&self) -> BitIter<'_> ⓘ
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);Sourcepub fn iter_zeroes(&self) -> BitValIter<'_> ⓘ
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);Sourcepub fn iter_ones(&self) -> BitValIter<'_> ⓘ
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);Sourcepub fn iter_zeroes_block(&self) -> BitBlocksIter<'_> ⓘ
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);Sourcepub fn iter_ones_block(&self) -> BitBlocksIter<'_> ⓘ
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);