-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
55 lines (46 loc) · 1.31 KB
/
utils.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
package main
import (
"container/heap"
"sort"
)
func getCodes(h HuffmanEle, currentCode []byte, codeMap map[rune][]byte) {
switch i := h.(type) {
case Node:
currentCode = append(currentCode, '0')
getCodes(i.left, currentCode, codeMap)
currentCode = currentCode[:len(currentCode)-1]
currentCode = append(currentCode, '1')
getCodes(i.right, currentCode, codeMap)
currentCode = currentCode[:len(currentCode)-1]
case Leaf:
// Assign a copy of the currentCode as the value of current char in map
temp := make([]byte, len(currentCode))
copy(temp, currentCode)
codeMap[i.char] = temp
}
}
// Makes the huffman tree, returns the main mode
func makeHuffTree(freqmap map[rune]int) HuffmanEle {
var huff HuffmanHeap
for i, v := range freqmap {
huff = append(huff, Leaf{v, i})
}
// Sort before initiating heap for uniformity
sort.Slice(huff, huff.LessLeaf)
heap.Init(&huff)
for huff.Len() > 1 {
lowest := heap.Pop(&huff).(HuffmanEle)
secondLowest := heap.Pop(&huff).(HuffmanEle)
heap.Push(&huff, Node{lowest.getValue() + secondLowest.getValue(), lowest, secondLowest})
}
return heap.Pop(&huff).(HuffmanEle)
}
// Sort helper Function
func (h HuffmanHeap) LessLeaf(i int, j int) bool {
return int(h[i].(Leaf).char) < int(h[j].(Leaf).char)
}
func panicErr(err error) {
if err != nil {
panic(err)
}
}