-
Notifications
You must be signed in to change notification settings - Fork 1
/
Huffman.h
67 lines (46 loc) · 1.11 KB
/
Huffman.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
61
62
63
64
65
66
#ifndef HUFFMAN_H
#define HUFFMAN_H
#include <utility>
#include <vector>
#include <string>
#include <map>
using namespace std;
typedef unsigned char Byte;
typedef int Element;
typedef pair<Element, int> Data;
struct TreeNode;
struct HuffmanHeader;
class Huffman {
private:
vector<Data> frequencies;
TreeNode* tree;
vector<Byte> data;
char lastByteBitCount;
map<Element, string> codeTable;
string getCodeFor(Element);
void buildCodeTable(TreeNode*, string code = "");
public:
void buildTree(vector<Data> frequencies);
void readFile(FILE*);
void huffmanize(int* samples, int size);
vector<int> dehuffmanize();
HuffmanHeader getHeader();
int* getFrequencies();
Byte* getData();
};
struct HuffmanHeader {
size_t byteCount;
char lastByteBitCount;
size_t sampleCount;
};
struct TreeNode {
Data d;
TreeNode* left;
TreeNode* right;
TreeNode(Data data)
: d(data) {
left = NULL;
right = NULL;
}
};
#endif // HUFFMAN_H