add Rle CompressionScheme and tests
This commit is contained in:
parent
d7d2125299
commit
07581599d2
2 changed files with 41 additions and 1 deletions
34
src/rle.rs
34
src/rle.rs
|
|
@ -1,3 +1,37 @@
|
|||
use std::io::Read;
|
||||
|
||||
use bitvec::{slice::BitSlice, vec::BitVec};
|
||||
|
||||
use crate::CompressionScheme;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Rle;
|
||||
|
||||
impl CompressionScheme for Rle {
|
||||
type Header = ();
|
||||
|
||||
fn encode(src: &[u8], buf: &mut BitVec) -> Self::Header {
|
||||
for Run { count, byte } in Encoder::new(src) {
|
||||
// TODO: surely this can be expressed better
|
||||
buf.extend_from_bitslice(BitSlice::<u8>::from_slice(&[count, byte]));
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(src: &BitSlice, (): &Self::Header, buf: &mut Vec<u8>) {
|
||||
#[allow(clippy::unbuffered_bytes)]
|
||||
let mut bytes = src.bytes();
|
||||
while let (Some(Ok(count)), Some(Ok(byte))) = (bytes.next(), bytes.next()) {
|
||||
for _ in 0..count {
|
||||
buf.push(byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn header_size((): &Self::Header) -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Run {
|
||||
pub count: u8,
|
||||
|
|
|
|||
|
|
@ -4,6 +4,12 @@ use crate::{CompressionScheme, Freq, Rle};
|
|||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
#[quickcheck]
|
||||
fn freq_roundtrip(src: Vec<u8>) -> bool {
|
||||
fn roundtrip_freq(src: Vec<u8>) -> bool {
|
||||
Freq::idempotent_on(&src)
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
#[quickcheck]
|
||||
fn roundtrip_rle(src: Vec<u8>) -> bool {
|
||||
Rle::idempotent_on(&src)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue