Struct indexed_bitvec_core::Bits
source · Expand description
Bits stored as a sequence of bytes (most significant bit first).
Implementations§
source§impl<T: Deref<Target = [u8]>> Bits<T>
impl<T: Deref<Target = [u8]>> Bits<T>
pub fn from(bytes: T, used_bits: u64) -> Option<Self>
sourcepub fn all_bytes(&self) -> &[u8]
pub fn all_bytes(&self) -> &[u8]
All of the bytes stored in the byte sequence: not just the ones actually used.
sourcepub fn bytes(&self) -> &[u8]
pub fn bytes(&self) -> &[u8]
The used bytes of the byte sequence: bear in mind some of the bits in the last byte may be unused.
sourcepub fn decompose(self) -> (T, u64)
pub fn decompose(self) -> (T, u64)
Deconstruct the bits storage to get back what it was constructed from.
sourcepub fn get(&self, idx_bits: u64) -> Option<bool>
pub fn get(&self, idx_bits: u64) -> Option<bool>
Get the byte at a specific index.
Returns None
for out-of-bounds.
use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.get(0), Some(true));
assert_eq!(bits.get(7), Some(false));
assert_eq!(bits.get(14), Some(true));
assert_eq!(bits.get(15), None);
sourcepub fn count_ones(&self) -> u64
pub fn count_ones(&self) -> u64
Count the set bits (O(n)).
use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.count_ones(), 14);
assert_eq!(bits.count_zeros(), 1);
assert_eq!(bits.count_ones() + bits.count_zeros(), bits.used_bits());
sourcepub fn count_zeros(&self) -> u64
pub fn count_zeros(&self) -> u64
Count the unset bits (O(n)).
use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.count_ones(), 14);
assert_eq!(bits.count_zeros(), 1);
assert_eq!(bits.count_ones() + bits.count_zeros(), bits.used_bits());
sourcepub fn rank_ones(&self, idx: u64) -> Option<u64>
pub fn rank_ones(&self, idx: u64) -> Option<u64>
Count the set bits before a position in the bits (O(n)).
Returns None
it the index is out of bounds.
use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert!((0..bits.used_bits()).all(|idx|
bits.rank_ones(idx).unwrap()
+ bits.rank_zeros(idx).unwrap()
== (idx as u64)));
assert_eq!(bits.rank_ones(7), Some(7));
assert_eq!(bits.rank_zeros(7), Some(0));
assert_eq!(bits.rank_ones(8), Some(7));
assert_eq!(bits.rank_zeros(8), Some(1));
assert_eq!(bits.rank_ones(9), Some(8));
assert_eq!(bits.rank_zeros(9), Some(1));
assert_eq!(bits.rank_ones(15), None);
sourcepub fn rank_zeros(&self, idx: u64) -> Option<u64>
pub fn rank_zeros(&self, idx: u64) -> Option<u64>
Count the unset bits before a position in the bits (O(n)).
Returns None
it the index is out of bounds.
use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert!((0..bits.used_bits()).all(|idx|
bits.rank_ones(idx).unwrap()
+ bits.rank_zeros(idx).unwrap()
== (idx as u64)));
assert_eq!(bits.rank_ones(7), Some(7));
assert_eq!(bits.rank_zeros(7), Some(0));
assert_eq!(bits.rank_ones(8), Some(7));
assert_eq!(bits.rank_zeros(8), Some(1));
assert_eq!(bits.rank_ones(9), Some(8));
assert_eq!(bits.rank_zeros(9), Some(1));
assert_eq!(bits.rank_ones(15), None);
sourcepub fn select_ones(&self, target_rank: u64) -> Option<u64>
pub fn select_ones(&self, target_rank: u64) -> Option<u64>
Find the position of a set bit by its rank (O(n)).
Returns None
if no suitable bit is found. It is
always the case otherwise that rank_ones(result) == Some(target_rank)
and get(result) == Some(true)
.
use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.select_ones(6), Some(6));
assert_eq!(bits.select_ones(7), Some(8));
assert_eq!(bits.select_zeros(0), Some(7));
assert_eq!(bits.select_zeros(1), None);
sourcepub fn select_zeros(&self, target_rank: u64) -> Option<u64>
pub fn select_zeros(&self, target_rank: u64) -> Option<u64>
Find the position of an unset bit by its rank (O(n)).
Returns None
if no suitable bit is found. It is
always the case otherwise that rank_zeros(result) == Some(target_rank)
and get(result) == Some(false)
.
use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.select_ones(6), Some(6));
assert_eq!(bits.select_ones(7), Some(8));
assert_eq!(bits.select_zeros(0), Some(7));
assert_eq!(bits.select_zeros(1), None);