From c230f1019568388388e1ea59c3fc1c554a2c51cf Mon Sep 17 00:00:00 2001 From: Carlos Rolo <3799585+cjrolo@users.noreply.github.com> Date: Fri, 29 Sep 2023 13:14:10 +0100 Subject: [PATCH] Decompression methods --- brro-compressor/src/compressor/constant.rs | 28 ++++++++++++++++++---- brro-compressor/src/compressor/noop.rs | 26 +++++++++++++++++--- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/brro-compressor/src/compressor/constant.rs b/brro-compressor/src/compressor/constant.rs index 274783d..6a70d09 100644 --- a/brro-compressor/src/compressor/constant.rs +++ b/brro-compressor/src/compressor/constant.rs @@ -7,7 +7,7 @@ const CONSTANT_COMPRESSOR_ID: u8 = 0; /// This is a temporary implementation, other implementations (FFT, Polynomial) might provide the same result /// as going through the data anyway. -#[derive(Encode, Decode, PartialEq, Debug)] +#[derive(Encode, Decode, PartialEq, Debug, Clone)] pub struct Constant { pub id: u8, pub constant: i64, @@ -52,7 +52,6 @@ impl Constant { } /// Compresses the data. Walks the data array and sets one value as the constant. - /// TODO: Fix residuals positions /// Performance consideration, we do O(3*n) in the worst case, best case is O(n). pub fn compress(&mut self, data: &[i64]) { // Count occurrences of each value in the data @@ -83,8 +82,17 @@ impl Constant { } } - /// This function transforms the structure into a Binary stream to be appended to the frame - pub fn to_bytes(self) -> Vec { + /// Receives a data stream and generates a Constant + pub fn decompress(data: &Vec) -> Self { + let config = BinConfig::get(); + match bincode::decode_from_slice(&data, config) { + Ok((constant, _)) => constant, + Err(e) => panic!("{e}") + } + } + + /// This function transforms the structure into a Binary stream + pub fn to_bytes(&self) -> Vec { // Use Bincode and flate2-rs? Do this at the Stream Level? let config = BinConfig::get(); bincode::encode_to_vec(self, config).unwrap() @@ -111,6 +119,18 @@ mod tests { assert_eq!(constant(&vector1), [0, 2, 0]); } + #[test] + fn test_compression() { + let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0]; + let mut c = Constant::new(vector1.len()); + c.compress(&Constant::optimize(&vector1)); + let bin_data = c.to_bytes(); + let c2 = Constant::decompress(&bin_data); + + assert_eq!(bin_data, [0, 2, 0]); + assert_eq!(c.clone(), c2); + } + #[test] fn test_define_constant() { let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0]; diff --git a/brro-compressor/src/compressor/noop.rs b/brro-compressor/src/compressor/noop.rs index 997acbd..6d4e671 100644 --- a/brro-compressor/src/compressor/noop.rs +++ b/brro-compressor/src/compressor/noop.rs @@ -4,7 +4,7 @@ use bincode::{Decode, Encode}; // 250 to optimize bincode encoding, since it checks for <251 for u8 const NOOP_COMPRESSOR_ID:u8 = 250; -#[derive(Encode, Decode, PartialEq, Debug)] +#[derive(Encode, Decode, PartialEq, Debug, Clone)] pub struct Noop { pub id: u8, pub data: Vec, @@ -32,8 +32,17 @@ impl Noop { self.data = Noop::optimize(data); } + /// Receives a data stream and generates a Constant + pub fn decompress(data: &Vec) -> Self { + let config = BinConfig::get(); + match bincode::decode_from_slice(&data, config) { + Ok((constant, _)) => constant, + Err(e) => panic!("{e}") + } + } + /// This function transforms the structure in a Binary stream to be appended to the frame - pub fn to_bytes(self) -> Vec { + pub fn to_bytes(&self) -> Vec { let config = BinConfig::get(); bincode::encode_to_vec(self, config).unwrap() } @@ -52,8 +61,19 @@ mod tests { use super::*; #[test] - fn test_constant() { + fn test_noop() { let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0]; assert_eq!(noop(&vector1), [250, 5, 2, 2, 2, 2, 2]); } + + #[test] + fn test_compression() { + let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0]; + let mut c = Noop::new(vector1.len()); + c.compress(&vector1); + let bin_data = c.to_bytes(); + let c2 = Noop::decompress(&bin_data); + + assert_eq!(c.clone(), c2); + } } \ No newline at end of file