-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanCode.h
60 lines (49 loc) · 1.31 KB
/
HuffmanCode.h
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
/*
* HuffmanCode.h
*
* Created on: 2016Äê8ÔÂ18ÈÕ
* Author: zhenlin
*/
#ifndef HUFFMANCODE_H_
#define HUFFMANCODE_H_
#include<string>
#include<vector>
#include<map>
#include<list>
#include<fstream>
using namespace std;
const int MAX_LEN = 10*1024*1024; // read 10MB every time
const unsigned char MARKS[8] = { 0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1 }; // x & MARKS[i] to get ith bit
class HuffmanCode {
public:
HuffmanCode();
void Compress(string src, string dest);
void Decompress(string src, string dest);
virtual ~HuffmanCode();
private:
void CountLetter(string src);
void ConstructHuffmanTree();
void GenerateHuffmanCode();
void WriteHuffmanCode(ofstream &os);
void Compressing(string src, string dest);
void InsertIntoHuffmanTree(char letter, string &code, int &k);
void ConstructHuffmanTreeFromFile(ifstream &is);
void Decompressing(ifstream &is, ofstream &os);
map<char, int> letter_count;
typedef struct Node {
int id;
bool is_leaf;
char letter;
int parent, lchild, rchild;
Node() {
}
Node(int i, bool il, char lt, int p, int lc, int rc) :
id(i), is_leaf(il), letter(lt), parent(p), lchild(lc), rchild(
rc) {
}
};
multimap<int, Node> count_node;
vector<Node> huffman_tree;
map<char, vector<char>> letter_hcode; // hufman code for each letter
};
#endif /* HUFFMANCODE_H_ */