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
extern crate bitreader;
use bitreader::BitReader;
macro_rules! visit {
($current_node:expr, $root:expr, $result:expr, $dir:ident) => {{
let node = unsafe { &*$current_node };
let $dir: &Tree = node.$dir.as_ref().unwrap();
if let Some(byte) = $dir.value {
$result.push(byte);
$current_node = $root as *const Tree;
}
else {
$current_node = $dir as *const Tree;
}
}};
}
#[derive(Default)]
pub struct Tree {
pub left: Option<Box<Tree>>,
pub right: Option<Box<Tree>>,
pub value: Option<u8>,
}
impl Tree {
pub fn new() -> Self {
Tree {
left: None,
right: None,
value: None,
}
}
pub fn new_with_value(value: u8) -> Self {
Tree {
left: None,
right: None,
value: Some(value),
}
}
}
pub fn decode(input: &[u8], tree: &Tree, decompressed_size: usize) -> Vec<u8> {
decode_with_offset(input, 0, tree, decompressed_size)
}
pub fn decode_with_offset(input: &[u8], offset: u8, tree: &Tree, decompressed_size: usize) -> Vec<u8> {
debug_assert!(offset <= 8);
let mut result = vec![];
let mut reader = BitReader::new(input);
let mut current_node = tree as *const Tree;
reader.read_u8(offset).unwrap();
while let Ok(bit) = reader.read_u8(1) {
if result.len() == decompressed_size {
break;
}
if bit == 1 {
visit!(current_node, tree, result, right);
}
else {
visit!(current_node, tree, result, left);
}
}
result
}
#[cfg(test)]
mod tests {
use std::collections::{BTreeMap, HashMap};
use std::u8;
use super::{Tree, decode, decode_with_offset};
fn create_tree(bytes: &[u8]) -> Tree {
let mut occurences = HashMap::new();
for byte in bytes {
let entry = occurences.entry(byte).or_insert(0);
*entry += 1;
}
let mut nodes = BTreeMap::new();
for (&byte, occurence) in occurences {
nodes.insert((occurence, byte), Tree::new_with_value(byte));
}
let mut index = u8::MAX;
while nodes.len() > 1 {
let ((occurence1, _), node1) = pop_first(&mut nodes);
let ((occurence2, _), node2) = pop_first(&mut nodes);
let new_occurence = occurence1 + occurence2;
let mut new_node = Tree::new();
new_node.left = Some(Box::new(node1));
new_node.right = Some(Box::new(node2));
nodes.insert((new_occurence, index), new_node);
index -= 1;
}
pop_first(&mut nodes).1
}
fn pop_first<K, V>(map: &mut BTreeMap<K, V>) -> (K, V)
where K: Clone + Ord
{
let key = map.keys().next().unwrap().clone();
let value = map.remove(&key).unwrap();
(key, value)
}
#[test]
fn test_decode() {
let input = [0b01110010, 0b00110110, 0b11100110, 0b11011110, 0b00001111, 0b10101001, 0b10000000, 0b10101100, 0b01011111, 0b10011101, 0b11110011, 0b10010010, 0b00110111, 0b01000010, 0b00001111, 0b01110101, 0b11011010];
let string = b"this is an example of a huffman tree";
let tree = create_tree(string);
let result = decode(&input, &tree, string.len());
let expected: Vec<u8> = string.iter()
.cloned()
.collect();
assert_eq!(expected, result);
let input = [0b01110010, 0b00110110, 0b11100110, 0b11011110, 0b00001111, 0b10101001, 0b10000000, 0b10101100, 0b01011111, 0b10011101, 0b11110011, 0b10010010, 0b00110111, 0b01000010, 0b00001111, 0b01110101, 0b11011010, 0b00000000];
let result = decode_with_offset(&input, 4, &tree, string.len() - 1);
assert_eq!(&expected[1..], &result[..]);
}
}