-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.h
97 lines (74 loc) · 1.84 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
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
//
// Created by Fedor on 28.03.2020.
//
#include <vector>
#include <ios>
#include <map>
#include <algorithm>
#include <bitset>
#ifndef ZIPPER_HUFFMAN_H
#define ZIPPER_HUFFMAN_H
typedef unsigned char byte;
using std::vector;
using std::string;
using std::map;
using std::pair;
using std::sort;
using std::tuple;
using std::get;
class Huffman
{
public:
vector<byte> Encode(vector<byte> input);
vector<byte> Decode(vector<byte> input);
private:
class HuffmanTree
{
public:
HuffmanTree();
~HuffmanTree();
void buildTree();
/**
* Decodes encoded string
*/
vector<byte> findSymbols(string code, byte lastByteUsedBitsCount);
private:
struct Node
{
byte symbol;
string code;
double probability;
struct Node *left;
struct Node *right;
explicit Node(double probability, byte symbol);
};
struct NodesComparator
{
inline bool operator() (const Node* node1, const Node* node2)
{
return (node1->probability > node2->probability);
}
};
bool isLeaf(Node* node);
void deleteTree(Node* root);
/**
* Traverses the subtree from root to leaves and stores the code in them
*/
void createCodes(Node* root);
vector<Node*> nodes; // node[0] -- tree root after buildTree() execution
Node* node;
protected:
vector<Node*> leaves;
};
void buildProbabilityTable();
static const int SYMBOL_DATA_SIZE = 9;
vector<byte> bytes;
static map<byte, tuple<string, double>> symbolsTable; //<byte, <code, probability>>
};
union DoubleLongShortByteUnion
{
double Double;
unsigned short uShort;
byte uBytes[8];
};
#endif //ZIPPER_HUFFMAN_H