Skip to main content

SetBitCounter

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: usize

Implementations§

§

impl<'a> 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

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

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

Trait Implementations§

§

impl<'a> Debug for SetBitCounter<'a>

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for SetBitCounter<'a>

§

impl<'a> Freeze for SetBitCounter<'a>

§

impl<'a> Move for SetBitCounter<'a>

§

impl<'a> RefUnwindSafe for SetBitCounter<'a>

§

impl<'a> Send for SetBitCounter<'a>

§

impl<'a> Sync for SetBitCounter<'a>

§

impl<'a> Unpin for SetBitCounter<'a>

§

impl<'a> UnsafeUnpin for SetBitCounter<'a>

§

impl<'a> UnwindSafe for SetBitCounter<'a>

Blanket Implementations§

§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

impl<T> Ungil for T
where T: Send,