Skip to content

Commit

Permalink
Fixed error in reading zero-width bit from hybrid RLE. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Sep 29, 2021
1 parent e122c0d commit 272b200
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/encoding/hybrid_rle/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl<'a> Decoder<'a> {
}

/// Returns the number of bits being used by this decoder.
#[inline]
pub fn num_bits(&self) -> u32 {
self.num_bits
}
Expand Down
20 changes: 20 additions & 0 deletions src/encoding/hybrid_rle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum HybridEncoded<'a> {
}

enum State<'a> {
None,
Bitpacked(bitpacking::Decoder<'a>),
Rle(std::iter::Take<std::iter::Repeat<u32>>),
}
Expand All @@ -29,7 +30,12 @@ pub struct HybridRleDecoder<'a> {
remaining: usize,
}

#[inline]
fn read_next<'a, 'b>(decoder: &'b mut Decoder<'a>, remaining: usize) -> State<'a> {
if decoder.num_bits() == 0 {
return State::None;
};

let state = decoder.next().unwrap();
match state {
HybridEncoded::Bitpacked(packed) => {
Expand Down Expand Up @@ -71,6 +77,7 @@ impl<'a> Iterator for HybridRleDecoder<'a> {
let result = match &mut self.state {
State::Bitpacked(decoder) => decoder.next(),
State::Rle(iter) => iter.next(),
State::None => Some(0),
};
if let Some(result) = result {
self.remaining -= 1;
Expand Down Expand Up @@ -203,4 +210,17 @@ mod tests {

assert_eq!(result, &[2]);
}

#[test]
fn zero_bit_width() {
let data = vec![3];

let num_bits = 0;

let decoder = HybridRleDecoder::new(&data, num_bits as u32, 2);

let result = decoder.collect::<Vec<_>>();

assert_eq!(result, &[0, 0]);
}
}

0 comments on commit 272b200

Please sign in to comment.