|
| 1 | +use std::io::Cursor; |
| 2 | + |
| 3 | +use crate::cursor::IncrementCursor; |
| 4 | +use crate::integer_codec::IntegerCodec; |
| 5 | +use crate::{Compressor, FastPForResult}; |
| 6 | + |
| 7 | +pub struct Composition { |
| 8 | + c1: IntegerCodec, |
| 9 | + c2: IntegerCodec, |
| 10 | +} |
| 11 | + |
| 12 | +impl Composition { |
| 13 | + pub fn new<C1, C2>(c1: C1, c2: C2) -> Self |
| 14 | + where |
| 15 | + C1: Into<IntegerCodec>, |
| 16 | + C2: Into<IntegerCodec>, |
| 17 | + { |
| 18 | + Composition { |
| 19 | + c1: c1.into(), |
| 20 | + c2: c2.into(), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn compress( |
| 25 | + &mut self, |
| 26 | + input: &[i32], |
| 27 | + mut input_length: i32, |
| 28 | + input_offset: &mut Cursor<i32>, |
| 29 | + output: &mut [i32], |
| 30 | + output_offset: &mut Cursor<i32>, |
| 31 | + ) -> FastPForResult<()> { |
| 32 | + if input_length == 0 { |
| 33 | + // Return early if there is no data to compress |
| 34 | + return Ok(()); |
| 35 | + } |
| 36 | + let inpos_init = input_offset.position(); |
| 37 | + let outpos_init = output_offset.position(); |
| 38 | + self.c1 |
| 39 | + .compress(input, input_length, input_offset, output, output_offset)?; |
| 40 | + if output_offset.position() == outpos_init { |
| 41 | + output[outpos_init as usize] = 0; |
| 42 | + output_offset.increment(); |
| 43 | + } |
| 44 | + input_length -= input_offset.position() as i32 - inpos_init as i32; |
| 45 | + self.c2 |
| 46 | + .compress(input, input_length, input_offset, output, output_offset) |
| 47 | + } |
| 48 | + |
| 49 | + pub fn uncompress( |
| 50 | + &mut self, |
| 51 | + input: &[i32], |
| 52 | + mut input_length: i32, |
| 53 | + input_offset: &mut Cursor<i32>, |
| 54 | + output: &mut [i32], |
| 55 | + output_offset: &mut Cursor<i32>, |
| 56 | + ) -> FastPForResult<()> { |
| 57 | + if input_length == 0 { |
| 58 | + // Return early if there is no data to compress |
| 59 | + return Ok(()); |
| 60 | + } |
| 61 | + let final_init = input_offset.position() as i32; |
| 62 | + self.c1 |
| 63 | + .uncompress(input, input_length, input_offset, output, output_offset)?; |
| 64 | + input_length -= input_offset.position() as i32 - final_init; |
| 65 | + self.c2 |
| 66 | + .uncompress(input, input_length, input_offset, output, output_offset) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + use crate::fastpfor::FastPFOR; |
| 74 | + use crate::variable_byte::VariableByte; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_composition() { |
| 78 | + let fastpfor = FastPFOR::default(); |
| 79 | + let vb = VariableByte::new(); |
| 80 | + let mut comp = Composition::new(fastpfor, vb); |
| 81 | + let input = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 82 | + let mut output = vec![0; 10]; |
| 83 | + let mut input_offset = Cursor::new(0); |
| 84 | + let mut output_offset = Cursor::new(0); |
| 85 | + let input_length = 10; |
| 86 | + comp.compress( |
| 87 | + &input, |
| 88 | + input_length, |
| 89 | + &mut input_offset, |
| 90 | + &mut output, |
| 91 | + &mut output_offset, |
| 92 | + ) |
| 93 | + .expect("Failed to compress"); |
| 94 | + } |
| 95 | +} |
0 commit comments