-
Notifications
You must be signed in to change notification settings - Fork 8
/
huffman.oc
289 lines (241 loc) · 6.72 KB
/
huffman.oc
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! Compression / Decompression using Huffman encoding
//!
//! Compressed Buffer Format:
//! - 4 bytes: big-endian u32, number of 1-byte characters encoded
//! - Huffman tree encoding (below)
//! - Bitstream of encoded characters, tightly packed
//!
//!
//! Huffman Tree Encoding:
//! - Single bit representing the type of the node: 0 for internal, 1 for leaf
//! - After the bit:
//! - 8 bits: the character if the node is a leaf
//! - (recursively) two more nodes if the node is an internal node
//!
//! For eg: (a ((b c) d)) is encoded as the bitstream: (with whitespace for clarity)
//! ```
//! 1
//! 0 01100001
//! 1
//! 1
//! 0 01100010
//! 0 01100011
//! 0 01100100
//! ```
//!
//! Bit order: (using std::bitio)
//! - Within a byte, read bit `i` with (byte >> i) & 1
//! - Within a byte, write bit `i` with byte |= (1 << i)
//! - For Nth bit in stream, read byte `N/8` and bit `N%8`
//!
import std::buffer::{ Buffer }
import std::heap::{ Heap }
import std::mem
import std::bitio::{ BitReader, BitWriter }
import std::sv::{ SV }
//! Decompress a huffman-compressed buffer
def decompress(inp: &Buffer): Buffer {
let total_chars = {
let reader = inp.reader()
yield reader.read_u32()
}
let remaining = inp.sv().skip(4)
let bio = BitReader::from_sv(remaining)
let tree = HuffmanTree::from_serialized_bits(&bio)
let res = Buffer::make()
let dcmp = Decompressor(
bio: bio,
res: res,
total_chars: total_chars,
tree: tree
)
let out = dcmp.decompress()
tree.free()
return out
}
//! Compress a buffer using huffman encoding
def compress(inp: &Buffer): Buffer {
let tree = HuffmanTree::from_text(inp.sv())
let codes = HuffmanCodes::make(tree)
let res = Buffer::make(capacity: inp.size)
// Number of characters encoded
res.write_u32(inp.size)
let bio = BitWriter::make(&res)
// Encode huffman tree
tree.serialize_bits(&bio, tree.root)
// Encode the characters
for c in inp.sv().chars() {
let code = codes.codes[c as u8]
for bit in code.iter_bits() {
bio.write_bit(bit)
}
}
bio.finish()
tree.free()
return res
}
///////// Internal details
enum NodeType {
Tree
Char
}
struct Node {
freq: u32
type: NodeType
// Only valid if type == Char
chr: char
// Only valid if type == Tree
left: u16
right: u16
}
def Node::compare(&this, other: &Node): i8 => .freq.compare(other.freq)
struct HuffmanTree {
nodes: [Node; 512] // At most 256 leaves and 255 internal nodes
num_nodes: u16
root: u16
}
def HuffmanTree::serialize_bits(&this, bio: &BitWriter, cur: u16) {
let node = &.nodes[cur]
if node.type == Char {
bio.write_bit(1)
bio.write_u8(node.chr as u8)
} else {
bio.write_bit(0)
.serialize_bits(bio, node.left)
.serialize_bits(bio, node.right)
}
}
def HuffmanTree::deserialize_bits(&this, bio: &BitReader): u16 {
let cur = .num_nodes++
let node = &.nodes[cur]
let bit = bio.read_bit()
if bit == 1 {
node.type = Char
node.chr = bio.read_u8() as char
} else {
node.type = Tree
node.left = .deserialize_bits(bio)
node.right = .deserialize_bits(bio)
}
return cur
}
def HuffmanTree::from_serialized_bits(bio: &BitReader): &HuffmanTree {
let tree = mem::alloc<HuffmanTree>()
let root = tree.deserialize_bits(bio)
return tree
}
def HuffmanTree::from_text(inp: SV): &HuffmanTree {
let tree = mem::alloc<HuffmanTree>()
let counts: [u32; 256]
for c in inp.chars() {
counts[c as u8] += 1
}
let heap = Heap<&Node>::new(Min, capacity: 256)
for let i = 0; i < 256; i++ {
let node = &tree.nodes[tree.num_nodes++]
if counts[i] > 0 {
node.type = Char
node.chr = i as char
node.freq = counts[i]
heap.push(node)
}
}
while heap.size() > 1 {
let left = heap.pop()
let right = heap.pop()
let node = &tree.nodes[tree.num_nodes++]
node.type = Tree
node.left = (left - tree.nodes) as u16
node.right = (right - tree.nodes) as u16
node.freq = left.freq + right.freq
heap.push(node)
}
let res = heap.pop()
heap.free()
tree.root = (res - tree.nodes) as u16
return tree
}
def HuffmanTree::free(&this) {
mem::free(this)
}
// FIXME: Would be nice to have this be something in the standard libary,
// but there's no way to template over the size of the array yet, and
// we want to avoid a separate allocation.
struct CharCode {
bits: u32 // Number of bits
code: [u8; 32] // Up to 256 bits for the code
}
def CharCode::set(&this, idx: u32, bit: u8) {
.bits = idx + 1
let byte_off = (idx / 8) as u8
let bit_off = (idx % 8) as u8
if bit == 0 {
.code[byte_off] = .code[byte_off] & ~(1u8 << bit_off)
} else {
.code[byte_off] = .code[byte_off] | (1u8 << bit_off)
}
}
def CharCode::get(&this, idx: u32): u8 {
let byte_off = (idx / 8) as u8
let bit_off = (idx % 8) as u8
return ((.code[byte_off] >> bit_off) & 1) as u8
}
def CharCode::iter_bits(&this): BitReader => BitReader(.code, num_bits: .bits, idx: 0)
struct HuffmanCodes {
codes: [CharCode; 256]
}
def HuffmanCodes::setup_from_node(&this, tree: &HuffmanTree, cur: u16, code: CharCode, idx: u32) {
let node = &tree.nodes[cur]
if node.type == Char {
.codes[node.chr as u8] = code
} else {
let lcode = code
lcode.set(idx, 0)
.setup_from_node(tree, node.left, lcode, idx + 1)
let rcode = code
rcode.set(idx, 1)
.setup_from_node(tree, node.right, rcode, idx + 1)
}
}
def HuffmanCodes::make(tree: &HuffmanTree): HuffmanCodes {
let ctree: HuffmanCodes
let code: CharCode
ctree.setup_from_node(tree, tree.root, code, 0)
return ctree
}
def HuffmanCodes::print(&this) {
for let i = 0; i < 256; i++ {
let code = .codes[i]
if code.bits > 0 {
print(f"{i:c} -> ")
for bit in code.iter_bits() {
print(f"{bit}")
}
println("")
}
}
}
struct Decompressor {
bio: BitReader
res: Buffer
total_chars: u32
tree: &HuffmanTree
}
def Decompressor::run(&this, cur: u16) {
let node = &.tree.nodes[cur]
if node.type == Char {
.res.write_char(node.chr)
} else {
if .bio.read_bit() == 0 {
.run(node.left)
} else {
.run(node.right)
}
}
}
def Decompressor::decompress(&this): Buffer {
while .res.size < .total_chars {
.run(.tree.root)
}
return .res
}