Skip to content

Commit d3d4f63

Browse files
committed
removed heap module. Used manual allocation
1 parent 3281512 commit d3d4f63

File tree

8 files changed

+45
-1377
lines changed

8 files changed

+45
-1377
lines changed

compressor/huffman.go

+41-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package compressor
22

3-
import (
4-
"container/heap"
5-
)
6-
73
// Node represents a node in the Huffman tree
84
type Node struct {
9-
char rune // Character stored in the node
10-
freq int // Frequency of the character
11-
left *Node // Left child node
12-
right *Node // Right child node
5+
char rune // Character stored in the node
6+
freq int // Frequency of the character
7+
left *Node // Left child node
8+
right *Node // Right child node
139
}
1410

15-
// PriorityQueue implements heap.Interface and holds Nodes
11+
// PriorityQueue implements a priority queue for Nodes
1612
type PriorityQueue []*Node
1713

1814
// Len returns the number of items in the priority queue
@@ -29,12 +25,12 @@ func (pq PriorityQueue) Swap(i, j int) {
2925
}
3026

3127
// Push adds an item (Node) to the priority queue
32-
func (pq *PriorityQueue) Push(x interface{}) {
33-
*pq = append(*pq, x.(*Node))
28+
func (pq *PriorityQueue) Push(x *Node) {
29+
*pq = append(*pq, x)
3430
}
3531

3632
// Pop removes and returns the highest priority item (Node) from the priority queue
37-
func (pq *PriorityQueue) Pop() interface{} {
33+
func (pq *PriorityQueue) Pop() *Node {
3834
old := *pq
3935
n := len(old)
4036
item := old[n-1]
@@ -50,27 +46,55 @@ func buildHuffmanTree(freq map[rune]int) *Node {
5046
pq[i] = &Node{char: char, freq: f}
5147
i++
5248
}
53-
heap.Init(&pq)
49+
buildMinHeap(pq)
5450

5551
for len(pq) > 1 {
56-
left := heap.Pop(&pq).(*Node)
57-
right := heap.Pop(&pq).(*Node)
52+
left := pq.Pop()
53+
right := pq.Pop()
5854

5955
internal := &Node{
6056
char: '\x00', // internal node character
6157
freq: left.freq + right.freq,
6258
left: left,
6359
right: right,
6460
}
65-
heap.Push(&pq, internal)
61+
pq.Push(internal)
6662
}
6763

6864
if len(pq) > 0 {
69-
return heap.Pop(&pq).(*Node) // root of Huffman tree
65+
return pq.Pop() // root of Huffman tree
7066
}
7167
return nil
7268
}
7369

70+
// buildMinHeap builds a min-heap for the priority queue
71+
func buildMinHeap(pq PriorityQueue) {
72+
n := len(pq)
73+
for i := n/2 - 1; i >= 0; i-- {
74+
heapify(pq, n, i)
75+
}
76+
}
77+
78+
// heapify maintains the heap property of the priority queue
79+
func heapify(pq PriorityQueue, n, i int) {
80+
smallest := i
81+
left := 2*i + 1
82+
right := 2*i + 2
83+
84+
if left < n && pq[left].freq < pq[smallest].freq {
85+
smallest = left
86+
}
87+
88+
if right < n && pq[right].freq < pq[smallest].freq {
89+
smallest = right
90+
}
91+
92+
if smallest != i {
93+
pq.Swap(i, smallest)
94+
heapify(pq, n, smallest)
95+
}
96+
}
97+
7498
// buildHuffmanCodes builds Huffman codes (bit strings) for each character
7599
func buildHuffmanCodes(root *Node) map[rune]string {
76100
codes := make(map[rune]string)

resources.rc

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
IDI_ICON1 ICON "icon.ico"
44

55
1 VERSIONINFO
6-
FILEVERSION 0,0,4
7-
PRODUCTVERSION 0,0,4
6+
FILEVERSION 0,0,5
7+
PRODUCTVERSION 0,0,5
88
FILEOS 0x4
99
FILETYPE 0x1
1010
{
@@ -14,12 +14,12 @@ FILETYPE 0x1
1414
{
1515
VALUE "CompanyName", "BrainbirdLab"
1616
VALUE "FileDescription", "File Compress-Encryption program"
17-
VALUE "FileVersion", "0.0.4"
17+
VALUE "FileVersion", "0.0.5"
1818
VALUE "InternalName", "piarch"
1919
VALUE "LegalCopyright", "BrainbirdLab"
2020
VALUE "OriginalFilename", "piarch"
2121
VALUE "ProductName", "piarch"
22-
VALUE "ProductVersion", "0.0.4"
22+
VALUE "ProductVersion", "0.0.5"
2323
VALUE "Author", "Fuad Hasan"
2424
}
2525
}

resources.syso

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)