forked from fast-pack/FastPFOR-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.rs
More file actions
153 lines (135 loc) · 4.16 KB
/
Copy pathcodec.rs
File metadata and controls
153 lines (135 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::io::Cursor;
use crate::rust::{FastPFOR, FastPForResult, Integer, JustCopy, VariableByte};
use crate::CodecToSlice;
/// Type-erased wrapper for compression codecs.
///
/// Allows different codec types to be used interchangeably through a unified interface.
pub enum Codec {
/// [`FastPFOR`] compression codec
FastPFor(Box<FastPFOR>),
/// [`VariableByte`] compression codec
VariableByte(VariableByte),
/// Pass-through codec (no compression)
JustCopy(JustCopy),
}
impl Integer<u32> for Codec {
fn compress(
&mut self,
input: &[u32],
input_length: u32,
input_offset: &mut Cursor<u32>,
output: &mut [u32],
output_offset: &mut Cursor<u32>,
) -> FastPForResult<()> {
match self {
Codec::FastPFor(fastpfor) => {
fastpfor.compress(input, input_length, input_offset, output, output_offset)
}
Codec::VariableByte(vb) => {
vb.compress(input, input_length, input_offset, output, output_offset)
}
Codec::JustCopy(jc) => {
jc.compress(input, input_length, input_offset, output, output_offset)
}
}
}
fn uncompress(
&mut self,
input: &[u32],
input_length: u32,
input_offset: &mut Cursor<u32>,
output: &mut [u32],
output_offset: &mut Cursor<u32>,
) -> FastPForResult<()> {
match self {
Codec::FastPFor(fastpfor) => {
fastpfor.uncompress(input, input_length, input_offset, output, output_offset)
}
Codec::VariableByte(vb) => {
vb.uncompress(input, input_length, input_offset, output, output_offset)
}
Codec::JustCopy(jc) => {
jc.uncompress(input, input_length, input_offset, output, output_offset)
}
}
}
}
impl CodecToSlice<u32> for Codec {
type Error = crate::rust::FastPForError;
fn compress_to_slice<'out>(
&mut self,
input: &[u32],
output: &'out mut [u32],
) -> Result<&'out [u32], Self::Error> {
let mut output_offset = Cursor::new(0);
let input_length = input
.len()
.try_into()
.map_err(|_| Self::Error::InvalidInputLength(input.len()))?;
self.compress(
input,
input_length,
&mut Cursor::new(0),
output,
&mut output_offset,
)?;
let written = output_offset.position() as usize;
Ok(&output[..written])
}
fn decompress_to_slice<'out>(
&mut self,
input: &[u32],
output: &'out mut [u32],
) -> Result<&'out [u32], Self::Error> {
let mut output_offset = Cursor::new(0);
let input_length: u32 = input
.len()
.try_into()
.map_err(|_| Self::Error::InvalidInputLength(input.len()))?;
self.uncompress(
input,
input_length,
&mut Cursor::new(0),
output,
&mut output_offset,
)?;
let written = output_offset.position() as usize;
Ok(&output[..written])
}
}
impl From<FastPFOR> for Codec {
fn from(fastpfor: FastPFOR) -> Self {
Codec::FastPFor(Box::new(fastpfor))
}
}
impl From<VariableByte> for Codec {
fn from(vb: VariableByte) -> Self {
Codec::VariableByte(vb)
}
}
impl From<JustCopy> for Codec {
fn from(jc: JustCopy) -> Self {
Codec::JustCopy(jc)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn supports_compress_to_slice() {
let data = vec![1, 2, 3, 4, 5];
let mut rust_codec = Codec::from(VariableByte::new());
let mut compressed = vec![0u32; data.len() * 4];
let compressed_len = {
let result = rust_codec
.compress_to_slice(&data, &mut compressed)
.unwrap();
result.len()
};
let mut decompressed = vec![0u32; data.len()];
let result = rust_codec
.decompress_to_slice(&compressed[..compressed_len], &mut decompressed)
.unwrap();
assert_eq!(result, &data[..]);
}
}