-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol.cpp
More file actions
309 lines (283 loc) · 6.32 KB
/
symbol.cpp
File metadata and controls
309 lines (283 loc) · 6.32 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
#include "symbol.h"
/**
* Void constructor for the Symbol class
*/
Symbol::Symbol()
{
this->fatherSymbol = 0;
}
/**
* A method for determining if the symbol is encodable.
*
* It is used while the input file is being read, so
* it counts only those symbols that we want to encode.
*
* @param symbol The symbol that we want to know if is
* encodable
*/
bool Symbol::symbolIsEncodable(char symbol)
{
return true;
}
/**
* A setter for the attribute "label".
*
* @param label The value that we want the "label" field
* to be set.
*/
void Symbol::setLabel(char label)
{
this->label = label;
}
/**
* A getter for the attribute "label".
*/
char Symbol::getLabel()
{
return this->label;
}
/**
* A setter for the attribute "probability".
*
* @param probability The value that we want the "probability" field
* to be set.
*/
void Symbol::setProbability(float probability)
{
this->probability = probability;
}
/**
* A getter for the attribute "probability".
*/
float Symbol::getProbability() const
{
return this->probability;
}
/**
* A setter for the attribute "fatherSymbol".
*
* @param symbol The symbol that we want the "fatherSymbol" field
* to point.
*/
void Symbol::setFather(Symbol* symbol)
{
fatherSymbol = symbol;
}
/**
* A getter for the attribute "fatherSymbol".
*/
Symbol* Symbol::getFather()
{
return this->fatherSymbol;
}
/**
* A getter for the attribute "leftChild".
*/
Symbol* Symbol::getLeftChild()
{
return 0;
}
/**
* A getter for the attribute "rightChild".
*/
Symbol* Symbol::getRightChild()
{
return 0;
}
/**
* A getter for the attribute "codification".
*/
std::string Symbol::getCodification()
{
return this->codification;
}
/**
* A function to know if this is a Symbol
* object or a CombinedSymbol object.
*
* (This one will return false)
*/
bool Symbol::isCombined()
{
return false;
}
/**
* A function to set the "codification" value to
* the correct value once the Huffman algorithm
* has been executed (the tree has been obtained).
*
* The function makes the symbol to look for its
* fathers and getting the binary values for its
* codification while doing it.
*/
void Symbol::obtainCodification()
{
Symbol* father = this->getFather();
Symbol* child = this;
std::string tmpCodification = "";
while (!(father == 0))
{
if (father->getLeftChild() == child)
tmpCodification.push_back('0');
else if (father->getRightChild() == child)
tmpCodification.push_back('1');
father = father->getFather();
child = child->getFather();
}
codification = "";
for (int i = tmpCodification.length() - 1; i >= 0; i--)
codification.push_back(tmpCodification[i]);
}
/**
* This method is recursive and it is called by the
* symbols to get the serial corresponding to the
* binary tree.
*
* If the current node is a CombinedSymbol, we
* will make a recursive call for serializing
* the leftChild and the RightChild, while appending
* a 0 to the serial.
*
* If it is not, then we append a 1 to the serial
* and the label of this symbol.
*
* @param serial The pointer to the string where the
* nodes will have to append the characters.
*/
void Symbol::serializeNode(std::string* serial)
{
if (this->isCombined())
{
serial->append("0");
leftChild->serializeNode(serial);
rightChild->serializeNode(serial);
}
else
{
serial->append("1");
serial->push_back(this->getLabel());
}
}
/**
* This method is used in the decodification step
* to build the tree from the serial.
*
* It creates nodes based on the characters that
* is reading from the serial in the input file.
*
* @param input The file where the serial is being
* read.
*/
Symbol* Symbol::unserializeNode(FILE *input)
{
char cur_c = fgetc(input);
Symbol* newSymbol;
if (cur_c == '0')
{
Symbol* symbol1 = Symbol::unserializeNode(input);
Symbol* symbol2 = Symbol::unserializeNode(input);
newSymbol = new CombinedSymbol(symbol1, symbol2);
}
else // cur_c == '1'
{
newSymbol = new Symbol();
cur_c = fgetc(input);
newSymbol->setLabel(cur_c);
}
return newSymbol;
}
/**
* This methods is recursive and it is used in
* the step of decodification.
*
* It will help to recollect those nodes that
* were symbols in the original file (leaves).
*
* @param symbolList A point to the list where
* the symbols will be recollected.
*/
void Symbol::addToListIfNotCombined(std::list <Symbol*> *symbolList)
{
if (this->isCombined())
{
Symbol *symbol1 = getLeftChild();
Symbol *symbol2 = getRightChild();
symbol1->addToListIfNotCombined(symbolList);
symbol2->addToListIfNotCombined(symbolList);
}
else
symbolList->push_back(this);
}
/**
* The only constructor for the CombinedSymbol class
* that we will use is this one.
*
* It creates an object CombinedSymbol that is "father"
* of two other symbols (from the point of view of
* the binary tree). This means that those two symbols
* have been merged in one, resulting in this
* CombinedSymbol.
*
* To do so, we sum the probabilities of the child
* symbols and we update the pointers.
*
* @param symbol1 The symbol that will be assigned as
* the "leftChild" for this object.
*
* @param symbol2 The symbol that will be assigned as
* the "rightChild" for this object.
*/
CombinedSymbol::CombinedSymbol(Symbol* symbol1, Symbol* symbol2)
{
this->probability = symbol1->getProbability() + symbol2->getProbability();
this->leftChild = symbol1;
this->rightChild = symbol2;
symbol1->setFather(this);
symbol2->setFather(this);
}
/**
* A setter for the attribute "leftChild".
*
* @param symbol The symbol that the "leftChild"
* pointer should point at.
*/
void CombinedSymbol::setLeftChild(Symbol* symbol)
{
this->leftChild = symbol;
symbol->setFather(this);
}
/**
* A setter for the attribute "rightChild".
*
* @param symbol The symbol that the "rightChild"
* pointer should point at.
*/
void CombinedSymbol::setRightChild(Symbol* symbol)
{
this->rightChild = symbol;
symbol->setFather(this);
}
/**
* A getter for the attribute "leftChild".
*/
Symbol* CombinedSymbol::getLeftChild()
{
return this->leftChild;
}
/**
* A getter for the attribute "leftChild".
*/
Symbol* CombinedSymbol::getRightChild()
{
return this->rightChild;
}
/**
* A function to know if this is a Symbol
* object or a CombinedSymbol object.
*
* (This one will return true)
*/
bool CombinedSymbol::isCombined()
{
return true;
}