Struct SetBitCounter
pub struct SetBitCounter<'a> {
chunks: BitChunks<'a>,
iter: BitChunkIterator<'a>,
remainder: u64,
word: u64,
word_start: usize,
count_before_word: usize,
len: usize,
}Expand description
Counts the set bits in ranges of a packed bitmask
Self::count_range returns the number of set bits in a range, and
Self::count_before the number before a position. A result depends only
on the arguments, so calls can come in any order and repeat.
§Example
let bits = BooleanBuffer::from(vec![true, false, true, true, false, true]);
let mut counter = bits.set_bit_counter();
assert_eq!(counter.count_range(0..2), 1);
assert_eq!(counter.count_range(2..4), 2);
assert_eq!(counter.count_before(6), 4);
// Calls can go back, and a repeated call returns the same count
assert_eq!(counter.count_range(0..2), 1);§Performance
The counter keeps the 64-bit word that holds the last position, and a call
reads the words between that position and the new one. When positions do
not decrease, as in a pass over the rows of a list, each word is read once,
and a call within the current word costs a mask and a popcount. Calling
Buffer::count_set_bits_offset for each range instead pays a setup cost on
every call, which dominates for short ranges.
When positions jump across the bitmask, such as the rows of a list view in
random order, a call reads every word it jumps over. Calling
Buffer::count_set_bits_offset for each range is faster there.
§Example: Count the valid elements of each row of a list
// A list with rows of 3, 0, and 3 elements, and the validity of its elements
let offsets = OffsetBuffer::<i32>::from_lengths([3, 0, 3]);
let valid = BooleanBuffer::from(vec![true, false, true, true, false, true]);
// One counter for all rows. The rows are visited in order, so each word of
// `valid` is read once.
let mut counter = valid.set_bit_counter();
// Each pair of adjacent offsets is the start and end of a row
let valid_per_row: Vec<usize> = offsets
.windows(2)
.map(|w| counter.count_range(w[0] as usize..w[1] as usize))
.collect();
assert_eq!(valid_per_row, [2, 0, 2]);Fields§
§chunks: BitChunks<'a>§iter: BitChunkIterator<'a>§remainder: u64§word: u64§word_start: usize§count_before_word: usize§len: usizeImplementations§
§impl<'a> SetBitCounter<'a>
impl<'a> SetBitCounter<'a>
pub fn new(buffer: &'a [u8], offset: usize, len: usize) -> SetBitCounter<'a>
pub fn new(buffer: &'a [u8], offset: usize, len: usize) -> SetBitCounter<'a>
Create a new SetBitCounter from the provided buffer,
and offset and len in bits
// Bits 3..9 of the buffer are 1, 0, 1, 1, 0, 1
let mut counter = SetBitCounter::new(&[0b0110_1000, 0b0000_0001], 3, 6);
assert_eq!(counter.count_range(2..4), 2);
assert_eq!(counter.count_before(6), 4);§Panics
Panics if buffer is too short for the provided offset and length
pub fn count_before(&mut self, end: usize) -> usize
pub fn count_before(&mut self, end: usize) -> usize
Returns the number of set bits before position end
let bits = BooleanBuffer::from(vec![true, false, true, true, false, true]);
let mut counter = bits.set_bit_counter();
assert_eq!(counter.count_before(0), 0);
assert_eq!(counter.count_before(3), 2);
assert_eq!(counter.count_before(6), 4);§Panics
Panics if end is greater than the length of the bitmask
pub fn count_range(&mut self, range: Range<usize>) -> usize
pub fn count_range(&mut self, range: Range<usize>) -> usize
Returns the number of set bits in range
let bits = BooleanBuffer::from(vec![true, false, true, true, false, true]);
let mut counter = bits.set_bit_counter();
assert_eq!(counter.count_range(0..2), 1);
assert_eq!(counter.count_range(2..2), 0);
assert_eq!(counter.count_range(2..4), 2);§Panics
Panics if range.start is greater than range.end, or if range.end
is greater than the length of the bitmask