Compare commits
No commits in common. "6fa7b62d2676c356300758724b6a2f41fc9cd12b" and "3a07eae54ff7ffee98f134e5721cbad7b322a847" have entirely different histories.
6fa7b62d26
...
3a07eae54f
5 changed files with 11 additions and 55 deletions
|
|
@ -8,4 +8,4 @@ bitvec = "1.0.1"
|
|||
|
||||
[dev-dependencies]
|
||||
quickcheck = "1"
|
||||
quickcheck_macros = "1"
|
||||
quickcheck_macros = "1"
|
||||
|
|
@ -53,12 +53,7 @@ pub enum Node {
|
|||
|
||||
impl Node {
|
||||
fn build(counts: &HashMap<u8, u32>) -> Option<Self> {
|
||||
let branch = match WeightedNode::build(counts).map(WeightedNode::unburden)? {
|
||||
leaf @ Node::Leaf { .. } => Self::join(leaf.clone(), leaf),
|
||||
branch @ Node::Branch { .. } => branch,
|
||||
};
|
||||
|
||||
Some(branch)
|
||||
WeightedNode::build(counts).map(WeightedNode::unburden)
|
||||
}
|
||||
|
||||
/// Write to `buf` the sequence of bits that this tree has assigned `byte`.
|
||||
|
|
@ -125,13 +120,6 @@ impl Node {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn join(path0: Self, path1: Self) -> Self {
|
||||
Self::Branch {
|
||||
path0: Box::new(path0),
|
||||
path1: Box::new(path1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Node {
|
||||
|
|
|
|||
28
src/id.rs
28
src/id.rs
|
|
@ -1,28 +0,0 @@
|
|||
use std::io::Read;
|
||||
|
||||
use bitvec::prelude::{BitSlice, BitVec};
|
||||
|
||||
use crate::CompressionScheme;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Id;
|
||||
|
||||
impl CompressionScheme for Id {
|
||||
type Header = ();
|
||||
|
||||
fn encode(src: &[u8], buf: &mut BitVec) -> Self::Header {
|
||||
buf.extend_from_bitslice(BitSlice::<u8>::from_slice(src));
|
||||
}
|
||||
|
||||
fn decode(src: &BitSlice, (): &Self::Header, buf: &mut Vec<u8>) {
|
||||
// TODO: is this actually fine
|
||||
#[allow(clippy::unbuffered_bytes)]
|
||||
for byte in src.bytes() {
|
||||
buf.push(byte.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
fn header_size((): &Self::Header) -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,6 @@ use std::{
|
|||
mod compression_scheme;
|
||||
mod freq;
|
||||
mod huffman;
|
||||
mod id;
|
||||
mod rle;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
|
@ -16,18 +15,18 @@ use bitvec::vec::BitVec;
|
|||
pub use compression_scheme::CompressionScheme;
|
||||
pub use freq::Freq;
|
||||
pub use huffman::Huffman;
|
||||
pub use id::Id;
|
||||
pub use rle::Rle;
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let debug = env::args().any(|arg| arg == "--debug" || arg == "-d");
|
||||
|
||||
let mut buf = Vec::new();
|
||||
io::stdin().read_to_end(&mut buf)?;
|
||||
let len_src = io::stdin().read_to_end(&mut buf)?;
|
||||
|
||||
println!("Original: {len_src}");
|
||||
|
||||
let mut bitbuf = BitVec::new();
|
||||
|
||||
run::<Id, _>(&buf, &mut bitbuf, "Original", debug);
|
||||
run::<Rle, _>(&buf, &mut bitbuf, "rle", debug);
|
||||
run::<Freq, _>(&buf, &mut bitbuf, "freq", debug);
|
||||
run::<Huffman, _>(&buf, &mut bitbuf, "Huffman", debug);
|
||||
|
|
@ -43,7 +42,7 @@ where
|
|||
bitbuf.clear();
|
||||
let header = Scheme::encode(buf, bitbuf);
|
||||
let len_freq = Scheme::header_size(&header) + bitbuf.len().div_ceil(8);
|
||||
println!("{name:>10}: {len_freq:>10}");
|
||||
println!("{name}'d: {len_freq}");
|
||||
if debug {
|
||||
eprintln!("{name} header: {header:#?}\n");
|
||||
}
|
||||
|
|
|
|||
13
src/test.rs
13
src/test.rs
|
|
@ -1,24 +1,21 @@
|
|||
#![allow(clippy::needless_pass_by_value)]
|
||||
|
||||
use crate::{CompressionScheme, Freq, Huffman, Id, Rle};
|
||||
use quickcheck_macros::quickcheck;
|
||||
|
||||
use crate::{CompressionScheme, Freq, Huffman, Rle};
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
#[quickcheck]
|
||||
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)
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
#[quickcheck]
|
||||
fn roundtrip_huffman(src: Vec<u8>) -> bool {
|
||||
Huffman::idempotent_on(&src)
|
||||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn roundtrip_id(src: Vec<u8>) -> bool {
|
||||
Id::idempotent_on(&src)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue