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 extending mutablebitmap. (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Sep 10, 2021
1 parent 8feda68 commit f326fed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/bitmap/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,14 @@ fn extend<I: Iterator<Item = bool>>(buffer: &mut [u8], length: usize, mut iterat

buffer[..chunks].iter_mut().for_each(|byte| {
(0..8).for_each(|i| {
if iterator.next().unwrap() {
*byte = set(*byte, i, true)
}
*byte = set(*byte, i, iterator.next().unwrap());
})
});

if reminder != 0 {
let last = &mut buffer[chunks];
iterator.enumerate().for_each(|(i, value)| {
if value {
*last = set(*last, i, true)
}
*last = set(*last, i, value);
});
}
}
Expand Down Expand Up @@ -354,9 +350,7 @@ impl MutableBitmap {
let byte = self.buffer.as_mut_slice().last_mut().unwrap();
let mut i = bit_offset;
for value in iterator {
if value {
*byte = set(*byte, i, true);
}
*byte = set(*byte, i, value);
i += 1;
}
self.length += length;
Expand All @@ -370,10 +364,7 @@ impl MutableBitmap {
// we are in the middle of a byte; lets finish it
let byte = self.buffer.as_mut_slice().last_mut().unwrap();
(bit_offset..8).for_each(|i| {
let value = iterator.next().unwrap();
if value {
*byte = set(*byte, i, true);
}
*byte = set(*byte, i, iterator.next().unwrap());
});
self.length += 8 - bit_offset;
length -= 8 - bit_offset;
Expand Down Expand Up @@ -445,19 +436,15 @@ impl MutableBitmap {
let data = buffer.as_mut_slice();
data[..chunks].iter_mut().try_for_each(|byte| {
(0..8).try_for_each(|i| {
if iterator.next().unwrap()? {
*byte = set(*byte, i, true)
};
*byte = set(*byte, i, iterator.next().unwrap()?);
Ok(())
})
})?;

if reminder != 0 {
let last = &mut data[chunks];
iterator.enumerate().try_for_each(|(i, value)| {
if value? {
*last = set(*last, i, true)
}
*last = set(*last, i, value?);
Ok(())
})?;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/it/bitmap/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,15 @@ fn extend_bitmap_one() {
assert_eq!(b.as_slice(), &[0b11111111]);
}
}

#[test]
fn extend_bitmap_other() {
let mut a = MutableBitmap::from([true, true, true, false, true, true, true, false, true, true]);
a.extend_from_slice(&[0b01111110u8, 0b10111111, 0b11011111, 0b00000111], 20, 2);
assert_eq!(
a,
MutableBitmap::from([
true, true, true, false, true, true, true, false, true, true, true, false
])
);
}

0 comments on commit f326fed

Please sign in to comment.