-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHuffman.h
224 lines (202 loc) · 6.87 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Main file to handle encoding and decoding of input file.
//
#include <string.h>
#include <iostream>
#include <unordered_map>
#include <fstream>
#include <bits/stdc++.h>
#include "Node.h"
using namespace std;
#ifndef HUFFMANCODING_HUFFER_H
#define HUFFMANCODING_HUFFER_H
#define INTERNAL_NODE_CHARACTER char(188)
#define PSEUDO_EOF char(129)
#define CHARACTER_CODE_SEPERATOR char(189)
#define HEADER_ENTRY_SEPERATOR char(190)
#define HEADER_TEXT_SEPERATOR char(191)
class Huffman
{
private:
unordered_map<char,string> codeMap;
void encodeCharacters(Node *rootNode, string codeString)
{
if (!rootNode)
{
return;
}
if (rootNode->getCharacter() != INTERNAL_NODE_CHARACTER)
{
codeMap[rootNode->getCharacter()] = codeString;
}
encodeCharacters(rootNode->getLeft(), codeString + "0");
encodeCharacters(rootNode->getRight(), codeString + "1");
}
void writeHeader(ofstream &outputStream)
{
for (const auto &item : codeMap)
outputStream << item.first << CHARACTER_CODE_SEPERATOR << item.second << HEADER_ENTRY_SEPERATOR;
outputStream << HEADER_TEXT_SEPERATOR;
}
void readHeader(ifstream &inputStream)
{
codeMap.clear();
char character;
inputStream.get(character);
char key = character;
while (character != HEADER_TEXT_SEPERATOR)
{
if (character == CHARACTER_CODE_SEPERATOR)
{
inputStream.get(character);
while (character != HEADER_ENTRY_SEPERATOR)
{
codeMap[key] += character;
inputStream.get(character);
}
}
else
key = character;
inputStream.get(character);
}
}
Node* buildDecodingTree(unordered_map<char,string> encodingMap)
{
Node *rootNode = new Node(INTERNAL_NODE_CHARACTER);
Node *previousNode;
for (const auto &item : encodingMap)
{
previousNode = rootNode;
Node *newNode = new Node(item.first);
string characterCode = item.second;
for (int i = 0; i < characterCode.size(); ++i)
{
if (characterCode[i] == '0')
{
if (i == characterCode.size() - 1)
previousNode->setLeft(newNode);
else
{
if (!previousNode->getLeft())
{
previousNode->setLeft(new Node(INTERNAL_NODE_CHARACTER));
previousNode = previousNode->getLeft();
}
else previousNode = previousNode->getLeft();
}
}
else
{
if (i == characterCode.size() - 1)
previousNode->setRight(newNode);
else
{
if (!previousNode->getRight())
{
previousNode->setRight(new Node(INTERNAL_NODE_CHARACTER));
previousNode = previousNode->getRight();
}
else previousNode = previousNode->getRight();
}
}
}
}
return rootNode;
}
class myCompartor
{
public :
int operator() (Node* node1, Node* node2)
{
return node1->getFrequency() > node2->getFrequency();
}
};
public :
void huffer(unordered_map<char, int> frequencyMap)
{
priority_queue <Node *, vector<Node *>, myCompartor> HufferQueue;
string tempString;
Node *leftNode, *rightNode, *newNode;
for (const auto &item : frequencyMap)
HufferQueue.push(new Node(item.first, item.second));
HufferQueue.push(new Node(PSEUDO_EOF, 1));
while (HufferQueue.size() != 1)
{
leftNode = HufferQueue.top();
HufferQueue.pop();
rightNode = HufferQueue.top();
HufferQueue.pop();
newNode = new Node(INTERNAL_NODE_CHARACTER, leftNode->getFrequency() + rightNode->getFrequency());
HufferQueue.push(newNode);
newNode->setLeft(leftNode);
newNode->setRight(rightNode);
}
encodeCharacters(HufferQueue.top(), tempString);
return;
}
void deHuffer(string compressedFileName,string decompressedFileName)
{
char character;
string codeString;
ifstream inputStream;
inputStream.open(compressedFileName, ios_base::in|ios::binary);
readHeader(inputStream);
do {
character = inputStream.get();
if(character==char_traits<wchar_t>::eof())
break;
bitset<8> bits(character);
codeString += bits.to_string();
} while(!inputStream.fail());
Node *rootNode = buildDecodingTree(codeMap);
decompressToFile(codeString, rootNode, decompressedFileName);
return;
}
void compressTofile(string InputfileName ,string OutputfileName)
{
char character;
string file;
ifstream inputStream;
ofstream outputStream;
outputStream.open(OutputfileName, ios::out);
inputStream.open(InputfileName, ios::in);
writeHeader(outputStream);
while (inputStream.get(character))
file += codeMap[character];
inputStream.close();
file += codeMap[PSEUDO_EOF];
unsigned long remainder = (file.size() - 1) % 8;
for (int i = 0; i < 8 - remainder; ++i)
file += '0';
stringstream stringStream(file);
while (stringStream.good()) {
bitset<8> bits;
stringStream >> bits;
char c = char(bits.to_ulong());
outputStream << c;
}
outputStream.flush();
outputStream.close();
}
void decompressToFile(string codeString,Node *rootNode,string decompressedFileName)
{
ofstream outputStream;
outputStream.open(decompressedFileName, ios::out);
Node *traversingPointer = rootNode;
for (int i = 0; i < codeString.size() + 1; ++i) {
if (codeString[i] == '0')
traversingPointer = traversingPointer->getLeft();
else
traversingPointer = traversingPointer->getRight();
if (traversingPointer->getCharacter() != INTERNAL_NODE_CHARACTER) {
if (traversingPointer->getCharacter() == PSEUDO_EOF)
break;
outputStream << traversingPointer->getCharacter();
traversingPointer = rootNode;
}
}
outputStream.flush();
outputStream.close();
}
};
#endif