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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use std::mem;
use std::io::{self, Read, Write};
use super::super::byteorder::{WriteBytesExt, ReadBytesExt};
pub type Symbol = u8;
pub type Rank = u8;
pub const TOTAL_SYMBOLS: usize = 0x100;
pub struct MTF {
pub symbols: [Symbol; TOTAL_SYMBOLS],
}
impl MTF {
pub fn new() -> MTF {
MTF { symbols: [0; TOTAL_SYMBOLS] }
}
pub fn reset_alphabetical(&mut self) {
for (i,sym) in self.symbols.iter_mut().enumerate() {
*sym = i as Symbol;
}
}
pub fn encode(&mut self, sym: Symbol) -> Rank {
let mut next = self.symbols[0];
if next == sym {
return 0
}
let mut rank: Rank = 1;
loop {
mem::swap(&mut self.symbols[rank as usize], &mut next);
if next == sym {
break;
}
rank += 1;
assert!((rank as usize) < self.symbols.len());
}
self.symbols[0] = sym;
rank
}
pub fn decode(&mut self, rank: Rank) -> Symbol {
let sym = self.symbols[rank as usize];
debug!("\tDecoding rank {} with symbol {}", rank, sym);
for i in (0 .. rank as usize).rev() {
self.symbols[i+1] = self.symbols[i];
}
self.symbols[0] = sym;
sym
}
}
pub struct Encoder<W> {
w: W,
mtf: MTF,
}
impl<W> Encoder<W> {
pub fn new(w: W) -> Encoder<W> {
let mut mtf = MTF::new();
mtf.reset_alphabetical();
Encoder {
w: w,
mtf: mtf,
}
}
pub fn finish(self) -> W {
self.w
}
}
impl<W: Write> Write for Encoder<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
for sym in buf.iter() {
let rank = self.mtf.encode(*sym);
try!(self.w.write_u8(rank));
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
self.w.flush()
}
}
pub struct Decoder<R> {
r: R,
mtf: MTF,
}
impl<R> Decoder<R> {
pub fn new(r: R) -> Decoder<R> {
let mut mtf = MTF::new();
mtf.reset_alphabetical();
Decoder {
r: r,
mtf: mtf,
}
}
pub fn finish(self) -> R {
self.r
}
}
impl<R: Read> Read for Decoder<R> {
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
let mut bytes_read = 0;
for sym in dst.iter_mut() {
let rank = match self.r.read_u8() {
Ok(r) => r,
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
Err(e) => return Err(e)
};
bytes_read += 1;
*sym = self.mtf.decode(rank);
}
Ok(bytes_read)
}
}
#[cfg(test)]
mod test {
use std::io::{self, Read, Write};
#[cfg(feature="unstable")]
use test::Bencher;
use super::{Encoder, Decoder};
fn roundtrip(bytes: &[u8]) {
info!("Roundtrip MTF of size {}", bytes.len());
let buf = Vec::new();
let mut e = Encoder::new(io::BufWriter::new(buf));
e.write_all(bytes).unwrap();
let encoded = e.finish().into_inner().unwrap();
debug!("Roundtrip MTF input: {:?}, ranks: {:?}", bytes, encoded);
let mut d = Decoder::new(io::BufReader::new(&encoded[..]));
let mut decoded = Vec::new();
d.read_to_end(&mut decoded).unwrap();
assert_eq!(&decoded[..], bytes);
}
#[test]
fn some_roundtrips() {
roundtrip(b"teeesst_mtf");
roundtrip(b"");
roundtrip(include_bytes!("../data/test.txt"));
}
#[cfg(feature="unstable")]
#[bench]
fn encode_speed(bh: &mut Bencher) {
let vec = Vec::new();
let input = include_bytes!("../data/test.txt");
let mem = io::BufWriter::with_capacity(input.len(), vec);
let mut e = Encoder::new(mem);
bh.iter(|| {
e.write_all(input).unwrap();
});
bh.bytes = input.len() as u64;
}
#[cfg(feature="unstable")]
#[bench]
fn decode_speed(bh: &mut Bencher) {
let vec = Vec::new();
let input = include_bytes!("../data/test.txt");
let mut e = Encoder::new(io::BufWriter::new(vec));
e.write_all(input).unwrap();
let encoded = e.finish().into_inner().unwrap();
bh.iter(|| {
let mut d = Decoder::new(io::BufReader::new(&encoded[..]));
let mut buf = Vec::new();
d.read_to_end(&mut buf).unwrap();
});
bh.bytes = input.len() as u64;
}
}