-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
169 lines (135 loc) · 3.45 KB
/
encode.go
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
package compressor
import (
"container/heap"
"encoding/binary"
"io"
"github.com/dgryski/go-bitstream"
)
type prefix struct {
numBits int8
code string
}
type encoder struct {
rawData *[]byte
root *node
w *bitstream.BitWriter
prefixes map[byte]*prefix
frequencies map[byte]int
}
// Implements huffman encoding algorithm
func (e *encoder) encode() {
e.buildFrequencyTable()
h := buildMinHeap(&e.frequencies)
e.root = buildPrefixTree(h)
e.buildPrefixTable(e.root, "")
writeHeader(e)
writeData(e.rawData, &e.prefixes, e.w)
}
func (e *encoder) buildFrequencyTable() {
for _, char := range *e.rawData {
// returns val or 0
val := e.frequencies[char]
e.frequencies[char] = val + 1
}
}
// builds a min heap based on the frequencies
func buildMinHeap(frequencies *map[byte]int) *minHeap {
h := make(minHeap, len(*frequencies))
idx := 0
for char, frequency := range *frequencies {
n := node{char, frequency, false, nil, nil}
h[idx] = &n
idx++
}
heap.Init(&h)
return &h
}
// generates a unique prefix for each leaf node in the tree
func (e *encoder) buildPrefixTable(n *node, code string) {
if n == nil {
return
}
// If we are at leaf node add prefix to the table
if !n.internal {
p := prefix{int8(len(code)), code}
e.prefixes[n.char] = &p
} else {
e.buildPrefixTable(n.left, code+"0")
e.buildPrefixTable(n.right, code+"1")
}
}
func buildPrefixTree(h *minHeap) *node {
// while we have more than 1 node in the heap
for {
if len(*h) <= 1 {
break
}
// grab the two minimum frequency nodes
left := heap.Pop(h).(*node)
right := heap.Pop(h).(*node)
// generate new intenal node
newNode := node{0, left.frequency + right.frequency, true, left, right}
heap.Push(h, &newNode)
}
return heap.Pop(h).(*node)
}
// Writes header data in the format:
// First byte: number of leaf nodes in tree
// Next 2 bytes: number of characters that will be encoded
// Next n bytes: the serialized tree
func writeHeader(e *encoder) {
numCharsInTree := len(e.prefixes)
// number of characters that will be encoded
numCharsInData := uint16(len(*e.rawData))
// convert the number to bytes
bs := make([]byte, 2)
binary.LittleEndian.PutUint16(bs, numCharsInData)
writeByte(e.w, byte(numCharsInTree))
writeByte(e.w, bs[0])
writeByte(e.w, bs[1])
writeTree(e.root, e.w)
}
func writePrefix(p *prefix, w *bitstream.BitWriter) {
var i int8
for i = 0; i < p.numBits; i++ {
if p.code[i] == '0' {
writeBit(w, bitstream.Zero)
} else {
writeBit(w, bitstream.One)
}
}
}
// loops over each character and writes the mapped prefix to the writer
func writeData(data *[]byte, prefixes *map[byte]*prefix, w *bitstream.BitWriter) {
for _, char := range *data {
p := (*prefixes)[char]
writePrefix(p, w)
}
}
//Serialize the tree to the writer
func writeTree(n *node, w *bitstream.BitWriter) {
if n == nil {
return
}
if n.internal {
// a zero bit denotes that this node is internal
writeBit(w, bitstream.Zero)
writeTree(n.left, w)
writeTree(n.right, w)
} else {
// denote that a leaf node has been reached with a value of one
writeBit(w, bitstream.One)
// write the character byte
writeByte(w, n.char)
}
}
//Encode compresses the data provided and saves the compressed data to the writer
func Encode(data *[]byte, w io.Writer) {
e := encoder{}
e.rawData = data
e.w = bitstream.NewWriter(w)
e.frequencies = make(map[byte]int)
e.prefixes = make(map[byte]*prefix)
e.encode()
e.w.Flush(bitstream.One)
}