-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.cpp
More file actions
380 lines (323 loc) · 9.03 KB
/
source.cpp
File metadata and controls
380 lines (323 loc) · 9.03 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "source.h"
/**
* This methods reads the input file looking for the
* symbols to be encoded, and puts them on a table
* with the purpose of counting the number of
* appearances.
*
* @param input The file that we are reading from.
*/
void Source::getFrequencies(FILE *input)
{
FILE *in = input;
char cur_c;
std::map<char, unsigned int>::iterator it;
symbolCounter.clear();
totalSymbols = 0;
cur_c = fgetc(in);
while (cur_c != EOF)
{
if (Symbol::symbolIsEncodable(cur_c))
{
it = symbolCounter.find(cur_c);
totalSymbols++;
if (it == symbolCounter.end())
symbolCounter[cur_c] = 1;
else
symbolCounter[cur_c]++;
}
cur_c = fgetc(in);
}
}
/**
* After counting the frequencies, the next step is to
* get the total probabilities.
*
* Later, we create the basic symbols for the source with
* the data we have analyzed and we put them on the symbol
* list.
*/
void Source::getProbabilities()
{
std::map<char, unsigned int>::iterator it;
for (it = symbolCounter.begin(); it != symbolCounter.end();it++)
{
float lab = (*it).first;
float prob = (float)((*it).second) / (float)totalSymbols;
Symbol* newSymbol = new Symbol();
newSymbol->setLabel(lab);
newSymbol->setProbability(prob);
symbolList.push_back(newSymbol);
}
}
/**
* This is a wrapper for getting the frequencies
* and then the probabilities.
*/
void Source::getProperties(FILE *input)
{
this->getFrequencies(input);
this->getProbabilities();
}
/**
* A getter for the attribute "serial".
*/
std::string Source::getSerial()
{
return this->serial;
}
/**
* The algorithm for solving the Huffman encoding is a greedy algorithm
* that must select the two symbols with the lowest probabilites and add
* another one with sum of its probabilities.
*
* This can be solved by using a priority queue as I did, and in fact, it
* is an optimal solution:
*
* We just create the queue associating it to the functor we created for
* establishing an ascendant order in the queue.
*
* Later we add the elements from the symbol list, and then we start the
* algorithm, popping 2 symbols from the queue and adding another one that
* we create from the symbols we extracted before.
*
* Finally, we store the last element on the queue (the root node for the
* tree) and we obtain the codification for the symbols in the original
* symbol list.
*/
void Source::solveHuffman()
{
std::priority_queue <Symbol*, std::vector <Symbol*> , SymbolComp> symbolQueue;
std::list <Symbol*>::iterator it;
for (it = symbolList.begin(); it != symbolList.end(); it++)
symbolQueue.push(*it);
while (symbolQueue.size() > 1)
{
Symbol* symbol1 = symbolQueue.top();
symbolQueue.pop();
Symbol* symbol2 = symbolQueue.top();
symbolQueue.pop();
CombinedSymbol* newCombinedSymbol = new CombinedSymbol(symbol1, symbol2);
symbolQueue.push(newCombinedSymbol);
}
// for serializing later...
rootSymbol = symbolQueue.top();
// for codifying later...
codificationTable.clear();
for (it = symbolList.begin(); it != symbolList.end(); it++)
{
(*it)->obtainCodification();
codificationTable[(*it)->getLabel()] = (*it)->getCodification();
}
}
/**
* This function just resets the serial and calls the method
* for serializing from the root node.
*/
void Source::serializeTree()
{
serial = "";
rootSymbol->serializeNode(&serial);
}
/**
* This method analyzes the character being read
* (in this case, the first one from the serial)
* and calls the same method from the childs in
* case the characters is a 0 and returns an unique
* symbol if it is a 1.
*
* Note that this is a particular case for the root
* node.
*
* In the end we call the serializeTree() method so
* we can build the serial from the tree.
*
* @param input The descriptor of the file that
* we are reading from.
*/
void Source::unserializeTree(FILE *input)
{
char cur_c = fgetc(input);
symbolList.clear();
if (cur_c == '0')
{
Symbol* symbol1 = Symbol::unserializeNode(input);
Symbol* symbol2 = Symbol::unserializeNode(input);
rootSymbol = new CombinedSymbol(symbol1, symbol2);
}
else
{
rootSymbol = new Symbol();
cur_c = fgetc(input);
rootSymbol->setLabel(cur_c);
}
this->serializeTree();
}
/**
* This method can be used after solving the Huffman
* algorithm and obtaining the codification for the
* original symbols.
*
* It will use the name of the input file passed as
* the first argument to determine the name of the
* output file.
*
* It will create and then it will write the codifications
* for the symbols it reads from the input in the output
* file.
*
* @param inputFileName The name of the input file.
*/
void Source::writeCodifiedFile(char* inputFileName)
{
FILE *input;
FILE *output;
std::string outputFileName;
char* dotPointer;
char cur_input;
Binarizer* binarizer = new Binarizer();
input = fopen(inputFileName, "r");
if (input == NULL)
{
printf("Fichero de entrada no encontrado.\n");
exit(-1);
}
dotPointer = strchr(inputFileName, '.');
if (dotPointer != NULL)
*dotPointer = '\0';
outputFileName = inputFileName;
outputFileName += ".huf";
output = fopen(outputFileName.c_str(), "w");
if (output == NULL)
{
printf("Fichero de salida no pudo crearse.\n");
exit(-1);
}
// write serial
fprintf(output, "%s ", (this->getSerial().c_str()));
cur_input = fgetc(input);
while (cur_input != EOF)
{
if (Symbol::symbolIsEncodable(cur_input))
binarizer->addStringToCode(codificationTable[cur_input].c_str());
cur_input = fgetc(input);
}
fprintf(output, "%d %d\n", binarizer->getOffset(), binarizer->getCodeLength());
std::string code = binarizer->getBinaryCode();
unsigned int code_length = binarizer->getCodeLength();
for (int i = 0; i < code_length; i++)
fprintf(output, "%c", code[i]);
fclose(input);
fclose(output);
}
/**
* This method makes the reverse operation of writing a codified
* file.
*
* It will look for the codes after having built the
* binary tree from the serial put in the first line of the header.
* Then it will start reading the codes and printing the original
* symbols to the output file.
*
* @param input The descriptor of the codified file that we are
* reading from.
*
* @param outputFileName The name of the file (it is passed by\
* the user as the second parameter of the main program.
*/
void Source::writeUncodifiedFile(FILE* input, char* outputFileName)
{
char cur_c;
FILE* output;
std::string binaryString = "";
Debinarizer* debinarizer = new Debinarizer();
int offset;
int n_chars;
fscanf(input, " %d %d\n", &offset, &n_chars);
debinarizer->setOffset(offset);
buildSymbolList();
buildCodeList();
output = fopen(outputFileName, "w");
if (output == NULL)
{
printf("Fichero de salida no pudo crearse.\n");
exit(-1);
}
cur_c = fgetc(input);
for (unsigned int i = 0; i < n_chars; i++)
{
debinarizer->addCharToString(cur_c);
cur_c = fgetc(input);
}
while (debinarizer->codesLeft())
{
debinarizer->readChar();
if (stringInCodeList(debinarizer->getTempCode()))
{
fprintf(output, "%c", decodificationTable[debinarizer->getTempCode()]);
debinarizer->resetTempCode();
}
}
fclose(input);
fclose(output);
}
/**
* This function triggers the addToListIfNotCombined
* from the root node so we get the original symbols
* in the symbol list.
*/
void Source::buildSymbolList()
{
rootSymbol->addToListIfNotCombined(&symbolList);
}
/**
* This method looks for the symbols in the symbol list
* and makes a table that relates its codification with
* its label.
*/
void Source::buildCodeList()
{
std::list <Symbol*>::iterator it;
decodificationTable.clear();
for (it = symbolList.begin(); it != symbolList.end(); it++)
{
(*it)->obtainCodification();
decodificationTable[((*it)->getCodification())] = (*it)->getLabel();
}
}
/**
* This methods verifies it the decodification table contains a code.
*
* @param binaryString The binary code that is being searched in the table.
*/
bool Source::stringInCodeList(std::string binaryString)
{
return (decodificationTable.find(binaryString) != decodificationTable.end());
}
/**
* A function for showing the statistics and results from the program.
*/
void Source::showProperties()
{
std::map<char, unsigned int>::iterator it;
std::list <Symbol*>::iterator it2;
#ifdef MODULO1
printf("Total number of symbols read: %d\n", totalSymbols);
printf("The frequencies for this file are...\n");
for (it = symbolCounter.begin(); it != symbolCounter.end();it++)
printf("%c => %d\n", (*it).first, (*it).second);
printf("\n");
printf("The probabilities for this file are...\n");
for (it2 = symbolList.begin(); it2 != symbolList.end(); it2++)
printf("%c => %f\n", (*it2)->getLabel(), (*it2)->getProbability());
printf("\n");
#endif
#ifdef MODULO2
printf("The codifications for the symbols are...\n");
for (it2 = symbolList.begin(); it2 != symbolList.end(); it2++)
{
printf("%c => %s\n", (*it2)->getLabel(), ((*it2)->getCodification()).c_str());
}
printf("\n");
#endif
}