Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed error in chunk iter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Aug 9, 2021
1 parent d545253 commit 4b49916
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/bitmap/utils/chunk_iterator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ pub struct BitChunks<'a, T: BitChunk> {
/// writes `bytes` into `dst`.
#[inline]
fn copy_with_merge<T: BitChunk>(dst: &mut T::Bytes, bytes: &[u8], bit_offset: usize) {
let mut last = bytes[bytes.len() - 1];
last >>= bit_offset;
dst[0] = last;
bytes
.windows(2)
.chain(std::iter::once([bytes[bytes.len() - 1], 0].as_ref()))
.take(std::mem::size_of::<T>())
.enumerate()
.for_each(|(i, w)| {
Expand Down Expand Up @@ -340,4 +338,23 @@ mod tests {
assert_eq!(iter.next(), None);
assert_eq!(iter.remainder(), 0b1_1111_1111u64);
}

#[test]
fn remainder_2() {
// (i % 3 == 0) in bitmap
let input: &[u8] = &[
0b01001001, 0b10010010, 0b00100100, 0b01001001, 0b10010010, 0b00100100, 0b01001001,
0b10010010, 0b00100100, 0b01001001, /* 73 */
0b10010010, /* 146 */
0b00100100, 0b00001001,
];
let offset = 10; // 8 + 2
let length = 90;

let mut iter = BitChunks::<u64>::new(input, offset, length);
let first: u64 = 0b0100100100100100100100100100100100100100100100100100100100100100;
assert_eq!(first, iter.next().unwrap());
assert_eq!(iter.next(), None);
assert_eq!(iter.remainder(), 0b10010010010010010010010010u64);
}
}
9 changes: 9 additions & 0 deletions src/bitmap/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub fn bytes_for(bits: usize) -> usize {

#[inline]
pub fn null_count(slice: &[u8], offset: usize, len: usize) -> usize {
//return BitmapIter::new(slice, offset, len).filter(|x| !*x).count();

// u64 results in optimal performance (verified via benches)
let mut chunks = chunk_iterator::BitChunks::<u64>::new(slice, offset, len);

Expand Down Expand Up @@ -138,4 +140,11 @@ mod tests {
assert_eq!(null_count(input, 13, 2), 1);
assert_eq!(null_count(input, 14, 2), 1);
}

#[test]
fn null_count_1() {
// offset = 10, len = 90 => remainder
let input: &[u8] = &[73, 146, 36, 73, 146, 36, 73, 146, 36, 73, 146, 36, 9];
assert_eq!(null_count(input, 10, 90), 60);
}
}

0 comments on commit 4b49916

Please sign in to comment.