|
| 1 | +//! Public any-length `FastPFOR` codecs supporting both 32- and 64-bit integers. |
| 2 | +//! |
| 3 | +//! [`FastPFor128`] and [`FastPFor256`] are the primary entry points. |
| 4 | +//! Each implements [`AnyLenCodec`] for `u32` and [`BlockCodec64`] for `u64`. |
| 5 | +//! Aligned blocks are coded with `FastPFOR` and the sub-block remainder with variable-byte coding. |
| 6 | +
|
| 7 | +use crate::codec::{AnyLenCodec, BlockCodec64}; |
| 8 | +use crate::rust::VariableByte; |
| 9 | +use crate::rust::composite::CompositeCodec; |
| 10 | +use crate::rust::integer_compression::fastpfor::{FastPForBlock128, FastPForBlock256}; |
| 11 | +use crate::rust::integer_compression::fastpfor64::FastPForWide; |
| 12 | +use crate::FastPForResult; |
| 13 | + |
| 14 | +macro_rules! define_fastpfor { |
| 15 | + ($(#[$meta:meta])* $name:ident, $block:ty, $n:literal) => { |
| 16 | + $(#[$meta])* |
| 17 | + #[derive(Debug, Default)] |
| 18 | + pub struct $name { |
| 19 | + narrow: CompositeCodec<$block, VariableByte>, |
| 20 | + wide: FastPForWide<$n>, |
| 21 | + } |
| 22 | + |
| 23 | + impl AnyLenCodec for $name { |
| 24 | + fn encode(&mut self, input: &[u32], out: &mut Vec<u32>) -> FastPForResult<()> { |
| 25 | + self.narrow.encode(input, out) |
| 26 | + } |
| 27 | + |
| 28 | + fn decode( |
| 29 | + &mut self, |
| 30 | + input: &[u32], |
| 31 | + out: &mut Vec<u32>, |
| 32 | + expected_len: Option<u32>, |
| 33 | + ) -> FastPForResult<()> { |
| 34 | + self.narrow.decode(input, out, expected_len) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + impl BlockCodec64 for $name { |
| 39 | + fn encode64(&mut self, input: &[u64], out: &mut Vec<u32>) -> FastPForResult<()> { |
| 40 | + self.wide.encode64(input, out) |
| 41 | + } |
| 42 | + |
| 43 | + fn decode64(&mut self, input: &[u32], out: &mut Vec<u64>) -> FastPForResult<()> { |
| 44 | + self.wide.decode64(input, out) |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +define_fastpfor! { |
| 51 | + /// Any-length `FastPFOR` codec with 128-value blocks. |
| 52 | + /// |
| 53 | + /// Compresses `u32` via [`AnyLenCodec`] and `u64` via [`BlockCodec64`]. |
| 54 | + FastPFor128, FastPForBlock128, 128 |
| 55 | +} |
| 56 | + |
| 57 | +define_fastpfor! { |
| 58 | + /// Any-length `FastPFOR` codec with 256-value blocks. |
| 59 | + /// |
| 60 | + /// Compresses `u32` via [`AnyLenCodec`] and `u64` via [`BlockCodec64`]. |
| 61 | + FastPFor256, FastPForBlock256, 256 |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn one_codec_handles_both_widths() { |
| 70 | + let mut codec = FastPFor256::default(); |
| 71 | + |
| 72 | + let data32: Vec<u32> = (0..600).collect(); |
| 73 | + let mut enc32 = Vec::new(); |
| 74 | + codec.encode(&data32, &mut enc32).unwrap(); |
| 75 | + let mut dec32 = Vec::new(); |
| 76 | + codec.decode(&enc32, &mut dec32, None).unwrap(); |
| 77 | + assert_eq!(dec32, data32); |
| 78 | + |
| 79 | + let data64: Vec<u64> = (0..600).map(|i| i * 1_000_000_000).collect(); |
| 80 | + let mut enc64 = Vec::new(); |
| 81 | + codec.encode64(&data64, &mut enc64).unwrap(); |
| 82 | + let mut dec64 = Vec::new(); |
| 83 | + codec.decode64(&enc64, &mut dec64).unwrap(); |
| 84 | + assert_eq!(dec64, data64); |
| 85 | + } |
| 86 | +} |
0 commit comments