-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
186 lines (139 loc) · 5.11 KB
/
main.cpp
File metadata and controls
186 lines (139 loc) · 5.11 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <queue>
#include <map>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <limits>
#include <iomanip>
#include <cstring>
#define PARENT_NODE '\1'
void clear_cin() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
void invert_endian_int(unsigned int& num) {
num = (num << 24) | ((num << 8) & 0x00ff0000) | ((num >> 8) & 0x0000ff00) | (num >> 24);
}
template<typename K, typename V>
void insertToMap(std::map<K, V>& mapRef, typename std::map<K, V>::iterator& hint, K key, V value) {
if(mapRef.empty()) {
hint = mapRef.insert(std::pair<K, V>(key, value)).first;
} else {
hint = mapRef.insert(hint, std::pair<K, V>(key, value));
}
}
class HuffmanCode {
struct Node {
char character; //represent character or no character with number -1
int weight;
Node* left;
Node* right;
//creates Node without weight for decompression
Node(char c): character(c), weight(0), left(nullptr), right(nullptr) {}
//create new node for nodes priority queue
Node(char c, int w): character(c), weight(w), left(nullptr), right(nullptr) {}
//create new node as parent of two front nodes in queue
Node(Node* leftChild, Node* rightChild):
character(-1), weight(leftChild->weight + rightChild->weight), left(leftChild), right(rightChild) {}
~Node() {
//recursive deletion
if(left!=nullptr) delete left;
if(right!=nullptr) delete right;
}
bool isLeaf() {
return (left==nullptr && right==nullptr);
}
//for custom comparator class
struct Comparator {
bool operator()(const Node* left, const Node* right) {
return left->weight > right->weight;
}
};
};
/*--------------------------------------------------------------------------------
Constants
*/
static const std::istreambuf_iterator<char> eosIn;
static const std::ostreambuf_iterator<char> eosOut;
/*
--------------------------------------------------------------------------------
*/
std::istream& inputStream;
std::ostream& outputStream;
//used for tree generation
std::map<char, int> frequencyMap;
std::map<char, int>::iterator frequencyMapIterator;
std::priority_queue<Node*, std::vector<Node*>, Node::Comparator> nodesQueue;
Node* root;
std::map<char, std::string> charmap;
std::map<char, std::string>::iterator charmapIterator;
unsigned long payloadSize;
unsigned long compressedBitsSize;
/*--------------------------------------------------------------------------------
Below are function routines for compress
*/
void createCharFreqMap() {
for(std::istreambuf_iterator<char> iter(inputStream); iter!= eosIn; iter++) {
if(frequencyMap.empty()) {
frequencyMapIterator = frequencyMap.insert(std::pair<char, int>(*iter, 1)).first;
} else {
frequencyMapIterator = frequencyMap.insert(frequencyMapIterator, std::pair<char, int>(*iter, 0));
frequencyMapIterator->second += 1;
}
}
}
void createInitialQueue() {
for(std::map<char, int>::const_iterator iter = frequencyMap.begin(); iter != frequencyMap.end(); iter++) {
nodesQueue.push(new Node(iter->first, iter->second));
}
}
void createHuffmanTreeFromQueue() {
while(nodesQueue.size() > 1) {
Node* firstNode = nodesQueue.top();
nodesQueue.pop();
Node* secondNode = nodesQueue.top();
nodesQueue.pop();
nodesQueue.push(new Node(firstNode, secondNode));
}
//set the root and clear the queue
root = nodesQueue.top();
nodesQueue.pop();
}
/*
----------------------------------------------------------------------------------
*/
void createCharMap(const Node* rootNode, std::string code = "") {
if(rootNode == nullptr) return;
if(rootNode->left != nullptr) createCharMap(rootNode->left, code + '0');
if(rootNode->right != nullptr) createCharMap(rootNode->right, code + '1');
if(rootNode->isLeaf()) {
insertToMap(charmap, charmapIterator, rootNode->character, code);
}
}
/*--------------------------------------------------------------------------------
Below are function helper for compression
*/
/*
--------------------------------------------------------------------------------
*/
int compress() {
int payloadSize = 0; //payload size in bits
int treeSize = 0; //serialized tree size in bytes
}
public:
enum OperationMode {
comp, decomp
};
//compress or decompress from inputStream and output to outputStream
HuffmanCode(OperationMode om, std::istream& is, std::ostream& os): inputStream(is), outputStream(os) {
}
double getCompressionRatio() {
}
~HuffmanCode() {
delete root;
}
};