-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman.h
More file actions
98 lines (76 loc) · 2.29 KB
/
huffman.h
File metadata and controls
98 lines (76 loc) · 2.29 KB
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
98
#ifndef HUFFMAN_H
#define HUFFMAN_H
#include<QObject>
#include<vector>
#include<queue>
#include<string>
#include<fstream>
#include<unordered_map>
namespace huffman
{
const size_t MAX_CODE_SIZE = 32;
struct Symbol{
size_t amount;
double freq;
char32_t code;
std::string s_code;
char symbol;
unsigned char length;
bool discovered;
//Symbol():amount{0},freq{0},code{0x00},symbol{0x00},length{0x00},discovered{false}{}
Symbol():amount{0},freq{0},code{0x00},s_code{""},symbol{0x00},length{0x00},discovered{false}{}
};
struct Node{
double key;
short curpos;
short parent;
short left;
short rigth;
bool leaf;
char symbol;
Node():key{0},parent{-1},left{-1},rigth{-1},leaf{false},symbol{0x00}{}
};
enum ProgressType : int
{
flwScanning = 0,
flwBuildingPTree,
flwBuildingTSymbol,
flwCompress,
flwUncompress
};
class ProgressNotify: public QObject
{
Q_OBJECT
signals:
void siglProgress(int type, double percent);
void sgnlLog(QString message);
public:
ProgressNotify(){}
void notifyProgress(ProgressType type, double percent){ emit siglProgress(type,percent); }
void notifyLog(std::string message){ emit sgnlLog(QString(message.c_str())); }
};
class CodecTable{
std::vector<Symbol> S;
std::vector<Node> tree;
short _root;
size_t size;
short addNode(char symbol, double k, bool leaf);
void buildTable(size_t curpos, size_t deep, std::string s_code, ProgressNotify& notifier, bool isLeft = true);
public:
CodecTable();
~CodecTable();
bool build(std::string path, ProgressNotify& notifier);
short Root();
const Symbol operator()(int symbol);
const Node operator[](int index);
};
namespace tool
{
//void getChainOfBits(char32_t code, unsigned char lenght, std::vector<char>& buff, short& bits_reminded);
void getChainOfBits(std::string code, std::vector<char>& buff, short& bits_reminded);
unsigned char getByte(CodecTable& codec, std::ifstream& file, int& curByte, int& curBut);
bool Compress (CodecTable& codec, std::string path, std::string outpath, ProgressNotify& notifier);
bool Uncompress (CodecTable& codec, std::string path, std::string outpath, ProgressNotify& notifier);
}//tool
} //compress
#endif // HUFFMAN_H